Resource Templates List
Resource Templates List
👨💼 Our users want to be able to see a list of all tags. So let's implement the
list callback for our resource templates.import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
agent.server.registerResource(
'hello',
new ResourceTemplate('hello://{name}', {
list: async () => {
// Imagine this is a call to your database to get all names
const names = await db.getAllNames()
return {
resources: names.map((name) => ({
name,
uri: `hello://${name}`,
// NOTE: this mimeType is whatever the client should expect if it retrieves the resource
mimeType: 'text/plain',
// note this does not include the contents of the resource
})),
}
},
}),
// ...
)
Notice how the
list callback queries the database and returns a resource
listing for each name. This pattern is exactly what you'll use to expose tags
from your own database.The reason for the
list callback is to make it easy for clients to get a
list of available resources without having to know the exact URIs and without
having to request the entire resource (notice we only return the name, URI,
and type in the list callback).To test this out, click the "List Resources" button in the MCP Inspector.