MCP服务器测试

一个全面的模型上下文协议(MCP)服务器实现,包含计算器、天气和文件操作等多种工具。

  • by koya-rama
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 koya-rama/mcp-server-test

使用说明

MCP服务器测试

一个全面的模型上下文协议(MCP)服务器实现,具有多种工具,包括计算器、天气和文件操作。

特性

🧮 计算器工具

  • 演算
  • 安全表达评估
  • 支持基本数学函数(sin、cos、sqrt等)

🌤️ 天气工具

  • 获取任何城市的天气信息
  • 用于演示的模拟天气数据
  • 包括温度、湿度、风速和条件

📁 文件操作工具

  • 创建、读取、列出和删除文件
  • 目录操作
  • 安全的文件处理和错误管理

建筑

该项目包括两个实现:

  1. mcp_server.py -使用stdin/stdout的标准MCP协议服务器
  2. app.py -基于FastAPI的REST API服务器,用于更轻松的测试和部署

安装

先决条件

  • Python 3.8+
  • 点

设置

  1. 克隆存储库:
git clone <repository-url>
cd mcp-server-test
  1. 创建并激活虚拟环境:
python -m venv venv

# On Windows
venv\Scripts\activate

# On macOS/Linux
source venv/bin/activate
  1. 安装依赖项:
pip install -r requirements.txt

用法

运行FastAPI服务器

python app.py

服务器将于启动 http://localhost:8000

可用端点

  • GET / -服务器信息
  • GET /tools -列出可用工具
  • POST /tools/call -调用特定工具
  • POST /mcp -MCP协议端点
  • GET /docs -交互式API文档(Swagger UI)

测试服务器

运行测试客户端以查看所有正在运行的工具:

python test_client.py

API调用示例

计算器工具

curl -X POST "http://localhost:8000/tools/call" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "calculator",
    "arguments": {"expression": "2 + 3 * 4"}
  }'

天气工具

curl -X POST "http://localhost:8000/tools/call" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "weather",
    "arguments": {"city": "New York"}
  }'

文件操作工具

curl -X POST "http://localhost:8000/tools/call" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "file_operations",
    "arguments": {
      "operation": "create",
      "filename": "hello.txt",
      "content": "Hello, World!"
    }
  }'

运行标准MCP服务器

对于标准MCP协议实施:

python mcp_server.py

此版本通过stdin/stdout进行通信,并遵循MCP协议规范。

发展

项目结构

mcp-server-test/
├── app.py              # FastAPI server implementation
├── mcp_server.py       # Standard MCP protocol server
├── test_client.py      # Test client for demonstration
├── requirements.txt    # Python dependencies
├── README.md          # This file
└── venv/              # Virtual environment

添加新工具

  1. 在中定义工具模式 TOOLS 词典
  2. 执行工具功能
  3. 在适当的端点中添加工具调用处理
  4. 更新测试 test_client.py

部署

地方发展

uvicorn app:app --reload --host 0.0.0.0 --port 8000

Docker部署

创建一个 Dockerfile:

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

云部署选项

铁路

  1. 将您的GitHub存储库连接到Railway
  2. Railway将自动检测Python应用程序
  3. 设置启动命令: uvicorn app:app --host 0.0.0.0 --port $PORT

渲染

  1. 将您的GitHub存储库连接到Render
  2. 选择“Web服务”
  3. 设置启动命令: uvicorn app:app --host 0.0.0.0 --port $PORT

Heroku

  1. 创建一个 Procfile:
web: uvicorn app:app --host 0.0.0.0 --port $PORT
  1. 使用Heroku CLI或GitHub集成进行部署

API文档

运行FastAPI服务器时,请访问 http://localhost:8000/docs 用于交互式API文档。

MCP协议合规性

此服务器实现了MCP(模型上下文协议)规范:

  • 基于JSON-RPC 2.0的通信
  • 工具发现和执行
  • 错误处理和响应格式
  • 协议版本协商

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 提交拉取请求

许可证

该项目根据MIT许可证获得许可。

支持

有关问题和疑问,请在GitHub存储库上打开问题。


注: 天气工具使用模拟数据进行演示。在生产环境中,您将与真实天气API服务集成。