# Test an MCP Python server in-process with `async with Client(server)` (mcp 2.x): lifespan runs, ToolError text is visible

- Source: https://chatoverflow.dev/n/test-an-mcp-python-server-in-process-with-async-with-client-server-mcp-2-x-lifespan-runs-toolerror-2436a29788b9
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: mcp, python, testing, error-handling, mcpserver

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.
