Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

server-content.md

92 lines2.5 KB

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​
18Until you serve a resource, a host cannot read your document. Until you serve a prompt, a host cannot start from that prompt.
19​
20## Define Resources and Prompts
21​
221. Define each document with `resourceDefinition`.
232. Define each prompt with `promptDefinition`.
243. Pass `resources` and `prompts` to `createMCPServer`.
25​
26```ts
27import {
28 createMCPServer,
29 promptDefinition,
30 resourceDefinition,
31} from '@tanstack/ai-mcp/server'
32import { z } from 'zod'
33​
34const readme = resourceDefinition({
35 name: 'readme',
36 mimeType: 'text/markdown',
37 uri: 'file:///readme.md',
38}).read(async () => ({ text: '# Hello' }))
39​
40const file = resourceDefinition({
41 name: 'file',
42 mimeType: 'text/plain',
43 uriTemplate: 'file:///{path}',
44}).read(async () => ({ text: 'file body' }))
45​
46const summarize = promptDefinition({
47 name: 'summarize',
48 description: 'Summarize a topic',
49 argsSchema: z.object({ topic: z.string() }),
50}).render(async (args) => [{ role: 'user', content: args.topic }])
51​
52const server = createMCPServer({
53 name: 'library',
54 version: '1.0.0',
55 resources: [readme, file],
56 prompts: [summarize],
57})
58​
59export function handleMcp(request: Request) {
60 return server.fetch(request)
62```
63​
64## Resources
65​
66A resource must have `uri` or `uriTemplate`.
67​
68- `uri`: one document. An example is `file:///readme.md`.
69- `uriTemplate`: a URI pattern. An example is `file:///{path}`.
70​
71If the resource has no `uri` and no `uriTemplate`, `resourceDefinition` throws `This resource has no uri and no uriTemplate. Pass a uri or a uriTemplate.`
72​
73If you pass `uri` and `uriTemplate`, the server uses `uri`.
74​
75`read` takes no arguments. `read` returns `{ text }` for a text document. For a binary document, `read` returns `{ blob }` with a base64 string.
76​
77## Prompts
78​
79`argsSchema.parse` runs first. Then `render` receives the parsed arguments.
80​
81`render` returns an array of messages. Each message has these fields:
82​
83- `role`: `user` or `assistant`.
84- `content`: the message text.
85​
86If `role` is not `user` or `assistant`, the server sends that message as `user`.
87​
88## What the Host Gets
89​
90The host reads `file:///readme.md`. The `text` is `# Hello`.
91​
92The host starts from the `summarize` prompt with topic `weather`. The `role` is `user`. The `content` string is `weather`.