Lesson 10: Model Context Protocol (MCP)
Connect to standard Model Context Protocol servers: spawn local/remote MCP servers, discover schema tools, flatten multi-part content, and execute tools seamlessly.
1. Why This Feature Exists
Anthropic Model Context Protocol (MCP) has emerged as an industry standard for external tool servers. Merci SDK includes built-in client support for MCP servers across all 8 supported providers.
2. Key Symbols and Exports
3. Implementation Walkthrough
import {
ProviderType,
resolveProvider,
initializeClient,
loadCredentials,
getToolModel
} from 'merci-sdk';
import { McpClient, loadMcpTools } from 'merci-sdk/tools';
const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });
// 1. Connect to local MCP Server
const mcpClient = new McpClient({
command: 'node',
args: ['examples/24_mcp_server.mjs']
});
await mcpClient.connect();
// 2. Load discovered MCP tools
const mcpTools = await loadMcpTools(mcpClient);
// 3. Register tools on session
const session = client.session(getToolModel({ provider: PROVIDER }))
.withTools(mcpTools);
const result = await session.run('Use the calculator tool to compute 42 * 1337.');
console.log('Result:', result);
await mcpClient.close();4. Wire and Telemetry Mechanics
McpClient initiates a JSON-RPC handshake with the MCP server subprocess. When the model invokes an MCP tool, the SDK dispatches the RPC tools/call message and parses the result.
5. Run the Example
Execute the runnable example directly from the repository root:
node examples/24_mcp_client.mjsRead the full example: examples/24_mcp_client.mjs →