Open History to see the last commit
1---
2title: Serve Resources and Prompts
3id: mcp-server-content
4order: 13
5description: "Serve documents and prompts from your MCP server, so a host can read a document or start from a prompt."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - mcp server
11 - resources
12 - prompts
13 - resourceDefinition
14 - promptDefinition
15 - createMCPServer
16---
17
18Serve resources and prompts from the same MCP server as your tools. A host can then read those documents and start from those prompts.
19
20Define each document with `resourceDefinition` and each prompt with `promptDefinition`. Then pass them to `createMCPServer`.
21
22## Quick setup
23
24You need an MCP server first. If you do not have one, start with [Serve Tools over HTTP](./server).
25
261. Define a resource for each document.
272. Define a prompt for each prompt.
283. Pass `resources` and `prompts` to `createMCPServer`.
29
30```ts
31import {
32 createMCPServer,
33 promptDefinition,
34 resourceDefinition,
35} from '@tanstack/ai-mcp/server'
36import { z } from 'zod'
37
38const readme = resourceDefinition({
39 name: 'readme',
40 mimeType: 'text/markdown',
41 uri: 'file:///readme.md',
42}).read(async () => ({ text: '# Hello' }))
43
44const file = resourceDefinition({
45 name: 'file',
46 mimeType: 'text/plain',
47 uriTemplate: 'file:///{path}',
48 argsSchema: z.object({ path: z.string() }),
49}).read(async (_uri, { path }) => ({ text: `The body of ${path}` }))
50
51const summarize = promptDefinition({
52 name: 'summarize',
53 description: 'Summarize a topic',
54 argsSchema: z.object({ topic: z.string() }),
55}).render(async (args) => [{ role: 'user', content: args.topic }])
56
57const server = createMCPServer({
58 name: 'library',
59 version: '1.0.0',
60 resources: [readme, file],
61 prompts: [summarize],
62})
63
64export function handleMcp(request: Request) {
65 return server.fetch(request)
66}
67```
68
69Now the host gets these results:
70
71- It reads `file:///readme.md`. The `text` is `# Hello`.
72- It reads `file:///notes.md`. The `text` is `The body of notes.md`.
73- It starts from the `summarize` prompt with the topic `weather`. It gets one message: the `role` is `user`, and the `content` is `weather`.
74
75## How it works
76
77A resource has one of two addresses:
78
79- `uri`: one document, for example `file:///readme.md`.
80- `uriTemplate`: a URI pattern for many documents, for example `file:///{path}`.
81
82`read` returns the body of the document:
83
84- `{ text }` for a text document.
85- `{ blob }` with a base64 string for a binary document.
86
87A 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.
88
89## Serve one resource per item
90
91A 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.
92
931. Read `itemId` from the `variables` argument of `read`.
942. Read the user from `ctx.context`.
953. Add `list` to return the real resources for `resources/list`.
96
97```ts
98import { createMCPServer, resourceDefinition } from '@tanstack/ai-mcp/server'
99
100const summaries = new Map([
101 ['1', 'First item'],
102 ['2', 'Second item'],
103])
104
105const summary = resourceDefinition({
106 name: 'item-summary',
107 mimeType: 'text/plain',
108 uriTemplate: 'myapp://items/{itemId}/summary',
109 list: async () => ({
110 resources: [...summaries.keys()].map((id) => ({
111 uri: `myapp://items/${id}/summary`,
112 name: `Item ${id}`,
113 })),
114 }),
115}).read(async (uri, variables, ctx) => {
116 const itemId = String(variables.itemId)
117 const userId = String(ctx.context.userId)
118 return { text: `${summaries.get(itemId) ?? 'Unknown'} (for ${userId})` }
119})
120
121const server = createMCPServer({
122 name: 'items',
123 version: '1.0.0',
124 resources: [summary],
125})
126
127export function handleMcp(request: Request, userId: string) {
128 return server.handle(request, { context: { userId } })
129}
130```
131
132The host lists `Item 1` and `Item 2`. A read of `myapp://items/1/summary` returns `First item` plus the user id.
133
134## Advanced
135
136### The arguments of `read`
137
138`read` gets three arguments:
139
140- `uri`: the URI that the host asked for, as a `URL`.
141- `variables`: the values from the template. A resource with `uri` gets `{}`. When the template has `argsSchema`, `argsSchema.parse` runs first, so `read` gets the parsed values. For `file:///notes.md`, `path` is `notes.md`.
142- `ctx.context`: the values from `handle(request, { context })`, plus the verified token as `authInfo`. A tool gets the same values, plus `requestInput` and `sample`.
143
144`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`.
145
146### Resource rules
147
148- If the resource has no `uri` and no `uriTemplate`, `resourceDefinition` throws `This resource has no uri and no uriTemplate. Pass a uri or a uriTemplate.`
149- If you pass `uri` and `uriTemplate`, the server uses `uri`.
150- When one template serves files of different types, add `mimeType` to the object that `read` returns.
151
152### Prompt rules
153
154- `argsSchema.parse` runs first. Then `render` receives the parsed arguments.
155- If `role` is not `user` or `assistant`, the server sends that message as `user`.