AI › Concept

Model Context Protocol architecture and JSON-RPC lifecycle

How MCP cuts tool integration from N×M to N+M through Host-Client-Server separation, stdio and SSE transports, and JSON-RPC 2.0 handshakes.

Model Context Protocol architecture and JSON-RPC lifecycle

Connecting AI apps to external tools used to mean writing custom adapters for every client and data source—an $N \times M$ maintenance trap. Model Context Protocol (MCP) standardizes that boundary the same way LSP did for editors. This post covers MCP’s Host-Client-Server architecture, stdio vs. SSE transports, and the JSON-RPC 2.0 initialization handshake.


Moving from N×M custom adapters to an N+M standard

When I built early LLM integrations, every provider (OpenAI, Anthropic, Google) and every host app expected its own tool wiring. That worked fine for a single chatbot, but it fell apart as soon as teams tried to share tools across environments:

  1. Zero tool reuse: A PostgreSQL lookup tool written for Claude Desktop couldn’t run inside Cursor, VS Code, or a Spring Boot backend agent without rewriting the adapter layer.
  2. Duplicated auth and security boundaries: Credentials, token refresh, and authorization checks for $M$ external systems ended up copied across $N$ separate AI clients.
  3. $N \times M$ integration overhead: Supporting $N$ clients against $M$ data sources required maintaining $N \times M$ bespoke connectors.
flowchart TD
    subgraph Legacy ["Before MCP: N x M Custom Adapters"]
        C1["Claude Desktop"] --> T1["PostgreSQL Adapter"]
        C1 --> T2["GitHub Adapter"]
        C2["Cursor IDE"] --> T1
        C2 --> T2
        C3["Spring AI App"] --> T1
        C3 --> T2
    end

    subgraph Standard ["With MCP: N + M Standard Protocol"]
        MC1["Claude Desktop"] --> MCP["MCP Protocol Bus"]
        MC2["Cursor IDE"] --> MCP
        MC3["Spring AI App"] --> MCP
        MCP --> MS1["MCP Server (PostgreSQL)"]
        MCP --> MS2["MCP Server (GitHub)"]
    end

Open-sourced by Anthropic in November 2024, Model Context Protocol (MCP) borrows the Language Server Protocol (LSP) playbook. Just as LSP decoupled IDEs from language analyzers, MCP isolates AI hosts from context providers behind a single JSON-RPC protocol, reducing integration complexity from $N \times M$ to $N + M$.


Host, Client, and Server responsibilities

MCP splits the runtime into three layers with strict boundaries: Host, Client, and Server.

flowchart TD
    subgraph HostLayer ["Host Environment"]
        Host["Host App (IDE, Desktop, Spring Boot)"]
        LLM["LLM Engine"]
        Host <--> LLM
    end

    subgraph ClientLayer ["MCP Clients"]
        ClientA["MCP Client 1"]
        ClientB["MCP Client 2"]
        Host --> ClientA
        Host --> ClientB
    end

    subgraph TransportLayer ["Transport Layer"]
        StdioPipe["Local Pipe (stdio: stdin/stdout)"]
        HttpSSE["Network Stream (SSE / Streamable HTTP)"]
    end

    subgraph ServerLayer ["MCP Servers"]
        ServerLocal["Local Server (Filesystem, SQLite)"]
        ServerRemote["Remote Server (GitHub API, Order DB)"]
    end

    ClientA <--> StdioPipe <--> ServerLocal
    ClientB <--> HttpSSE <--> ServerRemote

1. Host

The host is the top-level application users interact with—Claude Desktop, Cursor, or a custom Spring Boot agent service. It owns:

  • Direct communication with the LLM (prompts, completions, and reasoning loops).
  • Spawning, restarting, and tearing down one or more MCP Client instances.
  • Security enforcement, including tool allowlists and Human-in-the-Loop (HITL) approval prompts before destructive calls run.

2. Client

An MCP Client lives inside the host and maintains a dedicated 1:1 session with a single MCP Server. It handles:

  • Transport abstraction and JSON-RPC 2.0 session framing.
  • Capability negotiation during startup and syncing available tools, resources, and prompts.
  • Encoding model tool requests into JSON-RPC messages and deserializing server responses back to the host.

3. Server

An MCP Server is a lightweight, isolated process exposing one domain or data source. It handles:

  • Declaring metadata and JSON Schemas for its exposed primitives (Tools, Resources, and Prompts).
  • Running actual queries, file reads, or API calls and returning standardized payloads.
  • Staying decoupled from host UI state or LLM internals—it only responds to incoming JSON-RPC requests.

Choosing between stdio and SSE transports

MCP decouples the JSON-RPC message layer from how bytes move across processes or networks. Two standard transports cover local and remote deployments:

DimensionLocal process (stdio)Remote network (SSE / Streamable HTTP)
MechanismChild process stdin / stdout pipesClient sends HTTP POST; server streams via SSE / HTTP
Process ownershipHost/Client forks the server binary directlyRuns independently as a long-lived web service
LatencyNear-zero (in-memory OS pipe)Network RTT, timeouts, and reconnect handling required
Security modelOS user permissions and local sandboxingHTTPS, OAuth / Bearer tokens, mTLS
Best fitLocal filesystem, Git CLI, SQLiteShared enterprise DBs, cloud APIs, internal microservices

The stdout pitfall in stdio servers

When a client launches a local MCP server over stdio, it writes JSON-RPC requests to stdin and parses every line coming back from stdout.

The most common bug I see in stdio MCP servers is leaving System.out.println, console.log, or Spring Boot’s default console banner pointed at stdout. Any non-JSON log line written to stdout crashes the client’s JSON-RPC parser immediately. Route all application logs to stderr (or a file) and disable startup banners.


JSON-RPC 2.0 lifecycle and initialization handshake

Every message between an MCP Client and Server follows JSON-RPC 2.0. Before any tool or resource request can run, both sides complete a strict three-step handshake to negotiate protocol versions and capabilities.

sequenceDiagram
    autonumber
    participant Client as MCP Client
    participant Server as MCP Server

    Note over Client, Server: 1. Open transport (stdio fork or HTTP/SSE)

    Client->>Server: Request (method: "initialize")
    Note over Server: Check protocolVersion & capabilities
    Server-->>Client: Response (protocolVersion, capabilities, serverInfo)

    Client->>Server: Notification (method: "notifications/initialized")
    Note over Client, Server: 2. Session ready

    Client->>Server: Request (method: "tools/list")
    Server-->>Client: Response (available tools + schemas)

    Client->>Server: Request (method: "tools/call", name & arguments)
    Server-->>Client: Response (content payload)

Step 1: Client sends initialize

The client opens the session by declaring its protocol version, supported features, and identity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "roots": {
        "listChanged": true
      },
      "sampling": {}
    },
    "clientInfo": {
      "name": "NamjuBlogAgent",
      "version": "1.0.0"
    }
  }
}

Step 2: Server returns capabilities

The server confirms the negotiated version and advertises which primitives (Tools, Resources, Prompts) and change notifications it supports:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "tools": {
        "listChanged": true
      },
      "resources": {
        "subscribe": true,
        "listChanged": true
      },
      "prompts": {
        "listChanged": false
      }
    },
    "serverInfo": {
      "name": "enterprise-inventory-server",
      "version": "2.1.0"
    }
  }
}

Step 3: Client sends notifications/initialized

Once the client accepts the server’s capabilities, it sends a one-way JSON-RPC notification (no id field, no response expected):

1
2
3
4
5
{
  "jsonrpc": "2.0",
  "method": "notifications/initialized",
  "params": {}
}

Only after this notification may the client call tools/list or resources/list. If a client sends operational requests before initialization completes, the server must reject them with JSON-RPC error code -32600 (Invalid Request).


Design trade-offs and when not to use MCP

MCP isn’t free. It introduces process or network hops, JSON-RPC serialization, and lifecycle management.

Use caseUse MCP?Why
Sharing internal tools across IDE, desktop, and backend agentsYesBuild the server once and plug it into every compliant host
Exposing third-party SaaS or DB connectors with clear auth boundariesYesIsolates credentials and permissions outside the host app
Single Spring Boot app calling its own local @Service beanNoDirect in-process function calling is faster and simpler
High-throughput, sub-10ms internal RPC between microservicesNoStick to gRPC or REST; MCP is designed for LLM context exchange

When you do build an MCP server, three engineering rules prevent most production headaches:

  1. Enforce security boundaries in the Host: External MCP servers may expose destructive tools. Filter available tools with an allowlist and require explicit user approval for state-changing operations.
  2. Return domain errors inside result, not JSON-RPC error: If an order ID doesn’t exist, return result.isError = true with a clear text explanation instead of a protocol-level error object. That lets the LLM read the failure and recover on the next turn.
  3. Keep remote servers stateless: For HTTP/SSE servers behind a load balancer, don’t store conversation history inside the MCP server. Pass everything needed as explicit tools/call arguments so instances scale horizontally.

Production checklist and references

  • Redirect all stdio server logs to stderr and turn off framework startup banners.
  • Verify the three-step initialize -> response -> notifications/initialized sequence before invoking tools/list.
  • Distinguish protocol errors (JSON-RPC error) from tool execution errors (result.isError = true).
  • Apply host-side allowlists and authentication headers (Authorization, mTLS) for remote HTTP/SSE MCP servers.

To see how an MCP server models actions, read-only data, and reusable templates over this transport, check out the breakdown of MCP’s three primitives: Tools, Resources, and Prompts.

References

</div>

</div>

  • JSON-RPC 2.0 Specification:
    JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.

</div>

This post is licensed under CC BY 4.0 by the author.