About mcp-c
mcp-c is a C framework for building MCP servers. It uses annotations and generated code to handle tool signatures, JSON conversion, and related boilerplate.
Key features
- 01C framework for MCP servers
- 02Annotation-based function and struct exports
- 03Generated JSON-to-struct bridge code
- 04Generated tool signature definitions
Use cases
- 01Building MCP tools in C
- 02Generating schemas and serialization code for exported structs
MCP support
It provides a framework for implementing MCP servers in C, including exported tool functions and generated tool signatures.
Configuration
Local server config is the same for every client. Copy it into your client’s MCP config file.
We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.
Tools
Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.
Details
From the project READMEmcp-c
mcp server frarmwork written in c, developing efficiently and effortlessly utilize annotate to generate code automatically, only need to focus on what to need to write mcp server function
how to use
- write your own mcp tools under
src/mcp_server- add attributes
EXPORTandEXPORT_AS(name)to the functions and structs you want to use
- add attributes
- build the project,
cmake -B build -S . && cmake --build .,exportwill generate all the tedious code for you(like tools list, bridge json to struct, etc.) - run the project,
./mcpc
quick start
- prerequisites
- vcpkg(only for windows)
- cmake
- clang
- cJSON
- build the project 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 .
- run the project
./mcpc
supported feature
- export struct first you need to add the macro to the struct consider this file name 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;
after you execute export.exe struct.c this will generate the serialize code under the src folder generated_bridge_code.c contained the code to parse a json to object generated_function_signatures.c contained the code of definition of the struct
the above code sample will generate a get_all_function_signatures_json function to generate definition json like the following
{
"$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":[]
}
- export function signature and struct via annotate
first you need to add the macro for functions and struct you need to export consider this file name 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();
}
this will generate a get_all_function_signatures_json function which will return the following function signature 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#"
}
}]
}
- use EXPORT for short
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.