GPT 应用程序 SDK – 最小化 MCP 服务器(Python)
这个项目提供了一个最小化的MCP服务器,用于支持ChatGPT应用(自定义应用),通过使用 mcp 该软件包的FastMCP服务器。
你所得到的
- 两款工具已准备好用于Apps SDK的自动调用:
- hello-world返回一个问候语和一个内联小部件(ui://widget/hello.html)。 - clothes-near-me返回5个示例服装品牌分支以及一个内联小部件列表(ui://widget/brands.html)。
- 适用于ChatGPT应用的可流式传输HTTP端点。
栈(或堆栈)
- Python 3.11及以上版本
mcpFastMCP服务器,Uvicorn
在本地运行
- 见
README.run.md用于分步设置和验证命令。
(本地运行时的)终端点
- 可流式传输的HTTP:
POST http://localhost:8000/mcp
- 标题/页眉: Content-Type: application/json, Accept: application/json, text/event-stream
- SSE(可选):
- 开放流(或:公开流) GET http://localhost:8000/mcp (与)一起;带有 Accept: text/event-stream - 发送消息: POST http://localhost:8000/mcp/messages?sessionId={id}
示例请求(可流式传输的HTTP)
# List tools
curl -sS -H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":"t1","method":"tools/list"}' \
http://localhost:8000/mcp
# Call hello-world
curl -sS -H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":"t2","method":"tools/call","params":{"name":"hello-world","arguments":{"name":"gpt-demo"}}}' \
http://localhost:8000/mcp
# Call clothes-near-me
curl -sS -H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":"t3","method":"tools/call","params":{"name":"clothes-near-me","arguments":{"location":"Delhi"}}}' \
http://localhost:8000/mcpChatGPT 应用配置
- 在ChatGPT > 设置 > 连接器 > 自定义应用中:
- SSE URL(服务器发送事件统一资源定位符): http://localhost:8000/mcp - 消息URL: http://localhost:8000/mcp/messages?sessionId={sessionId}
- 然后在聊天中尝试:
- “调用 hello-world,名称为:gpt-demo。” - “找一家离我近的服装品牌。”(或明确地说“拨打clothes-near-me服务,位置:德里。”)
故障排除
- 406 不可接受:请确保您的请求包含
Accept: application/json, text/event-stream(对于可流式传输的HTTP),或Accept: text/event-stream对于SSE GET(注:SSE通常指服务器发送事件,GET是HTTP方法之一,此处可能指使用SSE技术的GET请求或相关操作)。
