GenAIHub
← Back to Technical Section

Model Context Protocol (MCP)

The Open Standard for AI-Tool Integration

What is MCP?

The Model Context Protocol (MCP) is an open standard developed by Anthropic for connecting AI assistants to external tools, data sources, and services. It provides a universal way for LLMs to interact with the outside world through a standardized, secure, and composable interface.

Think of MCP as "USB for AI"β€”a universal connector that lets any AI model work with any tool, without custom integrations for each combination.

Why MCP?

πŸ”Œ Standardization

Write tools once, use with any MCP-compatible AI. No more building separate integrations for GPT, Claude, Gemini, etc.

πŸ”’ Security

Clear permission model, sandboxed execution, and audit trails. Control what AI can and cannot do.

🧩 Composability

Combine multiple MCP servers to give AI access to databases, APIs, file systems, and custom toolsβ€”all at once.

🏠 Local-First

MCP servers can run locally, keeping sensitive data on your machine. No need to send everything to the cloud.

MCP Architecture

MCP Host (Claude, IDE, App) JSON-RPC MCP Server File System MCP Server Database MCP Server Web API πŸ“ Files πŸ—„οΈ Postgres 🌐 REST API

MCP Host

The AI application (Claude Desktop, VS Code, custom app) that connects to MCP servers.

MCP Server

Exposes tools, resources, and prompts to the host via the MCP protocol.

Transport

JSON-RPC over stdio (local) or HTTP/SSE (remote). Stateful connections.

Core Concepts

πŸ”§ Tools

Functions the AI can call to perform actions: read files, query databases, send emails, execute code, etc.

{
  "name": "read_file",
  "description": "Read contents of a file",
  "inputSchema": {
    "type": "object",
    "properties": {
      "path": { "type": "string", "description": "File path to read" }
    },
    "required": ["path"]
  }
}

πŸ“š Resources

Data sources the AI can read: files, database records, API responses. Resources are identified by URIs.

{
  "uri": "file:///path/to/document.md",
  "name": "Project README",
  "mimeType": "text/markdown"
}

πŸ’¬ Prompts

Reusable prompt templates that can be invoked by name. Useful for standardizing common interactions.

{
  "name": "summarize_document",
  "description": "Summarize a document with key points",
  "arguments": [
    { "name": "document_uri", "required": true }
  ]
}

Building an MCP Server

Here's a minimal MCP server in Python using the official SDK:

from mcp.server import Server
from mcp.types import Tool, TextContent

# Create server
server = Server("my-mcp-server")

# Define a tool
@server.tool()
async def get_weather(city: str) -> str:
    """Get current weather for a city."""
    # Your implementation here
    return f"Weather in {city}: Sunny, 22Β°C"

# List available tools
@server.list_tools()
async def list_tools() -> list[Tool]:
    return [
        Tool(
            name="get_weather",
            description="Get current weather for a city",
            inputSchema={
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"}
                },
                "required": ["city"]
            }
        )
    ]

# Run the server
if __name__ == "__main__":
    import asyncio
    from mcp.server.stdio import stdio_server
    
    asyncio.run(stdio_server(server))
            

Connecting to Claude Desktop

Configure Claude Desktop to use your MCP server by editing the config file:

// ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)
// %APPDATA%\Claude\claude_desktop_config.json (Windows)

{
  "mcpServers": {
    "my-weather-server": {
      "command": "python",
      "args": ["/path/to/my_server.py"],
      "env": {
        "API_KEY": "your-api-key"
      }
    },
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/dir"]
    }
  }
}
            

πŸ’‘ After saving: Restart Claude Desktop. You'll see your tools available in the πŸ”§ menu when chatting.

Popular MCP Servers

Server Purpose Install
@modelcontextprotocol/server-filesystem Read/write local files npx -y @modelcontextprotocol/server-filesystem
@modelcontextprotocol/server-postgres Query PostgreSQL databases npx -y @modelcontextprotocol/server-postgres
@modelcontextprotocol/server-github Interact with GitHub repos npx -y @modelcontextprotocol/server-github
@modelcontextprotocol/server-slack Read/send Slack messages npx -y @modelcontextprotocol/server-slack
@modelcontextprotocol/server-brave-search Web search via Brave npx -y @modelcontextprotocol/server-brave-search

Browse more at github.com/modelcontextprotocol/servers

Best Practices

  • Least Privilege: Only expose the minimum capabilities needed
  • Clear Descriptions: Write detailed tool descriptionsβ€”the AI relies on them
  • Validate Inputs: Always validate and sanitize inputs from the AI
  • Error Handling: Return clear error messages the AI can understand
  • Logging: Log all tool invocations for debugging and audit
  • Rate Limiting: Protect against runaway AI loops

Related Topics