MCP协议测试端点

一个基于FastAPI的MCP协议测试端点,提供Hello World消息和工具调用功能,用于验证MCP客户端集成。

  • by bankszach
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 bankszach/mcp-date-and-time

使用说明

Hello World MCP FastAPI端点

这个项目暴露了最小的 模型上下文协议 由FastAPI支持的服务器。它注册了一个资源和一个工具,并以“Hello World”消息进行响应,以便您可以端到端验证MCP客户端集成。

设置

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

运行服务器

uvicorn app:app --reload --port 8080

准备就绪探头可在 http://127.0.0.1:8080/,健康终点位于 http://127.0.0.1:8080/healthz,MCP可流式传输HTTP端点安装在 http://127.0.0.1:8080/mcp.

从MCP客户端尝试

将您的MCP兼容LLM或SDK指向 http://127.0.0.1:8080/mcp。您应该看到:

  • resource://hello 返回 "Hello from the Model Context Protocol!"
  • say_hello 工具返回问候。

这些是连接更丰富的资源和工具的起点。

使用Docker运行(单个命令)

docker compose up --build

服务器将在以下时间可访问 http://127.0.0.1:8080/ (以及 /healthz)构建完成后。

MCP(流式HTTP)

  • 端点: https://<service>/mcp
  • CORS:暴露 Mcp-Session-Id 浏览器客户端的标头(代理生成器)。

工具

  • ping() -> "pong"
  • server_time(fmt?: string) -> string (UTC)
  • echo(text: string) -> string

快速检查

# 1) Handshake (should return Mcp-Session-Id)
curl -i https://<service>/mcp

# 2) List tools (replace $SID with header from step 1)
SID=<paste-session-id>
curl -s -H "Content-Type: application/json" -H "Mcp-Session-Id: $SID" \
  -X POST --data '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' \
  https://<service>/mcp

代理生成器

  • MCP节点URL: https://<service>/mcp
  • 认证:无
  • 连接后,工具 ping, server_time, echo 应该是可选择的。

MCP(流式HTTP)

端点: https://<service>/mcp (不需要尾随斜线)

工具

  • ping()->“pong”
  • 服务器时间(fmt?:string)->字符串(UTC)
  • echo(文本:string)->string
  • date_math(expr:string)->string(UTC)。示例:“+2h”、“-15m”、“+1d 30m”

通过curl进行验证

# 1) Handshake — should return Mcp-Session-Id header
curl -i https://<service>/mcp

# 2) Save session id and list tools
SID=$(curl -sI https://<service>/mcp | awk -F': ' '/^Mcp-Session-Id:/ {print $2}' | tr -d '\r')

curl -s -H "Content-Type: application/json" -H "Mcp-Session-Id: $SID" \
  -X POST --data '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' \
  https://<service>/mcp | jq .

# 3) Call a tool
curl -s -H "Content-Type: application/json" -H "Mcp-Session-Id: $SID" \
  -X POST --data '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"date_math","arguments":{"expr":"+2h"}}}' \
  https://<service>/mcp | jq .

代理生成器

  • 节点类型:MCP
  • 网址: https://<service>/mcp
  • 传输:流式HTTP
  • 认证:无
  • 单击连接→ 工具应列出:ping、server_time、echo、date_math