MCPClient is an async Python client for talking to any MCP server. It handles the MCP handshake (initialize), transport setup, and session management—so you can focus on the operations you want to perform.
Use MCPClient when you want explicit, predictable control, like:
- Testing MCP servers during development
- Building applications that need reliable MCP interactions
- Building higher-level clients (including “agentic” flows) on top of a typed protocol layer
Quick start
Connect → do work → close.async with if you want guaranteed cleanup:
Connection
Connect
Protocol info
After connecting,initialize_result is populated:
Close
Alternative: open_connection(...)
open_connection(...) is a convenience wrapper that yields a connected MCPClient and cleans up automatically:
Ping
Operations
Most client code falls into one of these buckets:Tools
List and call server-side functions
Resources
List and read data sources
Prompts
List and get message templates
Client capabilities (server → client)
Some MCP features are initiated by the server. If you want to support them, you configure handlers when connecting.Sampling
Handle LLM completion requests from servers
Elicitation
Gather user input during tool execution
Roots
Advertise filesystem boundaries to servers
Logging
Receive log messages from servers