Skip to content

if mcp initialize fails, the streaming hangs #1404

@DeoLeung

Description

@DeoLeung
app 106 2025-08-08 16:10:36,948 - INFO - received  POST /chat/completions ({request-id}:{collector-user}:127.0.0.1)
_client 1740 2025-08-08 16:10:37,987 - INFO - HTTP Request: POST /mcp/?token=*** "HTTP/1.1 200 OK"
streamable_http 144 2025-08-08 16:10:37,988 - INFO - Received session ID: decd4c65299d41ccb3341e3e44a8e088
streamable_http 156 2025-08-08 16:10:37,988 - INFO - Negotiated protocol version: 2025-06-18
_client 1740 2025-08-08 16:10:38,100 - INFO - HTTP Request: POST /mcp/?token=*** "HTTP/1.1 202 Accepted"
_client 1740 2025-08-08 16:10:38,101 - INFO - HTTP Request: POST /mcp/?token=*** "HTTP/1.1 200 OK"
streamable_http 144 2025-08-08 16:10:38,101 - INFO - Received session ID: be27bf6b2ac643bbbee09996d11755f8
streamable_http 156 2025-08-08 16:10:38,101 - INFO - Negotiated protocol version: 2025-06-18
_client 1740 2025-08-08 16:10:38,122 - INFO - HTTP Request: GET /mcp/?token=*** "HTTP/1.1 400 Bad Request"
app 124 2025-08-08 16:10:38,123 - INFO - completed POST /chat/completions ({request-id}:25:3:127.0.0.1) 200 1174.42ms
INFO:     127.0.0.1:54010 - "POST /chat/completions HTTP/1.1" 200 OK
_client 1740 2025-08-08 16:10:38,187 - INFO - HTTP Request: POST /mcp/?token=*** "HTTP/1.1 400 Bad Request"
streamable_http 296 2025-08-08 16:10:38,189 - ERROR - ****error****
400
Bad Request: No valid session ID provided
Traceback (most recent call last):
  File "lib/python3.12/site-packages/mcp/client/streamable_http.py", line 291, in _handle_post_request
    response.raise_for_status()
  File "lib/python3.12/site-packages/httpx/_models.py", line 829, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '400 Bad Request' for url '/mcp/?token=***'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
streamable_http 442 2025-08-08 16:10:38,193 - ERROR - Error in post_writer
Traceback (most recent call last):
  File "lib/python3.12/site-packages/mcp/client/streamable_http.py", line 439, in post_writer
    await handle_request_async()
  File "lib/python3.12/site-packages/mcp/client/streamable_http.py", line 433, in handle_request_async
    await self._handle_post_request(ctx)
  File "lib/python3.12/site-packages/mcp/client/streamable_http.py", line 291, in _handle_post_request
    response.raise_for_status()
  File "lib/python3.12/site-packages/httpx/_models.py", line 829, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '400 Bad Request' for url '/mcp/?token=***'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

i implemented two mcp servers, due to some configuration it will lost the session id, then my streaming never ends (also it's not logging the mcp client 400 error which is hard to debug, i hack it here)
sample code

async def chat_completions(bg: BackgroundTasks
):
  start_agent, server_1, server_2 = get_agent()
  await server_1.connect()
  await server_2.connect()

  bg.add_task(server_1.cleanup)
  bg.add_task(server_2.cleanup)
  result = Runner.run_streamed(
    start_agent,
    input=input_messages,
    run_config=RunConfig(
      trace_id='trace_' + request.request_id,
      workflow_name='abc',
      trace_metadata={},
    ),
    session=session,
  )

  helper = ChatCompletionChunkHelper(model=request.model)

  return StreamingResponse(
    process_messages(result, helper, start_agent.name, request.config),
    media_type='text/event-stream',
  )

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions