Skip to content

Commit 6db6b53

Browse files
committed
networking: tools
Added tool registration, improved type definitions and tools/list Signed-off-by: David Piskula <david.piskula@nxp.com>
1 parent 43cc365 commit 6db6b53

File tree

4 files changed

+267
-56
lines changed

4 files changed

+267
-56
lines changed

include/zephyr/net/mcp/mcp_server.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,37 @@
88
#define ZEPHYR_INCLUDE_NET_MCP_SERVER_H_
99

1010
#include <zephyr/kernel.h>
11+
#include <errno.h>
1112

1213
#ifdef __cplusplus
1314
extern "C" {
1415
#endif
1516

17+
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
18+
typedef struct mcp_tool_metadata {
19+
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN];
20+
char input_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
21+
#ifdef CONFIG_MCP_TOOL_DESC
22+
char description[CONFIG_MCP_TOOL_DESC_MAX_LEN];
23+
#endif
24+
#ifdef CONFIG_MCP_TOOL_TITLE
25+
char title[CONFIG_MCP_TOOL_NAME_MAX_LEN];
26+
#endif
27+
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
28+
char output_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
29+
#endif
30+
} mcp_tool_metadata_t;
31+
32+
/* Tool callback function signature */
33+
typedef int (*mcp_tool_callback_t)(const char *params, uint32_t execution_token);
34+
35+
/* Tool definition structure */
36+
typedef struct mcp_tool_record {
37+
mcp_tool_metadata_t metadata;
38+
mcp_tool_callback_t callback;
39+
} mcp_tool_record_t;
40+
#endif
41+
1642
struct mcp_message_msg {
1743
uint32_t token;
1844
/* More fields will be added later */
@@ -21,22 +47,48 @@ struct mcp_message_msg {
2147
/**
2248
* @brief Initialize the MCP Server.
2349
*
50+
* @return 0 on success, negative errno code on failure
2451
*/
2552
int mcp_server_init(void);
2653

2754
/**
2855
* @brief Start the MCP Server.
2956
*
57+
* @return 0 on success, negative errno code on failure
3058
*/
3159
int mcp_server_start(void);
3260

3361
/**
3462
* @brief Queues a response to the MCP Server library, which takes care of sending it to
3563
* the MCP Client.
3664
*
65+
* @return 0 on success, negative errno code on failure
3766
*/
3867
int mcp_queue_response(void);
3968

69+
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
70+
/**
71+
* @brief Add a tool to the MCP server tool registry
72+
*
73+
* @param tool_record Pointer to tool record containing metadata and callback
74+
* @return 0 on success, negative errno code on failure
75+
* -EINVAL if tool_record is NULL or invalid
76+
* -EEXIST if tool with same name already exists
77+
* -ENOSPC if tool registry is full
78+
*/
79+
int mcp_server_add_tool(const mcp_tool_record_t *tool_record);
80+
81+
/**
82+
* @brief Remove a tool from the MCP server tool registry
83+
*
84+
* @param tool_name Name of the tool to remove
85+
* @return 0 on success, negative errno code on failure
86+
* -EINVAL if tool_name is NULL
87+
* -ENOENT if tool not found
88+
*/
89+
int mcp_server_remove_tool(const char *tool_name);
90+
#endif
91+
4092
#ifdef __cplusplus
4193
}
4294
#endif

samples/net/mcp/mcp_server_hello_world/src/main.c

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,99 @@
1010

1111
LOG_MODULE_REGISTER(mcp_sample_hello, LOG_LEVEL_INF);
1212

13+
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
14+
/* Tool callback functions */
15+
static int hello_world_tool_callback(const char *params, uint32_t execution_token)
16+
{
17+
printk("Hello World tool executed with params: %s, token: %u\n", params ? params : "none",
18+
execution_token);
19+
return 0;
20+
}
21+
22+
static int goodbye_world_tool_callback(const char *params, uint32_t execution_token)
23+
{
24+
printk("Goodbye World tool executed with params: %s, token: %u\n", params ? params : "none",
25+
execution_token);
26+
return 0;
27+
}
28+
29+
/* Tool definitions */
30+
static const mcp_tool_record_t hello_world_tool = {
31+
.metadata = {
32+
.name = "hello_world",
33+
.input_schema = "{\"type\":\"object\",\"properties\":{\"message\":{"
34+
"\"type\":\"string\"}}}",
35+
#ifdef CONFIG_MCP_TOOL_DESC
36+
.description = "A simple hello world greeting tool",
37+
#endif
38+
#ifdef CONFIG_MCP_TOOL_TITLE
39+
.title = "Hello World Tool",
40+
#endif
41+
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
42+
.output_schema = "{\"type\":\"object\",\"properties\":{\"response\":{"
43+
"\"type\":\"string\"}}}",
44+
#endif
45+
},
46+
.callback = hello_world_tool_callback};
47+
48+
static const mcp_tool_record_t goodbye_world_tool = {
49+
.metadata = {
50+
.name = "goodbye_world",
51+
.input_schema = "{\"type\":\"object\",\"properties\":{\"message\":{"
52+
"\"type\":\"string\"}}}",
53+
#ifdef CONFIG_MCP_TOOL_DESC
54+
.description = "A simple goodbye world farewell tool",
55+
#endif
56+
#ifdef CONFIG_MCP_TOOL_TITLE
57+
.title = "Goodbye World Tool",
58+
#endif
59+
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
60+
.output_schema = "{\"type\":\"object\",\"properties\":{\"response\":{"
61+
"\"type\":\"string\"}}}",
62+
#endif
63+
},
64+
.callback = goodbye_world_tool_callback};
65+
#endif /* CONFIG_MCP_TOOLS_CAPABILITY */
66+
1367
int main(void)
1468
{
69+
int ret;
70+
1571
printk("Hello World\n\r");
1672
printk("Initializing...\n\r");
17-
mcp_server_init();
73+
ret = mcp_server_init();
74+
if (ret != 0) {
75+
printk("MCP Server initialization failed: %d\n\r", ret);
76+
return ret;
77+
}
78+
79+
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
80+
printk("Registering Tool #1: Hello world!...\n\r");
81+
ret = mcp_server_add_tool(&hello_world_tool);
82+
if (ret != 0) {
83+
printk("Tool #1 registration failed.\n\r");
84+
return ret;
85+
}
86+
printk("Tool #1 registered.\n\r");
87+
88+
printk("Registering Tool #2: Goodbye world!...\n\r");
89+
ret = mcp_server_add_tool(&goodbye_world_tool);
90+
if (ret != 0) {
91+
printk("Tool #2 registration failed.\n\r");
92+
return ret;
93+
}
94+
printk("Tool #2 registered.\n\r");
95+
#else
96+
printk("MCP Tools capability not enabled - skipping tool registration\n\r");
97+
#endif /* CONFIG_MCP_TOOLS_CAPABILITY */
98+
1899
printk("Starting...\n\r");
19-
mcp_server_start();
100+
ret = mcp_server_start();
101+
if (ret != 0) {
102+
printk("MCP Server start failed: %d\n\r", ret);
103+
return ret;
104+
}
105+
106+
printk("MCP Server running...\n\r");
20107
return 0;
21108
}

subsys/net/lib/mcp/mcp_common.h

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ZEPHYR_SUBSYS_MCP_COMMON_H_
99

1010
#include <zephyr/kernel.h>
11+
#include <zephyr/net/mcp/mcp_server.h>
1112

1213
#define MCP_MAX_REQUESTS (CONFIG_HTTP_SERVER_MAX_CLIENTS * CONFIG_HTTP_SERVER_MAX_STREAMS)
1314

@@ -52,22 +53,6 @@ typedef enum {
5253
MCP_EXEC_ZOMBIE
5354
} mcp_execution_state_t;
5455

55-
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
56-
typedef struct mcp_tool_metadata {
57-
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN];
58-
char input_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
59-
#ifdef CONFIG_MCP_TOOL_DESC
60-
char description[CONFIG_MCP_TOOL_DESC_MAX_LEN];
61-
#endif
62-
#ifdef CONFIG_MCP_TOOL_TITLE
63-
char title[CONFIG_MCP_TOOL_NAME_MAX_LEN];
64-
#endif
65-
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
66-
char output_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
67-
#endif
68-
} mcp_tool_metadata_t;
69-
#endif
70-
7156
typedef struct mcp_system_msg {
7257
mcp_system_msg_type_t type;
7358
uint32_t request_id;
@@ -136,28 +121,9 @@ typedef struct mcp_response_queue_msg {
136121
} mcp_response_queue_msg_t;
137122

138123
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
139-
/* Tool callback function signature */
140-
typedef int (*mcp_tool_callback_t)(const char *params, uint32_t execution_token);
141-
142-
/* Tool definition structure */
143-
typedef struct {
144-
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN]; /* Required */
145-
char input_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN]; /* Required */
146-
#ifdef CONFIG_MCP_TOOL_DESC
147-
char description[CONFIG_MCP_TOOL_DESC_MAX_LEN]; /* Optional */
148-
#endif
149-
#ifdef CONFIG_MCP_TOOL_TITLE
150-
char title[CONFIG_MCP_TOOL_NAME_MAX_LEN]; /* Optional */
151-
#endif
152-
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
153-
char output_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN]; /* Optional */
154-
#endif
155-
mcp_tool_callback_t callback;
156-
} mcp_tool_info_t;
157-
158124
/* Tool registry structure */
159125
typedef struct {
160-
mcp_tool_info_t tools[CONFIG_MCP_MAX_TOOLS];
126+
mcp_tool_record_t tools[CONFIG_MCP_MAX_TOOLS];
161127
struct k_mutex registry_mutex;
162128
uint8_t tool_count;
163129
} mcp_tool_registry_t;

0 commit comments

Comments
 (0)