Mastering MCP: Connecting AI Agents to the World in One Lesson

This tutorial explains how the Model Context Protocol (MCP) standardizes AI agent integration by replacing custom tool code with a JSON‑RPC based, auto‑discovered ecosystem, walks through configuration, core loading logic, code implementation, a runnable example, and compares MCP with traditional tool use.

AI Tech Publishing
AI Tech Publishing
AI Tech Publishing
Mastering MCP: Connecting AI Agents to the World in One Lesson

Lesson Recap

The previous lesson added four tools— search, datetime, calculator and terminate —so the Agent could perform basic tasks, but each AI application still had to write its own integration code.

Problem MCP Solves

Without a standard protocol, every AI project rewrites similar integration code, leading to:

Duplicate effort – each project implements its own adapters.

Inconsistent standards – ad‑hoc APIs cause bugs.

Hard to extend – adding a new tool requires code changes.

MCP Solution Overview

MCP (Model Context Protocol) is a JSON‑RPC 2.0‑based protocol proposed by Anthropic. It works like a USB‑C interface: any tool can be accessed through the same port.

Core advantages:

One‑time integration – connect to an MCP Server once and gain access to all its tools.

Standard protocol – eliminates custom API code.

Ecosystem sharing – tools written by others can be used directly.

Lesson Goal

Add MCP‑based tools to the Agent so that tools are discovered automatically from an MCP Server.

MCP Architecture Details

Three Core Components

MCP Host (MiniManus) – initiates requests.

MCP Client – handles communication with the server.

MCP Server – provides tool services.

Configuration‑Driven Discovery

Declare MCP servers in a JSON file; the Agent loads the file at startup and discovers tools automatically.

// mcp_servers.json
{
  "mcp_servers": [
    {
      "name": "context7",
      "url": "https://mcp.context7.com/mcp",
      "description": "Query official documentation",
      "env_key": "CONTEXT7_API_KEY"
    }
  ]
}

Loading Process

Read the JSON configuration.

Connect to each MCP Server.

Call tools/list to obtain the list of available tools.

Dynamically create wrapper objects that implement BaseTool.

Register the wrappers in the Agent’s tool table.

Code Implementation

Server Configuration

The file shown above ( mcp_servers.json) is placed in the exercise/03_mcp directory.

Core Loading Logic

Read JSON: load_mcp_servers() Create client: MCPServer(**config) Auto‑discover tools: server.list_tools() Wrap each schema: MCPTool(server, schema) Insert into the tool registry: tools[schema["name"]] = ... Full implementation resides in exercise/03_mcp/tools/mcp_client.py:

def load_mcp_tools() -> dict[str, BaseTool]:
    servers = load_mcp_servers()  # read JSON
    tools = {}
    for server_config in servers:
        server = MCPServer(**server_config)
        for schema in server.list_tools():  # automatic discovery
            tools[schema["name"]] = MCPTool(server, schema)
    return tools

Tool Registration

MCP_TOOL_REGISTRY = {
    "search": SearchTool(),
    "terminate": TerminateTool(),
    **load_mcp_tools(),  # auto‑load MCP tools
}

Running Example

cd exercise
uv run python 03_mcp/main.py --task "next.js 如何启动"
Note: The first run requires setting CONTEXT7_API_KEY in a .env file (obtainable from context7.com ).

Expected output:

Step 1: 调用工具 resolve-library-id (MCP)
Step 1: 工具返回
[库 ID 列表...]

Step 2: 调用工具 query-docs (MCP)
Step 2: 工具返回
[文档内容...]

Step 3: 调用工具 terminate
* 最终答案 (Agent Loop 终止)

Adding a New MCP Server

Edit mcp_servers.json to include additional entries, for example a local filesystem server:

{
  "mcp_servers": [
    {
      "name": "context7",
      "url": "https://mcp.context7.com/mcp",
      "env_key": "CONTEXT7_API_KEY"
    },
    {
      "name": "filesystem",
      "url": "http://localhost:3000/mcp",
      "env_key": "FILESYSTEM_API_KEY"
    }
  ]
}

After restarting the Agent, all tools from the new server are loaded automatically.

Comparison with Traditional Tool Use

Tool source : hard‑coded in code vs. dynamically discovered via MCP Server.

Integration method : separate API per tool vs. unified MCP protocol.

Extensibility : requires code changes vs. modify JSON only.

Ecosystem : isolated vs. shared across projects.

Open‑Source Repository

https://github.com/HUANGLIWEN/mini-manus

PythonMCPTool IntegrationAI AgentJSON-RPCMiniManus
AI Tech Publishing
Written by

AI Tech Publishing

In the fast-evolving AI era, we thoroughly explain stable technical foundations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.