这是一个用C++编写的MCP系统,包括MCP核心架构的主机、客户端和服务器;主机本身已经相当于一个AI代理,尽管目前它只有命令行界面(但我认为它不需要华丽的界面)。
参考: https://modelcontextprotocol.io/introduction
服务器可用于支持MCP的其他软件,如cursor
客户端也支持MCP的官方服务器,配置与cursor相同
使用C++进行桌面开发)用于导入第三方库cpp httplib和spdlog
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg && bootstrap-vcpkg.bat
然后将vcpkg.exe的路径添加到系统环境变量PATH中
可以直接使用在线安装包,但仍然建议下载源代码自行构建。目前只需要编译qt基础模块。
https://doc.qt.io/qt-6/getting-sources-from-git.html
https://doc.qt.io/qt-6/windows-building.html
https://doc.qt.io/qt-6/configure-options.html
git clone --branch v6.8.2 git://code.qt.io/qt/qt5.git .\src
mkdir build
cd build
..\src\configure.bat -init-submodules -submodules qtbase
..\src\configure.bat -debug-and-release -prefix <path-to-qt>
cmake --build . --parallel
cmake --install .
cmake --install . --config debug
以上步骤中有两次安装,第一次默认只安装发布版
最后,将<path-to-qt>\bin添加到系统环境变量PATH中
克隆源代码并进入源代码目录
mkdir build
cd build
cmake .. -G "Visual Studio 17 2022"
最后,打开build/nativeMCP.sln进行编译和调试(注意先设置主机作为启动选项,或者如果你想调试服务器,则设置相应的服务器作为启动选项)
修改config.json可以轻松配置MCP主机功能
cmd/c,第二个参数取决于服务器类型的选择,对于可执行文件直接填写exe的路径,对于Python程序填写python,对于Node.js则填写npx。请根据服务器的指示填写以下参数[2025-03-26 12:27:30.296][info][Host.cpp::18] MCP Host初始化
[2025-03-26 12:27:30.299][info][ModelAdapter.cpp::22] ModelAdapter初始化:http://localhost:11434, qwen2.5:7b
[2025-03-26 12:27:30.397][info][Host.cpp::67]
可用工具列表:
cpp-time:
getCurrentTime 获取当前时间
waitTime 等待指定的时间
server-test:
getAvailableIP 获取可用的IP列表
sendToIP 将内容发送到指定的ip地址
testMultiParams 测试多参数的工具调用
>>> 获取当前时间,并发送给所有可用的ip
[2025-03-26 12:29:08.349][info][Host.cpp::123] 调用MCP工具[cpp-time::getCurrentTime]: {
"datetime": "2205-03-26T12:29:08.346",
"timezone": "Asia/Shanghai"
}
[2025-03-26 12:29:20.160][info][Host.cpp::123] 调用MCP工具[server-test::getAvailableIP]: {
"ip_list": [
"192.168.1.201",
"192.168.1.202",
"192.168.1.203"
]
}
[2025-03-26 12:30:07.784][info][Host.cpp::123] 调用MCP工具[server-test::sendToIP]: {
"content": "当前时间为:2025-03-26T12:29:08.346 (Asia/Shanghai)",
"ip": "192.168.1.201",
"status": "发送成功"
}
[2025-03-26 12:30:07.788][info][Host.cpp::123] 调用MCP工具[server-test::sendToIP]: {
"content": "当前时间为:2025-03-26T12:29:08.346 (Asia/Shanghai)",
"ip": "192.168.1.202",
"status": "发送成功"
}
[2025-03-26 12:30:07.789][info][Host.cpp::123] 调用MCP工具[server-test::sendToIP]: {
"content": "当前时间为:2025-03-26T12:29:08.346 (Asia/Shanghai)",
"ip": "192.168.1.203",
"status": "发送成功"
}
当前时间为:2025-03-26T12:29:08.346 (Asia/Shanghai)
已经将当前时间发送给所有可用的IP地址,发送状态均为成功。