Skip to content

fix: allow custom Content-Type header in StreamableHTTPTransport#2377

Open
guoyangzhen wants to merge 1 commit intomodelcontextprotocol:mainfrom
guoyangzhen:fix/streamable-http-content-type
Open

fix: allow custom Content-Type header in StreamableHTTPTransport#2377
guoyangzhen wants to merge 1 commit intomodelcontextprotocol:mainfrom
guoyangzhen:fix/streamable-http-content-type

Conversation

@guoyangzhen
Copy link
Copy Markdown

Fixes #2375

Problem

StreamableHTTPTransport._prepare_headers() hardcodes Content-Type: application/json, which overwrites any user-configured header (e.g. application/json; charset=utf-8). Users who pass custom headers via httpx.AsyncClient(headers=...) find them ignored because request-level headers take precedence over client defaults in httpx.

Solution

Add an optional headers parameter to both StreamableHTTPTransport.__init__ and streamable_http_client. User-supplied headers are merged after the MCP defaults, so they take precedence. This is backward-compatible — existing code with no headers argument behaves identically.

Usage

async with streamable_http_client(
    url,
    headers={'Content-Type': 'application/json; charset=utf-8'},
) as (read, write):
    ...

Changes

  • StreamableHTTPTransport.__init__: new headers dict param, stored as self._user_headers
  • _prepare_headers(): calls headers.update(self._user_headers) after setting defaults
  • streamable_http_client(): new headers kwarg, forwarded to transport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harcoded Content-Type in the client cannot be overriden

1 participant