Repository navigation
fix: wait for initialize endpoint before updating client configs - #2504
Conversation
This ensures MCP servers are fully ready before notifying clients, preventing connection timing issues. The proxy now: 1. Starts the container and proxy 2. Repeatedly calls the initialize endpoint until it succeeds (HTTP 200) 3. Waits up to 5 minutes with exponential backoff (100ms-2s) 4. Only then updates client configurations For streamable-http transport, the proxy calls POST /mcp with proper headers (Accept: application/json, text/event-stream) to ensure compatibility with servers that require both content types. Fixes #2499
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2504 +/- ##
==========================================
- Coverage 55.84% 55.72% -0.13%
==========================================
Files 312 312
Lines 29541 29608 +67
==========================================
Hits 16498 16498
- Misses 11601 11668 +67
Partials 1442 1442 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
amirejaz
left a comment
There was a problem hiding this comment.
./bin/thv run time --proxy-mode sse is not working, it is stuck on:
3:49PM �[34mINFO�[0m Waiting for MCP server to accept initialize requests at http://localhost:38870/messages (timeout: 5m0s).
Signed-off-by: nigel brown <[email protected]>
Signed-off-by: nigel brown <[email protected]>
Signed-off-by: nigel brown <[email protected]>
Signed-off-by: nigel brown <[email protected]>
because it waits for init. Longer timeout Signed-off-by: nigel brown <[email protected]>
Signed-off-by: nigel brown <[email protected]>
|
There is an issue with calling initialize for STDIO MCP servers multiple times. The behaviour is not determined by the spec so they may reject the request #1982. That's why ToolHive avoids calling initialize itself and only forwards the client request. |
|
Discussion here. |
The OSV server transport changed from sse to streamable-http on November 4, 2025 in commit 31f2d8b (PR #2441). Signed-off-by: nigel brown <[email protected]>
The OSV MCP server transport changed from sse to streamable-http in the registry update on 2025-11-04 (commit 31f2d8b). This was causing test failures with 5-minute timeouts as the tests were still trying to start the server with --transport sse. Changes: - Updated all transport references from sse to streamable-http - Changed MCP client initialization from NewMCPClientForSSE to NewMCPClientForStreamableHTTP - Updated URL endpoint expectations from /sse to /mcp - Fixed formatting issues with golangci-lint Tests now pass: 13 Passed | 0 Failed
Signed-off-by: nigel brown <[email protected]>
Description
This PR ensures MCP servers are fully ready before notifying clients, preventing connection timing issues described in #2499.
Changes
The proxy now:
Implementation Details
For streamable-http and SSE transports, the runner makes direct HTTP calls to the MCP initialize endpoint with proper headers:
Content-Type: application/jsonAccept: application/json, text/event-stream(required by some MCP servers)MCP-Protocol-Version: 2024-11-05The function uses exponential backoff to avoid hammering the server, with delays capped at 2 seconds between attempts and an overall timeout of 5 minutes for slow-starting servers like mcp-optimizer.
Testing
Fixes #2499, #2271
There is a video of this working here.