我的mcp服务器演示稳定的mcp服务器
概述
我的mcp服务器是一个全面的mcp服务器示例,演示了使用FastMCP框架的工具、资源和提示。它是模块化的、可扩展的,包括结构化输出、错误处理和异步支持。
本地测试说明
如果您想在克隆存储库或下载并解压缩包后在本地测试服务器:
- 打开
src/demo_server.py转到第114行和第115行。 - 注释mcp.run()
- 取消注释或更新这些行以启用
streamable-http模式:
# mcp.run(transport="streamable-http")
# print("Starting MCP server...")
mcp.run(transport="streamable-http")- 通过以下方式运行 与客户进行测试
- 要使用Cursor等IDE进行测试,请使用~/.cours/mcp.json中mcp-local.json的内容(可能需要重新启动Cursor)
远程测试(uvx)
- 在mcp.json、.fastmcp.json和pyproject.toml中,将Github所有者名称“jasonshen190”替换为您自己的名称
- 不要在本地运行demo_server.py,而是使用~/.cursor/mcp.json中的mcp.json将其用作远程mcp服务器
项目结构
my-mcp-server/
├── src/
│ ├── demo_server.py # Main MCP server with tool/resource/prompt registration
│ ├── setup_and_run.py # Interactive setup and run script
│ ├── test_client.py # Async test client for all features
| |── mcp-local.json # local test server configuration
│ ├── mcp.json # Server configuration
│ └── components/
│ ├── tools.py # Tool logic and models
│ ├── resources.py # Resource logic
│ ├── prompts.py # Prompt logic
│ └── __init__.py
├── requirements.txt
├── pyproject.toml
├── README.md用法
在本地测试服务器
方法1:直接执行
python src/demo_server.py方法2:交互式设置(推荐)
python src/setup_and_run.py与客户进行测试
运行测试客户端以查看所有功能的运行情况:
python src/test_client.py输出示例
运行测试客户端时,您应该看到如下输出:
Connected to my-mcp-server MCP Server!
==================================================
1. Testing Tools:
--------------------
add_numbers_tool(5, 3) = 8
multiply_numbers_tool(4.5, 2.0) = 9.0
calculate_bmi_tool(70kg, 1.75m) = 22.857142857142858
get_weather_tool('New York') = {"temperature": 22.5, "humidity": 65.0, "condition": "partly cloudy", "wind_speed": 12.3}
format_text_tool('hello world', 'uppercase') = HELLO WORLD
get_current_time_tool('UTC') = Current time in UTC: 2024-01-15 10:30:45
2. Testing Resources:
--------------------
config://app = {
"name": "my-mcp-server",
"version": "1.0.0",
"features": ["tools", "resources", "prompts"],
"status": "active"
}...
greeting://Alice = Hello, Alice! Welcome to the my-mcp-server MCP Server.
info://server = {
"server_name": "my-mcp-server",
"description": "A comprehensive MCP server demonstrating various features",
"available_tools": [...]
}...
math://constants = {
"pi": 3.14159265359,
"e": 2.71828182846,
"golden_ratio": 1.61803398875,
"sqrt_2": 1.41421356237
}...
3. Testing Prompts:
--------------------
Available prompts: ['calculator_assistant_prompt', 'weather_assistant_prompt', 'text_formatter_prompt']
calculator_assistant_prompt = You are a helpful calculator assistant. You can:
- Add numbers using the add_numbers tool
- Multiply numbers using the multiply_numbers tool
- Calculate BMI using the calculate_bmi tool
- Get mathematical constants from the math://constants resource
Please help users with their calculations!
==================================================
All tests completed successfully!特性
工具
- add_numbers_tool(a,b):加两个数字(int)
- multiply_numbers_tool(a,b):将两个数字相乘(浮点数)
- calculate_bmi_tool(weight_kg,height_m):根据体重(kg)和身高(m)计算BMI
- get_weather_tool(城市):获取城市的模拟天气数据(返回结构化WeatherData)
- format_text_tool(文本、样式):设置不同样式的文本格式(大写、小写、标题、反转、正常)
- get_current_time_tool(时区):获取指定时区的当前时间(默认:UTC)
资源
- config://app:应用程序配置(名称、版本、功能、状态)
- 问候语://{姓名}:个性化问候
- info://server:服务器信息和可用功能
- math://constants:常见的数学常数(pi、e、golden_ratio、sqrt_2)
鼓励
- 计算器_助手\_ mpt:数学计算助理
- 天气助理:天气相关查询助理
- text_formatter_prompt:文本格式化助手
主文件
- src/demo_server.py:在FastMCP中注册所有工具、资源和提示。运行服务器的入口点。
- src/setup_and_run.py:安装依赖项并以交互方式运行服务器或测试客户端。
- src/test_client.py:异步客户端,通过HTTP测试所有工具、资源和提示。
- src/components/tools.py:实现工具逻辑和WeatherData模型。
- src/components/resources.py:实现资源逻辑并返回JSON编码的数据。
- src/components/prompts.py:为助手实现提示逻辑。
高级用法
- 结构化输出:
get_weather_tool返回结构化天气数据的Pydantic模型。 - 错误处理:工具如
calculate_bmi_tool为无效输入(例如非正高度)引发错误。 - 定制:通过在中编辑相应的文件来添加新的工具/资源/提示
src/components/.
发展
- 模块化、解耦设计,易于扩展和调试
- 用于类型安全的Pydantic模型
- 异步/等待支持
- 全面的错误处理
后续步骤
- 探索 MCP Python SDK文档
- 使用自定义功能构建自己的MCP服务器
- 与Claude Desktop或其他MCP客户端集成
