Resources: Data the Application Reads
A tool lets the model do something. Often, though, what the model needs first is simply to know something: the contents of a document, a configuration, a customer's record. MCP has a separate kind of capability for that, the resource: read-only data that a server offers and the application loads into the model's context. Each resource has an address, and knowing how those addresses work is most of what you need to offer data this way and to read it back.
Data the application chooses
Recall the three things a server can offer, told apart by who decides to use them. A tool is controlled by the model: the model reads its name and description and decides to call it as an action. A resource is controlled by the application, the host you work in. The specification describes resources as contextual data "attached and managed by the client", with file contents and git history as typical examples.
In practice the host lists the resources a server offers and decides what to do with them. It might show them in a list or a tree so you can pick one, let you search and filter them, or attach some automatically when they look relevant. The protocol does not prescribe which: it only says how the application finds resources and reads them. Once read, the content goes into the model's context, much like a file you attach to a chat message.
- Server lists resources
- Host shows them, one is picked
- Host reads it
- Content in the context
- Server lists resources to Host shows them, one is picked
- Host shows them, one is picked to Host reads it
- Host reads it to Content in the context
Two more differences follow from that role. A resource is read-only: reading it changes nothing on the server, so a host can read one without the risk that an action carries. And a resource is found by its address, not by a function name: the client asks for config://app, never for the Python function that produces it.
| Tool | Resource | |
|---|---|---|
| Who decides to use it | The model | The application or its user |
| What it does | Takes an action, may change data | Returns data, changes nothing |
| How it is found | By its name | By its URI |
| Protocol methods | tools/list, tools/call | resources/list, resources/read |
True or false?
A model calls a resource the same way it calls a tool, whenever it decides it needs the data.
False. Resources are application-controlled: the host decides how to use them, for example by letting the user pick one. A host may attach some automatically, but that is its choice, not a call the model makes as an action.
Reading a resource should never change anything on the server.
True. A resource is data to read. Anything that changes state belongs in a tool, where the model takes it as an action and the host can ask for approval.
A URI names each resource
Every resource has a URI, the same kind of address a web link uses: a scheme before :// and a path after it. The URI is the resource's identity. A client lists resources to learn their URIs, and then reads one by sending its URI back.
The scheme is yours to choose. The specification mentions familiar ones, such as file:// for data that behaves like files (with no real file on disk required) and git:// for version control. It also states plainly that servers are free to define their own, as long as the URI follows the general URI rules (RFC 3986).
One scheme deserves care. The specification advises using https:// only when the client could fetch the resource from the web by itself. For data that only your server can produce, a custom scheme such as config:// or notes:// says more clearly where it comes from.
With the official Python SDK, a resource is an ordinary function with a decorator that gives its URI. The server class is MCPServer (in the 1.x line of the SDK and in many older tutorials it is called FastMCP, which is also the name of a separate framework):
from mcp.server import MCPServer
mcp = MCPServer("Bookshop")
@mcp.resource("config://app")
def get_config() -> str:
"""The active shop configuration."""
return "theme=dark\nlanguage=en"The decorator always takes the URI in parentheses: @mcp.resource("config://app"), never a bare @mcp.resource. From the function, the SDK builds the entry a client sees in the listing below. As with tools, the name is the function's name and the description is its docstring.
The entry also has a mimeType, the media type: the standard label that says what kind of data the content is, such as text/plain, application/json or image/png. It defaults to text/plain unless you declare another with mime_type=.
{
"name": "get_config",
"uri": "config://app",
"description": "The active shop configuration.",
"mimeType": "text/plain"
}True or false?
A resource URI has to start with file:// or https://.
False. Servers may define their own schemes, such as config:// or notes://. The specification even advises against https:// unless the client could fetch the resource from the web on its own.
To read the resource above, a client sends the URI config://app, not the name get_config.
True. Resources are addressed by URI. The name is a label for people and listings; the read request carries only the URI.
Templates: one URI for a family of resources
A fixed URI works for a single piece of data. Many servers hold data with the same shape for many items: a profile for every user, a page for every order. Listing each one separately would be impractical, and the list would change all the time. A resource template solves this with one URI that has a parameter in curly braces, following the URI template standard (RFC 6570). users://{user_id}/profile stands for users://7/profile, users://42/profile and every other user.
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
"""A customer's profile."""
return f"Profile of user {user_id}"The SDK decides between a fixed resource and a template purely by the URI: a parameter in braces makes it a template. The parameter in the URI and the parameter of the function must match by name. If they do not, for example {user_id} in the URI and user on the function, the decorator raises a ValueError as soon as the module is imported, before any client connects.
When a client reads a concrete URI, the SDK finds the template it matches, takes the value out of the URI and calls the function with it. A {name} parameter matches one path segment, so it stops at the next /. The value arrives as text, and a type hint on the parameter converts it: with user_id: int, the function receives a number.
- One template users://{user_id}/profile
- users://7/profile user_id = "7"
- users://42/profile user_id = "42"
- users://108/profile user_id = "108"
- One template to users://7/profile
- One template to users://42/profile
- One template to users://108/profile
Because a template is not one resource, it is not listed with them. Resources with a fixed URI come from resources/list; templates come from a second method, resources/templates/list, where each entry carries a uriTemplate instead of a uri. A host that shows only the first list will not show your templates, and a client that wants them asks for both lists. The content of a read, on the other hand, carries the concrete URI that was asked for, such as users://42/profile, not the template.
{
"name": "get_user_profile",
"uriTemplate": "users://{user_id}/profile",
"description": "A customer's profile.",
"mimeType": "text/plain"
}True or false?
A server with one fixed resource and one template returns both of them from resources/list.
False. resources/list returns only the fixed resource. The template appears in resources/templates/list, a separate request.
If the URI says {user_id} but the function parameter is called user, the server fails when its module is imported.
True. The SDK compares the URI parameters with the function parameters when the decorator runs and raises a ValueError on a mismatch, long before any read.
Listing is free, reading produces the content
Listing and reading are two separate requests, and only the second one runs your code. When a client asks for the list, the SDK answers from what the decorators recorded: URIs, names, descriptions, media types. None of the resource functions is called. Only when the client reads a URI does the SDK call the one function behind that URI. So a server can offer many resources, even ones that are slow to produce, and listing them still costs nothing.
- Client
- Server
- Client to Server: resources/list
- Server to Client: URIs and names, no function runs
- Client to Server: resources/read config://app
- Server to Client: contents, get_config ran once
A read returns a list called contents, usually with one item. Each item repeats the URI and the media type, and carries the data in one of two forms. Text goes in a text field. Binary data, such as an image, goes in a blob field as base64, a way of writing bytes as plain characters so they fit in a JSON message. The SDK picks the form from what your function returns:
| The function returns | The content | Media type without mime_type= |
|---|---|---|
| str | Text, as returned | text/plain |
| bytes | A base64 blob | text/plain |
| A dict, a list or a model | JSON text | text/plain |
The last column is easy to overlook. The SDK does not guess the media type from the value: a function that returns a dict produces JSON text, but the resource is still labelled text/plain unless the decorator says mime_type="application/json". A host relies on that label to decide how to treat the content, so declare it whenever the content is not plain text.
A client reads a resource with the same calls a test uses. The client code is asynchronous: async with opens the connection and await waits for each answer. Here the client connects to the server object in memory and reads the fixed resource:
from mcp import Client
from mcp.types import TextResourceContents
async def show_config():
async with Client(mcp) as client:
listing = await client.list_resources()
print([r.uri for r in listing.resources])
result = await client.read_resource("config://app")
item = result.contents[0]
if isinstance(item, TextResourceContents):
print(item.mime_type, item.text)Each item in result.contents is either a TextResourceContents, with a .text, or a BlobResourceContents, with a .blob; the isinstance check tells them apart.
Templates are listed with client.list_resource_templates(), whose .resource_templates each have a .uri_template. In Python the names are written with underscores (mime_type), while the JSON on the wire keeps mimeType.
True or false?
When a client lists the resources of a server, the SDK calls each resource function to find out what it returns.
False. The listing comes from what the decorators recorded. A function runs only when its URI is read, and only for that URI.
A resource function that returns a dict is advertised as text/plain unless mime_type is set.
True. The dict becomes JSON text, but the SDK does not infer the media type. The default stays text/plain until you declare application/json.
When a read fails: protocol errors
Every MCP request is a JSON-RPC message, and JSON-RPC gives the answer two possible shapes. A response carries either a result or an error, never both. A failed tool call normally uses the first shape: the call returns a result marked with isError, and its text tells the model what went wrong, so the model can read it and try again. The second shape, a protocol error, means the server rejected the request outright. There is no result at all, only an error code and a message.
{
"jsonrpc": "2.0",
"id": 4,
"result": {
"content": [
{"type": "text", "text": "No task with id 99."}
],
"isError": true
}
}{
"jsonrpc": "2.0",
"id": 5,
"error": {
"code": -32602,
"message": "Unknown resource: config://ap",
"data": {"uri": "config://ap"}
}
}The difference matters for who sees the failure. An error result is content, and the host can hand it to the model like any other result. A protocol error makes the request itself fail, so the client receives an exception instead of something to pass on, and a model is far less likely to recover from it.
Resources have only this second path. If the URI matches no fixed resource and no template, the SDK answers with a protocol error like the second example. It has three parts: the code -32602 (Invalid Params), the message Unknown resource: followed by the URI, and the URI again in data.
The specification requires exactly that for a resource that does not exist, and forbids answering with an empty contents list instead. Older revisions used the code -32002, which you will still see in older material.
A template needs one more step. It matches any value, so users://999/profile reaches your function even if there is no user 999. Only the function knows, and it reports it by raising ResourceNotFoundError, which the SDK turns into the same -32602 error with your message:
from mcp.server.mcpserver.exceptions import ResourceNotFoundError
PROFILES = {"7": "Ada, joined 2024", "42": "Lin, joined 2025"}
@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
"""A customer's profile."""
if user_id not in PROFILES:
raise ResourceNotFoundError(f"No user with id {user_id}.")
return PROFILES[user_id]Any other exception inside a resource function also becomes a protocol error, with code -32603 and a generic message that names only the URI (Error reading resource followed by the URI for a fixed resource, Error creating resource from template followed by the URI for a template), while the details stay in the server log. On the client side, a failed read raises MCPError (from mcp import MCPError), which client code or a test catches to see the code and message.
True or false?
Reading a URI that no resource or template matches returns a result with isError set to true.
False. Resources have no error result. The server answers with a JSON-RPC error, code -32602, and the client raises MCPError.
A template function should raise ResourceNotFoundError for a value it has no data for, rather than return an empty string.
True. The template matches every value, so only the function can tell that the record is missing. Raising the error gives the client a clear -32602 with your message instead of content that looks real.
A protocol error is easier for a model to recover from than an error result, because it carries an error code.
False. It is the other way round. An error result is content the host can show the model; a protocol error makes the request fail, so there is usually nothing for the model to read and retry from.