fix(mcp-tool): add httpx_client_factory support to SseConnectionParams#4842
fix(mcp-tool): add httpx_client_factory support to SseConnectionParams#4842ushelp wants to merge 12 commits intogoogle:mainfrom
Conversation
- Update SseConnectionParams model to include httpx_client_factory parameter (matching StreamableHTTPConnectionParams pattern) - Pass httpx_client_factory from SseConnectionParams to underlying sse_client() call - Add model_config to SseConnectionParams to allow arbitrary types for the factory function - Update docstrings to document the new httpx_client_factory attribute This change aligns SSE transport behavior with Streamable HTTP transport for MCP tools, enabling users to customize the httpx.AsyncClient for SSE-based MCP connections (e.g., proxy, auth, TLS config). Closes google#4841
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces the capability to customize the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly adds support for httpx_client_factory to SseConnectionParams, aligning it with StreamableHTTPConnectionParams. The implementation is clean and the new unit tests effectively cover the changes. I have a couple of suggestions regarding pickling support to make the changes more robust and prevent potential issues for users of the library.
|
Hi @ushelp, Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
|
Hi @wukath , can you please review this. LGTM. |
|
taking a look! will import internally |
Merge #4842 ### Link to Issue or Description of Change **1. Link to an existing issue** Closes #4841 **2. Changes** This change aligns SSE transport behavior with Streamable HTTP transport for MCP tools, enabling users to customize the httpx.AsyncClient for SSE-based MCP connections (e.g., proxy, auth, TLS config). - Update `SseConnectionParams` model to include httpx_client_factory parameter (matching StreamableHTTPConnectionParams pattern) - Pass `httpx_client_factory` from `SseConnectionParams` to underlying `sse_client()` call - Add `model_config` to `SseConnectionParams` to allow arbitrary types for the factory function - Update docstrings to document the new `httpx_client_factory attribute` - Add unit test for new senarios ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - `tests/unittests/tools/mcp_tool/test_mcp_session_manager.py` - [x] All unit tests pass locally. - All test cases have passed the test. **Manual End-to-End (E2E) Tests:** 1. Set custom httpx_client_factory parameter for SseConnectionParams ```Python from typing import Any import httpx from google.adk.agents.llm_agent import Agent from google.adk.models import LiteLlm from google.adk.tools.mcp_tool import SseConnectionParams, McpToolset # Cutom httpx client factory for MCP SSE transport def custom_httpx_client_factory( headers: dict[str, str] | None = None, timeout: httpx.Timeout | None = None, auth: httpx.Auth | None = None, ) -> httpx.AsyncClient: # Print message for debugging print(' === Custom httpx_client_factory running! ===') kwargs: dict[str, Any] = { "follow_redirects": True, } if timeout is None: kwargs["timeout"] = httpx.Timeout(30, read=300) else: kwargs["timeout"] = timeout if headers is not None: kwargs["headers"] = headers if auth is not None: kwargs["auth"] = auth return httpx.AsyncClient(**kwargs) connection_params = SseConnectionParams( url='http://127.0.0.1:9000/sse', httpx_client_factory=custom_httpx_client_factory, # Custom httpx client factory ) tool = McpToolset(connection_params=connection_params) root_agent = Agent( model=LiteLlm(model="ollama_chat/qwen3.5:35b-a3b-q4_K_M"), name='root_agent', description='A helpful assistant for user questions.', instruction='Answer user questions to the best of your knowledge', tools=[tool], ) ``` 2. Run the Agent, and display the debug log in console ``` 2026-03-15 23:54:14,304 - INFO - agent_loader.py:130 - Found root_agent in my_agent.agent INFO: 127.0.0.1:55642 - "POST /run_sse HTTP/1.1" 200 OK === Custom httpx_client_factory running! === 2026-03-15 23:54:14,345 - INFO - _client.py:1740 - HTTP Request: GET http://127.0.0.1:9000/sse "HTTP/1.1 200 OK" 2026-03-15 23:54:14,349 - INFO - _client.py:1740 - HTTP Request: POST http://127.0.0.1:9000/messages/?session_id=b5750c6e214048c28110dff11c412ecb "HTTP/1.1 202 Accepted" ``` ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. ### Additional context Co-authored-by: Kathy Wu <wukathy@google.com> COPYBARA_INTEGRATE_REVIEW=#4842 from ushelp:main e3125fb PiperOrigin-RevId: 890708694
Link to Issue or Description of Change
1. Link to an existing issue
Closes #4841
2. Changes
This change aligns SSE transport behavior with Streamable HTTP transport for MCP tools, enabling users to customize the httpx.AsyncClient for SSE-based MCP connections (e.g., proxy, auth, TLS config).
SseConnectionParamsmodel to include httpx_client_factory parameter (matching StreamableHTTPConnectionParams pattern)httpx_client_factoryfromSseConnectionParamsto underlyingsse_client()callmodel_configtoSseConnectionParamsto allow arbitrary types for the factory functionhttpx_client_factory attributeTesting Plan
Unit Tests:
tests/unittests/tools/mcp_tool/test_mcp_session_manager.pyManual End-to-End (E2E) Tests:
Checklist
Additional context