Configuring HTTP client for streamablehttp #742
+65
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The current streamablehttp_client doesn't support customizing the new client except header and timeout. I need to customize the httpx client because my MCP server integration requires mTLS connection, thus I need to setup certification during connection.
This allow to pass a http client or default to the existing factory.
I was able to "successfully" test my MCP integration by "monkey patching" the introduced
mcp.shared._httpx_utils.create_mcp_client = my_custom_client_factory
.Also, this supports base_url support by httpx, which is minor, but for my case, URLs are injected as they vary on deployment.
How Has This Been Tested?
I tested my branch integrating with the deployed MCP, and it worked the same way with the monkey patch version, except that this became explicit.
Breaking Changes
This doesn't introduce any breaking change.
Types of changes
Checklist
Additional context
This approach is similar to how other SDK configuration works, allow users to change the http client based on infrastructure requirements.
Why not kwargs? It felt that passing the client instance is more flexible than picking or passing the init constructors.
This couples with httpx.AsyncClient. Yes, in comparison to the current implementation that hides this detail. But, I didn't consider the scope of this PR, introducing a "wrapper" for the async client.
Update: I discovered a recent PR #734 after the PR opened. I didn't notice because I started to work prior it.