Connect to a Local MCP Server
The Python SDK can connect directly to an MCPServer object for tests, or launch a subprocess using stdio parameters for real local integrations.
In-process testing
from mcp.client import Client
from server import mcp
async with Client(mcp) as client:
print(await client.list_tools())Subprocess connection
For an actual local server, configure the executable, arguments, and only the environment variables that process needs. Avoid forwarding your entire shell environment automatically.
Operational checks
- Use absolute or well-defined working paths.
- Set timeouts for tool calls.
- Capture stderr for diagnostics.
- Terminate child processes cleanly.