Foundations to Frontier
Tests
F. NLP and Large Language Models › F11 Agents and Tool Use

Model Context Protocol (MCP)

The Model Context Protocol is an open standard for connecting AI applications to tools and data. A server exposes tools, resources and prompts over JSON-RPC; any MCP-compatible client (Claude, ChatGPT, IDEs, agent frameworks) can discover and use them without custom integration code. This page covers the architecture, the message flow, transports, authorisation, and the practical issues that appear as servers multiply: context consumed by tool definitions, name collisions, and malicious or changing tool descriptions. In the exercises you write a minimal MCP tool server that handles initialize, tools/list and tools/call with JSON-RPC errors. You then simulate clients connected to 1–20 servers with 8 tools each. Loading every definition cost 532 tokens for one server and 10658 for 20, against about 375 tokens with on-demand tool search. Two servers exposed the same tool name 98% of the time, so names must be namespaced. Pinning a hash of each approved definition flagged all 22 malicious description changes, along with 64 harmless ones.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A universal plug for AI tools

Before USB, every device needed its own cable. MCP is a common plug for AI applications: a company writes one MCP server for its database, calendar or design tool, and every AI app that speaks MCP can use it. The app asks the server what tools it offers, shows them to the model, and forwards the model’s calls. The demo shows a cost of this convenience: the more tools you plug in, the more of the model’s attention and context they take.

Agent lab
With 20 tools the model picks the right one about 83% of the time, and the tool descriptions take about 3000 tokens of context. Offer only the tools a task needs, or retrieve them.
Why engineers care

MCP turned tool integration from per-application code into a shared ecosystem of thousands of servers, which also makes trust in third-party servers an important question.

📝 Quick check (Level 0)
0/3 answered
  1. 1. MCP standardises…
  2. 2. An MCP server exposes…
  3. 3. Plugging in many servers…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Roles: host (the AI application), client (one connection per server inside the host), server (exposes capabilities).
  • Primitives: tools (model-invoked functions with JSON-schema inputs), resources (data the application can read, by URI), prompts (templates the user can pick); clients can offer sampling and elicitation back to servers.
  • Protocol: JSON-RPC 2.0; lifecycle initialize → capability negotiation → requests (tools/list, tools/call, resources/read) and notifications (list_changed).
  • Transports: stdio for local servers; streamable HTTP for remote servers, with OAuth 2.1 authorisation.
  • Scaling issues: tool definitions fill context; same-named tools collide; tool search and progressive disclosure load definitions on demand.
  • Security: servers run code with your permissions; tool descriptions can carry injected instructions (tool poisoning); descriptions can change after approval (rug pull); confused-deputy and token-passthrough risks with remote servers.
📝 Quick check (Level 1)
0/3 answered
  1. 1. MCP messages use…
  2. 2. Local servers usually use the transport…
  3. 3. Tool poisoning hides instructions in…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Building and using MCP servers

Exercise 1.

  • Handle initialize, tools/list and tools/call.
  • Return JSON-RPC errors for unknown methods and tools.
  • Report tool exceptions as isError results.

Practice.

  • Official SDKs: Python (FastMCP), TypeScript, and others; MCP Inspector for testing.
  • Write tool descriptions for the model, with clear input schemas; keep each server focused.
  • Use only trusted servers; review their code or provenance; run local servers with least privilege.
  • Namespace tools by server; enable tool search when many servers are connected.
  • Pin approved tool definitions and re-confirm on change; log all calls.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("telemetry")

@mcp.tool()
def read_sensor(sensor_id: str) -> float:
    """Latest reading of a telemetry sensor, in SI units."""
    return db.latest(sensor_id)

if __name__ == "__main__":
    mcp.run()          # stdio transport
🐍 Exercise 1: a minimal MCP tool server
Handle initialize, tools/list and tools/call as JSON-RPC 2.0, with errors for unknown methods and tools.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A Python framework for MCP servers:
  2. 2. A tool for testing servers:
  3. 3. Third-party servers should be…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Context cost. Eagerly loading all tool definitions costs Σ servers × tools × tokens per definition, before the user says anything, and degrades tool selection (F11-1). On-demand discovery keeps a fixed small cost: one search tool plus the few definitions it returns.

Name collisions. Tool names cluster on common verbs and nouns (‘search’, ‘get_file’). With names drawn from a skewed distribution, the chance two servers share a name rises quickly, like the birthday problem; clients must qualify names by server.

Integrity of definitions. A tool definition is untrusted input to the model. Pinning a cryptographic hash at approval time detects any later change, but cannot tell harmful from harmless changes: every update needs re-approval, trading user attention for safety. Content inspection can triage changes, but a hash is the only check that cannot be fooled.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Eager loading cost grows with…
  2. 2. Name collisions resemble…
  3. 3. Hash pinning cannot tell…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What happens to context, naming and trust as an agent connects to more MCP servers?

History.

  • Anthropic, “Introducing the Model Context Protocol” (November 2024); adopted by OpenAI, Google and Microsoft in 2025.
  • Specification revisions in 2025 added streamable HTTP, OAuth 2.1 authorisation, structured tool output and elicitation.
  • Invariant Labs (2025) described tool-poisoning and rug-pull attacks on MCP clients.

Exercise 2. Simulated MCP servers with 8 tools each; tool names drawn from 100 common verb_noun names with a Zipf-like distribution; definitions measured in JSON characters / 4.

  • Tool search: one search tool plus 5 definitions loaded on demand.
  • Pinning: over 2000 sessions a server changes a description harmlessly 4% of the time and maliciously 1%; definitions are compared with hashes pinned at approval.
servers eager definitions (tokens) with tool search (tokens) P(some name collides) colliding names
1 532 376 0.000 0.0
2 1066 375 0.975 2.5
5 2661 373 1.000 14.3
20 10658 375 1.000 103.6
pinning over 2000 sessions count
malicious changes / flagged 22 / 22
benign changes / flagged 64 / 64
re-approvals per 100 sessions 4.3
  • Context: eager loading grew linearly to over 10000 tokens at 20 servers; tool search stayed flat.
  • Collisions were near-certain from two servers on, because popular names dominate; namespacing removes them.
  • Pinning caught every malicious change, and also asked the user to re-approve every harmless update.
  • Seeds 1 and 2: identical patterns (collision at 2 servers 0.985 and 0.975; malicious changes 20 and 20, all flagged).
  • Caveat: server populations, change rates and token estimates are illustrative; real tool definitions are often several hundred tokens each.
🐍 Exercise 2 (research): what happens to context, naming and trust as an agent connects to more MCP servers?
Measure context cost of eager vs on-demand tool loading, tool-name collisions across servers, and hash pinning of tool definitions.

MCP in AI, robotics and aerospace

  • Engineering data access: MCP servers in front of PLM, requirements and telemetry databases give assistants governed, read-only access.
  • Robotics: ROS bridges exposed as MCP servers let language-model planners call robot skills through a standard interface.
  • Governance: organisations keep allow-lists of approved servers and pin their definitions.

References

  1. Anthropic (2024). “Introducing the Model Context Protocol.” Announcement and specification at modelcontextprotocol.io.
  2. Model Context Protocol specification (2025-06-18 revision). modelcontextprotocol.io/specification.
  3. Invariant Labs (2025). “MCP security notification: tool poisoning attacks.”
  4. JSON-RPC Working Group (2010). “JSON-RPC 2.0 specification.” jsonrpc.org.