Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion include/zephyr/net/mcp/mcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,107 @@
#ifndef ZEPHYR_INCLUDE_NET_MCP_SERVER_H_
#define ZEPHYR_INCLUDE_NET_MCP_SERVER_H_

/**
* @file
* @brief Model Context Protocol (MCP) Server API
*/

#include <zephyr/kernel.h>
#include <errno.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
#ifdef CONFIG_MCP_TOOLS_CAPABILITY
MCP_USR_TOOL_RESPONSE,
MCP_USR_TOOL_NOTIFICATION,
#endif
MCP_USR_GENERIC_RESPONSE
} mcp_app_msg_type_t;

#ifdef CONFIG_MCP_TOOLS_CAPABILITY
/**
* @brief Tool metadata structure
*/
typedef struct mcp_tool_metadata {
char name[CONFIG_MCP_TOOL_NAME_MAX_LEN];
char input_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
#ifdef CONFIG_MCP_TOOL_DESC
char description[CONFIG_MCP_TOOL_DESC_MAX_LEN];
#endif
#ifdef CONFIG_MCP_TOOL_TITLE
char title[CONFIG_MCP_TOOL_NAME_MAX_LEN];
#endif
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
char output_schema[CONFIG_MCP_TOOL_SCHEMA_MAX_LEN];
#endif
} mcp_tool_metadata_t;

/**
* @brief Tool callback function
*
* @param params JSON string with tool parameters
* @param execution_token Unique execution identifier
* @return 0 on success, negative errno on failure
*/
typedef int (*mcp_tool_callback_t)(const char *params, uint32_t execution_token);

/**
* @brief Tool definition structure
*/
typedef struct mcp_tool_record {
mcp_tool_metadata_t metadata;
mcp_tool_callback_t callback;
} mcp_tool_record_t;
#endif

typedef struct mcp_user_message {
mcp_app_msg_type_t type;
int length;
void *data;
} mcp_app_message_t;

/**
* @brief Initialize the MCP Server.
* @brief Initialize the MCP Server
*
* @return 0 on success, negative errno on failure
*/
int mcp_server_init(void);

/**
* @brief Start the MCP Server
*
* @return 0 on success, negative errno on failure
*/
int mcp_server_start(void);

int mcp_server_submit_app_message(const mcp_app_message_t *user_msg, uint32_t execution_token);

#ifdef CONFIG_MCP_TOOLS_CAPABILITY
/**
* @brief Add a tool to the server
*
* @param tool_record Tool definition with metadata and callback
* @return 0 on success, negative errno on failure
* @retval -EINVAL Invalid tool_record
* @retval -EEXIST Tool name already exists
* @retval -ENOSPC Registry full
*/
int mcp_server_add_tool(const mcp_tool_record_t *tool_record);

/**
* @brief Remove a tool from the server
*
* @param tool_name Name of tool to remove
* @return 0 on success, negative errno on failure
* @retval -EINVAL Invalid tool name
* @retval -ENOENT Tool not found
*/
int mcp_server_remove_tool(const char *tool_name);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 0 additions & 2 deletions samples/net/mcp/mcp_server_hello_world/boards/qemu_x86.conf

This file was deleted.

4 changes: 4 additions & 0 deletions samples/net/mcp/mcp_server_hello_world/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ CONFIG_MCP_SERVER=y
CONFIG_LOG=y
CONFIG_PRINTK=y

# MCP library logging level
CONFIG_MCP_LOG_LEVEL_INF=y

# Network configuration
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"

# Sufficient stack size for HTTP + JSON processing
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=16384
92 changes: 91 additions & 1 deletion samples/net/mcp/mcp_server_hello_world/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,99 @@

LOG_MODULE_REGISTER(mcp_sample_hello, LOG_LEVEL_INF);

#ifdef CONFIG_MCP_TOOLS_CAPABILITY
/* Tool callback functions */
static int hello_world_tool_callback(const char *params, uint32_t execution_token)
{
printk("Hello World tool executed with params: %s, token: %u\n", params ? params : "none",
execution_token);
return 0;
}

static int goodbye_world_tool_callback(const char *params, uint32_t execution_token)
{
printk("Goodbye World tool executed with params: %s, token: %u\n", params ? params : "none",
execution_token);
return 0;
}

/* Tool definitions */
static const mcp_tool_record_t hello_world_tool = {
.metadata = {
.name = "hello_world",
.input_schema = "{\"type\":\"object\",\"properties\":{\"message\":{"
"\"type\":\"string\"}}}",
#ifdef CONFIG_MCP_TOOL_DESC
.description = "A simple hello world greeting tool",
#endif
#ifdef CONFIG_MCP_TOOL_TITLE
.title = "Hello World Tool",
#endif
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
.output_schema = "{\"type\":\"object\",\"properties\":{\"response\":{"
"\"type\":\"string\"}}}",
#endif
},
.callback = hello_world_tool_callback};

static const mcp_tool_record_t goodbye_world_tool = {
.metadata = {
.name = "goodbye_world",
.input_schema = "{\"type\":\"object\",\"properties\":{\"message\":{"
"\"type\":\"string\"}}}",
#ifdef CONFIG_MCP_TOOL_DESC
.description = "A simple goodbye world farewell tool",
#endif
#ifdef CONFIG_MCP_TOOL_TITLE
.title = "Goodbye World Tool",
#endif
#ifdef CONFIG_MCP_TOOL_OUTPUT_SCHEMA
.output_schema = "{\"type\":\"object\",\"properties\":{\"response\":{"
"\"type\":\"string\"}}}",
#endif
},
.callback = goodbye_world_tool_callback};
#endif /* CONFIG_MCP_TOOLS_CAPABILITY */

int main(void)
{
int ret;

printk("Hello World\n\r");
mcp_server_init();
printk("Initializing...\n\r");
ret = mcp_server_init();
if (ret != 0) {
printk("MCP Server initialization failed: %d\n\r", ret);
return ret;
}

#ifdef CONFIG_MCP_TOOLS_CAPABILITY
printk("Registering Tool #1: Hello world!...\n\r");
ret = mcp_server_add_tool(&hello_world_tool);
if (ret != 0) {
printk("Tool #1 registration failed.\n\r");
return ret;
}
printk("Tool #1 registered.\n\r");

printk("Registering Tool #2: Goodbye world!...\n\r");
ret = mcp_server_add_tool(&goodbye_world_tool);
if (ret != 0) {
printk("Tool #2 registration failed.\n\r");
return ret;
}
printk("Tool #2 registered.\n\r");
#else
printk("MCP Tools capability not enabled - skipping tool registration\n\r");
#endif /* CONFIG_MCP_TOOLS_CAPABILITY */

printk("Starting...\n\r");
ret = mcp_server_start();
if (ret != 0) {
printk("MCP Server start failed: %d\n\r", ret);
return ret;
}

printk("MCP Server running...\n\r");
return 0;
}
1 change: 1 addition & 0 deletions subsys/net/lib/mcp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

zephyr_library()

zephyr_library_sources_ifdef(CONFIG_MCP_SERVER mcp_common.c)
zephyr_library_sources_ifdef(CONFIG_MCP_SERVER mcp_server.c)
zephyr_library_sources_ifdef(CONFIG_MCP_SERVER mcp_transport.c)
zephyr_library_sources_ifdef(CONFIG_MCP_SERVER mcp_json.c)
Loading