Model Context Protocol Introduction

Deep dive into the Model Context Protocol (MCP)

Protocol Specification40 min read

Protocol Overview

The Model Context Protocol (MCP) is an open standard that standardizes how applications provide context to large language models (LLMs). Think of MCP like a USB-C port for AI applicationsβ€”providing a standardized way to connect AI models to different data sources and tools.

Key Benefits

  • Standardized communication interface
  • Secure data transmission
  • Extensible architecture design
  • Cross-platform compatibility
  • Real-time context synchronization
  • Plugin-based extension support

Use Cases

  • Code analysis and understanding
  • Documentation generation and maintenance
  • Project management integration
  • Database queries and operations
  • API service integration
  • Workflow automation

Architecture Design

MCP protocol layered architecture and component overview

MCP Host

AI Application

The AI application that coordinates and manages one or multiple MCP clients

Key Responsibilities:

  • Coordinate multiple MCP clients
  • Manage AI model interactions
  • Process server responses
  • Handle session state

MCP Client

Connection Manager

A component that maintains a connection to an MCP server and obtains context from an MCP server for the MCP host to use

Key Responsibilities:

  • Maintain one-to-one server connection
  • Send context queries
  • Handle server responses
  • Manage connection lifecycle

MCP Server

Context Provider

A program that provides context to MCP clients through tools, resources, and prompts

Key Responsibilities:

  • Accept client connections
  • Process context requests
  • Return structured data
  • Maintain resource state

Transport Layer

Communication Channel

Manages communication channels and authentication between clients and servers

Key Responsibilities:

  • STDIO transport for local processes
  • HTTP transport for remote servers
  • Message framing and serialization
  • Authentication and security

Data Layer

Protocol Definition

Defines the JSON-RPC based protocol for client-server communication

Key Responsibilities:

  • JSON-RPC 2.0 message format
  • Lifecycle management
  • Primitive definitions (tools, resources, prompts)
  • Notification system

MCP Primitives

Core primitives that define what clients and servers can offer each other

Tools

Executable functions that AI applications can invoke to perform actions

Examples:

  • File operations (read, write, create)
  • API calls to external services
  • Database queries and operations
  • Code analysis and compilation
  • System commands execution

Available Methods:

tools/listtools/call

Resources

Data sources that provide contextual information to AI applications

Examples:

  • File contents and metadata
  • Database records and schemas
  • API responses and documentation
  • Git repository information
  • Configuration files

Available Methods:

resources/listresources/read

Prompts

Reusable templates that help structure interactions with language models

Examples:

  • System prompts for specific tasks
  • Few-shot examples for learning
  • Code review templates
  • Documentation generation prompts
  • Analysis and summary templates

Available Methods:

prompts/listprompts/get

Transport Layer

Communication mechanisms that enable data exchange between clients and servers

STDIO Transport

Uses standard input/output streams for direct process communication

Use Cases:

  • Local MCP servers on same machine
  • Direct process communication
  • Optimal performance with no network overhead
  • Simple setup and configuration

Example:

npx @modelcontextprotocol/server-filesystem /path/to/files

Launch filesystem server via STDIO

HTTP Transport

Uses HTTP POST for client-to-server messages with optional Server-Sent Events

Use Cases:

  • Remote MCP servers
  • Cloud-based services
  • Authentication with bearer tokens
  • Scalable server deployments

Example:

https://api.example.com/mcp

Connect to remote MCP server via HTTP

Message Types

JSON-RPC 2.0 based message types supported by the MCP protocol

initialize

Initialize connection and negotiate protocol version and capabilities

Client β†’ Server

Message Example:

{
  "method": "initialize",
  "params": {
    "protocolVersion": "2025-06-18",
    "capabilities": {
      "tools": {},
      "resources": {}
    },
    "clientInfo": {
      "name": "gemini-cli",
      "version": "1.0.0"
    }
  }
}

tools/list

Discover available tools on the server

Client β†’ Server

Message Example:

{
  "method": "tools/list",
  "params": {}
}

tools/call

Execute a specific tool with provided arguments

Client β†’ Server

Message Example:

{
  "method": "tools/call",
  "params": {
    "name": "com.example.weather/current",
    "arguments": {
      "location": "San Francisco",
      "units": "imperial"
    }
  }
}

resources/list

Get list of available resources

Client β†’ Server

Message Example:

{
  "method": "resources/list",
  "params": {}
}

resources/read

Read specific resource content

Client β†’ Server

Message Example:

{
  "method": "resources/read",
  "params": {
    "uri": "file:///path/to/file.ts"
  }
}

prompts/list

Get available prompt templates

Client β†’ Server

Message Example:

{
  "method": "prompts/list",
  "params": {}
}

notifications/tools/list_changed

Notify client when available tools change

Server β†’ Client

Message Example:

{
  "method": "notifications/tools/list_changed"
}

Tool Definition Example

Example of how tools are defined in the MCP protocol

Weather Tool Definition

This example shows how a weather tool is defined with proper input schema validation and documentation.

// MCP Tool Definition Example
{
  "name": "com.example.weather/current",
  "title": "Get Current Weather",
  "description": "Get current weather information for a specified location",
  "inputSchema": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "The location to get weather for"
      },
      "units": {
        "type": "string",
        "enum": ["metric", "imperial"],
        "description": "Temperature units",
        "default": "metric"
      },
      "include_forecast": {
        "type": "boolean",
        "description": "Whether to include forecast data",
        "default": false
      }
    },
    "required": ["location"]
  }
}

Key Components:

  • name: Unique identifier for the tool
  • title: Human-readable display name
  • description: Detailed explanation of functionality
  • inputSchema: JSON Schema for input validation

Best Practices:

  • Use namespaced tool names (e.g., com.example.weather/current)
  • Provide clear, descriptive documentation
  • Define comprehensive input schemas
  • Include default values where appropriate

Communication Flow

Complete communication flow between MCP clients and servers

1

Initialize Connection

Client establishes connection and negotiates capabilities with server

  • Client sends initialize request with protocol version
  • Server responds with supported capabilities
  • Both parties negotiate communication parameters
  • Connection established with agreed protocol version
2

Capability Discovery

Client discovers available tools, resources, and prompts

  • Request available tools list (tools/list)
  • Request available resources (resources/list)
  • Request available prompts (prompts/list)
  • Cache capability information for efficient access
3

Context Exchange

Client requests and receives contextual data from resources

  • Send resource read requests (resources/read)
  • Receive structured data in various formats
  • Process data format conversion as needed
  • Update local context with received information
4

Tool Execution

Client invokes server tools to perform actions

  • Construct tool call requests (tools/call)
  • Pass required parameters with proper validation
  • Wait for execution results from server
  • Process returned data and handle errors
5

Real-time Updates

Maintain synchronization through notifications

  • Listen for resource change notifications
  • Handle tool list updates dynamically
  • Process connection interruptions gracefully
  • Re-establish connections when needed

Security Features

Security mechanisms and protection measures in the MCP protocol

Authentication

Multiple authentication mechanisms supported

  • API Key authentication
  • OAuth 2.0 flows
  • Bearer tokens
  • Custom authentication headers

Authorization

Fine-grained permission management

  • Resource-level permissions
  • Operation type restrictions
  • Time-based access control
  • Rate limiting and quotas

Data Protection

End-to-end data security

  • TLS/SSL transport encryption
  • Message content encryption
  • Sensitive data masking
  • Secure key management

Audit & Monitoring

Comprehensive operation tracking

  • Request/response logging
  • Permission check records
  • Error and exception tracking
  • Performance metrics collection

Getting Started

Ready to start using MCP with Gemini CLI? Explore these resources