Skip to content

Commit c07a587

Browse files
committed
docs: document client request context
1 parent 161834d commit c07a587

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,58 @@ if __name__ == "__main__":
22752275
_Full example: [examples/snippets/clients/streamable_basic.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/streamable_basic.py)_
22762276
<!-- /snippet-source -->
22772277

2278+
#### Passing Request Context
2279+
2280+
The high-level `Client` API accepts protocol-level metadata on individual
2281+
requests. Use the `meta` argument when values such as trace IDs, tenant IDs, or
2282+
other application context should be sent as MCP JSON-RPC `_meta` data:
2283+
2284+
```python
2285+
from mcp.client import Client
2286+
2287+
2288+
async with Client("https://mcp.example.com/mcp") as client:
2289+
result = await client.call_tool(
2290+
"search",
2291+
{"query": "python sdk"},
2292+
meta={
2293+
"trace_id": "req-123",
2294+
"tenant_id": "tenant-a",
2295+
},
2296+
)
2297+
```
2298+
2299+
Transport configuration is separate from MCP request metadata. For HTTP
2300+
headers, authentication, timeouts, proxies, or other HTTP-layer settings, create
2301+
an `httpx.AsyncClient` and pass it to `streamable_http_client`:
2302+
2303+
```python
2304+
import httpx
2305+
2306+
from mcp import ClientSession
2307+
from mcp.client.streamable_http import streamable_http_client
2308+
2309+
2310+
async with httpx.AsyncClient(
2311+
headers={"Authorization": "Bearer user-token"},
2312+
) as http_client:
2313+
async with streamable_http_client(
2314+
"https://mcp.example.com/mcp",
2315+
http_client=http_client,
2316+
) as (read_stream, write_stream):
2317+
async with ClientSession(read_stream, write_stream) as session:
2318+
await session.initialize()
2319+
result = await session.call_tool(
2320+
"search",
2321+
{"query": "python sdk"},
2322+
meta={"trace_id": "req-123"},
2323+
)
2324+
```
2325+
2326+
If HTTP values need to change over time, put that behavior in the `httpx`
2327+
client, for example with an `httpx.Auth` implementation or event hooks that
2328+
read from your application's request state.
2329+
22782330
### Client Display Utilities
22792331

22802332
When building MCP clients, the SDK provides utilities to help display human-readable names for tools, resources, and prompts:

0 commit comments

Comments
 (0)