一个实用的Cookiecutter模板,用于构建连接Claude AI到外部API的MCP服务器。使用FastMCP构建,并准备好在Render.com上部署。
此模板生成一个完整的MCP服务器项目,使Claude AI能够与任何API交互。可以将其视为连接Claude和您想要使用的外部服务之间的桥梁。
示例场景:
要求:Python 3.11或更新版本
pip install cookiecutter
cookiecutter https://github.com/pietroperona/mcp-server-template
您将被询问一些问题:
project_name: 新闻API服务器
project_slug: news-api-server [自动生成]
author_name: 您的名字
author_email: you@example.com
github_username: yourusername
api_base_url: https://newsapi.org/v2
api_service_type: REST API
auth_type: API密钥
include_rate_limiting: 是
include_caching: 否
render_deployment: 是
docker_support: 是
license: MIT
cd news-api-server
cp .env.example .env
编辑.env文件,添加您的API凭证:
API_BASE_URL=https://newsapi.org/v2
API_KEY=your_news_api_key
pip install -r requirements.txt
python main.py
您的MCP服务器现在正在http://localhost:8000运行。
在您的Claude Desktop MCP配置中添加以下内容:
{
"mcpServers": {
"news-api": {
"command": "python",
"args": ["news-api-server/main.py"],
"cwd": "news-api-server"
}
}
}
现在Claude可以获取新闻:“最新的科技头条是什么?”
您的MCP服务器还公开了一个SSE(服务器发送事件)端点/sse,可以在浏览器中的Claude Web中使用:
/sse端点:
https://your-mcp-server.onrender.com/sse
注意:/sse端点自动包含在此模板生成的所有MCP服务器中。
每个生成的项目包括:
get_api_status - 检查您的API是否正常工作list_resources - 浏览可用的数据(文章、用户、产品等)get_resource_by_id - 获取特定项目的详细信息create_resource - 添加新数据update_resource - 修改现有数据delete_resource - 删除数据当您问Claude“展示最近的技术新闻”,会发生以下情况:
list_resources(resource_type="articles", category="technology")https://newsapi.org/v2/everything?q=technologynews-api-server/
├── core/
│ ├── config.py # API凭证及设置
│ ├── auth.py # 处理API认证
│ └── client.py # 带重试机制的HTTP客户端
├── tools/
│ └── example_tools.py # 6个MCP工具
├── docs/ # 设置指南及故障排除
│ ├── configuration.md # 详细的配置选项
│ ├── quick-start.md # 快速入门指南
│ ├── README.md # 文档概览
│ └── troubleshooting.md # 常见问题及解决方案
├── main.py # FastMCP服务器
└── .env.example # 配置模板
注意:当您的服务器运行时,可能会创建以_data/结尾的目录(如cache_data/)。这些目录通过.gitignore自动排除,因为它们包含不应提交的运行时数据。
模板处理不同的身份验证方法:
API密钥(最常见)
API_KEY=your_key_here
API_KEY_HEADER=X-API-Key
Bearer Token
BEARER_TOKEN=your_token_here
OAuth2(针对复杂API)
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
基本身份验证
USERNAME=your_username
PASSWORD=your_password
模板包括带有生产优化设置的render.yaml。
重要提示:对于Claude Web浏览器,请确保记录下您部署的服务的公共URL。Claude Web将通过/sse端点连接到您的MCP服务器:
https://your-service-name.onrender.com/sse
注意:Docker支持包含在此模板中,但尚未经过广泛测试。欢迎社区反馈和贡献。
docker build -t my-mcp-server .
docker run -p 8000:8000 --env-file .env my-mcp-server
对于Claude Web:访问SSE端点http://your-docker-host:8000/sse
# 生成项目
cookiecutter https://github.com/pietroperona/mcp-server-template
# 项目:新闻API服务器
# API:NewsAPI.org(免费层)
# 结果:Claude可以根据类别/关键词获取最新新闻
尝试一下:NewsAPI.org - 每天1000次免费请求
# 生成项目
cookiecutter https://github.com/pietroperona/mcp-server-template
# 项目:天气API服务器
# API:OpenWeatherMap(免费)
# 结果:Claude可以查询全球天气
尝试一下:OpenWeatherMap - 免费API,每天1000次调用
# 生成项目
cookiecutter https://github.com/pietroperona/mcp-server-template
# 项目:股市服务器
# API:Alpha Vantage(免费)
# 结果:Claude可以查询股票价格和市场数据
尝试一下:Alpha Vantage - 免费API密钥,每分钟5次调用
# 生成项目
cookiecutter https://github.com/pietroperona/mcp-server-template
# 项目:社交媒体服务器
# API:Twitter/X API(付费)
# 结果:Claude可以发布推文并阅读社交媒体动态
让我们逐步构建一个新闻API服务器:
cookiecutter https://github.com/pietroperona/mcp-server-template
project_name: 新闻API服务器
project_slug: news-api-server
author_name: John Smith
api_service_type: REST API
auth_type: API密钥
api_base_url: https://newsapi.org/v2
include_rate_limiting: 是
render_deployment: 是
cd news-api-server
cp .env.example .env
编辑.env:
API_BASE_URL=https://newsapi.org/v2
API_KEY=your_actual_api_key_here
API_KEY_HEADER=X-API-Key
python main.py
询问Claude:“最新的科技头条是什么?”
Claude将使用您的工具:
工具:list_resources
参数:resource_type="articles", category="technology"
结果:最新的科技新闻文章标题、描述和链接
生成的工具是通用的,但易于定制:
# 在tools/example_tools.py中
async def list_resources_async(resource_type: str = "articles", category: str = "general"):
# 根据您的API进行定制
endpoint = f"/everything?category={category}"
response = await client.get(endpoint)
return response
@mcp.tool()
def get_headlines_by_category(category: str, country: str = "us") -> str:
"""根据类别和国家获取头条新闻"""
result = run_async_tool(get_headlines_async, category, country)
return json.dumps(result, indent=2)
# 在.env中
RATE_LIMIT_REQUESTS=60 # 60次请求
RATE_LIMIT_WINDOW=60 # 每分钟
“身份验证失败”
“工具未出现在Claude中”
“Claude Web未显示我的工具”
/sse端点“连接超时”
.env中增加API_TIMEOUT“API版本化问题”
.env文件中设置API_VERSION=none或留空“事件循环已关闭”错误
python core/auth.py # 测试身份验证
python core/client.py # 测试API连接
python main.py # 启动MCP服务器
DEBUG=true python main.py
显示详细的请求/响应日志。
此模板采用优化的方法来管理aiohttp会话:
# 为每个请求创建一个新的会话(防止“事件循环已关闭”错误)
async with aiohttp.ClientSession() as session:
async with session.request(...) as response:
# 处理响应
这种模式确保:
构建MCP服务器涉及大量样板代码:
此模板立即为您提供所有这些功能,因此您可以专注于连接到您特定的API。
基于经过验证的工具:FastMCP用于MCP框架,模型上下文协议用于Claude集成。
适用于:
/sse端点)MIT许可 - 可用于任何目的,商业或个人用途。
/sse端点增加了对Claude Web浏览器的支持准备将Claude连接到您最喜欢的API了吗?
pip install cookiecutter
cookiecutter https://github.com/pietroperona/mcp-server-template