Skip to content

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.

Agentmodel + loopMCP clientlogs serverMCP clienttickets serverLogs serverstdio, localTickets serverHTTP + OAuthLog storeLoki, filesTicketing APIJira, GitHubHost: your agent appServers expose tools, resources and promptsJSON-RPCJSON-RPC
Write a tool once as a server and any MCP host can use it. The server, not the model, holds credentials and enforces scope.

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}}
initializeversion, client capsResultserver capsinitializednotificationtools/listnames + schemasModelpicks a tooltools/callname + argumentsServer runs ityour codeResultcontent, isErrorModelreads the resultOnce per sessionEvery tool call (JSON-RPC 2.0 request and response)schemas go into the prompt
The model never talks to the server. The host’s MCP client does, and a tool failure comes back as a normal result with isError set, so the model can read it and recover.
Common misconception: MCP doesn’t make a tool safe. It standardises how tools are described and called; authentication, least privilege and treating tool output as untrusted data are still your job.

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.

Tip: Write the tool’s logic as a plain function with its own tests, and keep the MCP layer thin. Then the same function can be called directly in unit tests and served over MCP.

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.

Common misconception: “MCP handles security.” MCP standardises how tools are called. Who may call them, with which scopes, and what the server is allowed to do are still your design.

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 code

Labs

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

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

Checkpoint quiz

1. In MCP, who controls which resources are read?
2. Which transport suits a local tool started by the host?
3. A tool call fails because the service is down. How should the server report it?
4. A third-party server changes a tool description to include extra instructions. Risk?
5. Should a remote MCP server forward the user’s access token to upstream APIs?
6. An MCP tool hits a timeout calling its API. How should the server report it?

Interview questions

  1. Explain MCP to a backend engineer: hosts, clients, servers and the primitives.
  2. When would you expose a tool over MCP instead of calling it directly?
  3. How do you secure a remote MCP server?
  4. What are tool poisoning and rug pulls, and how do you defend against them?
  5. 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).