Skip to content

LLM Engineering·Lesson 50 of 67·advanced

The Model Context Protocol (MCP)

One standard for exposing tools, resources and prompts to any model client — what the protocol actually is, how to connect to a server, and why an MCP server is code you are trusting.


In this lesson · 20 min

  • →Explain what MCP standardises and what problem that solves
  • →Describe the three primitives: tools, resources and prompts
  • →Convert an MCP tool descriptor into an API tool definition
  • →Connect to a local server over stdio and a remote one over HTTP
  • →Judge the trust you are extending to a server

Every integration in the last two lessons was bespoke: you wrote the tool definition, you wrote the dispatch, you wrote the execution. MCP standardises that, so a server someone else wrote plugs into a client you did not write.

What it standardises

PythonPython not started

Without MCP, connecting three assistants to four systems means twelve integrations. With it, each system exposes one server and each assistant speaks one protocol — four plus three.

Discovery

A server answers “what can you do?” at runtime, which is what makes it pluggable:

PythonPython not started

Converting to API tools

An MCP descriptor is nearly an API tool definition already — the field is inputSchema rather than input_schema:

PythonPython not started

The prefix matters once you connect more than one server: two servers may both offer search, and the model needs distinct names. Prefixing at conversion time keeps dispatch unambiguous.

PythonPython not started

Connecting for real

There are two shapes, and they suit different situations.

A local server over stdio — the client launches the process and talks to it over pipes. This is how a filesystem or database server on your own machine works:

from anthropic import AsyncAnthropic
from anthropic.lib.tools.mcp import async_mcp_tool
from mcp import ClientSession
from mcp.client.stdio import stdio_client, StdioServerParameters

client = AsyncAnthropic()

async with stdio_client(StdioServerParameters(command="my-mcp-server")) as (read, write):
    async with ClientSession(read, write) as mcp_client:
        await mcp_client.initialize()

        discovered = await mcp_client.list_tools()

        runner = client.beta.messages.tool_runner(
            model="claude-opus-5",
            max_tokens=16000,
            messages=[{"role": "user", "content": "What changed in the guide yesterday?"}],
            tools=[async_mcp_tool(t, mcp_client) for t in discovered.tools],
        )
        async for message in runner:
            print(message.stop_reason)

Install it with pip install anthropic[mcp]. async_mcp_tool wraps each discovered tool so the tool runner can call it — the conversion you wrote by hand above, plus the execution path.

A remote server over HTTP — the API connects to it directly, so nothing runs on your machine:

response = client.beta.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    betas=["mcp-client-2025-11-20"],
    mcp_servers=[
        {"type": "url", "url": "https://mcp.example.com/sse", "name": "docs"},
    ],
    tools=[
        {"type": "mcp_toolset", "mcp_server_name": "docs"},
    ],
    messages=[{"role": "user", "content": "Search the docs for chunking guidance."}],
)

Resources and prompts

Tools are the part people use first, but a server can also hand over content and templates:

from anthropic.lib.tools.mcp import mcp_resource_to_content, mcp_message

# A resource becomes a content block in a user message.
resource = await mcp_client.read_resource(uri="file:///runbooks/incident.md")
response = await client.beta.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    messages=[{"role": "user", "content": [
        mcp_resource_to_content(resource),
        {"type": "text", "text": "Summarise the escalation path."},
    ]}],
)

# A server-provided prompt becomes messages.
prompt = await mcp_client.get_prompt(name="summarise-incident")
response = await client.beta.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    messages=[mcp_message(m) for m in prompt.messages],
)

The distinction is worth holding on to: a tool is something the model chooses to invoke; a resource is something your client decides to include. Making a document a resource keeps the decision with you.

Trust

PythonPython not started
PythonPython not started

Try it yourself

Exercise 1

Write to_api_tools(descriptors, prefix): convert each MCP descriptor to an API tool definition, prefixing the name with prefix__ and renaming inputSchema to input_schema.

Your turnPython not started
Exercise 2

Write expose(discovered, allow_list): return only the tools whose names are in the allow-list, and rejected(discovered, allow_list) returning the names that were held back.

Your turnPython not started

Check yourself

You pass mcp_servers to the API and get a validation error. What is missing?

Check yourself

Why allow-list the tools an MCP server offers rather than exposing everything it discovers?

Recap

  • MCP standardises tools (the model invokes), resources (your client includes) and prompts (templates the server offers), discovered at runtime.
  • An MCP descriptor becomes an API tool by renaming inputSchema to input_schema — and prefix the names, because servers collide.
  • stdio for a local server your client launches; the mcp_servers URL connector for a remote one. The connector needs both mcp_servers and a matching mcp_toolset in tools.
  • A server contributes capability and text to your context: allow-list its tools, gate the destructive ones, and treat every result as data.