Literature: MCP Client Development
This literature note covers the technical implementation and conceptual framework for building Model Context Protocol (MCP) clients and host applications.
Host vs. Client
- MCP Host: The top-level AI application (e.g., Claude Desktop, IDE) that manages the user experience and coordinates multiple servers.
- MCP Client: The protocol-level component within the host that maintains a 1:1 connection with a specific server.
Advanced Client Capabilities
Clients can provide specialized services to servers to enable richer agentic workflows:
| Feature | Description | Example |
|---|---|---|
| Elicitation | Allows servers to request on-demand structured input from the user. | Asking for seat preferences during a flight booking tool call. |
| Roots | Defines advisory filesystem boundaries (file://) for server operations. |
Restricting a coding agent to a specific project directory. |
| Sampling | Allows servers to request LLM completions through the host's existing model access. | A tool asking the model to summarize a retrieved document. |
Implementation Flow (Python)
- Transport Setup: Initialize
StdioServerParameterswith the command (python/node) and absolute path to the server script. - Session Management: Use
ClientSessionwithin anAsyncExitStackfor proper resource cleanup. - Initialization: Call
session.initialize()to perform the protocol handshake. - Discovery: Retrieve server capabilities via
session.list_tools(). - Execution Loop:
-
- Receive user query.
- Pass available tools to LLM.
- Detect
tool_usein LLM response. - Invoke
session.call_tool(name, args). - Return result to LLM for final synthesis.
-
Best Practices
- Resource Management: Always use
AsyncExitStackor equivalent to ensure pipes are closed gracefully. - Path Handling: Use absolute paths for server scripts to avoid "FileNotFound" errors during host execution.
- Human-in-the-Loop: Implement approval checkpoints for
samplingand high-stakestool_calloperations.
See Also
- mcp-client-development (Permanent Note)
- mcp-moc
- lit-mcp-architecture
- mcp-best-practices