Skip to content

MCP Sample (HTTP SSE mode) Failed with HTTP 503 Error #1282

Closed
@neolee

Description

@neolee

Initial Checks

Description

Trying official example here: https://ai.pydantic.dev/mcp/client/

The stdio mode sample runs without any issue but the sse mode sample failed with error:

  + Exception Group Traceback (most recent call last):
  |   File "/Users/neo/Code/ML/neb/mcp_client.py", line 26, in <module>
  |     asyncio.run(run(agent_sse))
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
  |     return runner.run(main)
  |            ^^^^^^^^^^^^^^^^
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "/Users/neo/Code/ML/neb/mcp_client.py", line 18, in run
  |     async with agent.run_mcp_servers():
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 210, in __aenter__
  |     return await anext(self.gen)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/pydantic_ai/agent.py", line 1299, in run_mcp_servers
  |     await exit_stack.enter_async_context(mcp_server)
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 650, in enter_async_context
  |     result = await _enter(cm)
  |              ^^^^^^^^^^^^^^^^
  |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/pydantic_ai/mcp.py", line 86, in __aenter__
  |     self._read_stream, self._write_stream = await self._exit_stack.enter_async_context(self.client_streams())
  |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 650, in enter_async_context
  |     result = await _enter(cm)
  |              ^^^^^^^^^^^^^^^^
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 210, in __aenter__
  |     return await anext(self.gen)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/pydantic_ai/mcp.py", line 219, in client_streams
  |     async with sse_client(
  |   File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 210, in __aenter__
  |     return await anext(self.gen)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/mcp/client/sse.py", line 43, in sse_client
  |     async with anyio.create_task_group() as tg:
  |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/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/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/mcp/client/sse.py", line 53, in sse_client
    |     event_source.response.raise_for_status()
    |   File "/Users/neo/Code/ML/neb/.venv/lib/python3.11/site-packages/httpx/_models.py", line 829, in raise_for_status
    |     raise HTTPStatusError(message, request=request, response=self)
    | httpx.HTTPStatusError: Server error '503 Service Unavailable' for url 'http://localhost:3001/sse'

The server is already up with command:

❯ npx @pydantic/mcp-run-python sse
Running MCP server with SSE transport on localhost:3001

Any hint?

Example Code

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerHTTP

server_sse = MCPServerHTTP(url='http://localhost:3001/sse')
agent_sse = Agent('openai:gpt-4o', mcp_servers=[server_sse])

async def run(agent):
    async with agent.run_mcp_servers():
        result = await agent.run('How many days between 2000-01-01 and 2025-03-28?')
    print(result.data)


if __name__ == "__main__":
    import asyncio

    asyncio.run(run(agent_sse))

Python, Pydantic AI & LLM client version

Python: 3.11.11
pydantic: 2.11.0
pydantic-ai: 0.0.46
openai: 1.69.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions