Real-World MCP Patterns
- Design a stateful tool handle with the three required properties: opaque, time-limited, and authorization-scoped
- Explain what elicitation is, what problem it solves, and how it replaces the deprecated sampling feature
- Use the MCP Inspector to test resources and prompts, not just tools
The Stateful Handle Pattern in Depth
Lesson 4 introduced the handle pattern for stateful tools. Here we go deeper into how to design handles that are robust in production.
A handle is an opaque identifier — typically a UUID or a signed token — that the server returns from a creation tool and expects back on subsequent calls. The state lives in the server's backing store; the handle is just a key into that store. The model passes the handle as a tool argument, never modifying or constructing it.
Well-designed handles have three properties:
- Opaque: The client cannot parse the handle's internal structure. Use a UUID or a base64-encoded signed payload, never a human-readable ID like
session-42. If clients can read the structure, they may start relying on it, creating hidden coupling. - Time-limited: Every handle should have a declared lifetime. Document it in the tool's description: "Returns a session handle valid for 30 minutes." When the handle expires, return a clear error that tells the model to create a new session, not a generic "invalid handle" message. The model can act on "session expired — create a new one" but not on "error code 404."
- Authorization-scoped: If your server is multi-user, tie handles to a specific user or token. A handle that works for any user is an access control hole. Validate the requesting client's identity against the handle's owner on every call.
A typical handle-based tool set looks like this:
create_analysis_session→ returns{"session_id": "uuid-here"}add_data_to_session→ takessession_idanddatarun_analysis→ takessession_idandanalysis_typeclose_session→ takessession_id, cleans up resources
Multi-Server Aggregation and Disambiguation
A production host typically connects to several servers simultaneously. The host must handle name collisions between tools from different servers. The standard approach is to prefix each tool name with the server identifier when presenting them to the LLM. For example:
filesystem__read_filefrom the filesystem servergithub__read_filefrom the GitHub serverdrive__read_filefrom the Google Drive server
The host strips the prefix before forwarding calls to the server. The server only ever sees its own tool names. This design keeps servers simple — they do not need to know about other servers — while giving the LLM enough context to pick the right tool.
As a server author, you have no control over how hosts prefix your tools. But you can make disambiguation easier by giving your tools clear, specific names. read_local_file is better than read_file if your server specializes in local filesystem access, because it makes the tool's scope obvious even without a prefix.
Subscriptions and Notifications in Practice
Subscriptions let clients receive push notifications when resources change, without having to poll. Setting up a subscription involves the subscriptions/listen request, which takes the notification types the client wants:
{
"method": "subscriptions/listen",
"params": {
"toolsListChanged": true,
"resourceSubscriptions": ["file:///Users/jab/logs/app.log"]
}
}
The server acknowledges with a notifications/subscriptions/acknowledged message and then delivers events as they occur. Each event is tagged with io.modelcontextprotocol/subscriptionId so the client can correlate it with its subscription.
Important caveats:
- Notification delivery is best-effort. Network hiccups, server restarts, or connection interruptions can cause missed notifications.
- Clients should still poll periodically for freshness in latency-sensitive scenarios. Treat notifications as an optimization, not a guarantee.
- When you receive a
notifications/resources/updated, it tells you a resource changed but does not include the new content. You must re-fetch the resource withresources/read. - When you receive
notifications/tools/list_changed, re-fetch the tool list and update your registry. If the host uses prompt caching for the tool list, the cache is now stale.
The Tasks Extension
Some tool calls take a long time — running a build, generating a large report, processing a video. The base MCP protocol has no mechanism for long-running requests: a tool call either returns immediately or the client blocks indefinitely waiting for a response.
The Tasks extension solves this. When a client uses a server that supports Tasks, a tool call can return a task result instead of a final result. The task result includes a task ID and optionally a URI where the client can poll for status. The client can poll tasks/status at any interval and receive the final result when the task completes.
As a server author, you opt into the Tasks extension by declaring it in your capabilities. As a client author, you check for the extension on discovery and fall back gracefully if the server does not support it. If you are using an SDK, it handles the task polling loop for you.
Debugging with the MCP Inspector
The MCP Inspector is indispensable for development. Beyond the basic tools/list and tools/call commands you used in Lessons 7 and 8, it supports the full protocol surface:
# List resources
npx @modelcontextprotocol/inspector --cli uv run server.py
--protocol-era modern --method resources/list
# Read a resource
npx @modelcontextprotocol/inspector --cli uv run server.py
--protocol-era modern --method resources/read
--params '{"uri":"file:///path/to/file.txt"}'
# List prompts
npx @modelcontextprotocol/inspector --cli uv run server.py
--protocol-era modern --method prompts/list
# Get a prompt with arguments
npx @modelcontextprotocol/inspector --cli uv run server.py
--protocol-era modern --method prompts/get
--params '{"name":"review","arguments":{"language":"python"}}'
# Run server/discover to see capabilities
npx @modelcontextprotocol/inspector --cli uv run server.py
--protocol-era modern --method server/discover
The Inspector separates stdout (protocol messages) from stderr (server logs) in its output. If your server logs useful information during tool execution, you will see it in the Inspector's stderr section — invaluable for debugging tool failures.
Caching Discovery Results
The server/discover response includes ttlMs (time-to-live in milliseconds) and cacheScope (whether the result is valid for all clients or just the requesting one). Respecting these fields avoids unnecessary discovery round trips.
A server that changes its capabilities dynamically (adding tools based on feature flags, for example) should set a short TTL — perhaps 60,000 ms. A server that never changes should set a long TTL — perhaps 86,400,000 ms (24 hours). Clients can cache the result until the TTL expires and then re-discover.
If your SDK does this automatically (most do), you can verify it is working by watching the number of server/discover calls your server receives. It should be much lower than the number of tool calls.
Elicitation: Multi-Round-Trip Flows
Some tools need additional input that was not provided in the initial arguments. For example, a file creation tool might succeed but then discover a naming conflict and need to ask the user whether to overwrite or rename. In older versions of MCP, this was handled by sampling (now deprecated). In the current spec, it is handled by elicitation.
Elicitation allows a server to pause a tool call and ask the client for more input. The server returns an InputRequiredResult with an inputRequests field describing what it needs. The client prompts the user and retries the tool call with an inputResponses field containing the answers.
Elicitation is a client capability — the server can only use it if the client declared elicitation support. Well-written servers degrade gracefully when elicitation is not available, by either using sensible defaults or returning an execution error asking the user to provide the needed information as a tool argument.
Deprecated Features: Roots and Sampling
If you read older MCP tutorials or examine servers built before mid-2026, you will encounter two features that are now deprecated: roots and sampling. The protocol spec marks them deprecated as of version 2026-07-28 and says new implementations should not adopt them.
Roots was a mechanism that let clients tell servers which filesystem directories to focus on. The idea was that a server could limit its file access to the directories the user considered relevant. In practice, roots were informational only — the protocol never enforced that servers stayed within them — which made them a poor access control primitive. The current guidance is to pass directory paths as explicit tool arguments, resource URIs, or server configuration instead.
Sampling let servers send LLM completion requests back through the client, so the server could call the LLM without needing its own API key. This created complex trust and security questions. The current guidance is for servers that need LLM access to use their own provider API key directly. Elicitation covers the user-input use case that sampling was sometimes used for.
If you need to work with a server that uses roots or sampling, the SDK still supports them (they are deprecated for a minimum of twelve months before removal). But for new servers you build, skip them.
- Handles must be opaque, time-limited with clear expiry errors, and scoped to the authorizing user — not just UUIDs passed blindly
- Notifications are best-effort — clients must still poll for freshness in latency-sensitive scenarios
- Roots and sampling are deprecated in the 2026-07-28 spec; use explicit arguments, resource URIs, and direct API keys instead
Progress is saved in this browser only — clear your cache or switch device and it's gone. Create a free account and it follows you everywhere.