Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Frameworks and Deployment

Model Context Protocol (MCP)

Learn what the Model Context Protocol is, how hosts, clients and servers exchange tools, resources and prompts over JSON-RPC, and run a tiny MCP-style server in Python.

Every agent needs tools: read files, query a database, search tickets, post to chat. Without a standard, each agent framework needs its own connector for each service, and each service needs a connector for each framework. That is an N times M problem. The Model Context Protocol (MCP) is an open standard that turns it into N plus M: a service implements an MCP server once, and any MCP-capable application can use it.

This lesson explains the roles, the three kinds of things a server offers, how the messages look on the wire, and the security concerns. You will run a miniature MCP-style server and client in Python to see the request and response shapes.

The real MCP SDKs and servers are not available on this site. The runnable code is a stand-in that imitates the JSON-RPC message pattern in a single Python process; it is not a complete or compliant implementation. Consult the official specification for exact method names, fields and protocol versions before building a real server.

The cast

  • Host: the application the user runs (a chat app, an IDE, your agent program). It owns the model and the user relationship.
  • Client: a connector inside the host that keeps one connection to one server.
  • Server: a program that exposes capabilities, either local (started as a subprocess) or remote (over HTTP).
flowchart LR
  H["Host app with the model"] --> C1["Client 1"]
  H --> C2["Client 2"]
  C1 <--> S1["Files server"]
  C2 <--> S2["Issue tracker server"]

One host can talk to many servers, each through its own client.

What a server can offer

Three things servers expose

Tools Actions the model can call
Resources Data to read as context
1 / 2
  • Tools are functions with a name, a description and a JSON Schema for their inputs. The model decides when to call them. This is what most people mean by MCP.
  • Resources are readable data identified by a URI (a file, a database row, a document). The application decides what to put into context.
  • Prompts are named templates the user can pick, such as "review this pull request".

How messages look

MCP uses JSON-RPC 2.0: each request has an id, a method and params, and each response carries the same id with a result or an error. A session begins with an initialization handshake where both sides declare their capabilities. After that, the client typically asks the server to list its tools and then calls one.

JSON
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}
JSON
{
  "jsonrpc": "2.0", "id": 2, "method": "tools/call",
  "params": {"name": "get_weather", "arguments": {"city": "Oslo"}}
}

The host takes the tool list from the server, passes the descriptions and schemas to the model, and when the model asks for a tool call, the host sends tools/call and returns the result to the model.

A runnable MCP-style exchange

Here is a stand-in server that handles tools/list and tools/call, and a client that talks to it with JSON-RPC shaped messages. It shows how discovery and invocation work without any network.

Python A toy JSON-RPC tool server

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second reply carries a result with a content list; the third carries an error. Clients must handle both.

Writing a real server (read-only)

Official SDKs let you declare tools as ordinary typed functions and handle the protocol for you. A Python server with the official SDK looks roughly like this. It cannot run on this page, and the details vary by SDK version, so treat it as a shape to look up in the current docs.

Python
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("notes")

@mcp.tool()
def add_note(title: str, body: str) -> str:
    """Save a note and return its id."""
    ...

if __name__ == "__main__":
    mcp.run()

The SDK reads the type hints and docstring to produce the tool's schema and description, which is exactly the "function to schema" step from our framework lesson.

Transports

A local server usually talks over standard input and output: the host starts the process and exchanges messages through its pipes. Remote servers use HTTP, often with streaming for long operations, and need authentication. Pick local for personal tools and remote for shared services.

Security: treat servers as untrusted code

MCP makes it easy to plug capabilities into an agent, which also makes it easy to plug in danger.

  • Trust: a server runs code and returns text the model will read. A malicious or compromised server can inject instructions ("tool poisoning" in tool descriptions or results). Install only servers you trust.
  • Least privilege: give each server minimal credentials and file access.
  • Approvals: require human confirmation for tools that change things (see human-in-the-loop).
  • Validate output: treat returned text as untrusted data, as in guardrails.
  • Authenticate remote servers and keep secrets out of tool descriptions and logs.

When to use MCP

Use it when you want tools reusable across several applications, or when you want to use existing servers instead of writing connectors. For a single private agent with two simple tools, a plain function is simpler. MCP earns its keep as the number of tools and clients grows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does MCP solve?

    A service writes one server and any MCP-capable host can use it.
  2. 2. Which message format does MCP use?

    Requests and responses are JSON-RPC with ids, methods, results and errors.
  3. 3. What is a resource, compared with a tool?

    Tools are actions; resources are readable data; prompts are templates.
  4. 4. Why should you treat third-party MCP servers carefully?

    Use trusted servers, least privilege and approval gates.

Summary

  • MCP is an open protocol that lets any host connect to any server's tools, resources and prompts.
  • Hosts contain clients; each client connects to one server over stdio or HTTP using JSON-RPC.
  • Typical flow: initialize, tools/list, then tools/call with arguments that match the tool's schema.
  • SDKs turn typed functions into tool schemas for you.
  • Treat servers as untrusted: least privilege, approvals, output validation and trusted sources only.

Next: Structured Agent Workflows and Graphs