-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Open
lukacf
wants to merge
8
commits into
modelcontextprotocol:main
Choose a base branch
from
lukacf:fix-cancelled-error-response
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.
+192
−0
Open
Changes from all commits
Commits
Show all changes
8 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 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,186 @@ | ||
"""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_cancelled_request_no_double_response(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this test? it doesn't look like it's testing much and |
||
"""Verify server handles cancelled requests without double response.""" | ||
|
||
# Create server with a slow tool | ||
server = Server("test-server") | ||
|
||
# Track when tool is called | ||
ev_tool_called = anyio.Event() | ||
request_id = None | ||
|
||
@server.list_tools() | ||
async def handle_list_tools() -> list[Tool]: | ||
return [ | ||
Tool( | ||
name="slow_tool", | ||
description="A slow tool for testing cancellation", | ||
inputSchema={}, | ||
) | ||
] | ||
|
||
@server.call_tool() | ||
async def handle_call_tool(name: str, arguments: dict | None) -> list: | ||
nonlocal request_id | ||
if name == "slow_tool": | ||
request_id = server.request_context.request_id | ||
ev_tool_called.set() | ||
await anyio.sleep(10) # Long running operation | ||
return [types.TextContent(type="text", text="Tool called")] | ||
raise ValueError(f"Unknown tool: {name}") | ||
|
||
# Connect client to server | ||
async with create_connected_server_and_client_session(server) as client: | ||
# Start the slow tool call in a separate task | ||
async def make_request(): | ||
try: | ||
await client.send_request( | ||
ClientRequest( | ||
CallToolRequest( | ||
method="tools/call", | ||
params=CallToolRequestParams(name="slow_tool", arguments={}), | ||
) | ||
), | ||
CallToolResult, | ||
) | ||
pytest.fail("Request should have been cancelled") | ||
except McpError as e: | ||
# Expected - request was cancelled | ||
assert e.error.code == 0 # Request cancelled error code | ||
|
||
# Start the request | ||
request_task = anyio.create_task_group() | ||
async with request_task: | ||
request_task.start_soon(make_request) | ||
|
||
# Wait for tool to start executing | ||
await ev_tool_called.wait() | ||
|
||
# Send cancellation notification | ||
assert request_id is not None | ||
await client.send_notification( | ||
ClientNotification( | ||
CancelledNotification( | ||
method="notifications/cancelled", | ||
params=CancelledNotificationParams( | ||
requestId=request_id, | ||
reason="Test cancellation", | ||
), | ||
) | ||
) | ||
) | ||
|
||
# The request should be cancelled and raise McpError | ||
|
||
|
||
@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!