pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/modelcontextprotocol/python-sdk/pull/2377/files

.githubassets.com/assets/primer-primitives-10bf9dd67e3d70bd.css" /> fix: allow custom Content-Type header in StreamableHTTPTransport by guoyangzhen · Pull Request #2377 · modelcontextprotocol/python-sdk · GitHub
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/mcp/client/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ class RequestContext:
class StreamableHTTPTransport:
"""StreamableHTTP client transport implementation."""

def __init__(self, url: str) -> None:
def __init__(self, url: str, headers: dict[str, str] | None = None) -> None:
"""Initialize the StreamableHTTP transport.

Args:
url: The endpoint URL.
headers: Optional extra headers included with every request.
User-supplied headers take precedence over built-in MCP defaults.
"""
self.url = url
self.session_id: str | None = None
self.protocol_version: str | None = None
self._user_headers: dict[str, str] = headers or {}

def _prepare_headers(self) -> dict[str, str]:
"""Build MCP-specific request headers.
Expand All @@ -92,6 +95,8 @@ def _prepare_headers(self) -> dict[str, str]:
"accept": "application/json, text/event-stream",
"content-type": "application/json",
}
# User-supplied headers override MCP defaults (e.g. charset in Content-Type)
headers.update(self._user_headers)
# Add session headers if available
if self.session_id:
headers[MCP_SESSION_ID] = self.session_id
Expand Down Expand Up @@ -511,6 +516,7 @@ async def streamable_http_client(
url: str,
*,
http_client: httpx.AsyncClient | None = None,
headers: dict[str, str] | None = None,
terminate_on_close: bool = True,
) -> AsyncGenerator[TransportStreams, None]:
"""Client transport for StreamableHTTP.
Expand All @@ -520,6 +526,8 @@ async def streamable_http_client(
http_client: Optional pre-configured httpx.AsyncClient. If None, a default
client with recommended MCP timeouts will be created. To configure headers,
authentication, or other HTTP settings, create an httpx.AsyncClient and pass it here.
headers: Optional extra headers for every request (e.g. Content-Type with charset).
These override the built-in MCP defaults.
terminate_on_close: If True, send a DELETE request to terminate the session when the context exits.

Yields:
Expand All @@ -538,7 +546,7 @@ async def streamable_http_client(
# Create default client with recommended MCP timeouts
client = create_mcp_http_client()

transport = StreamableHTTPTransport(url)
transport = StreamableHTTPTransport(url, headers=headers)

logger.debug(f"Connecting to StreamableHTTP endpoint: {url}")

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy