| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_MCP_TOOLS_EXECUTE_FUNCTION_REGISTRY_H |
| 4 | #define NETDATA_MCP_TOOLS_EXECUTE_FUNCTION_REGISTRY_H |
| 5 | |
| 6 | #include "mcp.h" |
| 7 | #include "mcp-tools-execute-function-internal.h" |
| 8 | |
| 9 | // Registry entry TTL in seconds (10 minutes) |
| 10 | #define MCP_FUNCTIONS_REGISTRY_TTL 600 |
| 11 | |
| 12 | // Parameter type enumeration |
| 13 | typedef enum mcp_required_params_type { |
| 14 | MCP_REQUIRED_PARAMS_TYPE_SELECT = 0, |
| 15 | MCP_REQUIRED_PARAMS_TYPE_MULTISELECT |
| 16 | } MCP_REQUIRED_PARAMS_TYPE; |
| 17 | |
| 18 | ENUM_STR_DEFINE_FUNCTIONS_EXTERN(MCP_REQUIRED_PARAMS_TYPE) |
| 19 | ENUM_STR_DEFINE_FUNCTIONS_EXTERN(MCP_PAGINATION_UNITS) |
| 20 | |
| 21 | // Parameter option structure |
| 22 | typedef struct mcp_function_param_option { |
| 23 | STRING *id; |
| 24 | STRING *name; |
| 25 | STRING *info; // Additional information about the option (e.g., file count, size, coverage) |
| 26 | } MCP_FUNCTION_PARAM_OPTION; |
| 27 | |
| 28 | // Parameter structure |
| 29 | typedef struct mcp_function_param { |
| 30 | STRING *id; |
| 31 | STRING *name; |
| 32 | STRING *help; |
| 33 | MCP_REQUIRED_PARAMS_TYPE type; |
| 34 | bool unique_view; |
| 35 | size_t options_count; |
| 36 | MCP_FUNCTION_PARAM_OPTION *options; |
| 37 | } MCP_FUNCTION_PARAM; |
| 38 | |
| 39 | // Pagination structure for MCP-compliant cursor-based pagination |
| 40 | typedef struct mcp_function_pagination { |
| 41 | bool enabled; // whether pagination is supported |
| 42 | STRING *key; // parameter name for pagination (e.g., "anchor") |
| 43 | STRING *column; // column name in data (e.g., "timestamp") |
| 44 | MCP_PAGINATION_UNITS units; // units of the column - only known types supported |
| 45 | } MCP_FUNCTION_PAGINATION; |
| 46 | |
| 47 | // Registry entry structure |
| 48 | typedef struct mcp_function_registry_entry { |
| 49 | RW_SPINLOCK spinlock; // Read/write lock for thread-safe access |
| 50 | SPINLOCK update_spinlock; // Spinlock to coordinate updates without blocking readers |
| 51 | MCP_FUNCTION_TYPE type; // Function type (table, table with history, etc.) |
| 52 | bool has_history; // whether the function supports history |
| 53 | int update_every; // update interval in seconds |
| 54 | STRING *help; // help text |
| 55 | int version; // Function version (v3+ supports POST) |
| 56 | bool supports_post; // true if version >= 3 |
| 57 | size_t required_params_count; // number of required parameters |
| 58 | MCP_FUNCTION_PARAM *required_params; // array of required parameters |
| 59 | // Supported optional parameters (detected from accepted_params) |
| 60 | bool has_timeframe; // supports after, before parameters |
| 61 | bool has_last; // supports last parameter (row limit) |
| 62 | bool has_data_only; // supports data_only parameter |
| 63 | bool has_direction; // supports direction parameter |
| 64 | bool has_query; // supports query parameter for full-text search |
| 65 | bool has_slice; // supports slice parameter for database-level filtering |
| 66 | MCP_FUNCTION_PAGINATION pagination; // MCP-compliant cursor pagination support |
| 67 | time_t last_update; // timestamp of last info update |
| 68 | time_t expires; // expiration timestamp |
| 69 | } MCP_FUNCTION_REGISTRY_ENTRY; |
| 70 | |
| 71 | // Initialize the functions registry |
| 72 | void mcp_functions_registry_init(void); |
| 73 | |
| 74 | // Cleanup the functions registry |
| 75 | void mcp_functions_registry_cleanup(void); |
| 76 | |
| 77 | // Get a registry entry for a function (read-locked) |
| 78 | // This will fetch info if the entry doesn't exist or has expired |
| 79 | // The returned entry is read-locked and MUST be released with mcp_functions_registry_release() |
| 80 | // Returns NULL on error (with error details in the error buffer) |
| 81 | MCP_FUNCTION_REGISTRY_ENTRY *mcp_functions_registry_get(RRDHOST *host, const char *function_name, BUFFER *error); |
| 82 | |
| 83 | // Release a registry entry (unlocks the read lock) |
| 84 | // MUST be called after mcp_functions_registry_get() as soon as possible |
| 85 | void mcp_functions_registry_release(MCP_FUNCTION_REGISTRY_ENTRY *entry); |
| 86 | |
| 87 | #endif // NETDATA_MCP_TOOLS_EXECUTE_FUNCTION_REGISTRY_H |