Skip to content

Conversation

@jerome3o-anthropic
Copy link
Member

Summary

Add a new capability_extensions parameter to ClientSession.__init__() that allows clients to include additional capability fields in the initialize request.

Motivation

Currently, if a client wants to advertise protocol extensions (like io.modelcontextprotocol/ui for UI rendering) in the initialize request, they must override the entire initialize() method and duplicate its internal logic. This is error-prone and creates maintenance burden when the SDK's initialize() implementation changes.

This PR provides a clean escape hatch for clients to extend capabilities without subclassing.

Example Usage

session = ClientSession(
    read_stream,
    write_stream,
    capability_extensions={
        "extensions": {
            "io.modelcontextprotocol/ui": {
                "mimeTypes": ["text/html;profile=mcp-app"]
            }
        }
    }
)

Implementation

The extensions are merged into ClientCapabilities using Pydantic's extra fields feature (model_config = {'extra': 'allow'} on MCPModel).

Testing

Added a test that verifies capability extensions are properly included in the initialize request.

Add a new `capability_extensions` parameter to `ClientSession.__init__()` that
allows clients to include additional capability fields in the initialize request.

This enables clients to advertise protocol extensions (like `io.modelcontextprotocol/ui`)
without having to override the `initialize()` method.

Example usage:
```python
session = ClientSession(
    read_stream,
    write_stream,
    capability_extensions={
        "extensions": {
            "io.modelcontextprotocol/ui": {
                "mimeTypes": ["text/html;profile=mcp-app"]
            }
        }
    }
)
```

The extensions are merged into `ClientCapabilities` using Pydantic's extra fields
feature (`model_config = {'extra': 'allow'}`).
@jerome3o-anthropic jerome3o-anthropic force-pushed the jerome/add-capability-extensions-parameter branch from 8deee3f to 9830794 Compare January 17, 2026 10:20
experimental=None,
roots=roots,
tasks=self._task_handlers.build_capability(),
**self._capability_extensions,
Copy link
Member

@Kludex Kludex Jan 17, 2026

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 ClientCapabilities has a new key but it's used by someone.

Suggested change
**self._capability_extensions,
extensions=self._capability_extensions,

If the spec doesn't have this explicit, I think the python SDK should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants