Skip to content

Commit 519dca2

Browse files
committed
fix: allow custom Content-Type header in StreamableHTTPTransport
1 parent fb2276b commit 519dca2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/mcp/client/streamable_http.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ class RequestContext:
7272
class StreamableHTTPTransport:
7373
"""StreamableHTTP client transport implementation."""
7474

75-
def __init__(self, url: str) -> None:
75+
def __init__(self, url: str, headers: dict[str, str] | None = None) -> None:
7676
"""Initialize the StreamableHTTP transport.
7777
7878
Args:
7979
url: The endpoint URL.
80+
headers: Optional extra headers included with every request.
81+
User-supplied headers take precedence over built-in MCP defaults.
8082
"""
8183
self.url = url
8284
self.session_id: str | None = None
8385
self.protocol_version: str | None = None
86+
self._user_headers: dict[str, str] = headers or {}
8487

8588
def _prepare_headers(self) -> dict[str, str]:
8689
"""Build MCP-specific request headers.
@@ -92,6 +95,8 @@ def _prepare_headers(self) -> dict[str, str]:
9295
"accept": "application/json, text/event-stream",
9396
"content-type": "application/json",
9497
}
98+
# User-supplied headers override MCP defaults (e.g. charset in Content-Type)
99+
headers.update(self._user_headers)
95100
# Add session headers if available
96101
if self.session_id:
97102
headers[MCP_SESSION_ID] = self.session_id
@@ -511,6 +516,7 @@ async def streamable_http_client(
511516
url: str,
512517
*,
513518
http_client: httpx.AsyncClient | None = None,
519+
headers: dict[str, str] | None = None,
514520
terminate_on_close: bool = True,
515521
) -> AsyncGenerator[TransportStreams, None]:
516522
"""Client transport for StreamableHTTP.
@@ -520,6 +526,8 @@ async def streamable_http_client(
520526
http_client: Optional pre-configured httpx.AsyncClient. If None, a default
521527
client with recommended MCP timeouts will be created. To configure headers,
522528
authentication, or other HTTP settings, create an httpx.AsyncClient and pass it here.
529+
headers: Optional extra headers for every request (e.g. Content-Type with charset).
530+
These override the built-in MCP defaults.
523531
terminate_on_close: If True, send a DELETE request to terminate the session when the context exits.
524532
525533
Yields:
@@ -538,7 +546,7 @@ async def streamable_http_client(
538546
# Create default client with recommended MCP timeouts
539547
client = create_mcp_http_client()
540548

541-
transport = StreamableHTTPTransport(url)
549+
transport = StreamableHTTPTransport(url, headers=headers)
542550

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

0 commit comments

Comments
 (0)