-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: prevent silent failure on GET stream so clients don't hang #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sicoyle
wants to merge
23
commits into
modelcontextprotocol:main
Choose a base branch
from
sicoyle:fix-hang-github-mcp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−2
Open
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e2f8d62
fix(hang): fix streamable_http GET 405 handling to prevent hangs with…
sicoyle 709151f
Merge branch 'main' into fix-hang-github-mcp
sicoyle d2f14dd
fix: address conflict with main
sicoyle 48cfb00
style: end with newline char
sicoyle 1dd7f04
fix: address conflict with main again
sicoyle a86eabe
Merge branch 'main' into fix-hang-github-mcp
sicoyle 581c817
style: appease linter
sicoyle 6b172cc
fix(build): fixes for pre-commit run --all-files
sicoyle d460614
Merge branch 'main' into fix-hang-github-mcp
sicoyle b9fc7b4
Merge branch 'main' into fix-hang-github-mcp
sicoyle 22134fa
style: appease linter
sicoyle 9c24e70
Potential fix for pull request finding
sicoyle 0ee54bc
fix: address copilot feedback
sicoyle f0d5fb3
fix(build): up test coverage
sicoyle 7d8ec65
fix(build): add more test coverage
sicoyle 23d8ee8
fix: update to make build pass
sicoyle 0340602
fix: update coverage for 3.11 and 3.14
sicoyle eb4e691
Merge branch 'main' into fix-hang-github-mcp
sicoyle 740b503
fix: see if this makes build happy
sicoyle ca12fcd
fix: udpates for build
sicoyle 1aaad1d
fix: build again hopefully
sicoyle a368054
fix: try this
sicoyle 333e9ad
fix: updates for biuld
sicoyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Test for streamable_http client handling of 405 Method Not Allowed on GET requests. | ||
|
|
||
| This test verifies the fix for the race condition where the client hangs when connecting | ||
| to servers (like GitHub MCP) that don't support GET for SSE events. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import anyio | ||
| import httpx | ||
| import pytest | ||
| from starlette.applications import Starlette | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse, Response | ||
| from starlette.routing import Route | ||
|
|
||
| from mcp.client.session import ClientSession | ||
| from mcp.client.streamable_http import streamable_http_client | ||
| from mcp.types import InitializeResult | ||
|
|
||
|
|
||
| async def mock_github_endpoint(request: Request) -> Response: | ||
| """Mock endpoint that returns 405 for GET (like GitHub MCP).""" | ||
| if request.method == "GET": | ||
| return Response( | ||
| content="Method Not Allowed", | ||
| status_code=405, | ||
| headers={"Allow": "POST, DELETE"}, | ||
| ) | ||
| elif request.method == "POST": | ||
| body = await request.json() | ||
| if body.get("method") == "initialize": | ||
| return JSONResponse( | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": body.get("id"), | ||
| "result": { | ||
| "protocolVersion": "2025-03-26", | ||
| "serverInfo": {"name": "mock_github_server", "version": "1.0"}, | ||
| "capabilities": {"tools": {}}, | ||
| }, | ||
| }, | ||
| headers={"mcp-session-id": "test-session"}, | ||
| ) | ||
| elif body.get("method") == "notifications/initialized": | ||
| return Response(status_code=202) | ||
| elif body.get("method") == "tools/list": | ||
| return JSONResponse( | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": body.get("id"), | ||
| "result": { | ||
| "tools": [ | ||
| { | ||
| "name": "test_tool", | ||
| "description": "A test tool", | ||
| "inputSchema": {"type": "object", "properties": {}}, | ||
| } | ||
| ] | ||
| }, | ||
| } | ||
| ) | ||
| return Response(status_code=405) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_405_get_stream_does_not_hang(caplog: pytest.LogCaptureFixture): | ||
| """Test that client handles 405 on GET gracefully and doesn't hang.""" | ||
| app = Starlette(routes=[Route("/mcp", mock_github_endpoint, methods=["GET", "POST"])]) | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| async with httpx.AsyncClient( | ||
| transport=httpx.ASGITransport(app=app), base_url="http://testserver", timeout=5.0 | ||
| ) as http_client: | ||
| async with streamable_http_client("http://testserver/mcp", http_client=http_client) as ( | ||
| read_stream, | ||
| write_stream, | ||
| _, | ||
| ): | ||
| async with ClientSession(read_stream, write_stream) as session: | ||
| # Initialize sends the initialized notification internally | ||
| init_result = await session.initialize() | ||
| assert isinstance(init_result, InitializeResult) | ||
|
|
||
| # Give the GET stream task time to fail with 405 | ||
| await anyio.sleep(0.2) | ||
|
|
||
| # This should not hang and will now complete successfully | ||
| tools_result = await session.list_tools() | ||
| assert len(tools_result.tools) == 1 | ||
| assert tools_result.tools[0].name == "test_tool" | ||
|
sicoyle marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Verify the 405 was logged and no retries occurred | ||
| log_messages = [record.getMessage() for record in caplog.records] | ||
| assert any("Server does not support GET for SSE events (405 Method Not Allowed)" in msg for msg in log_messages), ( | ||
| f"Expected 405 log message not found in: {log_messages}" | ||
| ) | ||
|
|
||
| reconnect_messages = [msg for msg in log_messages if "reconnecting" in msg.lower()] | ||
| assert len(reconnect_messages) == 0, f"Should not retry on 405, but found: {reconnect_messages}" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.