我的mcp
my-mcp是一个使用FastMCP构建的模型上下文协议(mcp)服务器,具有动态工具加载功能。
特性
- 动态工具加载:工具会自动从以下位置发现和加载
src/tools/ - 每个文件一个工具:每个工具都是一个具有与文件名匹配的函数的单个文件
- FastMCP集成:利用FastMCP实现强大的MCP协议处理
- 配置管理:通过工具进行特定配置
mcp.yaml - 快速失败:如果任何工具加载失败,服务器将不会启动
- 自动生成的测试:工具验证的自动测试生成
项目结构
src/
├── tools/ # Tool implementations (one file per tool)
│ ├── echo.py # Example echo tool
│ └── __init__.py # Auto-generated tool registry
├── core/ # Dynamic loading framework
│ ├── server.py # Dynamic MCP server
│ └── utils.py # Shared utilities
└── main.py # Entry point
mcp.yaml # Configuration file
tests/ # Generated tests快速开始
选项1:本地开发(使用Python/uv)
- 安装依赖项:
uv sync- 运行服务器:
# Stdio mode (default MCP transport)
uv run python src/main.py
# HTTP mode with WebSocket MCP endpoint
uv run python src/main.py --http
# HTTP mode with custom host/port
uv run python src/main.py --http --host 0.0.0.0 --port 8080- 使用uv脚本:
# Development mode (HTTP on port 3000)
uv run dev
# HTTP mode
uv run dev-http
# Stdio mode
uv run start- 添加新工具:
# Create a new tool (no tool types needed!)
arctl mcp add-tool weather
# The tool file will be created at src/tools/weather.py
# Edit it to implement your tool logic选项2:仅Docker开发(不需要本地Python/uv)
- 构建Docker镜像:
arctl mcp build --verbose- 在容器中运行:
docker run -i my-mcp:latest- 添加新工具:
# Create a new tool
arctl mcp add-tool weather
# Edit the tool file, then rebuild
arctl mcp buildHTTP传输模式
出于开发和集成目的,服务器支持在HTTP模式下运行。
以HTTP模式启动
# Command line flag
python src/main.py --http
# Environment variable
MCP_TRANSPORT_MODE=http python src/main.py
# Custom host and port
python src/main.py --http --host localhost --port 8080创建工具
基本工具结构
每个工具都是一个Python文件 src/tools/ 包含一个装饰有 @mcp.tool():
# src/tools/weather.py
from core.server import mcp
from core.utils import get_tool_config, get_env_var
@mcp.tool()
def weather(location: str) -> str:
"""Get weather information for a location."""
# Get tool configuration
config = get_tool_config("weather")
api_key = get_env_var(config.get("api_key_env", "WEATHER_API_KEY"))
base_url = config.get("base_url", "https://api.openweathermap.org/data/2.5")
# TODO: Implement weather API call
return f"Weather for {location}: Sunny, 72°F"工具示例
生成的工具模板包括常见模式的注释示例:
# HTTP API calls
# async with httpx.AsyncClient() as client:
# response = await client.get(f"{base_url}/weather?q={location}&appid={api_key}")
# return response.json()
# Database operations
# async with asyncpg.connect(connection_string) as conn:
# result = await conn.fetchrow("SELECT * FROM weather WHERE location = $1", location)
# return dict(result)
# File processing
# with open(file_path, 'r') as f:
# content = f.read()
# return {"content": content, "size": len(content)}配置
在中配置工具 mcp.yaml:
tools:
weather:
api_key_env: "WEATHER_API_KEY"
base_url: "https://api.openweathermap.org/data/2.5"
timeout: 30
database:
connection_string_env: "DATABASE_URL"
max_connections: 10测试
运行生成的测试以验证您的工具是否正确加载:
uv run pytest tests/发展
添加依赖关系
更新 pyproject.toml 并运行:
uv sync代码质量
uv run black .
uv run ruff check .
uv run mypy .部署
码头工人
# Build image (handles lockfile automatically)
arctl mcp build
# Run container
docker run -i my-mcp:latest