-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(client): Add capability_extensions parameter to ClientSession #1901
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
jerome3o-anthropic
wants to merge
3
commits into
main
from
jerome/add-capability-extensions-parameter
+79
−0
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9830794
feat(client): Add capability_extensions parameter to ClientSession
jerome3o-anthropic 9a1fee3
refactor: use explicit extensions field instead of spreading kwargs
jerome3o-anthropic 67daa5a
fix: remove unnecessary 'or None' from extensions assignment
jerome3o-anthropic 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 |
|---|---|---|
|
|
@@ -768,3 +768,73 @@ async def mock_server(): | |
| await session.initialize() | ||
|
|
||
| await session.call_tool(name=mocked_tool.name, arguments={"foo": "bar"}, meta=meta) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_client_session_capability_extensions(): | ||
| """Test that capability_extensions are included in the initialize request.""" | ||
| client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage](1) | ||
| server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) | ||
|
|
||
| received_capabilities = None | ||
|
|
||
| # Define capability extensions (e.g., UI extension) | ||
| capability_extensions = {"extensions": {"io.modelcontextprotocol/ui": {"mimeTypes": ["text/html;profile=mcp-app"]}}} | ||
|
|
||
| async def mock_server(): | ||
| nonlocal received_capabilities | ||
|
|
||
| session_message = await client_to_server_receive.receive() | ||
| jsonrpc_request = session_message.message | ||
| assert isinstance(jsonrpc_request.root, JSONRPCRequest) | ||
| request = ClientRequest.model_validate( | ||
| jsonrpc_request.model_dump(by_alias=True, mode="json", exclude_none=True) | ||
| ) | ||
| assert isinstance(request.root, InitializeRequest) | ||
| received_capabilities = request.root.params.capabilities | ||
|
|
||
| result = ServerResult( | ||
| InitializeResult( | ||
| protocol_version=LATEST_PROTOCOL_VERSION, | ||
| capabilities=ServerCapabilities(), | ||
| server_info=Implementation(name="mock-server", version="0.1.0"), | ||
| ) | ||
| ) | ||
|
|
||
| async with server_to_client_send: | ||
| await server_to_client_send.send( | ||
| SessionMessage( | ||
| JSONRPCMessage( | ||
| JSONRPCResponse( | ||
| jsonrpc="2.0", | ||
| id=jsonrpc_request.root.id, | ||
| result=result.model_dump(by_alias=True, mode="json", exclude_none=True), | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
Comment on lines
+785
to
+816
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. Is the server supposed to do something when it's aware of the extension? |
||
| # Receive initialized notification | ||
| await client_to_server_receive.receive() | ||
|
|
||
| async with ( | ||
| ClientSession( | ||
| server_to_client_receive, | ||
| client_to_server_send, | ||
| capability_extensions=capability_extensions, | ||
| ) as session, | ||
| anyio.create_task_group() as tg, | ||
| client_to_server_send, | ||
| client_to_server_receive, | ||
| server_to_client_send, | ||
| server_to_client_receive, | ||
| ): | ||
| tg.start_soon(mock_server) | ||
| await session.initialize() | ||
|
|
||
| # Assert that the capability extensions were included in the request | ||
| assert received_capabilities is not None | ||
| # The extensions should be present via Pydantic's extra fields | ||
| caps_dict = received_capabilities.model_dump() | ||
| assert "extensions" in caps_dict | ||
| assert "io.modelcontextprotocol/ui" in caps_dict["extensions"] | ||
| assert caps_dict["extensions"]["io.modelcontextprotocol/ui"]["mimeTypes"] == ["text/html;profile=mcp-app"] | ||
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.
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.
I don't think it should allow any key here, this will be an issue if in the future
ClientCapabilitieshas a new key but it's used by someone.If the spec doesn't have this explicit, I think the python SDK should.
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.
@claude can you please make this change
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.
Uh, I don't actually think we should specify
extensionhere, I think this is just something made up for MCP apps (not sure, the SEP for extensions isn't in yet)But - in principle we should be able to allow any property to be added to any MCP message, that was the intention of making all the objects
allow_extra=trueetcThere 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 is what we're trying to build, but idk if "extensions" is the standard way to add things here
https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/draft/apps.mdx#clientserver-capability-negotiation
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.
Actually modelcontextprotocol/modelcontextprotocol#1724 looks like it will be specifically under
"extensions"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.
I'm not sure I agree on it being explicitly called "extensions" in the SDK as SEP-1724 (modelcontextprotocol/modelcontextprotocol#1724) hasn't landed yet, and this kinda feels like adding a convention which should really be in the spec.