[Fix] SSE event ordering: emit response.created before MCP discovery events#22286
[Fix] SSE event ordering: emit response.created before MCP discovery events#22286shivamrawat1 wants to merge 2 commits intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryFixes SSE event ordering in the MCP streaming iterator so that
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/responses/mcp/mcp_streaming_iterator.py | Core fix: introduces get_response_created phase to emit response.created before MCP discovery events. Refactors __anext__ into phase-specific helpers and fixes sync __next__ path. Logic is correct and clean. |
| tests/mcp_tests/test_aresponses_api_with_mcp.py | Adds two new tests for SSE event ordering (async and sync paths) and updates docstring for existing test. Tests directly verify the fix. |
Sequence Diagram
sequenceDiagram
participant Client as OpenAI SDK Client
participant MCP as MCPEnhancedStreamingIterator
participant LLM as aresponses (LLM call)
Note over MCP: Phase: get_response_created
MCP->>LLM: _create_initial_response_iterator()
LLM-->>MCP: base_iterator (streaming)
MCP->>MCP: await base_iterator.__anext__()
MCP-->>Client: response.created (first chunk)
Note over MCP: Phase: mcp_discovery
MCP-->>Client: response.mcp_list_tools.in_progress
MCP-->>Client: response.mcp_list_tools.completed
MCP-->>Client: response.output_item.done
Note over MCP: Phase: initial_response
loop Stream remaining chunks
MCP->>MCP: await base_iterator.__anext__()
MCP-->>Client: streaming chunk (text delta, etc.)
end
Note over MCP: Phase: tool_execution (if auto-execute)
MCP-->>Client: mcp_call events
Note over MCP: Phase: follow_up_response (if tools executed)
MCP-->>Client: follow-up streaming chunks
Last reviewed commit: c9ed2eb
Additional Comments (1)
The async Consider applying the same phase-based approach here, or at a minimum documenting that |
|
@greptile re-review with the new commit now that we implemented the fix for sync path. Also note thatthe contribution guideline is wrong about having the test in tests/litellm/. It is in the right place. |
Relevant issues
The /v1/responses streaming endpoint with MCP tools sent response.mcp_list_tools.in_progress, response.mcp_list_tools.completed, and response.output_item.done before response.created. The OpenAI Node SDK (openai@^6.7.0) expects response.created as the first SSE event and can fail when other event types appear first.
MCPEnhancedStreamingIterator started in the mcp_discovery phase and emitted MCP discovery events before creating the base response iterator and yielding its first chunk (response.created).
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
🐛 Bug Fix
🧹 Refactoring
✅ Test
Changes
Introduce a get_response_created phase that runs first: create the base iterator, yield its first chunk (response.created), then move to mcp_discovery and emit MCP events. Refactor anext into phase-specific helpers to keep the logic clear and satisfy the PLR0915 lint rule. Add test_sse_event_ordering_response_created_first to assert the correct event order.