Serve resources and prompts from the same MCP server as your tools. A host can then read those documents and start from those prompts.
Define each document with resourceDefinition and each prompt with promptDefinition. Then pass them to createMCPServer.
Quick setup#
You need an MCP server first. If you do not have one, start with Serve Tools over HTTP.
- Define a resource for each document.
- Define a prompt for each prompt.
- Pass
resourcesandpromptstocreateMCPServer.
import {
createMCPServer,
promptDefinition,
resourceDefinition,
} from '@tanstack/ai-mcp/server'
import { z } from 'zod'
const readme = resourceDefinition({
name: 'readme',
mimeType: 'text/markdown',
uri: 'file:///readme.md',
}).read(async () => ({ text: '# Hello' }))
const file = resourceDefinition({
name: 'file',
mimeType: 'text/plain',
uriTemplate: 'file:///{path}',
argsSchema: z.object({ path: z.string() }),
}).read(async (_uri, { path }) => ({ text: `The body of ${path}` }))
const summarize = promptDefinition({
name: 'summarize',
description: 'Summarize a topic',
argsSchema: z.object({ topic: z.string() }),
}).render(async (args) => [{ role: 'user', content: args.topic }])
const server = createMCPServer({
name: 'library',
version: '1.0.0',
resources: [readme, file],
prompts: [summarize],
})
export function handleMcp(request: Request) {
return server.fetch(request)
}
Now the host gets these results:
- It reads
file:///readme.md. Thetextis# Hello. - It reads
file:///notes.md. ThetextisThe body of notes.md. - It starts from the
summarizeprompt with the topicweather. It gets one message: theroleisuser, and thecontentisweather.
How it works#
A resource has one of two addresses:
uri: one document, for examplefile:///readme.md.uriTemplate: a URI pattern for many documents, for examplefile:///{path}.
read returns the body of the document:
{ text }for a text document.{ blob }with a base64 string for a binary document.
A prompt parses its arguments with argsSchema. Then render returns an array of messages. Each message has a role (user or assistant) and a content string.
Serve one resource per item#
A template such as myapp://items/{itemId}/summary serves many documents. The read must know which item the host asked for, and which user asks. A host also needs a list of the items that exist.
- Read
itemIdfrom thevariablesargument ofread. - Read the user from
ctx.context. - Add
listto return the real resources forresources/list.
import { createMCPServer, resourceDefinition } from '@tanstack/ai-mcp/server'
const summaries = new Map([
['1', 'First item'],
['2', 'Second item'],
])
const summary = resourceDefinition({
name: 'item-summary',
mimeType: 'text/plain',
uriTemplate: 'myapp://items/{itemId}/summary',
list: async () => ({
resources: [...summaries.keys()].map((id) => ({
uri: `myapp://items/${id}/summary`,
name: `Item ${id}`,
})),
}),
}).read(async (uri, variables, ctx) => {
const itemId = String(variables.itemId)
const userId = String(ctx.context.userId)
return { text: `${summaries.get(itemId) ?? 'Unknown'} (for ${userId})` }
})
const server = createMCPServer({
name: 'items',
version: '1.0.0',
resources: [summary],
})
export function handleMcp(request: Request, userId: string) {
return server.handle(request, { context: { userId } })
}
The host lists Item 1 and Item 2. A read of myapp://items/1/summary returns First item plus the user id.
Advanced#
The arguments of read#
read gets three arguments:
uri: the URI that the host asked for, as aURL.variables: the values from the template. A resource withurigets{}. When the template hasargsSchema,argsSchema.parseruns first, soreadgets the parsed values. Forfile:///notes.md,pathisnotes.md.ctx.context: the values fromhandle(request, { context }), plus the verified token asauthInfo. A tool gets the same values, plusrequestInputandsample.
list gets the same ctx. It returns { resources }, with a uri and a name for each resource. Only a resource with uriTemplate can have list.
Resource rules#
- If the resource has no
uriand nouriTemplate,resourceDefinitionthrowsThis resource has no uri and no uriTemplate. Pass a uri or a uriTemplate. - If you pass
urianduriTemplate, the server usesuri. - When one template serves files of different types, add
mimeTypeto the object thatreadreturns.
Prompt rules#
argsSchema.parseruns first. Thenrenderreceives the parsed arguments.- If
roleis notuserorassistant, the server sends that message asuser.