Resource Templates

Loading "Resource Templates"
πŸ‘¨β€πŸ’Ό Now that you've registered a static resource, let's make things more dynamic! In this step, you'll use MCP's resource templates to expose a whole family of resourcesβ€”each with its own unique URI and data.
Resource templates let you define parameterized resources, like epicme://entries/{id} or epicme://tags/{id}. This means clients can discover and read individual entries or tags by their unique identifiers, just like accessing a file by its path.
Here's a dynamic "hello world" resource template example that reads names from a database:
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'

agent.server.registerResource(
	'hello',
	new ResourceTemplate('hello://{name}', {
		// this is required to be specified, but we'll implement it later...
		list: undefined,
	}),
	{
		title: 'Hello',
		description: 'Say hello to anyone by name!',
	},
	async (uri, { name }) => {
		return {
			contents: [
				{
					mimeType: 'text/plain',
					text: `Hello, ${name}!`,
					uri: uri.toString(),
				},
			],
		}
	},
)
Your goal in this step:
  • Use resource templates to expose entries and tags from your database, each accessible by a unique URI.
Test this out by clicking the "List Templates" button in the MCP Inspector.

Please set the playground first

Loading "Resource Templates"
Loading "Resource Templates"

Access Denied

You must login or register for the workshop to view and run the tests.

Check out this video to see how the test tab works.