Courses / Agentic AI / Module 6
Module 6: MCP servers and clients: transports, auth, building and testing a server (Python and TypeScript)
14 h · theory 4 · hands-on 6 · project 3 · revision 1
You will be able to
- Explain MCP hosts, clients, servers, tools, resources and prompts
- Build an MCP server in Python and in TypeScript
- Use stdio and streamable HTTP transports with auth
- Test servers with MCP Inspector
Capstone increment: MCP servers for logs and ticketing, used by the assistant over HTTP with auth.
Job-description skills: MCP · Lab tools: mcp, fastmcp, node, @modelcontextprotocol/sdk
1. Why a protocol for tools
Without a standard, every agent framework needs its own integration for every system: N agents times M tools. The Model Context Protocol (MCP), introduced by Anthropic in late 2024 and now supported by most major model providers, frameworks and IDEs, defines one way for an application to discover and call tools and read data, so a tool written once works in any MCP host.
Roles: the host is your application (an agent, an IDE, a chat app); it runs one MCP client per server; each server wraps a real system and exposes capabilities.
2. Tools, resources and prompts
Servers offer three primitives. Tools are functions the model can call (search_logs, create_ticket), each with a JSON Schema for its input and, in newer protocol versions, optionally for its output. Resources are data the application can read by URI (a runbook, a log file), chosen by the app rather than the model. Prompts are reusable templates a user can pick.
Clients can offer features to servers too: sampling (the server asks the host’s model to generate), elicitation (the server asks the user for input) and roots (which files or folders the server may use). Messages are JSON-RPC 2.0; a session starts with initialize, where both sides agree a protocol version and capabilities.
Deep dive
Deep dive: the MCP conversation, message by message
MCP is JSON-RPC 2.0. Every request has an id, a method and params; every response has the same id and either a result or an error; notifications have no id and get no response. The transport only carries these messages: newline-delimited JSON over stdin and stdout for a local server, or HTTP POSTs (optionally streamed back as server-sent events) for a remote one.
A session starts with a handshake that agrees a protocol version and capabilities (does the server offer tools or resources? does the client support sampling?). Only then does the client list the tools and pass their schemas to the model. A tool that fails returns a normal result with isError: true, so the model can read the message and try again; JSON-RPC errors are for protocol problems such as an unknown method.
Worked example: the wire, trimmed (→ client to server, ← server to client)
→ {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"…","capabilities":{},"clientInfo":{"name":"ops-agent"}}}
← {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"…","capabilities":{"tools":{}},"serverInfo":{"name":"logs"}}}
→ {"jsonrpc":"2.0","method":"notifications/initialized"}
→ {"jsonrpc":"2.0","id":2,"method":"tools/list"}
← {"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"search_logs","inputSchema":{…}}]}}
→ {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"search_logs","arguments":{"service":"checkout"}}}
← {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"312 errors …"}],"isError":false}}3. Build a server in Python and TypeScript
In Python, the official SDK includes FastMCP: decorate a typed function with @mcp.tool() and its signature and docstring become the tool’s schema and description. The standalone FastMCP project adds more (composition, auth helpers, testing utilities). In TypeScript, the official SDK’s McpServer registers tools with Zod schemas.
Apply everything from Module 2: clear names, enums, limits, helpful errors. Report tool failures inside the result with isError so the model can react, and keep protocol errors (bad method, invalid params) for real protocol problems.
4. Transports and auth
Two standard transports. stdio: the host starts the server as a local process and talks over stdin and stdout; simple and private, for local tools. Streamable HTTP: the server runs as a web service, receives JSON-RPC over HTTP POST and can stream responses; it replaced the older HTTP-plus-SSE transport in 2025.
Remote servers need authorisation. The MCP spec builds on OAuth 2.1: the server is a resource server that validates access tokens issued for it. Give each server its own narrow credentials for the systems behind it, and never pass the user’s token straight through to an upstream API.
5. Test and secure your servers
Use MCP Inspector to connect to a server, list its tools and call them by hand. Add automated tests that call the server through an in-memory client, plus contract tests on tool schemas. Pin the versions of third-party servers you depend on.
Treat every server as untrusted code with a voice in your prompt. Tool descriptions can carry instructions (tool poisoning), a server can change its descriptions after you approve it (a rug pull), and tool results can carry indirect prompt injection. Review descriptions when you add a server, alert when they change, and apply the guardrails of Module 10.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 6.1: Logs server in Python
Build an ops-logs MCP server with FastMCP exposing search_logs and a logs:// resource over stdio. Test it in MCP Inspector and with an in-memory client in pytest.
Expected: Inspector screenshots and passing tests.
Lab 6.2: Tickets server in TypeScript
Build a tickets server with the TypeScript SDK over Streamable HTTP, protected by bearer tokens (OAuth if you can), with idempotent create_ticket.
Expected: Unauthenticated calls fail; authenticated calls work from Inspector.
Lab 6.3: Use both from the agent (capstone increment)
Connect the capstone agent to both servers, convert their tools for your model API, and block any tool whose description changed since review.
Expected: An incident run that calls both servers, visible in the trace.
Go deeper
Videos
- Building Agents with Model Context Protocol - Full Workshop with Mahesh Murag of Anthropic · AI Engineer · ~104 min. A full workshop from Anthropic on building agents with MCP: primitives, transports and design.
- Model Context Protocol Clearly Explained | MCP Beyond the Hype · codebasics · ~15 min. MCP explained clearly, beyond the hype: what it standardises and what it doesn’t.
- Build Anything With a CUSTOM MCP Server - Python Tutorial · Tech With Tim · ~26 min. Building a custom MCP server in Python step by step.
- Build your first MCP Server in TypeScript · Roy Derks · ~9 min. Building your first MCP server in TypeScript.
- Deep Dive MCP Server Protocol JSON-RPC Communication Analysis - David Parry · Dallas Software Developers · ~31 min. A talk that reads real MCP JSON-RPC traffic message by message.
Further reading
- Docs Model Context Protocol specification · modelcontextprotocol.io. The authoritative definition of lifecycle, tools, resources, prompts and transports.
- Docs Architecture overview · MCP docs. Hosts, clients and servers, the data layer and the transport layer, with example messages.
- Docs JSON-RPC 2.0 Specification · JSON-RPC Working Group. Five pages: requests, responses, notifications, errors and batches. Everything MCP messages are built on.
- Docs Security Best Practices · MCP docs. Confused-deputy attacks, token passthrough, session hijacking and how to avoid them.
GitHub repositories
- modelcontextprotocol/python-sdk: FastMCP in the official SDK: tools, resources, the stdio and Streamable HTTP transports, and testing with a client.
- modelcontextprotocol/typescript-sdk: McpServer.registerTool with Zod schemas, and the Streamable HTTP server example.
- PrefectHQ/fastmcp: The standalone FastMCP: server composition, auth providers and in-memory testing.
- modelcontextprotocol/inspector: Run it against your server to list and call tools by hand.
- modelcontextprotocol/servers: Reference servers (filesystem, git, fetch) to read before writing your own.
Checkpoint quiz
Interview questions
- Explain MCP to a backend engineer: hosts, clients, servers and the primitives.
- When would you expose a tool over MCP instead of calling it directly?
- How do you secure a remote MCP server?
- What are tool poisoning and rug pulls, and how do you defend against them?
- How do you test an MCP server automatically?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).