█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/AI Agents/Code-First Agents/Model Context Protocol (MCP): Connect Agents to External Tools
75 minAdvanced

Model Context Protocol (MCP): Connect Agents to External Tools

After this lesson, you will be able to: Understand the Model Context Protocol (MCP), build an MCP server that exposes tools, and connect an LLM to real external systems via MCP.

MCP is the open protocol Anthropic published to standardise how agents talk to tools. Instead of writing custom tool wrappers per framework, you build an MCP server once, and any compatible client (Claude Desktop, Claude Code, Cursor, custom Python) can use it. This lesson builds and connects to one.

Prerequisites:Raw SDK: Anthropic and OpenAI APIs from Scratch

Why MCP exists

Before MCP: every framework had its own tool format. Want your CRM available as a tool in 5 frameworks? Write 5 wrappers. After MCP: you write one MCP server. It exposes tools, resources, and prompts via a standard JSON-RPC protocol. Any MCP-aware agent can plug in.

MCP server
exposes tools over one protocol
┈
Claude Desktop
Cursor
Custom agent
Database
GitHub
Filesystem
Clients on one side, backends on the other. The MCP server is the single adapter between them, so each client speaks one protocol instead of a dozen custom integrations.

Install MCP SDK

  1. 1

    1. `pip install mcp` (Python SDK).

  2. 2

    2. We'll build a tiny server that exposes one tool: `get_user_by_id`.

  3. 3

    3. Then connect Claude Desktop (or write a Python client) and call it.

A minimal MCP server

Exposes one tool over stdio.

python
from mcp.server.fastmcp import FastMCP
mcp = FastMCP('demo-server')
USERS = {1: {'name': 'Alex', 'role': 'student'}, 2: {'name': 'Jordan', 'role': 'tutor'}}
@mcp.tool()
def get_user_by_id(user_id: int) -> dict:
"""Return user record by ID."""
return USERS.get(user_id, {'error': 'not found'})
@mcp.resource('users://list')
def list_users() -> str:
return str(list(USERS.values()))
if __name__ == '__main__':
mcp.run(transport='stdio')

Connect Claude Desktop to your server

  1. 1

    1. Save the file as `server.py`.

  2. 2

    2. Open Claude Desktop config (Settings → Developer → Edit Config).

  3. 3

    3. Add: `{ "mcpServers": { "demo": { "command": "python", "args": ["/abs/path/to/server.py"] } } }`

  4. 4

    4. Restart Claude Desktop. Look for the 🔌 plug icon, your tool should appear.

  5. 5

    5. Ask Claude: 'Get user 1.', it calls your tool.

💡 Tools, resources, prompts

Tools, actions the LLM can take. Resources, data the LLM can read (like files in a filesystem). Prompts, pre-written prompt templates the user can pick. All three travel over the same MCP protocol. Most servers expose all three.

Production MCP

Real MCP servers wrap GitHub, Postgres, Slack, internal APIs. The Anthropic-built ones are open source, read them as reference. The killer use case: build one MCP server for your company's internal data → every agent + IDE in the company can use it without per-tool integration work.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Raw SDK: Anthropic and OpenAI APIs from Scratch
Back to Code-First Agents
Multi-Agent Systems: Orchestration, Supervisor Patterns, and When Not To→