feat(client): support HTTP auth in ClientSessionGroup

MU
1mo ago
+272-2

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) to SseServerParameters and StreamableHttpParameters.
  • Added ConfigDict(arbitrary_types_allowed=True) to both parameter models.
  • Propagated auth to sse_client() and create_mcp_http_client() within ClientSessionGroup._establish_session().
  • Added unit tests in tests/client/test_session_group.py verifying:
    • auth is passed to sse_client() in SSE transport.
    • auth is passed to create_mcp_http_client() in Streamable HTTP transport.
    • Pydantic serialization (model_dump, model_dump_json) safely excludes auth.
    • 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.md with an authentication usage guide and example.

Backward Compatibility

100% backward compatible:

  • auth defaults to None.
  • Existing static headers configurations remain fully supported.
  • StdioServerParameters is unaffected.

Test Plan

  • uv run --frozen pytest tests/client/test_session_group.py (17 passed, 100% coverage on session_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 . and uv run --frozen ruff format --check . (passed).