mcp-c
用c编写的mcp服务器框架,高效轻松地开发 利用注释自动生成代码,只需要关注需要编写什么mcp服务器函数
如何使用
- 在下面编写自己的mcp工具
src/mcp_server
- 添加属性 EXPORT 和 EXPORT_AS(name) 到要使用的函数和结构
- 构建项目,
cmake -B build -S . && cmake --build .,export.cpp将为您生成所有繁琐的代码(如工具列表、将json桥接到struct等) - 运行项目,
./mcpc
快速开始
- 先决条件
- vcpkg(仅适用于windows)
- cmake
- 铿锵
- cJSON
- 构建项目
窗户
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ^
-DCMAKE_TOOLCHAIN_FILE="path/to/vcpkg/scripts/buildsystems/vcpkg.cmake" ^
-G "Ninja" ^
-DVCPKG_APPLOCAL_DEPS=ON ^
-DCMAKE_BUILD_TYPE=Release ^
-B build -S .
- 运行项目
./mcpc支持的功能
- 导出结构体
首先,您需要将宏添加到结构中 考虑这个文件名struct.c
#include "export_macro.h" //first include the export macro
typedef enum EXPORT COLOR {
RED,
GREEN,
BLUE
} COLOR;
typedef struct EXPORT_AS(cloth) cloth {
enum COLOR color DES("color of the cloth");
int size;
} cloth;
typedef struct EXPORT person {
bool isMale DES("whether is male or not male is true");
int age DES("age of the person");
char* name;
cloth wearing_cloths;
} person;执行export.exe struct.c后 这将在src文件夹下生成序列化代码 generatedbridge_code.c包含将json解析为对象的代码 generated函数签名.c包含结构体的定义代码
上面的代码示例将生成一个get_allfunction_signatures_json函数 生成定义json,如下所示
{
"$defs": {
"COLOR": {
"type": "integer",
"enum": ["RED", "GREEN", "BLUE"]
},
"cloth": {
"type": "object",
"properties": {
"color": {
"$ref": "#/$defs/COLOR",
"description": "color of the cloth"
},
"size": {
"type": "integer"
}
},
"required": ["color", "size"]
},
"person": {
"type": "object",
"properties": {
"isMale": {
"type": "boolean",
"description": "whether is male or not male is true"
},
"age": {
"type": "integer",
"description": "age of the person"
},
"name": {
"type": "string"
},
"wearing_cloths": {
"$ref": "#/$defs/cloth"
}
},
"required": ["isMale", "age", "name", "wearing_cloths"]
}
},
"tools":[]
}- 通过注释导出函数签名和结构
首先,您需要为需要导出的函数和结构添加宏 考虑这个文件名func.c
#include "export_macro.h" //first include the export macro
... //struct definition here, with the code sample in section 1
EXPORT_AS(get_color) //if the function is a handler of tool "get_color", you need to add this macro
cJSON* get_color(COLOR c)
{
}
EXPORT_AS(get_person_info) //if the function is a handler of tool "get_person_info", you need to add this macro
cJSON* get_person_info(person* p, int id)
{
}
EXPORT_AS(tools, list) // if the function is a handler of tool "tools/list", you need to add this macro, use the comma to separate the tool name and the function name
cJSON* tools_list() {
int i = 0;
return get_all_function_signatures_json();
}这将生成一个get_allfunction_signatures_json函数,该函数将返回以下函数签名json
{
"#def": { ... } // param type defition here
"tools": [{
"name": "tools/list",
"description": "",
"inputSchema": {
"type": "object",
"properties": {
},
"required": [],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}, {
"name": "get_color",
"description": "",
"inputSchema": {
"type": "object",
"properties": {
"c": {
"$ref": "#/$defs/COLOR"
}
},
"required": ["c"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}, {
"name": "get_person_info",
"description": "",
"inputSchema": {
"type": "object",
"properties": {
"p": {
"$ref": "#/$defs/person"
},
"id": {
"type": "integer"
}
},
"required": ["p", "id"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}]
}- 简称EXPORT
