Sub topic 8
Exposing read-only data as resources
This chapter assumes you have built a server with a tool, as in "Your first tool". You should know what the FastMCP object is and how a host launches a server.
What a resource is
A resource is a piece of read-only data that a server exposes under a URI. Examples are a runbook, a config file, or a database schema. The client reads a resource by URI, and the server returns its contents. Reading does not run side effects.
Resources differ from tools in who decides to use them:
| Row | Tools | Resources |
|---|---|---|
| Controlled by | The model decides to call | The host application decides what to attach |
| Purpose | Do something or compute something | Supply context |
| Addressed by | Name and arguments | URI |
| Side effects | Allowed | Should not have any |
Use a resource when the content is stable text or data that a person might want to attach to a conversation. Use a tool when the model needs to query or act on its own.
Static resources and templates
A static resource has a fixed URI. A resource template has placeholders, so one function serves many URIs. Templates follow URI template syntax, where {service} is a variable.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("team-tools")
RUNBOOKS = {
"billing": "1. Check the queue depth.\n2. Restart the worker.\n3. Page payments-team.",
"search": "1. Check index health.\n2. Reindex the shard.\n3. Page discovery-team.",
}
@mcp.resource("runbook://index", mime_type="text/plain")
def runbook_index() -> str:
"""List the services that have runbooks."""
return "\n".join(sorted(RUNBOOKS))
@mcp.resource("runbook://{service}", mime_type="text/plain")
def runbook(service: str) -> str:
"""Runbook text for a service."""
if service not in RUNBOOKS:
raise ValueError(f"No runbook for service: {service}")
return RUNBOOKS[service]
if __name__ == "__main__":
mcp.run()Two rules apply here:
- The function parameter names must match the placeholders in the URI.
{service}maps to theserviceargument. - A function with no placeholders, like
runbook_index, becomes a static resource.
A str return value is sent as text. A bytes return value is sent as a binary blob. Set mime_type so the host knows how to display the content.
How clients discover and read them
The server declares the resources capability during initialization. After that, the client uses three requests:
resources/listreturns static resources.resources/templates/listreturns templates.resources/readreturns the contents for one URI.
placeholderTemplates do not appear in resources/list. A host that only calls resources/list will never show runbook://billing. In the Inspector, check the templates view as well.
Errors and design choices
If a URI cannot be served, raise an exception. The client then receives a protocol error for that read, and the server keeps running. Returning a string such as "No runbook found." looks like real content, so a host may pass it to the model as if it were a runbook. Raising is clearer.
Keep these habits:
- Choose URI schemes that name your domain, such as
runbook://orschema://. - Keep resources side-effect free. If reading triggers a write, make it a tool.
- Validate placeholder values before using them. A template like
file://{path}that openspathdirectly allows path traversal, so check against an allow-list. - Keep each resource a reasonable size, because the host may put the whole text into the model's context.
Exercise
You add @mcp.resource("ticket://{ticket_id}") and the Inspector's resource list is empty, but your function works. What explains this, and where do you find the resource?
Show answer
resources/list only returns static resources. A URI with a placeholder is a resource template, so it appears under resources/templates/list. In the Inspector, open the resource templates view, fill in ticket_id, and read it from there.