-
Notifications
You must be signed in to change notification settings - Fork 197
feat(mcp): add Streamable HTTP client to extra/mcp #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adrienduchemin
wants to merge
7
commits into
main
Choose a base branch
from
adrienduchemin/extra-mcp-streamable-http
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−0
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46f91ee
feat(mcp): add Streamable HTTP client to extra/mcp
adrienduchemin f3d1efd
refactor(mcp): drop unused sse_read_timeout from StreamableHTTPServer…
adrienduchemin bc06284
test(mcp): cover Streamable HTTP client header wiring
adrienduchemin 8ba57bc
test(mcp): fix asynccontextmanager return type for pyright
adrienduchemin 96c20e6
fix(mcp): do not inherit ambient HTTP proxy in Streamable HTTP client
adrienduchemin 82a0437
refactor(mcp): make trust_env configurable (default True) instead of …
adrienduchemin 6560fd2
feat(mcp): add follow_redirects (default False) to Streamable HTTP cl…
adrienduchemin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import logging | ||
| from contextlib import AsyncExitStack | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
| from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream | ||
| from mcp.client.streamable_http import streamable_http_client # pyright: ignore[reportMissingImports] | ||
| from mcp.shared.message import SessionMessage # pyright: ignore[reportMissingImports] | ||
|
|
||
| from mistralai.extra.mcp.base import ( | ||
| MCPClientBase, | ||
| ) | ||
|
|
||
| from mistralai.client.types import BaseModel | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class StreamableHTTPServerParams(BaseModel): | ||
| """Parameters required for a MCPClient with Streamable HTTP transport""" | ||
|
|
||
| url: str | ||
| headers: dict[str, Any] | None = None | ||
| timeout: float = 30 | ||
| sse_read_timeout: float = 60 * 5 | ||
|
|
||
|
|
||
| class MCPClientStreamableHTTP(MCPClientBase): | ||
| """MCP client that uses the Streamable HTTP transport for communication. | ||
|
|
||
| Credentials (for example a bearer token, or a per-request integration token) | ||
| are provided as ``headers`` and set as the default headers of the underlying | ||
| ``httpx.AsyncClient``, so they are sent on every request including the | ||
| initialize call. Recent ``mcp`` releases deprecate and ignore the transport's | ||
| own ``headers`` argument, so configuring them on the client is required. | ||
| """ | ||
|
|
||
| _params: StreamableHTTPServerParams | ||
|
|
||
| def __init__( | ||
| self, | ||
| params: StreamableHTTPServerParams, | ||
| name: str | None = None, | ||
| ): | ||
| super().__init__(name=name) | ||
| self._params = params | ||
|
|
||
| async def _get_transport( | ||
| self, exit_stack: AsyncExitStack | ||
| ) -> tuple[ | ||
| MemoryObjectReceiveStream[SessionMessage | Exception], | ||
| MemoryObjectSendStream[SessionMessage], | ||
| ]: | ||
| http_client = await exit_stack.enter_async_context( | ||
| httpx.AsyncClient( | ||
| headers=self._params.headers, | ||
| timeout=self._params.timeout, | ||
| follow_redirects=True, | ||
| ) | ||
| ) | ||
| read_stream, write_stream, _ = await exit_stack.enter_async_context( | ||
| streamable_http_client(url=self._params.url, http_client=http_client) | ||
| ) | ||
| return read_stream, write_stream | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.