fix: optionize initialized notification tolerance#2765
Conversation
Add an explicit MCP streamable-HTTP option for tolerating failed initialized notifications, and opt into that behavior only when callers request it. Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
seratch
left a comment
There was a problem hiding this comment.
The overall direction makes sense. Making only the notifications/initialized request best-effort, behind an explicit opt-in flag, is a reasonable approach, and it correctly avoids hiding failures for other requests.
My main concern is the implementation layer. Right now, this change depends on create_mcp_http_client from mcp.client.streamable_http, which is not part of that module’s exported public surface and already breaks mypy. It also replaces httpx.AsyncClient._transport, which is a private attribute and therefore somewhat brittle.
A cleaner solution would be to handle this directly in the MCP Streamable HTTP transport, where the code already knows when it is sending the initialized notification. That would avoid re-parsing raw HTTP request bodies, avoid private httpx internals, and keep the behavior closer to the protocol logic.
So my recommendation would be:
- For a quick fix, keep the current behavior but remove the non-exported import issue and make sure the full typecheck/test gate passes.
- For the better long-term solution, move this tolerance into the upstream transport layer and let the SDK only pass through the opt-in flag.
It would also be good to add a test covering the combination of ignore_initialized_notification_failure=True with a custom httpx_client_factory, to ensure headers, timeout, and auth are still preserved correctly.
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
httpx_client_factory, rather than forking MCP's stream client orchestrationWhy
Best-effort
notifications/initializedPOST could fail and poison the transport, even though subsequent real requests could still succeed. This change keeps the behavior opt-in at the SDK layer so apps can choose the more tolerant transport policy explicitly.Validation
ruff check src/agents/mcp/server.py tests/mcp/test_streamable_http_client_factory.pyPYTHONPATH=src pytest -q tests/mcp/test_streamable_http_client_factory.py