Resources
🎯 Introduction
An AI customer support application needs more than functions such as checking stock and creating orders. It may also need to read refund policies and product manuals so the model can use them as reference material when answering questions.
MCP Resources give applications a standard way to discover and read this material. Using a refund policy and product manuals, this article covers registering resources with fixed URIs and dynamic resources, then reading them through an MCP client.
🧱 What Is a Resource?
Suppose a user asks, “How long after a purchase can I request a refund?” The application first needs the company's refund policy:
Refunds can be requested within 7 days of purchase. Sale items are not refundable.
We can register this material as an MCP resource with the address shop://policy/refund. This address is a URI that identifies the resource. A client uses the URI to read its content. It does not have to be a page that a browser can open, and it does not require an HTTP endpoint with the same address.
Resources and Tools
Tools can also return a refund policy. Why use Resources?
A customer support application could retrieve the policy in either of two ways:
- With Tools: provide a policy lookup tool to the model, let the model request a call, then execute the tool and pass the result to the model.
- With Resources: have the application read the policy directly, for example when the user opens an after-sales support page, then provide it to the model along with the user's question.
Both approaches can retrieve the same data. They differ in how the application uses it.
| Aspect | Tools | Resources |
|---|---|---|
| Design purpose | Provide callable functions | Provide readable material |
| Typical usage | The model requests a tool call | The application selects and reads a resource |
| Identified by | Tool name and arguments | Resource URI |
| Common scenarios | Check stock, create orders | Product manuals, policies, project documentation |
These usage patterns are not restrictions imposed by the protocol. An application can also call tools directly, and the host still decides whether resource content is included in the model's context.
🛠 Resources with Fixed URIs
1. Register a Resource
The refund policy has a single known address, so we can register it with a fixed URI.
The SDK's registerResource() method takes a resource name, URI, metadata, and a read callback.
In the existing 02.first-server.mjs, add the following inside createServer(), before return server;:
server.registerResource(
'refund-policy',
'shop://policy/refund',
{
description: 'Product refund policy',
mimeType: 'text/plain',
},
async uri => {
return {
contents: [
{
uri: uri.href,
mimeType: 'text/plain',
text: 'Refunds can be requested within 7 days of purchase. Sale items are not refundable.',
},
],
};
}
);
The third argument describes the resource. mimeType: "text/plain" indicates plain text. The fourth argument is the read callback, which the SDK runs only when a client requests this URI.
The read callback must return a contents array. Each element represents one item of resource content:
| Property | Purpose |
|---|---|
uri | The resource URI for this content |
mimeType | The content type |
text | The text that was read |
Resources return their content in contents, whereas tools use content. These are different result structures.
2. List Resources
How does a client find out which material a server provides? Use listResources(), which corresponds to the MCP resources/list request.
Restart the MCP server. In the existing 02.first-client.mjs, keep the connection and cleanup code, and replace the tool call and result-handling code after await client.connect(transport) inside try with:
const result = await client.listResources();
console.log(JSON.stringify(result, null, 4));
Run the client:
node 02.first-client.mjs
Expected output (key fields only; the SDK may return additional metadata):
{
"resources": [
{
"name": "refund-policy",
"uri": "shop://policy/refund",
"description": "Product refund policy",
"mimeType": "text/plain"
}
]
}
The list contains resource metadata and addresses, not the refund policy's text. The application can use this information to choose which resources to read.
3. Read Resource Content
Once you know the resource URI, use readResource() to read its content. This corresponds to the MCP resources/read request.
Replace the listResources() call and logging code inside the client's try block with:
const result = await client.readResource({
uri: 'shop://policy/refund',
});
console.log(JSON.stringify(result, null, 4));
Run it again:
node 02.first-client.mjs
Expected output:
{
"contents": [
{
"uri": "shop://policy/refund",
"mimeType": "text/plain",
"text": "Refunds can be requested within 7 days of purchase. Sale items are not refundable."
}
]
}
The client now has the refund policy's text, accessible through result.contents[0].text. No model call is needed in this article.
🧰 Dynamic Resources
1. Register a Resource Template
If every product has a manual, there will be multiple resource URIs, such as shop://products/P100/manual and shop://products/P200/manual.
Only the product ID differs, so there is no need to write separate registration code for each product. Use ResourceTemplate to define the URI template shop://products/{id}/manual, allowing one read callback to return the appropriate manual for an ID.
First, update the first import at the top of 02.first-server.mjs to include ResourceTemplate:
import { McpServer, ResourceTemplate, createMcpHandler } from '@modelcontextprotocol/server';
To focus on resource templates, this example uses two manual strings directly. Add the following inside createServer(), before return server;, keeping the registered refund policy:
const manuals = {
P100: 'Mechanical Keyboard: Connect it to your computer via USB. Keep liquids away from the keyboard.',
P200: 'Wireless Mouse: Install the battery, then connect it to your computer using the USB receiver.',
};
server.registerResource(
'product-manual',
new ResourceTemplate('shop://products/{id}/manual', {
list: undefined,
}),
{
description: 'Read the manual for a specified product',
mimeType: 'text/plain',
},
async (uri, { id }) => {
if (typeof id !== 'string' || !Object.hasOwn(manuals, id)) {
throw new Error('Product manual not found');
}
return {
contents: [
{
uri: uri.href,
mimeType: 'text/plain',
text: manuals[id],
},
],
};
}
);
{id} is a variable in the URI template. When a client reads shop://products/P100/manual, the SDK matches the template and passes P100 as id to the read callback, which looks up the manual.
list: undefined means this example does not enumerate all individual manual URIs. Applications can still discover the URI template and construct addresses using known product IDs.
Save 02.first-server.mjs, stop the process, and run node 02.first-server.mjs again to activate the new resource template.
2. List Resource Templates
Because this example does not enumerate individual manual URIs, listResources() only discovers the refund policy. Use listResourceTemplates(), corresponding to resources/templates/list, to retrieve the manual URI template.
Replace the resource-reading and logging code inside the client's try block with:
const result = await client.listResourceTemplates();
console.log(JSON.stringify(result, null, 4));
Run:
node 02.first-client.mjs
Expected output (key fields only):
{
"resourceTemplates": [
{
"name": "product-manual",
"uriTemplate": "shop://products/{id}/manual",
"description": "Read the manual for a specified product",
"mimeType": "text/plain"
}
]
}
The client receives an address template, not the manuals for all products.
3. Read a Dynamic Resource
Suppose the user is viewing product P100. The application already knows its ID and can substitute it for {id} to construct a concrete URI.
Replace the template-listing and logging code inside the client's try block with:
const id = 'P100';
const result = await client.readResource({
uri: `shop://products/${id}/manual`,
});
console.log(JSON.stringify(result, null, 4));
Run:
node 02.first-client.mjs
Expected output:
{
"contents": [
{
"uri": "shop://products/P100/manual",
"mimeType": "text/plain",
"text": "Mechanical Keyboard: Connect it to your computer via USB. Keep liquids away from the keyboard."
}
]
}
Both fixed-URI and dynamic resources are read with readResource(). The difference is whether the resource address is registered directly or matched by a template.
References: MCP SDK Resources documentation, client calling documentation.
❓ Knowledge Check
Q1: Must a resource with a fixed URI always have the same content?
No. “Fixed” refers to the URI, not the content. For example, the read callback for shop://policy/refund could fetch the latest refund policy from a database.
Q2: Should a missing product manual return isError: true, as a tool would?
No. Resources and tools report failures differently. When a resource read fails, readResource() rejects its promise with a request error. The client can handle it with try/catch rather than checking a tool result's isError field.
🧪 Exercise
Inside createServer() in 02.first-server.mjs, add a shipping policy resource with the URI shop://policy/shipping and the text “Orders ship within 48 hours of payment.” Restart the server, then use the Node.js client to verify that the URI appears in the resource list and returns the correct text.
🎉 You now know how to register, discover, and read resources with fixed URIs and dynamic resources.
