Module 08E: Projects

Agents & multimodal1 projects0 of 1 complete

Starter projects are guided step by step and suit freshers. Stretch projects are open-ended, add production concerns, and suit experienced readers.

One project, a starter: a custom MCP server (08E.P1). It builds on lesson 08E.1 and nothing else in this module. Everything runs keylessly. The MCP server and its in-memory client are pure Python over in-memory fake data, so you can finish the "Done when" checklist without a single API key. Real MCP hosts (a desktop app or an IDE) and provider-native MCP support are the stretch goal once you have keys; the lesson shows exactly where they plug in.

What the fakes can and cannot tell you. The in-memory client proves the protocol plumbing: that your tools, resource and prompt are listed, that calls return the right shapes, and that errors come back as errors. It cannot tell you how a real model will use your tools: which one it picks, whether it invents an argument, how it reads your descriptions. Any "expected output" below is labelled illustrative. That gap is what the tool-use evals of lesson 08A.6 are for.

First time? Do the shared setup once: uv init tools-agents --bare --python 3.13 && cd tools-agents, then uv add "mcp[cli]" pytest anyio (the lesson picked its own folder name; any works). Run scripts with uv run python file.py and tests with uv run pytest -q. No .env file is needed for anything below.

You will reuse lesson files rather than reinvent them. The names below are the real ones from the lesson. Copy each file into your project folder exactly as printed there:

  • From 08E.1: ops_server.py, test_ops_server.py, s2_mini_mcp.py

If you also work through the projects of Module 08A, keep this project in its own folder so file names do not collide.


Starter

Starter: Custom MCP server#

Goal: Expose your own data as an MCP server — a tool, a resource and a prompt — built with the official mcp SDK exactly as lesson 08E.1 teaches, tested in memory with the SDK's Client, served over stdio and Streamable HTTP, and connected from a small client that lists and calls it. Optionally map the server's tools into the provider-neutral shape so your agent loop could call them.

What you'll practise:

  • The three MCP primitives — tools, resources (and templates), prompts — and the 2026-07-28 protocol revision the SDK serves (lesson 08E.1, 2.3, 1.4)
  • MCPServer decorators, typed schemas from signatures, ToolAnnotations, structured results and ToolError for model-actionable failures (lesson 08E.1, 3.1–3.2)
  • In-memory testing with Client(mcp, raise_exceptions=True) (lesson 08E.1, 3.4)
  • Running and packaging: stdio default, mcp dev/mcp run, Streamable HTTP transport (lesson 08E.1, 3.5)
  • Mapping MCP tools onto provider formats as pure Python (lesson 08E.1, 4.2)
  • The security checklist you apply before trusting any server — yours included (lesson 08E.1, Part 5)

Setup: the shared project plus uv add "mcp[cli]" pytest anyio. The samples were checked against mcp 2.3.0 as of November 2026, and a plain install gives you the newest release; its high-level class is MCPServer (v1's FastMCP name does not import on v2 — 08E.1, 3.1).

Steps:

  1. Copy the pattern, change the data. Read 08E.1's ops_server.py and its test first. Then write garden_server.py: an MCPServer("garden-helper") for a fictional houseplant shop, over an in-memory PLANTS dict (provide your own five entries — e.g. monstera, snake_plant, pothos, fiddle_leaf_fig, succulent — each with light, water_every_days, toxic_to_pets, notes).
  2. Add the primitives. Three things, mirroring the lesson:
    • @mcp.tool(annotations=ToolAnnotations(read_only_hint=True)) plant_lookup(plant_id: str) -> PlantResult — a dataclass result (structured output) or ToolError for unknown IDs ("no plant 'x'; known ids: ...").
    • @mcp.tool() log_watering(plant_id: str, notes: str = "") -> str — appends to a WATERING_LOG list, returns a confirmation; not read-only, so hosts prompt before running it.
    • @mcp.resource("care://{plant_id}") care_guide(plant_id: str) -> str — a templated resource returning the care text.
    • @mcp.prompt() plant_triage(problem: str) -> str — a prompt template that inlines the relevant care guide, like the lesson's refund_triage.
  3. Test in memory. Write test_garden_server.py on the lesson's fixture pattern: Client(mcp, raise_exceptions=True); assert list_tools() contains both tools, call_tool("plant_lookup", ...) returns structured_content, a bad ID returns is_error with your message, read_resource on the care URI works, and get_prompt("plant_triage", ...) inlines the guide. Run uv run pytest -q — all green before you go further.
  4. Serve it. Keep mcp.run() (stdio) as the default in if __name__ == "__main__". For HTTP, add a commented variant mcp.run(transport="streamable-http", port=3001) and run it in a second terminal; confirm http://127.0.0.1:3001/mcp answers. Try uv run mcp dev garden_server.py to watch the inspector.
  5. Write the client. mcp_client.py: an async main() that connects with Client("http://127.0.0.1:3001/mcp") (or Client(mcp) in-process for the keyless path), calls list_tools(), call_tool("plant_lookup", {"plant_id": "monstera"}), read_resource("care://monstera") and get_prompt(...), and prints a readable summary. Then paginate correctly: follow next_cursor on list_tools if the SDK returns one (08E.1, 4.1).
  6. Map to provider format. Write map_tools.py: a pure function to_openai_tools(listing) -> list[dict] and to_anthropic_tools(listing) -> list[dict] converting client.list_tools() output into the Chat Completions / Messages tool shapes from 03.5 — this is the lesson's exercise 3, and the bridge between "MCP server" and "your agent loop". Unit-test it with a canned listing; no network.
  7. Security pass. Walk 08E.1's Part 5 checklist against your own server: are annotations honest (log_watering is not read-only)? Do error messages leak internals? Would you add an auth layer before exposing port 3001 beyond localhost? Write three sentences in your notes.

Verify it, and what you should see:

bash
uv run pytest -q test_garden_server.py     # all tests pass
uv run python mcp_client.py                # prints the tool list + one call result
Example output · illustrative4 lines
tools: plant_lookup(read_only), log_watering
plant_lookup(monstera): light=bright_indirect water_every_days=7 toxic_to_pets=true
resource care://monstera: "Monstera deliciosa — water when the top 5cm..."
prompt plant_triage: "A customer reports brown leaf tips..."

Done when:

  • test_garden_server.py passes entirely in memory — no port, no transport, no keys.
  • The server exposes at least one tool, one resource (templated or static) and one prompt, with honest ToolAnnotations.
  • mcp_client.py lists, calls, reads and prompts against your running server over Streamable HTTP (or in memory).
  • map_tools.py converts a real list_tools() listing into both provider shapes and its unit test passes.
  • Your security notes answer: what would you never expose from this server, and what would you add before a real host connects?

Stretch ideas:

  • Add ctx: Context and await ctx.report_progress(...) to a slow tool, plus lifespan state (AppContext) holding the watering log, per 08E.1, 3.3.
  • Add an approval-gated escalation with Resolve/Elicit — the escalate_ticket pattern from 08E.1, 3.6 — and drive it in a test with elicitation_callback.
  • Package it: uv build, then document the uvx/mcp install command a host would run (08E.1, 3.5).
  • Hosted, needs keys: point a provider's native MCP support at your server (08E.1, 4.3) or register it with Claude Desktop/Cursor config, and compare the tool schemas the host actually sends.

Common problems:

  • ImportError: cannot import name 'FastMCP': you're on SDK v2 — use from mcp.server import MCPServer (08E.1, 3.1).
  • Tests see "Internal server error": you forgot raise_exceptions=True on the test Client — real tracebacks are sanitised for remote callers by design.
  • Tool returns a dict the client can't read: return a dataclass or str for structured output; raise ToolError (not a bare Exception) for failures the model should read (08E.1, 3.2 notes).
  • Resource template 404s: URI must match the template exactly — care://monstera matches care://{plant_id}; care://plants/monstera does not.
  • HTTP transport won't start: check the port isn't taken and that you used transport="streamable-http", not the deprecated SSE transport (08E.1, 2.6).

Esc
02.2Tokens and TokenizationHow LLMs Work · 21k wordsLesson
02.3EmbeddingsHow LLMs Work · 18k wordsLesson
07Retrieval-Augmented Generation (RAG)11 lessons · Everyone who will build LLM features that must answer from knowledge the model doesn't have in its weights: internal handbooks and help centres, contracts, ticket histories, product docs, or any corpus that changes faster than a training run. The module covers the full RAG stack — architecture, retrieval strategy, query understanding, grounded generation, citations, conversation, managed services, advanced patterns, evaluation, debugging and production operation — and shows where each major provider's managed retrieval and citation features fit (OpenAI, Anthropic, Google Gemini and OpenRouter). All code is Python.Overview
08E.1The Model Context Protocol (MCP)Protocols and Extensibility · 12k wordsLesson
↑ ↓ navigate↵ openesc close