MCP Client — Connect to Any MCP Server
Quick Reference
Connect to any Model Context Protocol server, list its tools, invoke them with typed parameters, and handle results. The core building block for MCP-powered automation.
Overview
The MCP client is your connection layer to MCP servers. It handles transports (stdio for local processes, HTTP/SSE for remote), tool schema resolution, type-safe invocation, error classification (protocol errors, connection failures, timeouts), and result handling. Every server interaction starts here: connect, list tools, call tool, disconnect.
Quick Start
- Connect:
client = MCPClient("server-name", transport={"type": "stdio", "command": "node", "args": ["server.js"]})
- Discover tools:
tools = client.list_tools() — returns typed schemas for every tool
- Invoke:
result = client.call_tool("tool_name", {"param": "value"}) — returns structured output
Key Pattern: Resilient Tool Invocation
def safe_invoke(client, tool, params, retries=2):
errors = []
for attempt in range(retries + 1):
try:
result = client.call_tool(tool, params)
if result.is_error:
code = result.error.get("code", -1)
if code in (-32700, -32600, -32601):
raise RuntimeError(f"Protocol error: {result.error['message']}")
return result
except (ConnectionError, TimeoutError) as e:
errors.append(f"Attempt {attempt + 1}: {e}")
if attempt < retries:
time.sleep(2 ** attempt)
raise RuntimeError(f"Failed: {'; '.join(errors)}")
When NOT to Use
- You need a full SDK with application-level abstractions (use language-specific MCP SDK)
- You're building a long-running server (this is a client, not a server implementation)
- Connection management isn't needed (direct HTTP calls suffice for one-off tool calls)
Transports
| Transport |
When |
Example |
| stdio |
Local process |
{"command": "node", "args": ["server.js"]} |
| HTTP/SSE |
Remote server |
{"url": "https://mcp.example.com", "headers": {"Auth": "Bearer token"}} |
Client Checklist
Commands
# List all available tools from a connected MCP server
# (implement via client.list_tools())
# Call a tool with typed parameters
# (implement via client.call_tool("tool_name", {"param": "value"}))
# Standard health check before any tool call
# client.ping() → throws on unreachable server
Dependencies
- Python 3.10+ (for async/await patterns)
mcp package: Python MCP SDK
httpx or aiohttp: for HTTP/SSE transport
- No external services required (connects to local or remote MCP servers)
Verification
When to Use
Use when working with mcp client.
Workflow
Execute these steps sequentially:
- Connect:
client = MCPClient("server-name", transport={"type": "stdio", "command": "node", "args": ["server.js"]})
- Discover tools:
tools = client.list_tools() — returns typed schemas for every tool
- Invoke:
result = client.call_tool("tool_name", {"param": "value"}) — returns structured output
Anti-Rationalization Table
| Rationalization |
Reality |
| "I'll just use curl/requests directly" |
MCP handles auth, retries, streaming, and type-safe schemas — curl duplicates all of that manually |
| "I only need one connection, no abstraction needed" |
One connection today becomes three next month. The client abstraction pays for itself at server #2 |
| "Synchronous calls are fine" |
MCP pipelines need async for parallel tool execution. Plan for it from the start |
Process
- Connect — Create
MCPClient with transport (stdio, SSE, HTTP) and server command
- Discover — Call
list_tools() to get typed schemas for all available tools
- Invoke — Call
call_tool(name, params) with validated JSON Schema inputs
- Handle Errors — Retry with exponential backoff, handle MCP error codes gracefully
- Disconnect — Call
disconnect() to clean up processes, avoid dangling resources
- Monitor — Health checks, reconnect on failure, log tool invocations for debugging
Anti-Rationalization Table
1---2name: mcp-client3description: Use when generic MCP client implementation for connecting to any Model4 Context Protocol server with standardized tool access. Use when working with mcp5 client.6domain: mcp7author: oyi778license: Apache-2.09subdomain: mcp10tags:11- client12- mcp13- mcp-server14- model-context-protocol15- tool-integration16version: 1.0.017category: mcp18metadata:19 source: https://skillmd.com/skills/oyi77/mcp-client20---21222324# MCP Client — Connect to Any MCP Server2526## Quick Reference2728Connect to any Model Context Protocol server, list its tools, invoke them with typed parameters, and handle results. The core building block for MCP-powered automation.2930## Overview3132The MCP client is your connection layer to MCP servers. It handles transports (stdio for local processes, HTTP/SSE for remote), tool schema resolution, type-safe invocation, error classification (protocol errors, connection failures, timeouts), and result handling. Every server interaction starts here: connect, list tools, call tool, disconnect.3334## Quick Start35361. **Connect**: `client = MCPClient("server-name", transport={"type": "stdio", "command": "node", "args": ["server.js"]})`372. **Discover tools**: `tools = client.list_tools()` — returns typed schemas for every tool383. **Invoke**: `result = client.call_tool("tool_name", {"param": "value"})` — returns structured output3940## Key Pattern: Resilient Tool Invocation4142```python43def safe_invoke(client, tool, params, retries=2):44 errors = []45 for attempt in range(retries + 1):46 try:47 result = client.call_tool(tool, params)48 if result.is_error:49 code = result.error.get("code", -1)50 if code in (-32700, -32600, -32601):51 raise RuntimeError(f"Protocol error: {result.error['message']}")52 return result53 except (ConnectionError, TimeoutError) as e:54 errors.append(f"Attempt {attempt + 1}: {e}")55 if attempt < retries:56 time.sleep(2 ** attempt)57 raise RuntimeError(f"Failed: {'; '.join(errors)}")58```5960## When NOT to Use6162- You need a full SDK with application-level abstractions (use language-specific MCP SDK)63- You're building a long-running server (this is a client, not a server implementation)64- Connection management isn't needed (direct HTTP calls suffice for one-off tool calls)6566## Transports6768| Transport | When | Example |69|---|---|---|70| stdio | Local process | `{"command": "node", "args": ["server.js"]}` |71| HTTP/SSE | Remote server | `{"url": "https://mcp.example.com", "headers": {"Auth": "Bearer token"}}` |7273## Client Checklist7475- [ ] `ping()` succeeds before any tool invocation76- [ ] `list_tools()` returns valid JSON Schema for each parameter77- [ ] Error handling covers all 3 failure modes: protocol error, connection failure, timeout78- [ ] Exponential backoff configured for transient failures79- [ ] Disconnect/cleanup called after each session8081## Commands8283```bash84# List all available tools from a connected MCP server85# (implement via client.list_tools())8687# Call a tool with typed parameters88# (implement via client.call_tool("tool_name", {"param": "value"}))8990# Standard health check before any tool call91# client.ping() → throws on unreachable server92```9394## Dependencies9596- Python 3.10+ (for async/await patterns)97- `mcp` package: Python MCP SDK98- `httpx` or `aiohttp`: for HTTP/SSE transport99- No external services required (connects to local or remote MCP servers)100101## Verification102103- [ ] `client.ping()` succeeds before invoking tools104- [ ] `list_tools()` returns valid JSON Schema for every tool parameter105- [ ] Error handling tested for all 3 failure modes: protocol error, connection failure, timeout106- [ ] Exponential backoff configured for transient failures107- [ ] `disconnect()` called after each session (no dangling processes)108109## When to Use110111Use when working with mcp client.112113## Workflow114115Execute these steps sequentially:1161171. **Connect**: `client = MCPClient("server-name", transport={"type": "stdio", "command": "node", "args": ["server.js"]})`1182. **Discover tools**: `tools = client.list_tools()` — returns typed schemas for every tool1193. **Invoke**: `result = client.call_tool("tool_name", {"param": "value"})` — returns structured output120121## Anti-Rationalization Table122123| Rationalization | Reality |124|---|---|125| "I'll just use curl/requests directly" | MCP handles auth, retries, streaming, and type-safe schemas — curl duplicates all of that manually |126| "I only need one connection, no abstraction needed" | One connection today becomes three next month. The client abstraction pays for itself at server #2 |127| "Synchronous calls are fine" | MCP pipelines need async for parallel tool execution. Plan for it from the start |128129## Process1301311. **Connect** — Create `MCPClient` with transport (stdio, SSE, HTTP) and server command1322. **Discover** — Call `list_tools()` to get typed schemas for all available tools1333. **Invoke** — Call `call_tool(name, params)` with validated JSON Schema inputs1344. **Handle Errors** — Retry with exponential backoff, handle MCP error codes gracefully1355. **Disconnect** — Call `disconnect()` to clean up processes, avoid dangling resources1366. **Monitor** — Health checks, reconnect on failure, log tool invocations for debugging137138## Anti-Rationalization Table