Lesson 12 / 25

Adding Resources and Prompts

Expose read-only data and a reusable prompt template from the same server.

URIs for data

A resource is addressed by a URI such as notes://welcome. A URI can contain a parameter in braces, which makes a resource template. Prompts are functions that return the message text a user can insert into the conversation.

A resource and a prompt

The resource returns text for a given name. The prompt builds a code-review request from the code the user supplies.

@mcp.resource("notes://{name}")
def get_note(name: str) -> str:
    """Return the text of a saved note."""
    return NOTES.get(name, "Note not found")

@mcp.prompt()
def review_code(code: str) -> str:
    """Ask for a careful code review."""
    return f"Please review this code for bugs and clarity:\n\n{code}"

Keep resources small and stable

Resources are attached to the context, so large ones cost tokens on every use. Prefer focused documents over dumping a whole database, and let a tool fetch big data on demand.

Quick check: What does `notes://{name}` define?

  • A tool with no inputs
  • A resource template with a name parameter
  • A network port
  • A Python package
Answer

A resource template with a name parameter — The braces make part of the URI a parameter that is passed to the function.