MCP协议服务器

一个基于C++实现的MCP协议服务器,提供天气查询和随机名言功能,支持跨平台运行。

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

使用说明

C++MCP服务器

用C++实现的模型上下文协议(MCP)服务器。与Claude Desktop或其他MCP客户端一起提供天气信息和名言。

特点

  • 符合标准:实施MCP协议官方规格
  • 轻量化:用C++实现,运行速度快
  • 可扩展:轻松添加新工具
  • 跨平台:支持Windows、Linux和macOS

提供的功能

工具

  1. 获取天气:查询各城市的天气信息

    • OpenWeatherMap API集成
    • 无需API密钥即可使用示例数据进行测试
  2. Getrandomquote:提供随机名言

    • 内置20句韩语名言
    • 每次呼叫随机选择

要求

必需

  • CMake 3.20或更高版本
  • 支持C++17的编译器(GCC、Clang、MSVC)
  • nlohmann/json库

选择

  • CURL库(使用实际天气API时)
  • vcpkg(包管理)

安装

1.存储库克隆

git clone https://github.com/yourusername/cpp-mcp-server.git
cd cpp-mcp-server

2.安装依赖性

使用vcpkg(建议)

# vcpkg 설치 (아직 없다면)
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh  # Linux/macOS
# 또는
.\bootstrap-vcpkg.bat  # Windows

# 패키지 설치
./vcpkg install nlohmann-json
./vcpkg install curl  # 선택사항

# VCPKG_ROOT 환경변수 설정
export VCPKG_ROOT=/path/to/vcpkg  # Linux/macOS
# 또는
set VCPKG_ROOT=C:\path\to\vcpkg  # Windows

手动安装

nlohmann/json是头文件库。 include/ 目录中 json.hpp下载即可。

3.构建

视窗

.\build.bat

Linux/macOS

mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
cmake --build . --config Release

构建完成后,将生成可执行文件:

  • 窗户: build/Release/mcp_server.exe
  • Linux/macOS: build/mcp_server

用法

1.设置环境变量(可选)

要使用实际天气数据,需要OpenWeatherMap API密钥。

# Linux/macOS
export OPENWEATHER_API_KEY=your_api_key_here

# Windows (CMD)
set OPENWEATHER_API_KEY=your_api_key_here

# Windows (PowerShell)
$env:OPENWEATHER_API_KEY="your_api_key_here"

API密钥 开放天气地图可以在免费发放。

2.运行服务器

直接运行(用于测试)

# Windows
.\build\Release\mcp_server.exe

# Linux/macOS
./build/mcp_server

与Claude Desktop配合使用

修改Claude Desktop设置文件。

视窗: %APPDATA%\Claude\claude_desktop_config.json

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

Linux: ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "cpp-mcp-server": {
      "command": "C:/path/to/build/Release/mcp_server.exe",
      "env": {
        "OPENWEATHER_API_KEY": "your_api_key_here"
      }
    }
  }
}

设置后重新启动Claude Desktop时,服务器将自动连接。

3.测试

提供Python测试脚本。

python test.py

项目结构

cpp-mcp-server/
├── src/
│   ├── main.cpp              # 메인 진입점
│   ├── mcp_server.cpp        # MCP 서버 구현
│   ├── mcp_server.h          # MCP 서버 헤더
│   ├── weather_api.cpp       # 날씨 API 구현
│   ├── weather_api.h         # 날씨 API 헤더
│   ├── quote_api.cpp         # 명언 API 구현
│   └── quote_api.h           # 명언 API 헤더
├── include/
│   └── json.hpp              # nlohmann/json (선택)
├── CMakeLists.txt            # CMake 빌드 설정
├── build.bat                 # Windows 빌드 스크립트
├── test.py                   # 테스트 스크립트
└── README.md

添加新工具

  1. 创建API类
// my_api.h
#pragma once
#include "mcp_server.h"

class MyAPI {
public:
    ToolResult myFunction(const json& arguments);
};
  1. 注册Tool
// main.cpp
MyAPI myApi;

Tool myTool{
    "myFunction",
    "My function description",
    {
        {"type", "object"},
        {"properties", {
            {"param1", {
                {"type", "string"},
                {"description", "Parameter description"}
            }}
        }},
        {"required", json::array({"param1"})}
    }
};

server.registerTool(myTool, 
    [&myApi](const json& args) {
        return myApi.myFunction(args);
    }
);

MCP协议结构

消息格式

Content-Length: <byte_count>\r\n
\r\n
<JSON-RPC message>

JSON-RPC消息

{
  "jsonrpc": "2.0",
  "id": "unique_id",
  "method": "method_name",
  "params": {}
}

支持方法

  • initialize:初始化服务器
  • tools/list:查看工具列表
  • tools/call:运行Tool

技术堆栈

  • 语言:C++17
  • 构建系统:CMake
  • 解析JSON:nlohmann/json
  • HTTP通信:libcurl(可选)
  • 协议:JSON-RPC 2.0

故障排除

找不到CURL

✗ CURL not found - Weather API will return sample data only

未安装CURL库。可以使用vcpkg安装,也可以使用示例数据进行测试,无需API密钥。

vcpkg install curl

找不到nlohmann/json

Could not find nlohmann_json

使用vcpkg安装, include/ 目录中 json.hpp请直接下载。

vcpkg install nlohmann-json

构建错误(Windows)

请确保安装了MSVC编译器。需要Visual Studio 2019或更高版本。

运行错误(Linux)

./mcp_server: error while loading shared libraries: libcurl.so.4

安装CURL库。

# Ubuntu/Debian
sudo apt-get install libcurl4-openssl-dev

# CentOS/RHEL
sudo yum install libcurl-devel

许可证

MIT许可证

参考资料

贡献

请通过Issue告知错误报告或功能建议。欢迎Pull Request!

作者