Skip to content

Expose mcp's streamablehttp_client' httpx_client_factory on akd.tools.mcp_tools.StreamableHTTPConnectionParams #3005

Closed

Description

@mikeedjones

I want to be able to set the ssl cert for requests made by an adk agent to an MCP server.

You can customise the httpx client used by mcp using the httpx_client_factory on mcp.client.streamable_http.streamablehttp_client but to access that parameter you need to subclass both MCPSessionManager and MCPToolset as:

import os
import ssl
from datetime import timedelta

import httpx
from aiohttp_retry import Any
from google.adk.tools.mcp_tool import MCPToolset as _MCPToolset
from google.adk.tools.mcp_tool.mcp_session_manager import (
    MCPSessionManager as _MCPSessionManager,
)
from google.adk.tools.mcp_tool.mcp_session_manager import (
    StreamableHTTPConnectionParams,
)
from mcp.client.streamable_http import streamablehttp_client

SSLCONTEXT = ssl.create_default_context(cafile=os.environ.get("MY_CERT", None))


def create_mcp_http_client(
    headers: dict[str, str] | None = None,
    timeout: httpx.Timeout | None = None,
    auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
    # Set MCP defaults
    kwargs: dict[str, Any] = {
        "follow_redirects": True,
    }

    # Handle timeout
    if timeout is None:
        kwargs["timeout"] = httpx.Timeout(30.0)
    else:
        kwargs["timeout"] = timeout

    # Handle headers
    if headers is not None:
        kwargs["headers"] = headers

    # Handle authentication
    if auth is not None:
        kwargs["auth"] = auth

    return httpx.AsyncClient(verify=SSLCONTEXT, **kwargs)


class MCPSessionManager(_MCPSessionManager):
    def _create_client(self, merged_headers: dict[str, str] | None = None):
        if not isinstance(self._connection_params, StreamableHTTPConnectionParams):
            raise ValueError(
                "Unable to initialize connection. Connection should be"
                " StreamableHTTPConnectionParams, but got"
                f" {self._connection_params.__class__.__name__}"
            )
        client = streamablehttp_client(
            url=self._connection_params.url,
            headers=merged_headers,
            timeout=timedelta(seconds=self._connection_params.timeout),
            sse_read_timeout=timedelta(seconds=self._connection_params.sse_read_timeout),
            terminate_on_close=self._connection_params.terminate_on_close,
            httpx_client_factory=create_mcp_http_client,
        )
        return client


class MCPToolset(_MCPToolset):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._mcp_session_manager = MCPSessionManager(
            connection_params=self._connection_params,
            errlog=self._errlog,
        )

Can we instead expose httpx_client_factory on StreamableHTTPConnectionParams?

Activity

  1. added
    mcp
    on Sep 23, 2025
  2. added theissue type on Sep 23, 2025
  3. added this to the 10/27 ADK Week milestone on Oct 27, 2025
  4. dongyuj1 commented on Oct 28, 2025

    @dongyuj1

    This seems to be talking about same ask for #2963

  5. hangfei commented on Nov 6, 2025

    @hangfei
    Collaborator

    duplicate of #2963

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

Metadata

Metadata

Assignees

Labels

mcp

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions