Advanced about 5 h
The picture
01
Host app
the LLM client
02
MCP client
one per server
03
Transport
stdio or HTTP
04
MCP server
tools, resources, prompts
05
Your system
files, APIs, DBs
The problem it solves
Without a standard, every app writes custom glue for every tool, an M×N integration problem. MCP defines one protocol: an application (the host) runs an MCP client per MCP server, and any compliant client can use any compliant server.
Three primitives
- Tools: functions the model can call (model-controlled).
- Resources: data the application can attach as context, addressed by URI (application-controlled).
- Prompts: reusable templates the user picks (user-controlled).
Transports include stdio (the host launches the server as a subprocess) and streamable HTTP for remote servers, usually with OAuth-based authorisation.
A server in a few lines
# server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("notes")
NOTES: dict[str, str] = {}
@mcp.tool()
def add_note(title: str, body: str) -> str:
"""Save a note. Use when the user asks you to remember or write something down."""
NOTES[title] = body
return f"saved {title!r}"
@mcp.tool()
def search_notes(query: str) -> list[str]:
"""Return titles of notes whose title or body contains the query."""
q = query.lower()
return [t for t, b in NOTES.items() if q in t.lower() or q in b.lower()]
@mcp.resource("notes://{title}")
def get_note(title: str) -> str:
"""The full text of one note."""
return NOTES[title]
if __name__ == "__main__":
mcp.run() # stdio transport
A client talking to it
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
params = StdioServerParameters(command="python", args=["server.py"])
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await session.list_tools()
print([t.name for t in tools.tools])
print(await session.call_tool("add_note", {"title": "idea", "body": "try MCP"}))
print(await session.call_tool("search_notes", {"query": "mcp"}))
asyncio.run(main())
To use the server from a desktop assistant or an IDE, register it in that host's MCP configuration with the command that starts it. The exact file and key names vary by host, so follow the host's documentation.
To expose MCP tools to your own agent loop, convert each tool's name, description and inputSchema into the tool format of your model API, and route tool_use blocks to session.call_tool.
Security model: assume the server is hostile until proven otherwise
An MCP server is code that runs with your permissions and whose tool descriptions go straight into a model's prompt.
- Tool poisoning: a malicious description can instruct the model to exfiltrate data or misuse other tools.
- Rug pulls: a server that behaves for a month and then changes its tools. Pin versions and review changes.
- Confused deputy and over-broad scopes: a server with one all-powerful token. Prefer narrow, per-user, short-lived credentials.
- Cross-server leakage: one server's output steering calls to another. Isolate sensitive tools.
- Prompt injection via results: content returned by a tool is untrusted data.
Mitigate with an allow-list of servers, human confirmation for sensitive calls, least-privilege tokens, sandboxing, and logging every call.
When to use it
Use MCP when you want reusable, interoperable integrations across several hosts, or to consume the growing ecosystem of servers. For a single app with a handful of internal functions, plain tool definitions are simpler.
Common mistakes
- Installing community servers without reading their code or tool descriptions.
- Exposing a server over HTTP without authentication.
- Returning huge results that overwhelm context.
Exercises
- Write a read-only MCP server over a SQLite database with a row limit and a query allow-list.
- Connect it to your agent loop from the earlier topic and ask a question that needs two tool calls.
You are done when you can…
- Write an MCP server exposing a tool and a resource and connect a client to it
- Explain the three server primitives and who controls each
- List the security risks of connecting third-party MCP servers