adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Pass Httpx Auth with token provider to MCPSessionManager

Open dannovikov opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

MCPToolset parameter auth_credential takes an access token for the authenticated tool flow. However, our access tokens expire, and I don't see a way to pass a token provider to fetch refreshed tokens automatically.

I did see that the MCPSessionManager has a function _create_client which creates streamablehttp_client and sse_client, depending on the transport mode. These functions accept an auth parameter which is passed internally to the underlying httpx client. But this auth parameter isn't being exposed in the MCPSessionManager nor is it exposed in the top level user interface.

The approach we took was to subclass MCPSessionManager to add a token_provider property on init, and override _create_client, to create our Httpx auth and pass it to the client creation functions. This approach led us to bypass the auth_scheme and auth_credentials mechanisms because, in our estimation, they don't support token refresh mechanisms that the httpx client natively does.

Describe the solution you'd like I think a preferable solution would be to expose the httpx auth property in the top-level user interface, like MCPToolset, or lower down in MCPTool or MCPSessionManager, so that I can pass an httpx auth object that goes into underlying the httpx MCP client.

Describe alternatives you've considered An alternative I've considered is to allow us to pass a callable as the auth credential, rather than the token itself, which internally would be called to fetch an updated access token.

Additional context I'm open to the possibilities that either:

  1. there may already be a token refresh mechanism in ADK for authenticated tools which I just don't know about, or
  2. there is a reason you haven't exposed such a mechanism. Would appreciate your thoughts on the situation.

dannovikov avatar Aug 27 '25 14:08 dannovikov

Hello,

We are also running on the same issue when using Agent Engine. Since the MCPToolset object is instantiated when the agent gets pickled, we end up shipping the access token of the CI system to Agent Engine. We have tried to pass a function, but it yields the same result.

Code snippet:

my_mcp = MCPToolset(
    connection_params=StreamableHTTPConnectionParams(
        url="https://airflow-mcp-staging-gcp-external.naturalint.com/mcp",
        auth=get_sa_token()
    ),
    errlog=None
)

root_agent = Agent(
    name="test_agent",
    model="gemini-2.0-flash",
    description="test_agent",
    instruction="Make HTTP GET requests to specified URLs and provide agent introduction when requested",
    tools=[
      my_mcp
    ]
)

rei-arifi avatar Sep 22 '25 08:09 rei-arifi

Please use the main issue to track, closing this for now.

hangfei avatar Nov 08 '25 04:11 hangfei

@dannovikov @rei-arifi We have added a custom httpx_client_factory param exposed when creating a McpSessionManager (see https://github.com/google/adk-python/pull/2997) -- I think this should allow you to pass in a httpx auth object. Let me know if it works?

wukath avatar Jan 14 '26 23:01 wukath

@wukath Ah, cool! Will check it out tomorrow and report back. Thanks for the update

dannovikov avatar Jan 14 '26 23:01 dannovikov