Skip to content

MCPServerStdio initialization exception not propagated #2607

@jomonson

Description

@jomonson

Initial Checks

Description

I'm using the following code to create an MCP server:

file_system_mcp = MCPServerStdio(
    'docker',
    args=[
        'run',
        '-i',
        '--rm',
        "--mount",
        "type=bind,src=...",
        "-w", "/src",
        'mcp/filesystem',
        "...",
    ],
)

However I forgot to start Docker deamon.

Later when I use async with file_system_mcp: I get the exception:

unhandled errors in a TaskGroup (1 sub-exception)

instead of a helpful exception such as:

docker: Cannot connect to the Docker daemon at unix:///Users/jmonsonego.ctr/.docker/run/docker.sock. Is the docker daemon running?

I can see the mentioned sub exception as McpError('Connection closed') when I view the local variables during debugging, but that's not specific enough.

Why doesn't Pydantic propagate the exceptions that happen during the __aenter__ of the MCP context manager outside?

Example Code

Python, Pydantic AI & LLM client version

Python 3.13
Pydantic AI 0.7.3

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions