fix(mcp-server): correct SSE message route to prevent initialization timeout

The SSE transport's POST handler was mounted at /messages/, but the
SseServerTransport SDK prepends the Starlette root_path ("/sse") to
the endpoint parameter, instructing clients to POST to /sse/messages/.
This path mismatch caused MCP initialization requests to never reach
the handler, resulting in a 60s+ timeout on connect.

Fix: move the Mount to /sse/messages/ and place it before the /sse
catch-all route, since Starlette's Mount uses prefix matching and
/sse would otherwise intercept /sse/messages/ requests.
main
FincherYuan 2026-07-28 17:15:15 +08:00 committed by lyingbug
parent c3b08d6c26
commit 0f8232b7eb
1 changed files with 1 additions and 1 deletions

View File

@ -1357,8 +1357,8 @@ async def run_sse(host: str, port: int):
starlette_app = Starlette(
routes=[
Mount("/sse/messages/", app=sse.handle_post_message), # 更具体的路由必须在 /sse 前面
Mount("/sse", app=handle_sse),
Mount("/messages/", app=sse.handle_post_message),
]
)
starlette_app = MCPAuthMiddleware(starlette_app, auth_token)