Model Context Protocol (MCP)

Model Context Protocol (MCP)

Problems without Model Context Protcol

AI models like Claude know a lot about general topics but can't see your personal data or systems. They're cut off from your world.

Without MCP, linking AI to your data means creating custom links for each source. Want Claude to access your Google Drive? You need a custom link. Your GitHub? Another custom link. Your database? Yet another one. This method:

  1. Creates separate connections → each one is different and isolated

  2. Doesn't grow well → you have to manage many different links

  3. Lacks consistency → every company makes its own way

  4. Raises security concerns → each link has its own security setup

Imagine before USB, every device had its own unique connector. Printers, keyboards, and cameras all had different ports, making things complicated.

MCP fixes this by creating a standard way for AI models to connect to any data source.

This means:

  1. Build once, connect to many → make your data "MCP-compatible," and any MCP-enabled AI can use it.

  2. Better security → with standardized authentication and permissions.

  3. Scalability → easily add new data sources as needed.

  4. Flexibility → switch between different AI providers without changing your data connections.

Basically, MCP connects "AI that knows general stuff" with "AI that can use your specific info." It turns AI from a standalone tool into a system that can interact with your digital world.

MCP Architecture: The Complete Picture

MCP uses a client-host-server setup that lets AI systems connect with different data sources and tools.

Key Components

  1. Host → This is the main application (like Claude Desktop) that:

    • Contains and coordinates multiple client instances

    • Handles user interactions and security permissions

    • Manages the AI/LLM integration

    • Controls which servers can be connected

  2. Clients → Each client is a connector that:

    • Establishes a 1:1 relationship with a specific server

    • Handles communication between the host and that server

    • Maintains isolation between different servers

    • Negotiates capabilities with the server

  3. Servers → These are specialized programs that:

    • Provide access to specific data sources or tools

    • Focus on a single responsibility (like "weather data" or "database access")

    • Expose their capabilities through resources, tools, and prompts

    • Know nothing about other servers or the full conversation

How Information Flows

When you ask a question in Claude Desktop:

  1. The host (Claude Desktop) looks at your question.

  2. It figures out which connected servers can help answer it.

  3. Relevant clients talk to their servers to get data or run tools.

  4. The servers send back information or tool results.

  5. The host gathers all this information.

  6. The AI uses this gathered context to create a response.

Communication Protocol

All communication uses JSON-RPC 2.0 with three message types:

  • Requests: Messages that expect a response

  • Responses: Answers to specific requests

  • Notifications: One-way messages (no response needed)

Security Model

Security is built into the architecture:

  • Servers only receive the information they need

  • Servers can't access the full conversation history

  • Servers are isolated from each other

  • The host enforces security boundaries

  • Users must approve tool usage

Capability Negotiation

When a client connects to a server, they exchange information about their capabilities:

  1. Client: "I can handle these features..."

  2. Server: "I support these resources and tools..."

  3. Both agree on which capabilities will be used

This negotiation makes sure everything works well together and allows for future updates.

The Big Picture Benefit

This setup solves a big problem: it lets AI systems securely and easily access your data and tools without needing custom solutions for each case.

Here's how it works:

  • Developers can create servers for specific data sources.

  • Users can mix and match servers as needed.

  • Security is kept tight.

  • New features can be added over time.

  • Different AI systems can use the same servers.

By standardizing these connections, this system allows AI to safely and efficiently use the data and tools they need to be really helpful.

Chronological Guide to Building an MCP Server in TypeScript

  1. Set up project

     mkdir my-mcp-server
     cd my-mcp-server
     npm init -y
     npm install @modelcontextprotocol/sdk zod
     npm install -D typescript @types/node
    
  2. Configure TypeScript

     # Create tsconfig.json with ES2022 target and Node16 module
    
  3. Update package.json

     {
       "type": "module",
       "scripts": {
         "build": "tsc"
       }
     }
    
  4. Create basic server structure

     // src/index.ts
     import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
     import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
    
     const server = new McpServer({
       name: "my-server",
       version: "1.0.0",
     });
    
  5. Add capabilities

    • Register tools with schema validation

    • Add resources if needed

    • Add prompts if needed

  6. Implement tool handlers

     server.tool(
       "tool-name",
       "Tool description",
       {
         param1: z.string().describe("Parameter description"),
       },
       async ({ param1 }) => {
         // Tool implementation
         return {
           content: [{ type: "text", text: "Result" }],
         };
       }
     );
    
  7. Start the server

     async function main() {
       const transport = new StdioServerTransport();
       await server.connect(transport);
       console.error("Server running");
     }
    
     main().catch(console.error);
    
  8. Build the server

     npm run build
    

Your main concern will be building MCP servers

  1. Existing clients like Claude Desktop already manage the client side, so you don't need to build that part.

  2. The real value comes from creating MCP servers that share your specific data, APIs, or features.

  3. Building servers is easier → they focus on one main task instead of managing multiple connections.

  4. Most developers will want to expand AI capabilities by linking it to their own systems → this means building servers.

  5. The server is where you'll add your business logic or special features.

For example, if you want Claude to access your company's internal API or database, or use a specific SDK like imgly, you'd build an MCP server for that. Then you can connect it to Claude Desktop or other MCP-compatible clients.

How AI Models Choose Which Tools to Call

When you ask a question, the AI goes through these steps to pick the right tools:

  1. Tool Registration: All tools are listed with names, descriptions, and what they need to work.

  2. Context Analysis: The AI looks at your question to figure out what you want.

  3. Tool Matching: The AI checks your intent against the tool descriptions.

  4. Parameter Extraction: If a tool fits, the AI finds out what details it needs from your question.

  5. Decision: The AI decides to:

    • Answer directly without tools

    • Use a tool to get more info

    • Use several tools in order

For instance, if you ask "What's the weather in Paris?" and there's a weather tool described as "Get weather data for a location," the AI sees the match, pulls "Paris" as the location, and chooses to use that tool.

The AI bases this choice on how well the tool descriptions match your question → clear, specific tool descriptions are key for picking the right tool.