Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: isolate streamable HTTP POST errors#2613
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
Closed
pragnyanramtha
wants to merge
2
commits into
modelcontextprotocol:v1.x
from
pragnyanramtha:pragnyan/v1x-2604-post-error-isolation
+163
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -1043,6 +1043,154 @@ async def test_streamable_http_client_error_handling(initialized_client_session: | ||
| assert "Unknown resource: unknown://test-error" in exc_info.value.error.message | ||
| @pytest.mark.anyio | ||
| async def test_streamable_http_client_http_error_does_not_cancel_concurrent_request(): | ||
| """Test that one POST HTTP error does not tear down an unrelated request.""" | ||
| good_request_started = anyio.Event() | ||
| allow_good_response = anyio.Event() | ||
| async def handler(request: httpx.Request) -> httpx.Response: | ||
| payload = json.loads(request.content) | ||
| request_id = payload["id"] | ||
| uri = payload["params"]["uri"] | ||
| if uri == "foobar://bad": | ||
| with anyio.fail_after(5): | ||
| await good_request_started.wait() | ||
| return httpx.Response(400, request=request, json={"error": "boom"}) | ||
| assert uri == "foobar://good" | ||
| good_request_started.set() | ||
| with anyio.fail_after(5): | ||
| await allow_good_response.wait() | ||
| return httpx.Response( | ||
| 200, | ||
| request=request, | ||
| headers={"content-type": "application/json"}, | ||
| json={ | ||
| "jsonrpc": "2.0", | ||
| "id": request_id, | ||
| "result": { | ||
| "contents": [ | ||
| { | ||
| "uri": uri, | ||
| "mimeType": "text/plain", | ||
| "text": "good response", | ||
| } | ||
| ] | ||
| }, | ||
| }, | ||
| ) | ||
| good_result: types.ReadResourceResult | None = None | ||
| bad_error: Exception | None = None | ||
| bad_request_failed = anyio.Event() | ||
| async def run_good_request(session: ClientSession) -> None: | ||
| nonlocal good_result | ||
| good_result = await session.send_request( | ||
| types.ClientRequest( | ||
| types.ReadResourceRequest( | ||
| params=types.ReadResourceRequestParams(uri=AnyUrl("foobar://good")), | ||
| ) | ||
| ), | ||
| types.ReadResourceResult, | ||
| ) | ||
| async def run_bad_request(session: ClientSession) -> None: | ||
| nonlocal bad_error | ||
| try: | ||
| await session.send_request( | ||
| types.ClientRequest( | ||
| types.ReadResourceRequest( | ||
| params=types.ReadResourceRequestParams(uri=AnyUrl("foobar://bad")), | ||
| ) | ||
| ), | ||
| types.ReadResourceResult, | ||
| ) | ||
| except Exception as exc: | ||
| bad_error = exc | ||
| bad_request_failed.set() | ||
| transport = httpx.MockTransport(handler) | ||
| async with httpx.AsyncClient(transport=transport) as http_client: | ||
| async with streamable_http_client("http://test/mcp", http_client=http_client) as streams: # pragma: no branch | ||
| read_stream, write_stream, _ = streams | ||
| async with ClientSession(read_stream, write_stream) as session: # pragma: no branch | ||
| async with anyio.create_task_group() as tg: | ||
| tg.start_soon(run_good_request, session) | ||
| with anyio.fail_after(5): | ||
| await good_request_started.wait() | ||
| tg.start_soon(run_bad_request, session) | ||
| with anyio.fail_after(5): | ||
| await bad_request_failed.wait() | ||
pragnyanramtha marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| allow_good_response.set() | ||
| assert isinstance(bad_error, McpError) | ||
| assert bad_error.error.code == types.INTERNAL_ERROR | ||
| assert bad_error.error.message == "Server returned HTTP 400" | ||
| assert bad_error.error.data == {"status_code": 400} | ||
| assert good_result is not None | ||
| assert isinstance(good_result.contents[0], types.TextResourceContents) | ||
| assert good_result.contents[0].text == "good response" | ||
| @pytest.mark.anyio | ||
| async def test_streamable_http_client_notification_http_error_does_not_cancel_transport(): | ||
| """Test POST HTTP errors for notifications do not synthesize responses.""" | ||
| notification_seen = anyio.Event() | ||
| async def handler(request: httpx.Request) -> httpx.Response: | ||
| payload = json.loads(request.content) | ||
| if "id" not in payload: | ||
| notification_seen.set() | ||
| return httpx.Response(500, request=request, json={"error": "boom"}) | ||
| return httpx.Response( | ||
| 200, | ||
| request=request, | ||
| headers={"content-type": "application/json"}, | ||
| json={ | ||
| "jsonrpc": "2.0", | ||
| "id": payload["id"], | ||
| "result": { | ||
| "contents": [ | ||
| { | ||
| "uri": "foobar://good", | ||
| "mimeType": "text/plain", | ||
| "text": "good response", | ||
| } | ||
| ] | ||
| }, | ||
| }, | ||
| ) | ||
| transport = httpx.MockTransport(handler) | ||
| async with httpx.AsyncClient(transport=transport) as http_client: | ||
| async with streamable_http_client("http://test/mcp", http_client=http_client) as streams: # pragma: no branch | ||
| read_stream, write_stream, _ = streams | ||
| async with ClientSession(read_stream, write_stream) as session: # pragma: no branch | ||
| await session.send_notification(types.ClientNotification(types.RootsListChangedNotification())) | ||
| with anyio.fail_after(5): | ||
| await notification_seen.wait() | ||
| result = await session.send_request( | ||
| types.ClientRequest( | ||
| types.ReadResourceRequest( | ||
| params=types.ReadResourceRequestParams(uri=AnyUrl("foobar://good")), | ||
| ) | ||
| ), | ||
| types.ReadResourceResult, | ||
| ) | ||
| assert isinstance(result.contents[0], types.TextResourceContents) | ||
| assert result.contents[0].text == "good response" | ||
| @pytest.mark.anyio | ||
| async def test_streamable_http_client_session_persistence(basic_server: None, basic_server_url: str): | ||
| """Test that session ID persists across requests.""" | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.