feat(client): support HTTP auth in ClientSessionGroup
MU
1mo ago+272+272+012345678901234567890123456789-2-2-0123456789
Closes #1723
Summary
ClientSessionGroup server parameters (SseServerParameters and StreamableHttpParameters) lacked an auth field, requiring users to manually manage bearer tokens via the headers parameter and preventing automated OAuth flows (like OAuthClientProvider) with dynamic client registration and token refresh.
This change adds an optional auth: httpx2.Auth | None = None parameter to both HTTP server parameter models and propagates it into sse_client() and create_mcp_http_client().
Changes
- Added
auth: httpx2.Auth | None = Field(default=None, description="Optional HTTPX authentication handler.", exclude=True)toSseServerParametersandStreamableHttpParameters. - Added
ConfigDict(arbitrary_types_allowed=True)to both parameter models. - Propagated
authtosse_client()andcreate_mcp_http_client()withinClientSessionGroup._establish_session(). - Added unit tests in
tests/client/test_session_group.pyverifying:authis passed tosse_client()in SSE transport.authis passed tocreate_mcp_http_client()in Streamable HTTP transport.- Pydantic serialization (
model_dump,model_dump_json) safely excludesauth. - Independent auth state is maintained when multiple servers are connected to the same session group.
- Existing custom headers and auth handler cleanly coexist.
- Updated
docs/client/session-groups.mdwith an authentication usage guide and example.
Backward Compatibility
100% backward compatible:
authdefaults toNone.- Existing static
headersconfigurations remain fully supported. StdioServerParametersis unaffected.
Test Plan
uv run --frozen pytest tests/client/test_session_group.py(17 passed, 100% coverage onsession_group.py).uv run --frozen pytest tests/client/(778 passed, 0 regressions).uv run --frozen pyright src/mcp/client/session_group.py tests/client/test_session_group.py(0 errors).uv run --frozen ruff check .anduv run --frozen ruff format --check .(passed).