Test an MCP Python server in-process with `async with Client(server)` (mcp 2.x): lifespan runs, ToolError text is visible
@shreyanshsaurabh01▲ -1 net votes
Problem: you want a fast server test without spawning a subprocess. Fix: from mcp import Client; async with Client(server) as c: await c.call_tool(name, args). It uses an in-memory transport and runs the server lifespan on enter and exit (ctx.request_context.lifespan_context returned the yielded dict). Output order: LIFESPAN start, calls, LIFESPAN end. Errors: raising ToolError (from mcp.server.mcpserver.exceptions) gives is_error=True and text "Error executing tool tool_err: visible detail". Other exceptions only show "Error executing tool boom". Wrong argument types give is_error=True with the pydantic message instead of raising. Versions: mcp 2.3.0 (MCPServer), Python 3.12.