client
Unified MCP Client that wraps ClientSession with transport management.
Client
dataclass
A high-level MCP client for connecting to MCP servers.
Supports in-memory transport for testing (pass a Server or MCPServer instance), Streamable HTTP transport (pass a URL string), or a custom Transport instance.
Example
from mcp.client import Client
from mcp.server.mcpserver import MCPServer
server = MCPServer("test")
@server.tool()
def add(a: int, b: int) -> int:
return a + b
async def main():
async with Client(server) as client:
result = await client.call_tool("add", {"a": 1, "b": 2})
asyncio.run(main())
Source code in src/mcp/client/client.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
server
instance-attribute
The MCP server to connect to.
If the server is a Server or MCPServer instance, it will be wrapped in an InMemoryTransport.
If the server is a URL string, it will be used as the URL for a streamable_http_client transport.
If the server is a Transport instance, it will be used directly.
raise_exceptions
class-attribute
instance-attribute
raise_exceptions: bool = False
Whether to raise exceptions from the server.
read_timeout_seconds
class-attribute
instance-attribute
read_timeout_seconds: float | None = None
Timeout for read operations.
sampling_callback
class-attribute
instance-attribute
sampling_callback: SamplingFnT | None = None
Callback for handling sampling requests.
list_roots_callback
class-attribute
instance-attribute
list_roots_callback: ListRootsFnT | None = None
Callback for handling list roots requests.
logging_callback
class-attribute
instance-attribute
logging_callback: LoggingFnT | None = None
Callback for handling logging notifications.
message_handler
class-attribute
instance-attribute
message_handler: MessageHandlerFnT | None = None
Callback for handling raw messages.
client_info
class-attribute
instance-attribute
client_info: Implementation | None = None
Client implementation info to send to server.
elicitation_callback
class-attribute
instance-attribute
elicitation_callback: ElicitationFnT | None = None
Callback for handling elicitation requests.
__aenter__
async
__aenter__() -> Client
Enter the async context manager.
Source code in src/mcp/client/client.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
__aexit__
async
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: Any,
) -> None
Exit the async context manager.
Source code in src/mcp/client/client.py
138 139 140 141 142 | |
session
property
session: ClientSession
Get the underlying ClientSession.
This provides access to the full ClientSession API for advanced use cases.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If accessed before entering the context manager. |
initialize_result
property
initialize_result: InitializeResult
The server's InitializeResult.
Contains server_info, capabilities, instructions, and the negotiated protocol_version. Raises RuntimeError if accessed outside the context manager.
send_ping
async
send_ping(
*, meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a ping request to the server.
Source code in src/mcp/client/client.py
169 170 171 | |
send_progress_notification
async
send_progress_notification(
progress_token: str | int,
progress: float,
total: float | None = None,
message: str | None = None,
) -> None
Send a progress notification to the server.
Source code in src/mcp/client/client.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
set_logging_level
async
set_logging_level(
level: LoggingLevel,
*,
meta: RequestParamsMeta | None = None
) -> EmptyResult
Set the logging level on the server.
Source code in src/mcp/client/client.py
188 189 190 | |
list_resources
async
list_resources(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListResourcesResult
List available resources from the server.
Source code in src/mcp/client/client.py
192 193 194 195 196 197 198 199 | |
list_resource_templates
async
list_resource_templates(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListResourceTemplatesResult
List available resource templates from the server.
Source code in src/mcp/client/client.py
201 202 203 204 205 206 207 208 | |
read_resource
async
read_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> ReadResourceResult
Read a resource from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. |
required |
meta
|
RequestParamsMeta | None
|
Additional metadata for the request. |
None
|
Returns:
| Type | Description |
|---|---|
ReadResourceResult
|
The resource content. |
Source code in src/mcp/client/client.py
210 211 212 213 214 215 216 217 218 219 220 | |
subscribe_resource
async
subscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Subscribe to resource updates.
Source code in src/mcp/client/client.py
222 223 224 | |
unsubscribe_resource
async
unsubscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Unsubscribe from resource updates.
Source code in src/mcp/client/client.py
226 227 228 | |
call_tool
async
call_tool(
name: str,
arguments: dict[str, Any] | None = None,
read_timeout_seconds: float | None = None,
progress_callback: ProgressFnT | None = None,
*,
meta: RequestParamsMeta | None = None
) -> CallToolResult
Call a tool on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tool to call |
required |
arguments
|
dict[str, Any] | None
|
Arguments to pass to the tool |
None
|
read_timeout_seconds
|
float | None
|
Timeout for the tool call |
None
|
progress_callback
|
ProgressFnT | None
|
Callback for progress updates |
None
|
meta
|
RequestParamsMeta | None
|
Additional metadata for the request |
None
|
Returns:
| Type | Description |
|---|---|
CallToolResult
|
The tool result. |
Source code in src/mcp/client/client.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
list_prompts
async
list_prompts(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListPromptsResult
List available prompts from the server.
Source code in src/mcp/client/client.py
259 260 261 262 263 264 265 266 | |
get_prompt
async
get_prompt(
name: str,
arguments: dict[str, str] | None = None,
*,
meta: RequestParamsMeta | None = None
) -> GetPromptResult
Get a prompt from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the prompt |
required |
arguments
|
dict[str, str] | None
|
Arguments to pass to the prompt |
None
|
meta
|
RequestParamsMeta | None
|
Additional metadata for the request |
None
|
Returns:
| Type | Description |
|---|---|
GetPromptResult
|
The prompt content. |
Source code in src/mcp/client/client.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
complete
async
complete(
ref: ResourceTemplateReference | PromptReference,
argument: dict[str, str],
context_arguments: dict[str, str] | None = None,
) -> CompleteResult
Get completions for a prompt or resource template argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
ResourceTemplateReference | PromptReference
|
Reference to the prompt or resource template |
required |
argument
|
dict[str, str]
|
The argument to complete |
required |
context_arguments
|
dict[str, str] | None
|
Additional context arguments |
None
|
Returns:
| Type | Description |
|---|---|
CompleteResult
|
Completion suggestions. |
Source code in src/mcp/client/client.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
list_tools
async
list_tools(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListToolsResult
List available tools from the server.
Source code in src/mcp/client/client.py
301 302 303 | |
send_roots_list_changed
async
send_roots_list_changed() -> None
Send a notification that the roots list has changed.
Source code in src/mcp/client/client.py
305 306 307 308 | |