-
Notifications
You must be signed in to change notification settings - Fork 2.2k
server: skip duplicate response on CancelledError #1153
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
Merged
ihrpr
merged 9 commits into
modelcontextprotocol:main
from
lukacf:fix-cancelled-error-response
Jul 25, 2025
+117
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4e14a4
server: skip duplicate response on CancelledError
lukacf 36805b6
Apply ruff formatting to test file
lukacf 452dfb9
Use pytest.mark.anyio instead of asyncio
lukacf 58e1c66
Add type ignore comments for test mocks
lukacf 81f3e36
Remove unused ServerResult import
lukacf 81bd191
Remove empty params dict from PingRequest
lukacf a5a112a
Fix cancel handling to use anyio idioms and improve tests
lukacf b006981
Merge branch 'main' into fix-cancelled-error-response
lukacf 76da5e4
remove redundant test
ihrpr 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,111 @@ | ||
"""Test that cancelled requests don't cause double responses.""" | ||
|
||
import anyio | ||
import pytest | ||
|
||
import mcp.types as types | ||
from mcp.server.lowlevel.server import Server | ||
from mcp.shared.exceptions import McpError | ||
from mcp.shared.memory import create_connected_server_and_client_session | ||
from mcp.types import ( | ||
CallToolRequest, | ||
CallToolRequestParams, | ||
CallToolResult, | ||
CancelledNotification, | ||
CancelledNotificationParams, | ||
ClientNotification, | ||
ClientRequest, | ||
Tool, | ||
) | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_server_remains_functional_after_cancel(): | ||
"""Verify server can handle new requests after a cancellation.""" | ||
|
||
server = Server("test-server") | ||
|
||
# Track tool calls | ||
call_count = 0 | ||
ev_first_call = anyio.Event() | ||
first_request_id = None | ||
|
||
@server.list_tools() | ||
async def handle_list_tools() -> list[Tool]: | ||
return [ | ||
Tool( | ||
name="test_tool", | ||
description="Tool for testing", | ||
inputSchema={}, | ||
) | ||
] | ||
|
||
@server.call_tool() | ||
async def handle_call_tool(name: str, arguments: dict | None) -> list: | ||
nonlocal call_count, first_request_id | ||
if name == "test_tool": | ||
call_count += 1 | ||
if call_count == 1: | ||
first_request_id = server.request_context.request_id | ||
ev_first_call.set() | ||
await anyio.sleep(5) # First call is slow | ||
return [types.TextContent(type="text", text=f"Call number: {call_count}")] | ||
raise ValueError(f"Unknown tool: {name}") | ||
|
||
async with create_connected_server_and_client_session(server) as client: | ||
# First request (will be cancelled) | ||
async def first_request(): | ||
try: | ||
await client.send_request( | ||
ClientRequest( | ||
CallToolRequest( | ||
method="tools/call", | ||
params=CallToolRequestParams(name="test_tool", arguments={}), | ||
) | ||
), | ||
CallToolResult, | ||
) | ||
pytest.fail("First request should have been cancelled") | ||
except McpError: | ||
pass # Expected | ||
|
||
# Start first request | ||
async with anyio.create_task_group() as tg: | ||
tg.start_soon(first_request) | ||
|
||
# Wait for it to start | ||
await ev_first_call.wait() | ||
|
||
# Cancel it | ||
assert first_request_id is not None | ||
await client.send_notification( | ||
ClientNotification( | ||
CancelledNotification( | ||
method="notifications/cancelled", | ||
params=CancelledNotificationParams( | ||
requestId=first_request_id, | ||
reason="Testing server recovery", | ||
), | ||
) | ||
) | ||
) | ||
|
||
# Second request (should work normally) | ||
result = await client.send_request( | ||
ClientRequest( | ||
CallToolRequest( | ||
method="tools/call", | ||
params=CallToolRequestParams(name="test_tool", arguments={}), | ||
) | ||
), | ||
CallToolResult, | ||
) | ||
|
||
# Verify second request completed successfully | ||
assert len(result.content) == 1 | ||
# Type narrowing for pyright | ||
content = result.content[0] | ||
assert content.type == "text" | ||
assert isinstance(content, types.TextContent) | ||
assert content.text == "Call number: 2" | ||
assert call_count == 2 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of mocking happening. I'd suggest to use
test_integration
or leveragecreate_connected_server_and_client_session
and test withCancelledNotification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks for the directions!