Repository navigation
Closed
Description
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?
Metadata
Metadata
Assignees
Labels
mcp[Component] This issues is related to MCP support
Activity
dongyuj1 commentedon Oct 28, 2025
This seems to be talking about same ask for #2963
hangfei commentedon Nov 6, 2025
duplicate of #2963
feat: expose mcps streamable http custom httpx factory parameter
feat: expose mcps streamable http custom httpx factory parameter