0Pricing
MCP Academy · Lesson

Build Content at Read Time

Compute a resource's body when it is requested.

Build Content at Read Time is a free MCP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Lazy, Not Eager

A dynamic resource doesn't store a fixed body. Its content is computed on demand, only when a client actually asks to read it. ⚡

The Read Triggers the Work

Your resource function runs at the moment of a read request. Nothing happens until a client reads it, so you never waste effort building content no one wants.

Return the Body

Whatever your function returns becomes the resource's content. Return a string and the client receives that exact text as the resource body.

@mcp.resource("time://now")
def now() -> str:
    return datetime.now().isoformat()

Fresh Every Read

Because the body is rebuilt each time, the model always sees current data. Read the clock twice and you get two different timestamps.

Use the URI Variables

Combine read-time building with a template. The captured parameters shape what you compute, so the same function serves tailored content per URI.

@mcp.resource("greeting://{name}")
def greet(name: str) -> str:
    return f"Hello, {name}!"

Pull From Anywhere

At read time you can do real work: query a database, call an API, or read a file. The result of that work is what you return as the body.

@mcp.resource("stats://summary")
def summary() -> str:
    return db.fetch_summary()

Async When You Wait

If building content means waiting on I/O, make the function async. The server can serve other reads while yours awaits a slow network call.

@mcp.resource("feed://latest")
async def latest() -> str:
    return await fetch_feed()

Shape the Returned Text

You decide the format of the body. Returning JSON text is common when the model needs structured data it can reason over cleanly.

import json

def report() -> str:
    return json.dumps({"ok": True})

Keep It Quick

A read should feel snappy. If the work is heavy, cache the result so repeated reads don't redo expensive computation every single time.

Handle Missing Data

What if the requested thing doesn't exist? Raise an error from the function so the client learns the resource couldn't be read, rather than returning junk.

def get_doc(doc_id: str) -> str:
    if doc_id not in store:
        raise ValueError("not found")
    return store[doc_id]

Why This Matters

Read-time building turns a static idea into a live window on your data. The model always reads what is true right now, not a stale snapshot. 🎯

Quick Check

When does the body of a dynamic resource get produced?

Recap

You saw how returning a value from a resource function builds content on read, giving the model fresh, computed data every time. Next: listed vs templated resources. 🚀

Frequently asked questions

Is the “Build Content at Read Time” lesson free?

Yes — the full text of “Build Content at Read Time” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Build Content at Read Time”?

Compute a resource's body when it is requested. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Build Content at Read Time” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Parameterized Resource URIs
  2. Build Content at Read Time
  3. List vs Templated Resources
  4. Notify Clients of Updates
← Back to MCP Academy