master
c 109 lines 4.3 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 /**
4 * MCP Completion Namespace
5 *
6 * The MCP Completion namespace provides methods for handling input argument completions.
7 * In the MCP protocol, completion methods allow clients to request suggestions
8 * for input fields, providing a better user experience when interacting with tools,
9 * resources, or prompts.
10 *
11 * Standard methods in the MCP specification:
12 *
13 * 1. completion/complete - Requests completion suggestions for an input argument
14 * - Takes an argument name, current value, and reference context
15 * - Reference can be to a tool, resource, or prompt
16 * - Returns an array of possible completion values
17 * - May include pagination information for large result sets
18 *
19 * The completion namespace enhances user interactions by providing
20 * real-time suggestions for input fields, improving usability
21 * and reducing errors when working with complex arguments.
22 *
23 * Completions are context-aware, meaning they take into account what the
24 * completion is for (which tool, resource, or prompt) and provide
25 * relevant suggestions based on that context.
26 */
27
28 #include "mcp-completion.h"
29
30 // Implementation of completion/complete (transport-agnostic)
31 static MCP_RETURN_CODE mcp_completion_method_complete(MCP_CLIENT *mcpc, struct json_object *params, MCP_REQUEST_ID id __maybe_unused) {
32 if (!mcpc) return MCP_RC_ERROR;
33
34 // Extract argument and ref parameters
35 struct json_object *argument_obj = NULL;
36 struct json_object *ref_obj = NULL;
37
38 if (!json_object_object_get_ex(params, "argument", &argument_obj)) {
39 buffer_sprintf(mcpc->error, "Missing required parameter 'argument'");
40 return MCP_RC_BAD_REQUEST;
41 }
42
43 if (!json_object_object_get_ex(params, "ref", &ref_obj)) {
44 buffer_sprintf(mcpc->error, "Missing required parameter 'ref'");
45 return MCP_RC_BAD_REQUEST;
46 }
47
48 // Extract argument name and value
49 struct json_object *name_obj = NULL;
50 struct json_object *value_obj = NULL;
51
52 if (!json_object_object_get_ex(argument_obj, "name", &name_obj)) {
53 buffer_sprintf(mcpc->error, "Missing required parameter 'argument.name'");
54 return MCP_RC_BAD_REQUEST;
55 }
56
57 if (!json_object_object_get_ex(argument_obj, "value", &value_obj)) {
58 buffer_sprintf(mcpc->error, "Missing required parameter 'argument.value'");
59 return MCP_RC_BAD_REQUEST;
60 }
61
62 const char *name = json_object_get_string(name_obj);
63 const char *value = json_object_get_string(value_obj);
64
65 // Log that we received this request (actual implementation would generate completion options)
66 netdata_log_info("MCP received completion/complete request for argument '%s' with value '%s'", name, value);
67
68 // Initialize success response
69 mcp_init_success_result(mcpc, id);
70
71 // Add completion data (sample implementation with a few static options)
72 buffer_json_member_add_object(mcpc->result, "completion");
73
74 // Add values-array with sample completion options
75 buffer_json_member_add_array(mcpc->result, "values");
76 buffer_json_add_array_item_string(mcpc->result, "option1");
77 buffer_json_add_array_item_string(mcpc->result, "option2");
78 buffer_json_add_array_item_string(mcpc->result, "option3");
79 buffer_json_array_close(mcpc->result); // Close values array
80
81 // Add optional fields
82 buffer_json_member_add_boolean(mcpc->result, "hasMore", false);
83 buffer_json_member_add_int64(mcpc->result, "total", 3);
84
85 buffer_json_object_close(mcpc->result); // Close completion object
86 buffer_json_finalize(mcpc->result);
87
88 return MCP_RC_OK;
89 }
90
91 // Completion namespace method dispatcher (transport-agnostic)
92 MCP_RETURN_CODE mcp_completion_route(MCP_CLIENT *mcpc, const char *method, struct json_object *params, MCP_REQUEST_ID id) {
93 if (!mcpc || !method) return MCP_RC_INTERNAL_ERROR;
94
95 netdata_log_debug(D_MCP, "MCP completion method: %s", method);
96
97 MCP_RETURN_CODE rc;
98
99 if (strcmp(method, "complete") == 0) {
100 rc = mcp_completion_method_complete(mcpc, params, id);
101 }
102 else {
103 // Method not found in completion namespace
104 buffer_sprintf(mcpc->error, "Method 'completion/%s' not supported. The MCP specification only defines 'complete' method.", method);
105 rc = MCP_RC_NOT_IMPLEMENTED;
106 }
107
108 return rc;
109 }