AI Hints
Home›MCP Tutorial›Resources
MCP · Lesson 32 of 120

Resource Templates

One pattern, many URIs.

⏱ 2 min read▶ Video at 29:15🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

Northwind has three stores. Instead of three resources, we write one template, store colon slash slash, then a city in curly braces. Any city fills the pattern.

Resource Templates
From the video: Resource Templates
  • Three stores
  • One template
  • {city} fills it

A store template

The URI has a city parameter in braces. The function takes the same parameter. The client lists the templates, then reads two stores. Run it. One template serves both York and Leeds.

template.pypython
import asyncio
from mcp import Client
from mcp.server.mcpserver import MCPServer

mcp = MCPServer("northwind")
HOURS = {"York": "Mon to Sat, 9am to 5:30pm", "Leeds": "Mon to Sun, 9am to 6pm"}

@mcp.resource("store://{city}")
def store(city: str) -> str:
    """Opening hours for one Northwind store."""
    return f"{city}: {HOURS.get(city, 'no store here')}"

async def main():
    async with Client(mcp) as client:
        print([t.uri_template for t in (await client.list_resource_templates()).resource_templates])
        for city in ["York", "Leeds"]:
            print((await client.read_resource(f"store://{city}")).contents[0].text)

asyncio.run(main())
Output
['store://{city}']
York: Mon to Sat, 9am to 5:30pm
Leeds: Mon to Sun, 9am to 6pm

Templates

Templates use URI patterns with parameters in braces. They're listed separately from fixed resources. The client fills in the parameters, often helped by completions, which we'll see later.

keyvalue
Patternstore://{city}
Functiontakes city
Listed bylist_resource_templates
Filled bythe client

Summary

A resource template is one pattern that serves many URIs. Next, JSON resources.

Key takeaways
  • {param}
  • One function
  • Many URIs
Next up: JSON Resources — mime types