-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: raise clear error for server-to-client requests in stateless mode #1827
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| """Tests for stateless HTTP mode limitations. | ||
| Stateless HTTP mode does not support server-to-client requests because there | ||
| is no persistent connection for bidirectional communication. These tests verify | ||
| that appropriate errors are raised when attempting to use unsupported features. | ||
| See: https://github.com/modelcontextprotocol/python-sdk/issues/1097 | ||
| """ | ||
|
|
||
| import anyio | ||
| import pytest | ||
|
|
||
| import mcp.types as types | ||
| from mcp.server.models import InitializationOptions | ||
| from mcp.server.session import ServerSession | ||
| from mcp.shared.message import SessionMessage | ||
| from mcp.types import ServerCapabilities | ||
|
|
||
|
|
||
| def create_test_streams(): | ||
| """Create memory streams for testing.""" | ||
| server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) | ||
| client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1) | ||
| return ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) | ||
|
|
||
|
|
||
| def create_init_options(): | ||
| """Create default initialization options for testing.""" | ||
| return InitializationOptions( | ||
| server_name="test", | ||
| server_version="0.1.0", | ||
| capabilities=ServerCapabilities(), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_list_roots_fails_in_stateless_mode(): | ||
| """Test that list_roots raises RuntimeError in stateless mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
|
Member
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. Add the
Contributor
Author
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. Yep fair will do |
||
| await session.list_roots() | ||
|
|
||
| assert "stateless HTTP mode" in str(exc_info.value) | ||
| assert "list_roots" in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_create_message_fails_in_stateless_mode(): | ||
| """Test that create_message raises RuntimeError in stateless mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await session.create_message( | ||
| messages=[ | ||
| types.SamplingMessage( | ||
| role="user", | ||
| content=types.TextContent(type="text", text="hello"), | ||
| ) | ||
| ], | ||
| max_tokens=100, | ||
| ) | ||
|
|
||
| assert "stateless HTTP mode" in str(exc_info.value) | ||
| assert "sampling" in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_elicit_form_fails_in_stateless_mode(): | ||
| """Test that elicit_form raises RuntimeError in stateless mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
|
Comment on lines
+110
to
+128
Member
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. Can't this whole thing be a fixture? |
||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await session.elicit_form( | ||
| message="Please provide input", | ||
| requestedSchema={"type": "object", "properties": {}}, | ||
| ) | ||
|
|
||
| assert "stateless HTTP mode" in str(exc_info.value) | ||
| assert "elicitation" in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_elicit_url_fails_in_stateless_mode(): | ||
| """Test that elicit_url raises RuntimeError in stateless mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await session.elicit_url( | ||
| message="Please authenticate", | ||
| url="https://example.com/auth", | ||
| elicitation_id="test-123", | ||
| ) | ||
|
|
||
| assert "stateless HTTP mode" in str(exc_info.value) | ||
| assert "elicitation" in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_elicit_deprecated_fails_in_stateless_mode(): | ||
| """Test that the deprecated elicit method also fails in stateless mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await session.elicit( | ||
| message="Please provide input", | ||
| requestedSchema={"type": "object", "properties": {}}, | ||
| ) | ||
|
|
||
| assert "stateless HTTP mode" in str(exc_info.value) | ||
| assert "elicitation" in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_require_stateful_mode_does_not_raise_in_stateful_mode(): | ||
| """Test that _require_stateful_mode does not raise in stateful mode.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=False, # Stateful mode | ||
| ) as session: | ||
| # These should not raise - the check passes in stateful mode | ||
| session._require_stateful_mode("list_roots") | ||
| session._require_stateful_mode("sampling") | ||
| session._require_stateful_mode("elicitation") | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_stateless_error_message_is_actionable(): | ||
| """Test that the error message provides actionable guidance.""" | ||
| ( | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| ) = create_test_streams() | ||
|
|
||
| async with ( | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| async with ServerSession( | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| create_init_options(), | ||
| stateless=True, | ||
| ) as session: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await session.list_roots() | ||
|
|
||
| error_message = str(exc_info.value) | ||
| # Should mention it's stateless mode | ||
| assert "stateless HTTP mode" in error_message | ||
| # Should explain why it doesn't work | ||
| assert "server-to-client requests" in error_message | ||
| # Should tell user how to fix it | ||
| assert "stateless_http=False" in error_message | ||
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.
This kind of private function adds a bit of overhead while reading the code. If possible, can we create a specific exception and do the check on each feature method itself?
On
MethodNotSupportedyou can set up the message.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.
MethodNotSupported what are you thinking for the error message? As currently it's specific to stateless http and MethodNotSupported sounds more generic
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.
You can think of a better name, but the point stands.
Although I suggested this, I still think that something is missing conceptually - we should be able to check this or define somehow the capabilities of stateless in a different way - and be able to handle this before it reaches the methods.
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.
Yea, although this is more of a bandaid fix given that this version of stateless mode isn't really supported in the spec. The upcoming changes to the spec will significantly change how statefulness will work anyway so I didn't want to spend too much time worrying about how it looks since it has to change anyway.