Connect an MCP Client
About 42 min
Goal
By the end of this lesson, you can connect an MCP client to task-relevant capabilities and explain how validation and explicit failure handling keep a compatible remote server from becoming trusted by default.
Start with a server that offers more than you need
Imagine a host application connects to a server and discovers these tools:
get_order_status
search_orders
refund_order
delete_order
The user only asked, “Has order 4172 shipped?” Discovery tells the host what the server offers. It does not mean the model should receive or use every offered tool.
The host already knows the current user, task, and local permission rules. It can therefore reduce the discovered list to the capability needed here—perhaps only get_order_status—and expose that smaller choice to the model-facing workflow.
This is the client-side responsibility: treat remote capability descriptions as information, then apply local policy before use. Protocol compatibility answers “Can these components communicate?” Permission answers “May this action happen for this user and task?” Those are separate questions.
The same rule applies after a call. A server response still has to match the expected result shape before the host puts it into model context or application state.
Build a versioned request
A client request should carry the pinned protocol version and the expected operation metadata. If the transport exposes method/name headers, the client should construct them consistently with the request body. Tests should detect disagreement rather than relying on whichever representation a downstream component happens to read.
discover → compatibility check → local authorization → MCP call → validate result
Before a side-effecting call, the host still performs permission and approval checks. The server will have its own checks too. Defense in depth means both sides can enforce their responsibilities without assuming the other side is enough.
Validate remote results and caching
Results are remote input. Validate the expected shape and preserve provenance. If a tool claims it returned an order status, the client should not silently accept a completely different object and feed it into later actions.
Cache behavior needs context. The 2026-07-28 release added cache hints for list and resource results. A client can use those hints, but it should also respect user scope and local sensitivity policy. A response safe to cache for one user is not automatically safe to share with another.
Map protocol errors into recovery
Network and protocol errors should map into the Level 12 retry model. An unsupported version is not a transient timeout. A malformed result usually needs investigation or fallback, not repeated identical calls. A rate limit may be retryable under a bounded policy.
Tracing should connect the host task to the protocol call. Record the endpoint identity, expected protocol version, capability name, attempt, policy result, remote latency, and validation outcome. This makes failures diagnosable even when the remote server is maintained by another team.
A robust client therefore acts as a trust adapter. It translates a remote protocol capability into local controller concepts while keeping remote descriptions, results, and errors inside explicit validation boundaries.
Predict
Run the local Lab
Run:
python labs/notebooks/level-13/l13-05-mcp-client.py
The Lab filters a discovered capability list through version and local role policy.
- Run it unchanged. With
role="reader", the exposed set should containget_orderbut notrefund_orderor the old-version tool. - Before editing, predict which capability will be added if the trusted local role changes to
operator. The remote catalog itself must stay fixed. - Change only
role="reader"torole="operator", then rerun. - Confirm that
refund_orderbecomes locally exposed whileold_toolremains excluded by its protocol version.
Loading lab…