使用说明
MCP计算器
使用FastMCP构建的带有计算器工具的模型上下文协议(MCP)服务器示例。
设置和安装
先决条件
- Python 3.8或更高版本
- pip包管理器
1.克隆存储库
git clone https://github.com/arulrajnet/mcp-calculator.git
cd mcp-calculator
2.创建虚拟环境(推荐)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
3.安装依赖项
# Install the package in development mode
pip install -e .
# Or install with development dependencies
pip install -e .[dev]
运行服务器
启动MCP计算器服务器:
python server.py
服务器将于启动 http://localhost:8000/mcp/calculator 使用可流式传输的HTTP。
您应该看到类似于以下内容的输出:
Uvicorn running on http://0.0.0.0:8000
与客户进行测试
1.运行测试客户端
在新终端中(服务器运行时):
python client.py
这将连接到服务器并列出所有可用工具。
2.客户端使用示例
您可以修改 client.py 测试特定的计算器功能:
from fastmcp import Client
import asyncio
async def main():
# Connect via SSE
async with Client("http://localhost:8000/mcp/calculator") as client:
# List available tools
tools = await client.list_tools()
print(f"Available tools: {[tool['name'] for tool in tools]}")
# Test some calculations
result = await client.call_tool("add", {"a": 5, "b": 3})
print(f"5 + 3 = {result}")
result = await client.call_tool("factorial", {"n": 5})
print(f"5! = {result}")
result = await client.call_tool("is_prime", {"n": 17})
print(f"Is 17 prime? {result}")
if __name__ == "__main__":
asyncio.run(main())
发展
代码格式化
black .
isort .
类型检查
mypy .
运行测试
pytest
在覆盖范围内运行测试
pytest --cov=. --cov-report=html
作者
Arul
