# How MCP Servers Integrate with Agent Lessons in AI Engineering From Scratch

> Learn how MCP servers integrate with agent lessons using JSON-RPC 2.0 and Claude-Agent SDK. Discover automatic negotiation, discovery, and lifecycle management for AI engineering from scratch.

- Repository: [Rohit Ghumare/ai-engineering-from-scratch](https://github.com/rohitg00/ai-engineering-from-scratch)
- Tags: how-to-guide
- Published: 2026-06-13

---

**MCP servers integrate with agent lessons by providing a standardized JSON-RPC 2.0 tool surface that the Claude-Agent SDK consumes through automatic capability negotiation, tool discovery, and lifecycle management.**

The `rohitg00/ai-engineering-from-scratch` curriculum teaches agent engineering through a progressive integration of the **Model Context Protocol (MCP)**. This standardized protocol serves as the bridge between the tool-oriented fundamentals taught in phase 13 and the autonomous agent implementations built in phases 14 and 16.

## Understanding the MCP Architecture

The curriculum treats MCP as the **lingua franca** between agents and external capabilities. It defines a client-server relationship where the agent acts as the client and external tools run as MCP servers.

### The Three-Phase Lifecycle

According to the source in [`phases/13-tools-and-protocols/06-mcp-fundamentals/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/06-mcp-fundamentals/docs/en.md), every MCP connection follows a strict handshake:

1. **Initialize** – The client advertises its capabilities (roots, sampling, elicitation) via the `initialize` method, and the server responds with `initialized`.
2. **Operation** – The client discovers available tools using `tools/list` and invokes them via `tools/call`.
3. **Shutdown** – The transport closes cleanly over Streamable HTTP or stdio.

This lifecycle ensures that agents never attempt to call tools the server cannot support, and servers can request additional context (sampling) or user input (elicitation) mid-flow.

### The Agent SDK as MCP Client

In [`phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md), the Claude-Agent SDK generates code that automatically implements the MCP client primitives. Students do not write raw JSON-RPC; instead, they instantiate `MCPClient` with declared capabilities:

```python
from mcp_client import MCPClient

client = MCPClient(
    transport="streamable_http",
    capabilities={"roots": {"listChanged": True},
                  "sampling": {},
                  "elicitation": {}},
)

# Automatic handshake happens here

tools = client.tools.list()
result = client.tools.call("echo", {"msg": "Hello from agent!"})

```

The SDK handles the capability negotiation, translating high-level `tool_name(arg)` calls into `tools/call` JSON-RPC requests behind the scenes.

## How Agent Lessons Consume MCP Servers

The curriculum demonstrates MCP integration through four specific agent lessons, each illustrating different integration patterns.

### Lesson 17: Claude-Agent SDK Fundamentals

The foundational agent lesson in [`phases/14-agent-engineering/17-claude-agent-sdk/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/14-agent-engineering/17-claude-agent-sdk/docs/en.md) scaffolds an agent that imports `mcp_client` and references external tools in its skill definition. This demonstrates **capability negotiation** and **tool discovery** without requiring students to implement the wire protocol manually.

### Lesson 06: DevOps Troubleshooting Agent

Located in [`phases/19-capstone-projects/06-devops-troubleshooting-agent/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/19-capstone-projects/06-devops-troubleshooting-agent/docs/en.md), this lesson implements a **multi-server MCP client** that separates read-only and destructive operations. The agent first queries a read-only MCP server; when destructive tools are required, it authenticates against a second MCP server using an OAuth 2.1-issued scope (`approved:by:human`). This pattern teaches **scope enforcement** and **gateway gating**.

### Lesson 13: MCP Server with Registry

The capstone project in [`phases/19-capstone-projects/13-mcp-server-with-registry/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/19-capstone-projects/13-mcp-server-with-registry/docs/en.md) has students build a production-grade FastMCP server that exposes a `.well-known/mcp-capabilities` discovery document. Agents query this endpoint during handshake to validate server capabilities, illustrating **registry discovery** and **OPA policy gating**.

### Lesson 23: Capstone Tool Ecosystem

The final integration in phase 23 demonstrates a **combined MCP + A2A flow**: an agent calls an MCP server for literature search, delegates summarization to an A2A sub-agent, and renders results via an MCP App (`ui://`). This shows how MCP servers compose with other agentic patterns.

## Production Security Patterns

The curriculum addresses enterprise deployment through specific security layers:

**OAuth 2.1 Scope Binding** – Each tool is bound to a scope (e.g., `jira:read`). The server validates the token on every `tools/call`, as implemented in [`phases/13-tools-and-protocols/18-mcp-auth-production/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/18-mcp-auth-production/docs/en.md).

**OPA Policy Gates** – Destructive tools require a human-approved token. The Open Policy Agent (OPA) evaluates requests against organizational policies before allowing invocation.

**Registry Discovery** – The `.well-known/mcp-capabilities` endpoint allows centralized auditing and version control across an organization's MCP server fleet.

## Minimal Implementation Example

Below is the minimal MCP server from [`phases/13-tools-and-protocols/06-mcp-fundamentals/code/main.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/06-mcp-fundamentals/code/main.py) that agent lessons connect to:

```python
import json, sys

def emit(msg):
    sys.stdout.write(json.dumps(msg) + "\n")
    sys.stdout.flush()

# Handshake initiation

initialize = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
        "clientInfo": {"name": "demo-server"},
        "capabilities": {"tools": {"listChanged": True}}
    },
}
emit(initialize)

# Tools list response

def handle_list():
    response = {
        "jsonrpc": "2.0",
        "id": 2,
        "result": {
            "tools": [
                {"name": "echo", 
                 "description": "Echoes a string",
                 "inputSchema": {"type": "object",
                                 "properties": {"msg": {"type": "string"}}}}
            ]
        },
    }
    emit(response)

# Tool execution handler

def handle_call(params):
    if params["name"] == "echo":
        result = {"content": [{"type": "text", "text": params["arguments"]["msg"]}]}
        return {"jsonrpc": "2.0", "id": params["id"], "result": result}

```

When an agent using the Claude-Agent SDK connects to this server, it automatically performs the `initialize` handshake, queries `tools/list`, and maps `client.tools.call()` to the `tools/call` JSON-RPC method.

## Summary

- **MCP servers provide the standardized tool surface** that all agent lessons in the curriculum consume, starting with phase 13 fundamentals and progressing through phase 14 agent engineering.
- **The Claude-Agent SDK embeds an MCP client** that handles JSON-RPC lifecycle management, capability negotiation, and tool discovery automatically.
- **Agent lessons demonstrate progressive integration** from simple single-server calls to multi-server OAuth-gated deployments with registry discovery.
- **Production security** relies on OAuth 2.1 scopes, OPA policies, and the `.well-known/mcp-capabilities` discovery endpoint.
- **Key source files** include [`phases/13-tools-and-protocols/06-mcp-fundamentals/code/main.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/06-mcp-fundamentals/code/main.py) for server implementation and [`phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md) for client consumption patterns.

## Frequently Asked Questions

### What is the Model Context Protocol (MCP) in the context of this curriculum?

The MCP is a standardized client-server protocol defined in [`phases/13-tools-and-protocols/06-mcp-fundamentals/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/06-mcp-fundamentals/docs/en.md) that enables LLM-powered agents to discover and invoke external tools via JSON-RPC 2.0 endpoints. It serves as the abstraction layer between agent logic (phases 14 and 16) and tool implementations (phase 13).

### How does the Claude-Agent SDK handle MCP server connections?

The SDK automatically implements the MCP client primitives including the `initialize` handshake, `tools/list` discovery, and `tools/call` invocation. As shown in [`phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/14-agent-engineering/17-claude-agent-sdk/outputs/skill-claude-agent-scaffold.md), students instantiate `MCPClient` with capability flags, and the SDK manages all JSON-RPC serialization and transport concerns.

### What security mechanisms protect MCP servers in production deployments?

According to [`phases/13-tools-and-protocols/18-mcp-auth-production/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/13-tools-and-protocols/18-mcp-auth-production/docs/en.md) and [`phases/19-capstone-projects/13-mcp-server-with-registry/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/19-capstone-projects/13-mcp-server-with-registry/docs/en.md), production MCP servers implement OAuth 2.1 scope validation on every tool call, OPA policy gates for destructive operations, and registry-based discovery via `.well-known/mcp-capabilities` endpoints for centralized auditing.

### Can an agent connect to multiple MCP servers simultaneously?

Yes. The DevOps Troubleshooting Agent lesson in [`phases/19-capstone-projects/06-devops-troubleshooting-agent/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/19-capstone-projects/06-devops-troubleshooting-agent/docs/en.md) demonstrates a multi-server client pattern where the agent maintains connections to separate read-only and destructive-operation servers, switching between them based on OAuth scope requirements and approval tokens.