# What Is the Model Context Protocol (MCP) for Tool Communication?

> Discover the Model Context Protocol MCP an open communication layer letting LLMs invoke tools via a universal JSON-RPC interface. Simplify tool integration.

- Repository: [Bojie Li/ai-agent-book](https://github.com/bojieli/ai-agent-book)
- Tags: deep-dive
- Published: 2026-08-24

---

**The Model Context Protocol (MCP) is an open, standards-based communication layer that enables large language models to invoke external tools through a universal JSON-RPC-like interface, eliminating the need for framework-specific adapters.**

The `bojieli/ai-agent-book` repository implements MCP as a bridge between AI agents and external capabilities. This protocol standardizes how LLMs discover and call tools across different frameworks, from browser automation to general AI agents, by defining a single "socket" that all systems can plug into.

## Core Components of the MCP Architecture

MCP decouples tool providers from AI agents through three fundamental abstractions.

### MCP Server

An **MCP Server** is a standalone process that implements the MCP JSON-RPC-like API and advertises available capabilities. According to the source in [`chapter9/gaia-experience/AWorld/aworld/mcp_client/server.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter9/gaia-experience/AWorld/aworld/mcp_client/server.py), the abstract base class `MCPServer` defines concrete subclasses for **stdio**, **SSE**, and **HTTP** transports. Each server exposes a `list_tools` method that returns tool metadata.

### MCP Client

The **MCP Client**, implemented in [`chapter9/browser-use-rpa/browser-use/browser_use/mcp/client.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter9/browser-use-rpa/browser-use/browser_use/mcp/client.py), acts as a lightweight wrapper that manages server lifecycle and tool registration. The `MCPClient` class handles server startup via `StdioServerParameters`, maintains a `ClientSession`, and converts MCP tool definitions into framework-native actions.

### Tool Definition Schema

Every MCP tool is described by a **JSON-Schema** `inputSchema` and a `description` field. The client converts these schemas into Pydantic models through the `_json_schema_to_python_type` method, enabling automatic argument validation before tool invocation.

## How MCP Works: The Four-Stage Data Flow

The protocol operates through a standardized pipeline that connects agents to external tools:

1. **Connect** – `MCPClient.connect()` launches the server process and initializes a `ClientSession` (lines 80‑95 of [`client.py`](https://github.com/bojieli/ai-agent-book/blob/main/client.py)).

2. **Discovery** – `session.list_tools()` queries the server and returns a list of `Tool` objects, which the client stores in `self._tools`.

3. **Registration** – `MCPClient.register_to_tools()` iterates over discovered tools, builds Pydantic parameter models via `_json_schema_to_python_type` → `create_model`, and registers async wrappers with the host's action registry.

4. **Invocation** – When the host calls a registered action, the wrapper serializes validated arguments and forwards them to `session.call_tool(name, params)`. The `_format_mcp_result` method normalizes raw MCP responses into framework-specific `ActionResult` objects.

The protocol also emits `MCPClientTelemetryEvent` instances that capture connection latency, tool-call duration, and error messages for full observability.

## Implementation in the ai-agent-book Repository

The repository provides concrete implementations across multiple AI frameworks. In [`chapter9/browser-use-rpa/browser-use/browser_use/mcp/controller.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter9/browser-use-rpa/browser-use/browser_use/mcp/controller.py), the `MCPToolWrapper` class adapts discovered MCP tools into browser-use actions, while the `@mcp_server` decorator in [`chapter9/gaia-experience/AWorld/aworld/mcp_client/decorator.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter9/gaia-experience/AWorld/aworld/mcp_client/decorator.py) enables any Python class to expose its methods as MCP tools.

### Registering MCP Tools in Browser-Use

```python
from browser_use import Tools
from browser_use.mcp.client import MCPClient

async def enable_mcp():
    # 1️⃣ Create the host tool registry

    tools = Tools()

    # 2️⃣ Initialise an MCP client that will launch the server

    mcp = MCPClient(
        server_name="playwright-mcp",
        command="npx",
        args=["@playwright/mcp@latest"]
    )

    # 3️⃣ Connect and auto‑register every discovered tool

    await mcp.register_to_tools(tools, prefix="pw_")

    # 4️⃣ The tools are now available as normal actions:

    #    await tools.run("pw_click", selector="#submit")

    #    await tools.run("pw_screenshot", path="out.png")

```

The `MCPClient` automatically handles Pydantic model generation from JSON-Schema definitions and wraps each tool with the appropriate transport logic.

## Key Classes and Methods

| Class / Function | Location | Purpose |
|------------------|----------|---------|
| `MCPClient` | [`browser_use/mcp/client.py`](https://github.com/bojieli/ai-agent-book/blob/main/browser_use/mcp/client.py) | Connects to servers, discovers tools, and forwards calls |
| `MCPServer` (ABC) | [`aworld/mcp_client/server.py`](https://github.com/bojieli/ai-agent-book/blob/main/aworld/mcp_client/server.py) | Base class for stdio, SSE, and HTTP transports |
| `@mcp_server` | [`aworld/mcp_client/decorator.py`](https://github.com/bojieli/ai-agent-book/blob/main/aworld/mcp_client/decorator.py) | Decorator converting Python classes into MCP servers |
| `MCPToolWrapper` | [`browser_use/mcp/controller.py`](https://github.com/bojieli/ai-agent-book/blob/main/browser_use/mcp/controller.py) | Maps MCP `Tool` objects to browser-use actions |
| `_json_schema_to_python_type` | [`browser_use/mcp/client.py`](https://github.com/bojieli/ai-agent-book/blob/main/browser_use/mcp/client.py) | Recursively converts JSON-Schema to Pydantic types |
| `_format_mcp_result` | [`browser_use/mcp/client.py`](https://github.com/bojieli/ai-agent-book/blob/main/browser_use/mcp/client.py) | Normalizes diverse MCP result shapes |

## Summary

- **MCP standardizes tool communication** through a JSON-RPC-like protocol, eliminating per-framework adapter code.
- **Three core components**—Server, Client, and Schema definitions—enable plug-and-play integration.
- **Automatic type safety** is achieved by converting JSON-Schema definitions into Pydantic models before invocation.
- **Built-in telemetry** captures latency and errors across the entire tool chain via `MCPClientTelemetryEvent`.
- **Multi-transport support** includes stdio, Server-Sent Events (SSE), and HTTP through the `MCPServer` abstraction.

## Frequently Asked Questions

### What is the Model Context Protocol (MCP) for tool communication?

The Model Context Protocol (MCP) is an open standard that defines how large language models discover and invoke external tools through a uniform JSON-RPC-like interface. It acts as a "universal socket" that allows any AI agent to connect to any tool provider without requiring framework-specific integrations.

### How does MCP differ from OpenAI function calling or LangChain tools?

While OpenAI function calling and LangChain tools require specific adapter code for each framework, MCP provides a **single abstraction layer** that works across all systems. The `MCPClient` in [`browser_use/mcp/client.py`](https://github.com/bojieli/ai-agent-book/blob/main/browser_use/mcp/client.py) automatically converts MCP tool definitions into framework-native actions, eliminating the need to maintain separate integrations for each provider.

### What transport mechanisms does MCP support?

MCP supports three transport mechanisms implemented in [`aworld/mcp_client/server.py`](https://github.com/bojieli/ai-agent-book/blob/main/aworld/mcp_client/server.py): **stdio** for local process communication, **Server-Sent Events (SSE)** for persistent HTTP connections, and **streamable HTTP** for stateless request-response patterns. The `MCPServer` abstract base class defines the interface for all transport implementations.

### How does MCP ensure type safety when calling tools?

MCP ensures type safety by requiring tools to expose JSON-Schema definitions via their `inputSchema` property. The client implementation uses `_json_schema_to_python_type` to recursively convert these schemas into Python type hints, then constructs Pydantic models through `create_model`. This allows the host framework to validate arguments before serializing them for `session.call_tool()`.