Resources and Prompt Templates
- Register a resource in FastMCP with a URI and a function that returns the resource content
- Define a URI template resource that accepts a variable path segment
- Explain the practical difference between using a tool and a resource for the same data
- Register a prompt template that accepts a parameter and returns a list of TextContent objects
Resources: Read-Only Context Data
A resource is data that the agent can read but not modify. Where a tool executes an action and returns a result, a resource exposes a data source that the agent reads for context. The distinction matters for how the host presents them to the LLM: tools are "things you can do," resources are "things you can read."
Practical examples of resources:
- A file's contents, identified by its path
- A database schema, identified by the database name
- An API's documentation, identified by a URL
- A configuration file the agent should not modify
Resources are identified by URIs, not names. The URI format is flexible — common schemes are file:// for local files, db:// for database content, and custom schemes like notes:// for application-specific data. The URI appears in the resource listing and is what the client uses to fetch the resource.
Defining Resources in FastMCP
Register a resource with the @mcp.resource decorator, passing the URI as the argument:
from fastmcp import FastMCP
from pathlib import Path
mcp = FastMCP("Notes")
@mcp.resource("notes://schema")
def notes_schema() -> str:
"""The schema of the notes data structure stored by this server."""
return """{
"id": "integer — unique note identifier",
"title": "string — short summary of the note",
"content": "string — full note body",
"created": "ISO 8601 datetime string"
}"""
When the client calls resources/list, it receives this resource with the URI notes://schema and the function's docstring as the description. When it calls resources/read with that URI, FastMCP calls the function and returns the string.
Resources can also use URI templates for parameterised access — the client fills in a variable part of the URI to request a specific resource:
@mcp.resource("notes://{note_id}/content")
def note_content(note_id: int) -> str:
"""The full content of a specific note by ID."""
notes = _load()
for note in notes:
if note["id"] == note_id:
return note["content"]
return f"Note {note_id} not found."
A client can request notes://3/content and FastMCP routes it to this function with note_id=3.
Tools vs Resources: When to Use Which
The choice between a tool and a resource for the same data is a design decision that affects how the agent uses it. The practical rule:
- Use a resource when the data is relatively static, the agent reads it for context without acting on it immediately, and reading it has no side effects. The notes schema is a good resource — the agent reads it to understand the data format, not to trigger an action.
- Use a tool when retrieving the data is itself an action (a database query, an API call), when the result depends on input parameters the LLM must supply, or when the agent should explicitly decide to access it as a step in a plan. The
get_notefunction is a tool because the agent must decide to call it with a specific ID.
In practice, most things in an agent-focused MCP server should be tools. Resources work best for reference data the host might want to inject into context automatically, without waiting for the LLM to decide to request it.
Prompt Templates
A prompt template is a reusable instruction structure — a system prompt, a few-shot example, or a structured question format — that the host can retrieve from the server and inject into the conversation. The host, not the LLM, decides when to use a prompt template.
Register a prompt template with @mcp.prompt:
from fastmcp import FastMCP
from mcp.types import TextContent
mcp = FastMCP("Notes")
@mcp.prompt
def research_summary_prompt(topic: str) -> list:
"""A structured prompt for summarising research notes on a topic."""
return [
TextContent(
type="text",
text=(
f"You are summarising research notes about: {topic}
"
"Steps:
"
"1. Call list_notes to see all available notes.
"
"2. Call get_note for each note that seems relevant.
"
"3. Write a concise summary covering key findings, open questions, "
"and next steps.
"
"Return only the summary, no preamble."
)
)
]
The host retrieves this template by calling prompts/get with the template name and passing the topic argument. The returned text is injected into the conversation — typically as a system message before the user's request. For this track's agent, prompt templates are optional; the agent constructs its own system prompt directly. But if you are building a server for use with Claude Desktop or other MCP hosts, prompt templates let users apply structured workflows from the host's UI.
Updating the Notes Server
Add the schema resource and the research summary prompt to your notes_server.py from Lesson 3:
@mcp.resource("notes://schema")
def notes_schema() -> str:
"""The structure of a note object stored by this server."""
return "id (int), title (str), content (str), created (ISO datetime)"
@mcp.resource("notes://{note_id}/content")
def note_content(note_id: int) -> str:
"""The raw content of a specific note. Use get_note for formatted output."""
notes = _load()
for note in notes:
if note["id"] == note_id:
return note["content"]
return f"Note {note_id} not found."
Restart the server after adding these. MCP Inspector (Lesson 6) will show all three sections — Tools, Resources, and Prompts — once they are registered. For the agent in this track, only the tools are used directly by Hermes 3; the resources and prompt template are available to any MCP host that connects.
Notifications: Dynamic Updates
If your server's tool or resource list can change at runtime, it can notify connected clients by declaring listChanged: true in its capabilities and sending a notifications/tools/list_changed message. FastMCP handles this automatically when you add or remove tools dynamically. For static servers (all tools defined at startup, as in this track), notifications are not needed and do not need to be configured.
- Resources are identified by URIs, not names — file://, db://, or custom schemes like notes:// are all valid; the URI appears in the resource listing the client receives
- Use a resource for static reference data with no side effects; use a tool when retrieval requires a decision by the LLM, depends on user-supplied parameters, or triggers an action
- Prompt templates are retrieved by the host, not the LLM — the host injects them into the conversation context; for a custom Python agent loop, you can construct the system prompt directly instead
- FastMCP generates the resource schema from the function signature automatically, the same way it does for tools
Progress is saved in this browser only — clear your cache or switch device and it's gone. Create a free account and it follows you everywhere.