MCP服务器核心

faCtMCP是一个独立的C++ MCP服务器核心,具有C ABI,旨在被开发人员下载并嵌入到构建代理工具中。

  • by balrogbob
  • C++
  • 本地部署
  • 更新于
在 GitHub 查看 balrogbob/faCtMCP

使用说明

faCtMCP

faCtMCP 是一个具有C ABI的独立C++MCP服务器核心,旨在由构建代理工具的开发人员下载和嵌入。

它旨在嵌入到其他应用程序中,以便它们可以公开自定义MCP工具,而无需重新实现传输、JSON-RPC框架或工具调度。

发布状态

  • 稳定: 1.0.0
  • 支持MCP协议版本: 2024-11-05, 2025-03-26, 2025-06-18
  • 包括旧客户端的传统协议兼容模式

自首次提交以来

  • 带有C ABI的C++原生独立MCP内核(include/factmcp.h)
  • 用于下游嵌入的共享和静态库输出
  • 支持JSON-RPC 2.0请求处理的stdio和HTTP传输
  • MCP流量和发现的HTTP端点(/mcp, /tools, /sse)
  • 具有GET/POST支持和SSE事件传递的流式HTTP行为
  • 协商协议版本处理和响应头更新
  • 旧协议/客户端行为的遗留SSE处理改进
  • 示例stdio服务器:echo、math、文件系统/搜索套件、代码/项目/知识图套件
  • 用于快速协议验证的C++烟雾客户端实用程序
  • stdio和HTTP流的协议和端点测试覆盖率
  • CMake包导出方便 find_package(faCtMCP) 整合
  • 验证了针对多个MCP客户端的互操作性(包括传统学习客户端行为)

什么船

  • 共享库构建(faCtMCP.dll)
  • 静态库构建(faCtMCP_static.lib)
  • 公共C标头: include/factmcp.h
  • MIT许可证
  • C++烟雾客户端: factmcp_smoke_client
  • 可运行的stdio示例服务器:
    • factmcp_stdio_echo
    • factmcp_stdio_math
    • factmcp_stdio_filesystem_suite
    • factmcp_stdio_code_suite

当前交通支持

  • STDIO
  • HTTP

公共C API

typedef struct factmcp_server factmcp_server;

typedef enum factmcp_transport {
    FACTMCP_TRANSPORT_HTTP = 0,
    FACTMCP_TRANSPORT_STDIO = 1
} factmcp_transport;

typedef const char* (*factmcp_tool_handler)(const char* json_params, void* user_data);

factmcp_server* factmcp_create(factmcp_transport transport, int port);
void factmcp_destroy(factmcp_server* server);
int factmcp_start(factmcp_server* server);
void factmcp_stop(factmcp_server* server);
void factmcp_run(factmcp_server* server);
int factmcp_port(const factmcp_server* server);

int factmcp_register_tool(
    factmcp_server* server,
    const char* name,
    const char* tool_json,
    factmcp_tool_handler handler,
    void* user_data);

const char* factmcp_process_jsonrpc(factmcp_server* server, const char* json_body);
void factmcp_free_string(const char* text);

最小嵌入流量

  1. 使用创建服务器 factmcp_create
  2. 使用JSON模式元数据注册一个或多个工具
  3. 启动服务器
  4. 使用stdio或HTTP运行它

示例

static const char* echo_handler(const char* json_params, void* user_data) {
    (void)user_data;
    return "{\"success\":true,\"message\":\"hello from faCtMCP\"}";
}

int main(void) {
    factmcp_server* server = factmcp_create(FACTMCP_TRANSPORT_STDIO, 0);
    factmcp_register_tool(server, "echo", TOOL_JSON, echo_handler, NULL);
    factmcp_start(server);
    factmcp_run(server);
    factmcp_destroy(server);
    return 0;
}

构建

cmake -S . -B build
cmake --build build --config Release
ctest -C Release --test-dir build --output-on-failure

入门指南

最低消费 CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(my_mcp_tool LANGUAGES C CXX)

find_package(faCtMCP REQUIRED)

add_executable(my_mcp_tool main.cpp)
target_link_libraries(my_mcp_tool PRIVATE faCtMCP::faCtMCP)

最小化 main.cpp:

#include "factmcp.h"

static const char* kToolJson =
    "{"
    "\"name\":\"hello\"," 
    "\"description\":\"Return a greeting\"," 
    "\"inputSchema\":{\"type\":\"object\",\"properties\":{}}"
    "}";

static const char* hello_handler(const char*, void*) {
    return "{\"success\":true,\"message\":\"hello from faCtMCP\"}";
}

int main() {
    factmcp_server* server = factmcp_create(FACTMCP_TRANSPORT_STDIO, 0);
    factmcp_register_tool(server, "hello", kToolJson, hello_handler, nullptr);
    factmcp_start(server);
    factmcp_run(server);
    factmcp_destroy(server);
    return 0;
}

二进制文件示例

  • factmcp_stdio_echo.exe 暴露一个 echo 工具
  • factmcp_stdio_math.exe 暴露一个 add 工具
  • factmcp_stdio_filesystem_suite.exe 公开转换后的文件系统/搜索工具:
    • create_directory
    • list_directory
    • list_directory_with_sizes
    • directory_tree
    • move_file
    • search_files
    • get_file_info
    • glob_files
    • grep_files
  • factmcp_stdio_code_suite.exe 公开转换后的代码/项目工具:
    • symbols
    • git
    • project
    • analyze
    • refactor
    • create_entities
    • create_relations
    • add_observations
    • delete_entities
    • delete_observations
    • delete_relations
    • read_graph
    • search_nodes
    • open_nodes

这些是有意设计的小型参考服务器,展示了消费者如何在单个源文件中实现自己的工具回调。

独立工具与编辑器耦合工具

独立的faCtMCP包目前提供了可以在没有实时编辑器UI的情况下运行的工具。

优秀的独立示例:

  • 文件系统/搜索工具
  • git/项目/分析/重构/符号工具
  • 知识图存储工具

尚未作为独立示例发货:

  • read_file / write_file / edit_file 以他们当前的编辑器桥接形式
  • sequentialthinking 以当前编辑器集成形式
  • bash 在其当前 WinuxCmdBridge-依赖形式

稍后可以通过以下方式添加:

  • 将它们与仅限编辑器的桥解耦,或
  • 提供独立的faCtMCP原生替代品

发布布局

  • include/ 公用标头
  • src/ 图书馆资源
  • examples/ 独立示例服务器
  • tests/ 协议和端点验证
  • tools/ 烟雾测试公用电源
  • .github/workflows/ CI定义