关于 mcp-c
mcp-c 是用于构建 MCP 服务的 C 框架,通过注解生成工具签名、JSON 转换等样板代码,让开发者专注于实现服务功能。
主要功能
- 01用于构建 MCP 服务的 C 框架
- 02通过注解导出函数和结构体
- 03生成 JSON 到结构体的桥接代码
- 04生成工具签名定义
适用场景
- 01使用 C 构建 MCP 工具
- 02为导出的结构体生成模式和序列化代码
MCP 相关
它提供以 C 实现 MCP 服务的框架,支持导出工具函数并生成工具签名。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
暂未收录安装方式
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
还没有获取到工具列表
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
mcp-c
用 C 编写的 MCP 服务框架,开发高效又轻松 利用注解自动生成代码,只需专注于需要编写的 MCP 服务函数
如何使用
- 在
src/mcp_server下编写自己的 MCP 工具- 为想要使用的函数和结构体添加属性
EXPORT和EXPORT_AS(name)
- 为想要使用的函数和结构体添加属性
- 构建项目,
cmake -B build -S . && cmake --build .,export会为你生成所有繁琐的代码(例如工具列表、将 JSON 转换为结构体的桥接代码等) - 运行项目,
./mcpc
快速入门
- 先决条件
- vcpkg(仅适用于 windows)
- cmake
- clang
- cJSON
- 构建项目 windows
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 文件夹下生成序列化代码 generated_bridge_code.c 包含将 json 解析为对象的代码 generated_function_signatures.c 包含结构体定义的代码
上面的代码示例会生成一个 get_all_function_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_all_function_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 简化操作
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。