使用说明
MCP服务器测试
一个全面的模型上下文协议(MCP)服务器实现,具有多种工具,包括计算器、天气和文件操作。
特性
🧮 计算器工具
- 演算
- 安全表达评估
- 支持基本数学函数(sin、cos、sqrt等)
🌤️ 天气工具
- 获取任何城市的天气信息
- 用于演示的模拟天气数据
- 包括温度、湿度、风速和条件
📁 文件操作工具
- 创建、读取、列出和删除文件
- 目录操作
- 安全的文件处理和错误管理
建筑
该项目包括两个实现:
mcp_server.py-使用stdin/stdout的标准MCP协议服务器app.py-基于FastAPI的REST API服务器,用于更轻松的测试和部署
安装
先决条件
- Python 3.8+
- 点
设置
- 克隆存储库:
git clone <repository-url>
cd mcp-server-test
- 创建并激活虚拟环境:
python -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
- 安装依赖项:
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
添加新工具
- 在中定义工具模式
TOOLS词典 - 执行工具功能
- 在适当的端点中添加工具调用处理
- 更新测试
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"]
云部署选项
铁路
- 将您的GitHub存储库连接到Railway
- Railway将自动检测Python应用程序
- 设置启动命令:
uvicorn app:app --host 0.0.0.0 --port $PORT
渲染
- 将您的GitHub存储库连接到Render
- 选择“Web服务”
- 设置启动命令:
uvicorn app:app --host 0.0.0.0 --port $PORT
Heroku
- 创建一个
Procfile:
web: uvicorn app:app --host 0.0.0.0 --port $PORT
- 使用Heroku CLI或GitHub集成进行部署
API文档
运行FastAPI服务器时,请访问 http://localhost:8000/docs 用于交互式API文档。
MCP协议合规性
此服务器实现了MCP(模型上下文协议)规范:
- 基于JSON-RPC 2.0的通信
- 工具发现和执行
- 错误处理和响应格式
- 协议版本协商
贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加新功能的测试
- 提交拉取请求
许可证
该项目根据MIT许可证获得许可。
支持
有关问题和疑问,请在GitHub存储库上打开问题。
注: 天气工具使用模拟数据进行演示。在生产环境中,您将与真实天气API服务集成。
