Skip to main content

**MCP Communication — How It Talks

Calculating read time…

Why MCP Exists: The Problem AI Needed to Solve

Imagine you're building a super-smart AI assistant.

You want it to check the weather, search files, calculate expenses, and book appointments.

But here's the challenge:

How do you give it access to all these different tools?

Without MCP, every AI application looks like this:

  • Custom code for each tool
  • No standard way to discover available tools
  • Security nightmares (what can the AI access?)
  • One-off implementations that don't work together

💡 Real-World Analogy: Think of MCP as USB for AI tools.

Before USB, every device needed its own special port and driver.

After USB, everything plugs in the same way.

MCP does this for AI tools.

What Exactly is MCP? (No Jargon, I Promise!)

MCP = Model Context Protocol

Let's break that down:

  • Model: Your AI (like ChatGPT, Claude, etc.)
  • Context: Information the AI needs to work with
  • Protocol: Standard way of communicating

Simplest definition:

MCP is a standard language that lets AI systems ask for tools and information.

Think of it like this:

You → "Hey AI, what's the weather?"
AI → [Uses MCP to ask weather tool] → "It's 75°F and sunny"
AI → "It's 75°F and sunny today!"

The magic happens in that middle step where AI uses MCP to talk to tools.

The Heartbeat: Request/Response Cycle

All MCP communication follows a simple pattern:

  1. Request: "Can you do this for me?"
  2. Response: "Here's what I found/did"

✅ DO: Think of every MCP interaction as a conversation.

One side asks, the other answers.

This keeps everything predictable and reliable.

Real Example: Weather Check

Let's trace through a complete example:

CLIENT (AI) → SERVER (Weather Tool)
Request: "What's the weather in San Francisco?"
↓
SERVER processes request
↓
CLIENT ← SERVER
Response: "72°F, sunny, light breeze"

This happens hundreds of times in complex AI applications.

Each request-response pair is one complete MCP conversation.

JSON-RPC Basics: The Language They Speak

JSON-RPC is just JSON with specific rules.

Think of it as filling out a form with specific fields.

Every MCP message needs:

  • jsonrpc: Always "2.0" (the version)
  • method: What you want to do
  • params: Information needed
  • id: Conversation tracking number

❌ DON'T: Make up your own field names.

JSON-RPC has specific required fields.

Missing "jsonrpc" or "id" will break communication.

Your First JSON-RPC Message

Here's what a simple "hello" message looks like:

{
  "jsonrpc": "2.0",
  "method": "initialize",
  "params": {
    "clientInfo": {
      "name": "MyAIApp",
      "version": "1.0.0"
    }
  },
  "id": 1
}

See the structure?

  • jsonrpc: Always 2.0
  • method: What action (initialize)
  • params: Extra info needed
  • id: Message #1 in conversation

Transport Methods: How Messages Travel

Messages need a way to get from AI to tools.

MCP supports two main transport methods.

Method 1: stdio (Standard Input/Output)

Think of stdio as two pipes between programs.

One pipe for sending, one for receiving.

AI Program → [stdin pipe] → Tool Program
AI Program ← [stdout pipe] ← Tool Program

📝 When to use stdio:

  • Tools running on the same computer
  • Simple, fast communication
  • No network setup needed
  • Great for development and testing

Real-World Analogy: Sending Letters

stdio is like sending letters within the same building.

You write a letter, put it in the internal mail tube.

The recipient gets it immediately and replies through the same system.

It's fast, contained, and doesn't leave the building.

Method 2: HTTP (Web Protocol)

HTTP is how web browsers talk to websites.

MCP uses this same technology.

AI → HTTP Request → http://tools.example.com/mcp
AI ← HTTP Response ← http://tools.example.com/mcp

✅ DO use HTTP when:

  • Tools are on different computers
  • You need internet access
  • Multiple clients need the same tool
  • You want web security features

Real-World Analogy: Instant Chat

HTTP is like instant messaging between cities.

You send a message, it travels across the internet.

The recipient could be anywhere in the world.

You get a reply almost instantly, but there's more setup involved.

Transport Comparison: Quick Reference

stdio vs HTTP: Which to Choose?

Feature stdio HTTP
Speed Very Fast Fast (network dependent)
Location Same Computer Anywhere (local or internet)
Setup Simple More Complex
Security Computer Security Web Security (HTTPS, auth)
Best For Development, Local Tools Production, Remote Services

Complete MCP Conversation Example

Let's walk through a real calculator conversation.

We'll use stdio transport and see every message.

Step 1: Initialize Connection

The AI client starts the conversation:

{
  "jsonrpc": "2.0",
  "method": "initialize",
  "params": {
    "clientInfo": {
      "name": "MathHelperAI",
      "version": "1.0.0"
    },
    "capabilities": {}
  },
  "id": 1
}

The calculator tool responds:

{
  "jsonrpc": "2.0",
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "tools": ["add", "subtract", "multiply", "divide"]
    },
    "serverInfo": {
      "name": "CalculatorTool",
      "version": "2.1.0"
    }
  },
  "id": 1
}

💡 Notice: The response has the same id: 1.

This tells the AI, "This response is for your message #1."

Without matching IDs, conversations get confused.

Step 2: List Available Tools

AI asks what tools are available:

{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "params": {},
  "id": 2
}

Calculator lists its tools:

{
  "jsonrpc": "2.0",
  "result": {
    "tools": [
      {
        "name": "add",
        "description": "Add two numbers together",
        "inputSchema": {
          "type": "object",
          "properties": {
            "a": {
              "type": "number",
              "description": "First number"
            },
            "b": {
              "type": "number",
              "description": "Second number"
            }
          },
          "required": ["a", "b"]
        }
      }
    ]
  },
  "id": 2
}

Step 3: Actually Use a Tool

Now the AI can ask for calculations:

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "add",
    "arguments": {
      "a": 42,
      "b": 17
    }
  },
  "id": 3
}

The calculator does the math and responds:

{
  "jsonrpc": "2.0",
  "result": {
    "content": [
      {
        "type": "text",
        "text": "59"
      }
    ]
  },
  "id": 3
}

Success! The AI got the answer: 42 + 17 = 59.

Your Turn: Exercise Time! 🏋️‍♂️

Exercise: Write a Calculator Message

Scenario: Your AI needs to multiply 8 by 9.

Task: Write the complete JSON-RPC message.

Hint: Look at the "add" example above, but change:

  • Method parameters for multiplication
  • The numbers to multiply
  • The tool name (check what tools are available)

❌ Common mistakes to avoid:

  • Forgetting "jsonrpc": "2.0"
  • Missing the id field
  • Wrong method name (should be "tools/call")
  • Incorrect argument structure

Try it yourself first, then check below!

Solution: Multiplication Request

Here's the correct message:

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "multiply",
    "arguments": {
      "a": 8,
      "b": 9
    }
  },
  "id": 42
}

✅ What we got right:

  • Correct jsonrpc version
  • Proper method: "tools/call"
  • Tool name matches what server offers
  • Arguments in correct structure
  • Unique ID (42) for tracking

Common Beginner Mistakes & How to Fix Them

Mistake 1: Wrong JSON Structure

WRONG: Missing required fields

{
  "method": "tools/call",
  "arguments": {
    "a": 5,
    "b": 3
  }
  // Missing jsonrpc and id!
}

RIGHT: Complete structure

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "add",
    "arguments": {
      "a": 5,
      "b": 3
    }
  },
  "id": 1
}

Mistake 2: Incorrect Parameter Nesting

Parameters must be inside params, not loose:

// ❌ WRONG - arguments in wrong place
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "name": "add",           // Wrong! Should be in params
  "arguments": {
    "a": 1,
    "b": 2
  },
  "id": 1
}

// ✅ RIGHT - everything in params
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {              // Correct!
    "name": "add",
    "arguments": {
      "a": 1,
      "b": 2
    }
  },
  "id": 1
}

Mistake 3: Forgetting to Initialize

Always start with initialize before using tools.

The server needs to know who's connecting and what they support.

Advanced Patterns: Leveling Up Your MCP Skills

Pattern 1: Parallel Requests

You can send multiple requests before getting responses.

Use different id values to track them.

// Send three requests at once
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": { ... },
  "id": 100
}

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": { ... },
  "id": 101
}

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": { ... },
  "id": 102
}

// Responses come back with matching IDs
{
  "jsonrpc": "2.0",
  "result": { ... },
  "id": 101
}  // Response for request 101

{
  "jsonrpc": "2.0",
  "result": { ... },
  "id": 100
}  // Response for request 100

{
  "jsonrpc": "2.0",
  "result": { ... },
  "id": 102
}  // Response for request 102

📝 Pro Tip: Responses can come in any order!

That's why IDs are crucial.

Without IDs, you wouldn't know which response matches which request.

Pattern 2: Error Handling

What happens when something goes wrong?

MCP has standard error responses:

// If you ask for a tool that doesn't exist:
{
  "jsonrpc": "2.0",
  "error": {
    "code": -32601,
    "message": "Method not found",
    "data": "No tool named 'fly_to_moon' exists"
  },
  "id": 5
}

Common error codes:

  • -32600: Invalid Request (bad JSON)
  • -32601: Method Not Found
  • -32602: Invalid Parameters
  • -32700: Parse Error (not valid JSON)

Pattern 3: Notifications (Fire and Forget)

Sometimes you don't need a response.

Use notifications by omitting the id field:

// Notification - no response expected
{
  "jsonrpc": "2.0",
  "method": "log",
  "params": {
    "message": "AI started calculation",
    "level": "info"
  }
  // No id field!
}

❌ Warning: Don't use notifications for important actions.

If the server fails, you won't know.

Always use regular requests (with IDs) for critical operations.

Visual Summary: How MCP Works End-to-End

MCP Communication Flow

┌─────────────┐     1. Initialize      ┌─────────────┐
│             │ ──────────────────────> │             │
│    AI       │                         │   Tool      │
│   Client    │ <────────────────────── │   Server    │
│             │     2. Tool List        │             │
│             │                         │             │
│             │     3. Call Tool        │             │
│             │ ──────────────────────> │             │
│             │                         │             │
│             │ <────────────────────── │             │
│             │     4. Result           │             │
└─────────────┘                         └─────────────┘
         │ stdio or HTTP Transport │
         └─────────────────────────┘

Four-step dance: Initialize → Discover → Call → Result

Every MCP conversation follows this pattern.

Real-World Application: Building a Smart Assistant

Let's see how MCP enables real applications.

Imagine a travel assistant that needs:

  • Weather information
  • Flight prices
  • Currency conversion
  • Translation services

Without MCP: Write custom code for each service.

With MCP: Each service provides standard MCP interface.

// Travel assistant asking multiple services
1. Ask weather tool: "Weather in Tokyo?"
2. Ask flight tool: "Flights to Tokyo next week?"
3. Ask currency tool: "Convert 1000 USD to JPY"
4. Ask translation tool: "Translate 'thank you' to Japanese"

// All through standard MCP messages
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "get_weather",
    "arguments": {
      "city": "Tokyo"
    }
  },
  "id": 1
}

Security Considerations

❌ NEVER DO: Give AI unlimited tool access

Without security, AI could:

  • Delete important files
  • Send unauthorized emails
  • Make purchases without permission
  • Access sensitive data

✅ ALWAYS DO: Implement security layers

  • Tool-level permissions: Which tools can AI use?
  • User confirmation: Ask human before critical actions
  • Input validation: Check all parameters
  • Logging: Record all MCP conversations

Final Thought: Why This Matters

MCP represents a fundamental shift in how AI systems work together.

Before MCP: Every AI-tool connection was custom-built.

After MCP: Tools plug in like USB devices.

🌟 The Big Picture:

MCP enables the AI ecosystem we've been promised.

Where any AI can use any tool.

Where developers build tools once that work everywhere.

Where innovation accelerates because we're not reinventing connections.

Comments