Sub topic 7
Resources: context the application can read
This chapter assumes you know the MCP roles (host, client, server), the JSON-RPC message shapes, and the initialization handshake from earlier chapters. You should also be comfortable reading short Python functions.
What a resource is
A resource is a piece of data that a server exposes so the host application can read it and give it to the model as context. Examples are a file, a database row, an API response, or a log excerpt.
Resources differ from tools in who is in control. The model decides when to call a tool. The application decides when to read a resource. A host might show a list of resources in a picker, let the user attach some, or pull some in by its own rules. The server only describes what is available.
Each resource has a URI, a name, and usually a MIME type. The URI scheme is up to the server. file:///project/README.md, postgres://db/customers/42, and greeting://Ada are all valid.
Declaring the capability
A server that offers resources announces it during initialization:
{"capabilities": {"resources": {"subscribe": true, "listChanged": true}}}Both flags are optional. subscribe means clients can ask to be told when one resource changes. listChanged means the server will send a notification when the set of available resources changes. If the server does not declare resources at all, the client must not send resource requests.
The request flow
Clients use a small set of methods:
| Method | Purpose |
|---|---|
| `resources/list` | List the concrete resources the server offers |
| `resources/templates/list` | List URI templates with parameters |
| `resources/read` | Fetch the contents of one URI |
| `resources/subscribe` | Ask for updates to one URI |
A read request looks like this, followed by the server's reply:
{"jsonrpc": "2.0", "id": 3, "method": "resources/read", "params": {"uri": "greeting://Ada"}}
{"jsonrpc": "2.0", "id": 3, "result": {"contents": [{"uri": "greeting://Ada", "mimeType": "text/plain", "text": "Hello, Ada!"}]}}Text content is returned in a text field. Binary content, such as an image, is returned base64-encoded in a blob field. The contents array can hold several items, for example when one URI stands for a directory.
The update notification carries only the URI. The client then calls resources/read again to get the new contents.
Resource templates
Listing every row of a large database is not practical. A resource template is a URI pattern with a placeholder, such as greeting://{name}. The client fills in the placeholder and reads the resulting URI.
The Python SDK builds a template from a function whose parameter names match the placeholders:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
"""Return a personalized greeting."""
return f"Hello, {name}!"
if __name__ == "__main__":
mcp.run(transport="stdio")A URI with no placeholder, such as config://app, becomes a fixed resource and appears in resources/list.
Common errors
- Using a resource for an action. Resources should be read-only views of data. If reading triggers a side effect, make it a tool.
- Assuming the model reads resources on its own. The host decides what reaches the model. If you want the model to fetch data when it chooses, expose a tool too.
- Trusting template arguments. The
nameingreeting://{name}comes from outside. If you build a file path or query from it, validate it first to avoid path traversal or injection. - Reading an unknown URI. The server should return a JSON-RPC error, not an empty result.
- Forgetting to re-read after an update. The notification does not include the new data.
Exercise
A server exposes logs://{date} as a resource template. A client wants the log for 2026-11-27 and wants to be told if it changes later. Which methods does the client call, and what does the server send when the log changes?
Show answer
The client calls resources/read with the URI logs://2026-11-27, then resources/subscribe with the same URI. This requires the server to have declared subscribe: true. When the log changes, the server sends a notifications/resources/updated notification containing the URI. The notification has no log data, so the client calls resources/read again.