Skip to content

MCP Server Initialization Error - 502 Bad Gateway when Connecting to SSE Server #402

@Yaozy-C

Description

@Yaozy-C

Question:
When I run openai-agents-python sse example,some error happend. Can you tell me how to make it work properly?

Thanks

Environment:
Python version: 3.12
OS: macOS

openai-agents:0.0.7

tarting SSE server at http://localhost:8000/sse ...
INFO:     Started server process [72759]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
SSE server started. Running example...


Error initializing MCP server: unhandled errors in a TaskGroup (1 sub-exception)
  + Exception Group Traceback (most recent call last):
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/main.py", line 93, in <module>
  |     asyncio.run(main())
  |   File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 194, in run
  |     return runner.run(main)
  |            ^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/main.py", line 54, in main
  |     async with MCPServerSse(
  |                ^^^^^^^^^^^^^
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/agents/mcp/server.py", line 89, in __aenter__
  |     await self.connect()
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/agents/mcp/server.py", line 102, in connect
  |     transport = await self.exit_stack.enter_async_context(self.create_streams())
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 659, in enter_async_context
  |     result = await _enter(cm)
  |              ^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 210, in __aenter__
  |     return await anext(self.gen)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/mcp/client/sse.py", line 43, in sse_client
  |     async with anyio.create_task_group() as tg:
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/mcp/client/sse.py", line 53, in sse_client
    |     event_source.response.raise_for_status()
    |   File "/Users/yaozy/PycharmProjects/mcp-demo/.venv/lib/python3.12/site-packages/httpx/_models.py", line 829, in raise_for_status
    |     raise HTTPStatusError(message, request=request, response=self)
    | httpx.HTTPStatusError: Server error '502 Bad Gateway' for url 'http://localhost:8000/sse'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502
    +------------------------------------
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [72759]

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions