MCP服务器骨架
一个基本的模型上下文协议(MCP)服务器框架,演示如何在逻辑文件夹层次结构中组织和构造MCP工具。
概述
该项目为构建具有干净、有序结构的MCP服务器提供了一个基础模板。它包括:
- 模块化工具架构:工具以专用的方式组织
tools/便于管理和发现的目录 - 自动工具注册:服务器会自动发现并加载工具目录中的所有工具
- 示例实现:包括一个简单的串反转工具,用于演示MCP工具模式
特性
- 组织结构:工具、资源和配置的明确分离
- 自动发现:自动注册所有Python模块
tools/目录 - 示例工具:基本
reverse_string反转任何输入字符串的工具 - FastMCP集成:使用FastMCP框架构建,便于开发
项目结构
src/
├── tools/ # All MCP tools go here
│ └── tool.py # Example string reversal tool
├── resources/ # Static resources and data files
├── main.py # FastAPI server entry point
├── shared_mcp_object.py # Shared MCP instance and tool registration
└── config.py # Configuration management入门指南
- 再进行:
uv sync- 运行服务器:
uv run fastapi dev main.py- 通过MCP连接:配置您的MCP客户端以连接到
http://localhost:8000/sse
添加新工具
要添加新工具,请执行以下操作:
- 在中创建一个新的Python文件
tools/目录 - 导入共享MCP对象:
from shared_mcp_object import mcp - 使用
@mcp.tool()装饰器来注册你的函数 - 服务器启动时,将自动发现并注册该工具
工具使用示例
包括 reverse_string 该工具演示了基本模式:
@mcp.tool()
def reverse_string(input: str) -> str:
"""Reverses the input string"""
return input[::-1]该框架为构建具有多种工具和功能的更复杂的MCP服务器提供了坚实的基础。
