# MCP - Model Context Protocol ## Overview The Model Context Protocol (MCP) is a standardized protocol that enables AI applications to securely connect with external data sources and tools. It acts as a bridge between AI models and various systems they need to interact with, providing a unified interface for dynamic capability discovery and execution. ## Core Architecture ### Components - **MCP Client**: The AI application (like Claude Desktop, IDEs, or custom AI tools) that initiates connections and consumes capabilities - **MCP Server**: Standalone processes that expose specific capabilities - data access (resources) and actions (tools) - **Transport Layer**: Communication over standard transports (stdio for local processes, SSE for remote connections) ### Key Concepts - **Resources**: Read-only data that can be accessed by the AI (files, database records, API responses) - **Tools**: Actions the AI can execute (running code, making API calls, modifying files) - **Prompts**: Reusable prompt templates exposed by servers ## MCP vs Traditional Tool Calling MCP adds significant value beyond simple "remoting" of tool calls: ### Dynamic Capability Discovery - Traditional: Hardcoded tools at compile time - MCP: Runtime discovery of available tools and resources ### Standardized Protocol - Uniform request/response structure across all servers - Transport agnostic (stdio, SSE, WebSockets) - Pluggable architecture - swap servers without changing client code ### Resource Management - Distinction between **tools** (actions) and **resources** (data) - URI-based resource addressing (e.g., `file://config.json`, `git://recent_commits`) - Context-aware RAG - dynamic data access based on conversation ### Security & Isolation - Process isolation for each MCP server - Capability-based security model - Controlled interface preventing arbitrary system access ## Resource Discovery and Access Resources use URI-based addressing with flexible schemes: ``` file:///absolute/path/file.txt git://log/main postgres://schema/users api://github/repos/owner/repo ``` ### Discovery Patterns 1. **Hierarchical Browsing**: `list_resources()` with optional URI prefixes 2. **Search/Query**: Search across resources by content or metadata 3. **Direct Access**: Read specific resources by URI ### LLM Awareness Two main patterns for making the LLM aware of resources: 1. **Proactive Injection**: Client fetches relevant resources and injects into system prompt 2. **Resource Access Tools**: LLM calls tools like `read_resource(uri)` or `search_resources(query)` ## Practical Implementation Flow ### Setup Phase 1. Connect to MCP servers 2. Discover available tools and resources 3. Build system prompt with tool schemas ### Conversation Loop 1. User sends message 2. LLM processes with available tools context 3. LLM may request tool execution or resource access 4. Agent orchestrates MCP calls 5. Results fed back to LLM for final response ## Sequence Diagram ```mermaid sequenceDiagram participant User participant Agent as AI Agent/Host participant Client as MCP Client participant Server as MCP Server participant LLM Note over Agent, Server: 1. Initial Setup & Discovery Agent->>Client: Initialize MCP Client Client->>Server: Connect (stdio/SSE transport) Server-->>Client: Connection established Client->>Server: list_tools() Server-->>Client: Available tools schema Client->>Server: list_resources() Server-->>Client: Available resources Client->>Server: list_prompts() Server-->>Client: Available prompts Agent->>Agent: Build system prompt with tool schemas Note over User, LLM: 2. User Interaction Flow User->>Agent: "What's in my config.json?" Agent->>LLM: System prompt + User message LLM-->>Agent: Response with tool_use request Note over Agent, Server: 3. Tool Execution Agent->>Agent: Parse tool call from LLM response Agent->>Client: Execute tool call Client->>Server: call_tool("file_read", {"path": "config.json"}) Server->>Server: Read file from filesystem Server-->>Client: Tool result (file contents) Client-->>Agent: Tool execution result Note over Agent, LLM: 4. Result Processing Agent->>Agent: Format tool result for LLM Agent->>LLM: Previous context + tool result LLM-->>Agent: Final response to user Agent-->>User: "Here's what's in your config.json..." Note over Agent, Server: 5. Multiple Tool Calls (if needed) User->>Agent: "Now update the API key in that file" Agent->>LLM: System prompt + conversation history LLM-->>Agent: Response with file_write tool call Agent->>Client: Execute file_write tool Client->>Server: call_tool("file_write", {"path": "config.json", "content": "..."}) Server->>Server: Write to filesystem Server-->>Client: Success confirmation Client-->>Agent: Write operation result Agent->>LLM: Tool result + context LLM-->>Agent: "File updated successfully" Agent-->>User: Confirmation message Note over Agent, Server: 6. Resource Access (Dynamic Context) User->>Agent: "Summarize my recent commits" Agent->>LLM: System prompt + User message LLM-->>Agent: Requests git resource access Agent->>Client: Get resource Client->>Server: read_resource("git://recent_commits") Server->>Server: Execute git log command Server-->>Client: Commit history data Client-->>Agent: Resource content Agent->>LLM: Resource data + context LLM-->>Agent: Formatted summary Agent-->>User: "Here's a summary of your recent commits..." Note over Client, Server: 7. Connection Management Agent->>Client: Shutdown Client->>Server: Disconnect Server-->>Client: Connection closed ``` ## Key Benefits - **Dynamic Extensibility**: Add new capabilities without modifying the core AI application - **Secure Integration**: Controlled access to external systems through standardized interfaces - **Context-Aware Operation**: AI can access real-time data and adapt to current environment - **Ecosystem Approach**: Creates a plugin-like ecosystem for AI tool integration ## Common Use Cases - **Development Tools**: File system access, Git operations, code execution - **Database Integration**: Query databases, access schemas, perform CRUD operations - **API Integration**: Access external services and APIs - **System Monitoring**: Access logs, system metrics, process information - **Content Management**: Document access, knowledge base integration --- *Created: [[2025-09-12]]* *Tags: #MCP #AI #Protocol #Tools #Resources*