Resources
MCP Resources: Data AI Can Read
Resources represent data that AI can read — files, database records, configuration, API responses. Unlike tools (which perform actions), resources are read-only data sources.
typescript
// Static resource - fixed URI
server.resource(
"config",
"config://app",
async () => ({
contents: [{
uri: "config://app",
mimeType: "application/json",
text: JSON.stringify({ theme: "dark", version: "2.0" }),
}],
})
);
// Dynamic resource with template
server.resource(
"user-profile",
new ResourceTemplate("users://{userId}", { list: undefined }),
async (uri, { userId }) => ({
contents: [{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify(await db.users.findById(userId)),
}],
})
);Tip:Use resources for data the AI needs to READ. Use tools for actions the AI needs to PERFORM. This separation keeps your server clean and secure.
PreviewTypeScriptRead Only
Copy this code and run it locally with
npx tsx server.ts