MCP服务器Bootstrap:构建模块化指挥中心服务器的模板
MCP Server Bootstrap是一个基于Python的模板,用于使用Mondel上下文协议(MCP)创建模块化指挥中心服务器。它为构建可扩展的指挥中心应用程序提供了结构化的基础,支持基本的数学运算和模块化算术函数。
该项目实现了一个模块化架构,在运行时将单个功能文件组合到一个服务器核心中。这种方法在开发过程中保持代码组织,同时适应当前MCP SDK在模块化方面的限制。该模板为构建具有自定义功能的可扩展命令中心应用程序提供了基础。
存储库结构
mcp_server/ # Main package directory
├── build_mcp.py # Script to combine function modules into core server file
├── functions/ # Directory containing individual function implementations
│ ├── add.py # Addition operation implementation
│ ├── product19.py # Modulo 19 multiplication implementation
│ └── subtract.py # Subtraction operation implementation
├── utils/ # Utility functions and helper modules
│ └── helper.py # Common helper functions
├── pyproject.toml # Project metadata and dependencies
├── requirements.txt # Project dependencies
└── setup.sh # Installation and setup script使用说明
先决条件
- Python 3.11或更高版本
- 已安装并配置Amazon Q CLI
- pip包管理器
- 类Unix环境(用于setup.sh)
所需软件包:
- fastmcp>=1.0.0
- 媒染剂>=1.10.0
安装
# Clone the repository
git clone
cd mcp-bootstrap
# Make the setup script executable
chmod +x setup.sh
# The setup script will configure the server for use with Amazon Q CLI
# by creating necessary configuration in $HOME/.aws/amazonq/mcp.json
# Run the setup script
./setup.sh快速开始
- 在中创建新函数
mcp_server/functions目录:
# mcp_server/functions/example.py
@mcp.tool(
name="example",
description="Example function"
)
def example_function(a: int, b: int) -> int:
return a + b- 构建服务器:
python mcp_server/build_mcp.py- 运行服务器:
python mcp_server/core_combined.py故障排除
常见问题和解决方案:
- 未找到模块错误
- 错误: ModuleNotFoundError: No module named 'fastmcp' - 解决方案:确保您已激活虚拟环境并安装了依赖项:
source .venv/bin/activate
pip install -r requirements.txt- 构建失败
- 错误: FileNotFoundError: core_combined.py not found - 解决方案:从项目根目录运行构建脚本:
python mcp_server/build_mcp.py- 版本兼容性
- 错误: Python version X.X is less than required 3.11 - 解决方案:安装Python 3.11或更高版本,并确保它在您的PATH中
数据流
MCP服务器通过将单个功能模块组合到一个核心服务器文件中来处理函数调用,然后该文件处理传入的请求并将其路由到适当的函数实现。
[Client Request] -> [MCP Server Core] -> [Function Router] -> [Individual Function] -> [Response]
|-> [Function Registry]组件交互:
- 构建脚本将单个功能文件组合到一个核心服务器文件中
- MCP服务器使用组合功能进行初始化
- 客户端请求由服务器核心接收
- 根据工具名称将请求路由到相应的功能
- 函数处理输入并返回结果
- 服务器核心格式化并将响应发送回客户端
- 错误处理在服务器和功能级别进行管理
