Skip to content

Commit 659959d

Browse files
committed
networking: refactorization
Refactored comments and code Signed-off-by: David Piskula <david.piskula@nxp.com>
1 parent 6db6b53 commit 659959d

File tree

3 files changed

+392
-410
lines changed

3 files changed

+392
-410
lines changed

include/zephyr/net/mcp/mcp_server.h

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
#ifndef ZEPHYR_INCLUDE_NET_MCP_SERVER_H_
88
#define ZEPHYR_INCLUDE_NET_MCP_SERVER_H_
99

10+
/**
11+
* @file
12+
* @brief Model Context Protocol (MCP) Server API
13+
*/
14+
1015
#include <zephyr/kernel.h>
1116
#include <errno.h>
1217

@@ -15,6 +20,9 @@ extern "C" {
1520
#endif
1621

1722
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
23+
/**
24+
* @brief Tool metadata structure
25+
*/
1826
typedef struct mcp_tool_metadata {
1927
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN];
2028
char input_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
@@ -29,10 +37,18 @@ typedef struct mcp_tool_metadata {
2937
#endif
3038
} mcp_tool_metadata_t;
3139

32-
/* Tool callback function signature */
40+
/**
41+
* @brief Tool callback function
42+
*
43+
* @param params JSON string with tool parameters
44+
* @param execution_token Unique execution identifier
45+
* @return 0 on success, negative errno on failure
46+
*/
3347
typedef int (*mcp_tool_callback_t)(const char *params, uint32_t execution_token);
3448

35-
/* Tool definition structure */
49+
/**
50+
* @brief Tool definition structure
51+
*/
3652
typedef struct mcp_tool_record {
3753
mcp_tool_metadata_t metadata;
3854
mcp_tool_callback_t callback;
@@ -41,50 +57,48 @@ typedef struct mcp_tool_record {
4157

4258
struct mcp_message_msg {
4359
uint32_t token;
44-
/* More fields will be added later */
4560
};
4661

4762
/**
48-
* @brief Initialize the MCP Server.
63+
* @brief Initialize the MCP Server
4964
*
50-
* @return 0 on success, negative errno code on failure
65+
* @return 0 on success, negative errno on failure
5166
*/
5267
int mcp_server_init(void);
5368

5469
/**
55-
* @brief Start the MCP Server.
70+
* @brief Start the MCP Server
5671
*
57-
* @return 0 on success, negative errno code on failure
72+
* @return 0 on success, negative errno on failure
5873
*/
5974
int mcp_server_start(void);
6075

6176
/**
62-
* @brief Queues a response to the MCP Server library, which takes care of sending it to
63-
* the MCP Client.
77+
* @brief Queue a response for delivery
6478
*
65-
* @return 0 on success, negative errno code on failure
79+
* @return 0 on success, negative errno on failure
6680
*/
6781
int mcp_queue_response(void);
6882

6983
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
7084
/**
71-
* @brief Add a tool to the MCP server tool registry
85+
* @brief Add a tool to the server
7286
*
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
87+
* @param tool_record Tool definition with metadata and callback
88+
* @return 0 on success, negative errno on failure
89+
* @retval -EINVAL Invalid tool_record
90+
* @retval -EEXIST Tool name already exists
91+
* @retval -ENOSPC Registry full
7892
*/
7993
int mcp_server_add_tool(const mcp_tool_record_t *tool_record);
8094

8195
/**
82-
* @brief Remove a tool from the MCP server tool registry
96+
* @brief Remove a tool from the server
8397
*
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
98+
* @param tool_name Name of tool to remove
99+
* @return 0 on success, negative errno on failure
100+
* @retval -EINVAL Invalid tool name
101+
* @retval -ENOENT Tool not found
88102
*/
89103
int mcp_server_remove_tool(const char *tool_name);
90104
#endif

subsys/net/lib/mcp/mcp_common.h

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ typedef enum {
2929
MCP_MSG_RESPONSE_TOOLS_CALL,
3030
#endif
3131
MCP_MSG_NOTIFICATION,
32-
/* unknown method, unknown tool, other errors */
3332
} mcp_queue_msg_type_t;
3433

3534
typedef enum {
36-
MCP_SYS_DISCONNECTION,
35+
MCP_SYS_CLIENT_SHUTDOWN,
3736
MCP_SYS_CANCEL
3837
} mcp_system_msg_type_t;
3938

@@ -56,15 +55,12 @@ typedef enum {
5655
typedef struct mcp_system_msg {
5756
mcp_system_msg_type_t type;
5857
uint32_t request_id;
58+
uint32_t client_id;
5959
} mcp_system_msg_t;
6060

6161
typedef struct mcp_initialize_request {
6262
uint32_t request_id;
6363
uint32_t client_id;
64-
65-
/* MCP Core ignores client params and clientInfo and won't try
66-
* to use any client capabilities, in this implementation.
67-
*/
6864
} mcp_initialize_request_t;
6965

7066
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
@@ -76,7 +72,6 @@ typedef struct mcp_tools_list_request {
7672
typedef struct mcp_tools_call_request {
7773
uint32_t request_id;
7874
uint32_t client_id;
79-
8075
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN];
8176
char arguments[CONFIG_MCP_TOOL_INPUT_ARGS_MAX_LEN];
8277
} mcp_tools_call_request_t;
@@ -109,26 +104,23 @@ typedef struct mcp_server_notification {
109104
mcp_notification_method_type_t method;
110105
} mcp_server_notification_t;
111106

112-
/* Queue message structures using void pointers */
113107
typedef struct mcp_request_queue_msg {
114108
mcp_queue_msg_type_t type;
115-
void *data; /* Points to allocated message data */
109+
void *data;
116110
} mcp_request_queue_msg_t;
117111

118112
typedef struct mcp_response_queue_msg {
119113
mcp_queue_msg_type_t type;
120-
void *data; /* Points to allocated message data */
114+
void *data;
121115
} mcp_response_queue_msg_t;
122116

123117
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
124-
/* Tool registry structure */
125118
typedef struct {
126119
mcp_tool_record_t tools[CONFIG_MCP_MAX_TOOLS];
127120
struct k_mutex registry_mutex;
128121
uint8_t tool_count;
129122
} mcp_tool_registry_t;
130123

131-
/* Execution context for tracking active tool executions */
132124
typedef struct {
133125
uint32_t execution_token;
134126
uint32_t request_id;
@@ -141,26 +133,13 @@ typedef struct {
141133
mcp_execution_state_t execution_state;
142134
} mcp_execution_context_t;
143135

144-
/* Execution registry for tracking active executions */
145136
typedef struct {
146137
mcp_execution_context_t executions[MCP_MAX_REQUESTS];
147138
struct k_mutex execution_mutex;
148139
} mcp_execution_registry_t;
149140
#endif
150141

151-
/**
152-
* @brief Allocate memory for MCP operations
153-
*
154-
* @param size Size in bytes to allocate
155-
* @return Pointer to allocated memory, or NULL on failure
156-
*/
157142
void *mcp_alloc(size_t size);
158-
159-
/**
160-
* @brief Free memory allocated by mcp_alloc()
161-
*
162-
* @param ptr Pointer to memory to free, or NULL (no-op if NULL)
163-
*/
164143
void mcp_free(void *ptr);
165144

166145
#endif /* ZEPHYR_SUBSYS_MCP_COMMON_H_ */

0 commit comments

Comments
 (0)