使用说明
FastAPI MCP 服务器(单工具)
一个简洁且可投入生产的 MCP服务器 用……实现 FastAPI 并且可部署至 **Google Cloud Run(谷歌云运行)**它暴露了一个单一工具 get_time 通过OpenAI 模型上下文协议(MCP) 通过HTTP使用JSON-RPC 2.0。
特点/功能
tools/list和tools/call严格按照规范实施- 回报 内容 (供人类阅读的文本)和 结构化内容 (机器用的JSON)
- 正确的JSON-RPC错误代码和输入验证
jsonschema - 已启用CORS(开发环境)+ 可选的API密钥认证
x-api-key - 健康检查(
/healthz) 以及简单的请求记录 - Dockerfile 和一键式 Cloud Run 部署
终点(或:试验终点、研究终点)
POST /— JSON-RPC 2.0 的终端接口用于tools/list并且tools/callGET /healthz— 返回{ "ok": true }
工具
get_time
描述: 返回当前的UTC时间戳,可选地进行格式化。
输入模式(或输入架构):
{
"type": "object",
"properties": {
"format": {
"type": "string",
"description": "strftime format, e.g. %Y-%m-%dT%H:%M:%SZ"
}
},
"required": []
}
输出结构化内容:
{
"iso": "2025-01-01T00:00:00Z",
"formatted": "optional if format passed"
}
本地开发
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
uvicorn app:app --reload --port 8000
测试:
curl -s localhost:8000/ -H 'content-type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | jq
curl -s localhost:8000/ -H 'content-type: application/json' -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_time","arguments":{"format":"%Y-%m-%dT%H:%M:%SZ"}}}' | jq
如果你设置 API_KEY=secret,包含一个标题: -H 'x-api-key: secret'
部署到 Google Cloud Run
选项1 — 来自源代码(最简单):
./deploy.sh
# Set PROJECT_ID, REGION, SERVICE (or export them)
选项2 — 通过Cloud Build流水线:
gcloud builds submit --project $PROJECT_ID --substitutions=_REGION=us-central1,_SERVICE=mcp-clock
将(代码/功能)接入OpenAI Agent Builder
- 添加一个新 MCP服务器 给你的代理(或:给你的经纪人)
- 服务器URL您的 Cloud Run URL(例如。
https://mcp-clock-xxxx-uc.a.run.app) - 标签:
clock - 允许使用的工具:
get_time(或留空以允许所有)
许可证
麻省理工学院(MIT)
