Skip to content

SSE-safe HTTP timeouts in SamClient, self peer_id in get_mesh_info - #326

Merged
aojea merged 2 commits into
google:mainfrom
kaisoz:kaisoz/sse-timeout-peer-id
Aug 31, 2026
Merged

SSE-safe HTTP timeouts in SamClient, self peer_id in get_mesh_info#326
aojea merged 2 commits into
google:mainfrom
kaisoz:kaisoz/sse-timeout-peer-id

Conversation

@kaisoz

@kaisoz kaisoz commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

The two changes from #314 requested for immediate merge (#314 (comment)):

  • sam_mcp/client.py: use the MCP SDK's create_mcp_http_client so the SSE stream isn't dropped by httpx's default 5s read timeout.
  • get_mesh_info now returns the node's own peer_id.

@kaisoz
kaisoz requested a review from aojea August 30, 2026 21:29

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds the local peer ID to the mesh information handler in the Go node implementation and updates the Python client to use an SSE-friendly HTTP client from the MCP library. The review feedback advises against importing from the private mcp.shared._httpx_utils module, suggesting instead to configure a standard httpx.AsyncClient with custom timeouts to avoid potential breaking changes in future library updates.

Comment thread sam-mcp-python/src/sam_mcp/client.py Outdated
from typing import Any, Dict, List, Optional
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
from mcp.shared._httpx_utils import create_mcp_http_client

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Importing from a private module (mcp.shared._httpx_utils) is risky because private implementation details (indicated by the leading underscore) are not part of the public API and can change or be removed in future releases of the mcp library without warning.

Instead, import httpx directly and configure the client with custom timeouts to prevent the SSE stream from being dropped.

Suggested change
from mcp.shared._httpx_utils import create_mcp_http_client
import httpx

@kaisoz kaisoz Aug 30, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on not reaching into _httpx_utils. Since there's no public re-export of create_mcp_http_client I switched to a plain httpx.AsyncClient. Kept the SDK's actual defaults rather than the suggested values: Timeout(30.0, read=300.0) and follow_redirects=True, since read=None would make a dead connection hang forever instead of failing.

Comment thread sam-mcp-python/src/sam_mcp/client.py Outdated
self._http_client = httpx.AsyncClient(headers=headers)

# Applies MCP's SSE-friendly timeouts; a plain httpx client reads for 5s and drops the stream.
self._http_client = create_mcp_http_client(headers=headers)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of using the private create_mcp_http_client helper, configure httpx.AsyncClient directly with a custom timeout. This achieves the same SSE-friendly behavior (disabling the read timeout) while relying entirely on public, stable APIs.

Suggested change
self._http_client = create_mcp_http_client(headers=headers)
self._http_client = httpx.AsyncClient(headers=headers, timeout=httpx.Timeout(timeout=60.0, read=None))

@kaisoz kaisoz Aug 30, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on not reaching into _httpx_utils: since there's no public re-export of create_mcp_http_client, switched to a plain httpx.AsyncClient. Kept the SDK's actual defaults rather than the suggested values: Timeout(30.0, read=300.0) and follow_redirects=True, since read=None would make a dead connection hang forever instead of failing.

kaisoz added 2 commits August 30, 2026 21:38
A plain httpx.AsyncClient applies its default 5s read timeout to the MCP
Streamable HTTP SSE stream, killing the session whenever it sits quiet
longer than that. Use the SDK's create_mcp_http_client, which sets
SSE-friendly timeouts.
@kaisoz
kaisoz force-pushed the kaisoz/sse-timeout-peer-id branch from 32c9bf7 to 3f56055 Compare August 30, 2026 21:43
@aojea
aojea merged commit 3e32be7 into google:main Aug 31, 2026
19 checks passed
@aojea

aojea commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

LGTM

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.

2 participants