mcp-c について
mcp-cは、MCPサーバーを構築するためのCフレームワークです。アノテーションからツールの署名やJSON変換などのコードを生成し、機能の実装に集中できます。
主な機能
- 01MCPサーバー向けCフレームワーク
- 02アノテーションによる関数と構造体の公開
- 03JSONから構造体への変換コード生成
- 04ツール署名定義の生成
活用シーン
- 01Cでの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();
}
これにより、次の関数シグネチャ JSON を返す get_all_function_signatures_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 を使用します
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。