Skip to content

Commit 0321dee

Browse files
author
mritunjaypratapsingh
committed
fix: align timeout params between sse_client and streamable_http_client
Fixes #936 - sse_client: replace hardcoded timeout defaults (5.0/300.0) with shared constants MCP_DEFAULT_TIMEOUT and MCP_DEFAULT_SSE_READ_TIMEOUT - streamable_http_client: add headers, timeout, sse_read_timeout, and auth parameters to match sse_client's interface - Both transports now use consistent defaults from _httpx_utils.py
1 parent 7ba41dc commit 0321dee

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/mcp/client/sse.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
from httpx_sse._exceptions import SSEError
1313

1414
from mcp import types
15-
from mcp.shared._httpx_utils import McpHttpClientFactory, create_mcp_http_client
15+
from mcp.shared._httpx_utils import (
16+
MCP_DEFAULT_SSE_READ_TIMEOUT,
17+
MCP_DEFAULT_TIMEOUT,
18+
McpHttpClientFactory,
19+
create_mcp_http_client,
20+
)
1621
from mcp.shared.message import SessionMessage
1722

1823
logger = logging.getLogger(__name__)
@@ -31,8 +36,8 @@ def _extract_session_id_from_endpoint(endpoint_url: str) -> str | None:
3136
async def sse_client(
3237
url: str,
3338
headers: dict[str, Any] | None = None,
34-
timeout: float = 5.0,
35-
sse_read_timeout: float = 300.0,
39+
timeout: float = MCP_DEFAULT_TIMEOUT,
40+
sse_read_timeout: float = MCP_DEFAULT_SSE_READ_TIMEOUT,
3641
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
3742
auth: httpx.Auth | None = None,
3843
on_session_created: Callable[[str], None] | None = None,

src/mcp/client/streamable_http.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pydantic import ValidationError
1717

1818
from mcp.client._transport import TransportStreams
19-
from mcp.shared._httpx_utils import create_mcp_http_client
19+
from mcp.shared._httpx_utils import MCP_DEFAULT_SSE_READ_TIMEOUT, MCP_DEFAULT_TIMEOUT, create_mcp_http_client
2020
from mcp.shared.message import ClientMessageMetadata, SessionMessage
2121
from mcp.types import (
2222
INTERNAL_ERROR,
@@ -513,16 +513,26 @@ def get_session_id(self) -> str | None:
513513
async def streamable_http_client(
514514
url: str,
515515
*,
516+
headers: dict[str, str] | None = None,
517+
timeout: float = MCP_DEFAULT_TIMEOUT,
518+
sse_read_timeout: float = MCP_DEFAULT_SSE_READ_TIMEOUT,
519+
auth: httpx.Auth | None = None,
516520
http_client: httpx.AsyncClient | None = None,
517521
terminate_on_close: bool = True,
518522
) -> AsyncGenerator[TransportStreams, None]:
519523
"""Client transport for StreamableHTTP.
520524
525+
`sse_read_timeout` determines how long (in seconds) the client will wait for a new
526+
event before disconnecting. All other HTTP operations are controlled by `timeout`.
527+
521528
Args:
522529
url: The MCP server endpoint URL.
523-
http_client: Optional pre-configured httpx.AsyncClient. If None, a default
524-
client with recommended MCP timeouts will be created. To configure headers,
525-
authentication, or other HTTP settings, create an httpx.AsyncClient and pass it here.
530+
headers: Optional headers to include in requests.
531+
timeout: HTTP timeout for regular operations (in seconds).
532+
sse_read_timeout: Timeout for SSE read operations (in seconds).
533+
auth: Optional HTTPX authentication handler.
534+
http_client: Optional pre-configured httpx.AsyncClient. If provided, `headers`,
535+
`timeout`, `sse_read_timeout`, and `auth` are ignored.
526536
terminate_on_close: If True, send a DELETE request to terminate the session when the context exits.
527537
528538
Yields:
@@ -542,7 +552,11 @@ async def streamable_http_client(
542552

543553
if client is None:
544554
# Create default client with recommended MCP timeouts
545-
client = create_mcp_http_client()
555+
client = create_mcp_http_client(
556+
headers=headers,
557+
timeout=httpx.Timeout(timeout, read=sse_read_timeout),
558+
auth=auth,
559+
)
546560

547561
transport = StreamableHTTPTransport(url)
548562

0 commit comments

Comments
 (0)