|
| 1 | +# Writing MCP Clients |
| 2 | + |
| 3 | +This guide covers building MCP clients -- programs that connect to MCP servers and use |
| 4 | +their tools, resources, prompts, and other capabilities. |
| 5 | + |
| 6 | +## Client Session Basics |
| 7 | + |
| 8 | +Every MCP client starts by opening a transport connection and creating a `ClientSession`. |
| 9 | +The session handles the MCP protocol lifecycle: initialization, request/response exchange, |
| 10 | +and shutdown. |
| 11 | + |
| 12 | +```python |
| 13 | +from mcp import ClientSession, StdioServerParameters |
| 14 | +from mcp.client.stdio import stdio_client |
| 15 | + |
| 16 | +server_params = StdioServerParameters(command="uv", args=["run", "my-server"]) |
| 17 | + |
| 18 | +async with stdio_client(server_params) as (read_stream, write_stream): |
| 19 | + async with ClientSession(read_stream, write_stream) as session: |
| 20 | + await session.initialize() |
| 21 | + # Session is now ready to use |
| 22 | +``` |
| 23 | + |
| 24 | +## Transports |
| 25 | + |
| 26 | +### stdio Transport |
| 27 | + |
| 28 | +The `stdio_client` context manager spawns a subprocess and communicates over |
| 29 | +stdin/stdout. This is the most common transport for local servers. |
| 30 | + |
| 31 | +```python |
| 32 | +from mcp import ClientSession, StdioServerParameters |
| 33 | +from mcp.client.stdio import stdio_client |
| 34 | + |
| 35 | +server_params = StdioServerParameters( |
| 36 | + command="uv", |
| 37 | + args=["run", "my-server"], |
| 38 | + env={"API_KEY": "secret"}, # merged with safe defaults |
| 39 | + cwd="/path/to/working/dir", |
| 40 | +) |
| 41 | + |
| 42 | +async with stdio_client(server_params) as (read_stream, write_stream): |
| 43 | + async with ClientSession(read_stream, write_stream) as session: |
| 44 | + await session.initialize() |
| 45 | +``` |
| 46 | + |
| 47 | +### Streamable HTTP Transport |
| 48 | + |
| 49 | +The `streamablehttp_client` connects to a remote server over HTTP, with optional |
| 50 | +SSE streaming for server-to-client messages. |
| 51 | + |
| 52 | +```python |
| 53 | +from mcp import ClientSession |
| 54 | +from mcp.client.streamable_http import streamablehttp_client |
| 55 | + |
| 56 | +async with streamablehttp_client("http://localhost:8000/mcp") as ( |
| 57 | + read_stream, |
| 58 | + write_stream, |
| 59 | + get_session_id, |
| 60 | +): |
| 61 | + async with ClientSession(read_stream, write_stream) as session: |
| 62 | + await session.initialize() |
| 63 | + # get_session_id() returns the server-assigned session ID, or None |
| 64 | +``` |
| 65 | + |
| 66 | +### SSE Transport (Legacy) |
| 67 | + |
| 68 | +The `sse_client` context manager connects to a server using the legacy |
| 69 | +Server-Sent Events transport. This transport is deprecated in favor of Streamable |
| 70 | +HTTP, but remains available for backward compatibility with older servers. |
| 71 | + |
| 72 | +```python |
| 73 | +from mcp import ClientSession |
| 74 | +from mcp.client.sse import sse_client |
| 75 | + |
| 76 | +async with sse_client( |
| 77 | + url="http://localhost:8000/sse", |
| 78 | + headers={"Authorization": "Bearer token"}, |
| 79 | + timeout=5, # HTTP timeout for regular operations |
| 80 | + sse_read_timeout=300, # how long to wait for a new SSE event |
| 81 | +) as (read_stream, write_stream): |
| 82 | + async with ClientSession(read_stream, write_stream) as session: |
| 83 | + await session.initialize() |
| 84 | +``` |
| 85 | + |
| 86 | +## Roots |
| 87 | + |
| 88 | +Roots tell the server which directories or files the client is making available. |
| 89 | +A server may request the list of roots at any time during a session. |
| 90 | + |
| 91 | +### Providing Roots |
| 92 | + |
| 93 | +Supply a `list_roots_callback` when constructing the `ClientSession`. The callback |
| 94 | +receives a `RequestContext` and must return a `ListRootsResult`. |
| 95 | + |
| 96 | +```python |
| 97 | +from mcp import ClientSession, types |
| 98 | +from mcp.client.stdio import stdio_client |
| 99 | +from mcp.shared.context import RequestContext |
| 100 | + |
| 101 | +async def handle_list_roots( |
| 102 | + context: RequestContext[ClientSession, None], |
| 103 | +) -> types.ListRootsResult: |
| 104 | + return types.ListRootsResult( |
| 105 | + roots=[ |
| 106 | + types.Root( |
| 107 | + uri="file:///home/user/project", |
| 108 | + name="My Project", |
| 109 | + ), |
| 110 | + types.Root( |
| 111 | + uri="file:///home/user/data", |
| 112 | + name="Data Directory", |
| 113 | + ), |
| 114 | + ] |
| 115 | + ) |
| 116 | + |
| 117 | +server_params = StdioServerParameters(command="uv", args=["run", "my-server"]) |
| 118 | + |
| 119 | +async with stdio_client(server_params) as (read_stream, write_stream): |
| 120 | + async with ClientSession( |
| 121 | + read_stream, |
| 122 | + write_stream, |
| 123 | + list_roots_callback=handle_list_roots, |
| 124 | + ) as session: |
| 125 | + await session.initialize() |
| 126 | +``` |
| 127 | + |
| 128 | +When a `list_roots_callback` is provided, the client automatically advertises the |
| 129 | +`roots` capability (with `listChanged=True`) during initialization. |
| 130 | + |
| 131 | +### Sending Roots Changed Notifications |
| 132 | + |
| 133 | +When the set of roots changes after initialization, notify the server so it can |
| 134 | +re-request the list: |
| 135 | + |
| 136 | +```python |
| 137 | +# After updating what handle_list_roots would return: |
| 138 | +await session.send_roots_list_changed() |
| 139 | +``` |
| 140 | + |
| 141 | +The server will then call the `list_roots_callback` again to get the updated list. |
| 142 | + |
| 143 | +## Tools |
| 144 | + |
| 145 | +### Listing Tools |
| 146 | + |
| 147 | +```python |
| 148 | +result = await session.list_tools() |
| 149 | +for tool in result.tools: |
| 150 | + print(f"{tool.name}: {tool.description}") |
| 151 | +``` |
| 152 | + |
| 153 | +Paginated listing is supported via `PaginatedRequestParams`: |
| 154 | + |
| 155 | +```python |
| 156 | +result = await session.list_tools() |
| 157 | +while result.nextCursor: |
| 158 | + result = await session.list_tools( |
| 159 | + params=types.PaginatedRequestParams(cursor=result.nextCursor) |
| 160 | + ) |
| 161 | +``` |
| 162 | + |
| 163 | +### Calling Tools |
| 164 | + |
| 165 | +```python |
| 166 | +result = await session.call_tool( |
| 167 | + "get_weather", |
| 168 | + arguments={"city": "San Francisco"}, |
| 169 | +) |
| 170 | +for content in result.content: |
| 171 | + if isinstance(content, types.TextContent): |
| 172 | + print(content.text) |
| 173 | +``` |
| 174 | + |
| 175 | +You can pass a `read_timeout_seconds` for long-running tools and a |
| 176 | +`progress_callback` for progress updates: |
| 177 | + |
| 178 | +```python |
| 179 | +from datetime import timedelta |
| 180 | + |
| 181 | +async def on_progress( |
| 182 | + progress: float, total: float | None, message: str | None |
| 183 | +) -> None: |
| 184 | + print(f"Progress: {progress}/{total} - {message}") |
| 185 | + |
| 186 | +result = await session.call_tool( |
| 187 | + "long_running_analysis", |
| 188 | + arguments={"dataset": "large.csv"}, |
| 189 | + read_timeout_seconds=timedelta(minutes=5), |
| 190 | + progress_callback=on_progress, |
| 191 | +) |
| 192 | +``` |
| 193 | + |
| 194 | +## Resources |
| 195 | + |
| 196 | +### Listing Resources |
| 197 | + |
| 198 | +```python |
| 199 | +result = await session.list_resources() |
| 200 | +for resource in result.resources: |
| 201 | + print(f"{resource.uri}: {resource.name}") |
| 202 | +``` |
| 203 | + |
| 204 | +### Listing Resource Templates |
| 205 | + |
| 206 | +```python |
| 207 | +result = await session.list_resource_templates() |
| 208 | +for template in result.resourceTemplates: |
| 209 | + print(f"{template.uriTemplate}: {template.name}") |
| 210 | +``` |
| 211 | + |
| 212 | +### Reading Resources |
| 213 | + |
| 214 | +```python |
| 215 | +from pydantic import AnyUrl |
| 216 | + |
| 217 | +result = await session.read_resource(AnyUrl("file:///path/to/file.txt")) |
| 218 | +for content in result.contents: |
| 219 | + if isinstance(content, types.TextResourceContents): |
| 220 | + print(content.text) |
| 221 | + elif isinstance(content, types.BlobResourceContents): |
| 222 | + print(f"Binary data: {len(content.blob)} bytes") |
| 223 | +``` |
| 224 | + |
| 225 | +## Prompts |
| 226 | + |
| 227 | +### Listing Prompts |
| 228 | + |
| 229 | +```python |
| 230 | +result = await session.list_prompts() |
| 231 | +for prompt in result.prompts: |
| 232 | + print(f"{prompt.name}: {prompt.description}") |
| 233 | +``` |
| 234 | + |
| 235 | +### Getting a Prompt |
| 236 | + |
| 237 | +```python |
| 238 | +result = await session.get_prompt( |
| 239 | + "code_review", |
| 240 | + arguments={"language": "python", "code": "def hello(): pass"}, |
| 241 | +) |
| 242 | +print(f"Description: {result.description}") |
| 243 | +for message in result.messages: |
| 244 | + print(f"[{message.role}]: {message.content}") |
| 245 | +``` |
| 246 | + |
| 247 | +## Completions |
| 248 | + |
| 249 | +The `complete` method requests argument completions from the server. This is useful |
| 250 | +for building interactive UIs where users fill in prompt or resource template arguments. |
| 251 | + |
| 252 | +```python |
| 253 | +# Complete a prompt argument |
| 254 | +result = await session.complete( |
| 255 | + ref=types.PromptReference(type="ref/prompt", name="code_review"), |
| 256 | + argument={"name": "language", "value": "py"}, |
| 257 | +) |
| 258 | +for value in result.completion.values: |
| 259 | + print(value) # e.g., "python" |
| 260 | + |
| 261 | +# Complete a resource template argument |
| 262 | +result = await session.complete( |
| 263 | + ref=types.ResourceTemplateReference( |
| 264 | + type="ref/resource", uri="file:///{path}" |
| 265 | + ), |
| 266 | + argument={"name": "path", "value": "/home/user/"}, |
| 267 | +) |
| 268 | +``` |
| 269 | + |
| 270 | +## Ping |
| 271 | + |
| 272 | +Send a ping to verify the server is responsive: |
| 273 | + |
| 274 | +```python |
| 275 | +await session.send_ping() |
| 276 | +``` |
| 277 | + |
| 278 | +## Logging |
| 279 | + |
| 280 | +Set the server's logging level and handle log messages via a callback: |
| 281 | + |
| 282 | +```python |
| 283 | +async def handle_log(params: types.LoggingMessageNotificationParams) -> None: |
| 284 | + print(f"[{params.level}] {params.logger}: {params.data}") |
| 285 | + |
| 286 | +async with ClientSession( |
| 287 | + read_stream, |
| 288 | + write_stream, |
| 289 | + logging_callback=handle_log, |
| 290 | +) as session: |
| 291 | + await session.initialize() |
| 292 | + await session.set_logging_level("info") |
| 293 | +``` |
| 294 | + |
| 295 | +## Full Example |
| 296 | + |
| 297 | +Putting it all together -- a client that connects to a server, lists its |
| 298 | +capabilities, and calls a tool: |
| 299 | + |
| 300 | +```python |
| 301 | +import anyio |
| 302 | +from mcp import ClientSession, StdioServerParameters, types |
| 303 | +from mcp.client.stdio import stdio_client |
| 304 | + |
| 305 | + |
| 306 | +async def main() -> None: |
| 307 | + server_params = StdioServerParameters( |
| 308 | + command="uv", args=["run", "my-server"] |
| 309 | + ) |
| 310 | + |
| 311 | + async with stdio_client(server_params) as (read_stream, write_stream): |
| 312 | + async with ClientSession(read_stream, write_stream) as session: |
| 313 | + await session.initialize() |
| 314 | + |
| 315 | + # List available tools |
| 316 | + tools_result = await session.list_tools() |
| 317 | + for tool in tools_result.tools: |
| 318 | + print(f"Tool: {tool.name}") |
| 319 | + |
| 320 | + # List available resources |
| 321 | + resources_result = await session.list_resources() |
| 322 | + for resource in resources_result.resources: |
| 323 | + print(f"Resource: {resource.uri}") |
| 324 | + |
| 325 | + # Call a tool |
| 326 | + result = await session.call_tool( |
| 327 | + "greet", arguments={"name": "World"} |
| 328 | + ) |
| 329 | + print(result.content[0].text) |
| 330 | + |
| 331 | + |
| 332 | +anyio.run(main) |
| 333 | +``` |
0 commit comments