You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MCP servers can request LLM completions from connected clients that support the sampling capability. This lets your tools offload summarisation or generation to the client’s model.
4
+
5
+
For a runnable server that combines tools, logging and tasks, see:
- Call `server.server.createMessage(...)` from within a tool handler.
13
+
- Return the model’s response as structured content and/or text.
14
+
15
+
Refer to the MCP spec’s sampling section for full request/response details.
16
+
17
+
## Elicitation
18
+
19
+
### Form elicitation
20
+
21
+
Form elicitation lets a tool ask the user for additional, **non‑sensitive** information via a schema‑driven form. The server sends a schema and message, and the client is responsible for collecting and returning the data.
The `simpleStreamableHttp` server also includes a `collect-user-info` tool that demonstrates how to drive elicitation from a tool and handle the response.
29
+
30
+
### URL elicitation
31
+
32
+
URL elicitation is designed for sensitive data and secure web‑based flows (e.g., collecting an API key, confirming a payment, or doing third‑party OAuth). Instead of returning form data, the server asks the client to open a URL and the rest of the flow happens in the browser.
- Use `mode: 'url'` when calling `server.server.elicitInput(...)`.
42
+
- Implement a client‑side handler for `ElicitRequestSchema` that:
43
+
- Shows the full URL and reason to the user.
44
+
- Asks for explicit consent.
45
+
- Opens the URL in the system browser.
46
+
47
+
Sensitive information **must not** be collected via form elicitation; always use URL elicitation or out‑of‑band flows for secrets.
48
+
49
+
## Task-based execution (experimental)
50
+
51
+
Task-based execution enables “call-now, fetch-later” patterns for long-running operations. Instead of returning a result immediately, a tool creates a task that can be polled or resumed later.
52
+
53
+
The APIs live under the experimental `.experimental.tasks` namespace and may change without notice.
54
+
55
+
### Server-side concepts
56
+
57
+
On the server you will:
58
+
59
+
- Provide a `TaskStore` implementation that persists task metadata and results.
60
+
- Enable the `tasks` capability when constructing the server.
61
+
- Register tools with `server.experimental.tasks.registerToolTask(...)`.
62
+
63
+
For a runnable example that uses the in-memory store shipped with the SDK, see:
See `src/examples/client/simpleStreamableHttp.ts` for an interactive CLI client that exercises these methods and shows how to handle notifications, elicitation and tasks.
28
+
See [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts) for an interactive CLI client that exercises these methods and shows how to handle notifications, elicitation and tasks.
29
29
30
30
## Transports and backwards compatibility
31
31
@@ -36,7 +36,7 @@ To support both modern Streamable HTTP and legacy SSE servers, use a client that
@@ -8,11 +8,11 @@ This SDK lets you build MCP servers in TypeScript and connect them to different
8
8
9
9
For a complete, runnable example server, see:
10
10
11
-
-`src/examples/server/simpleStreamableHttp.ts` – feature‑rich Streamable HTTP server
12
-
-`src/examples/server/jsonResponseStreamableHttp.ts` – Streamable HTTP with JSON response mode
13
-
-`src/examples/server/simpleStatelessStreamableHttp.ts` – stateless Streamable HTTP server
14
-
-`src/examples/server/simpleSseServer.ts` – deprecated HTTP+SSE transport
15
-
-`src/examples/server/sseAndStreamableHttpCompatibleServer.ts` – backwards‑compatible server for old and new clients
11
+
-[`simpleStreamableHttp.ts`](../src/examples/server/simpleStreamableHttp.ts) – feature‑rich Streamable HTTP server
12
+
-[`jsonResponseStreamableHttp.ts`](../src/examples/server/jsonResponseStreamableHttp.ts) – Streamable HTTP with JSON response mode
13
+
-[`simpleStatelessStreamableHttp.ts`](../src/examples/server/simpleStatelessStreamableHttp.ts) – stateless Streamable HTTP server
14
+
-[`simpleSseServer.ts`](../src/examples/server/simpleSseServer.ts) – deprecated HTTP+SSE transport
15
+
-[`sseAndStreamableHttpCompatibleServer.ts`](../src/examples/server/sseAndStreamableHttpCompatibleServer.ts) – backwards‑compatible server for old and new clients
16
16
17
17
## Transports
18
18
@@ -27,9 +27,9 @@ Streamable HTTP is the modern, fully featured transport. It supports:
-[`jsonResponseStreamableHttp.ts`](../src/examples/server/jsonResponseStreamableHttp.ts) – `enableJsonResponse: true`, no SSE
32
+
-[`standaloneSseWithGetStreamableHttp.ts`](../src/examples/server/standaloneSseWithGetStreamableHttp.ts) – notifications with Streamable HTTP GET + SSE
- Stateful with resumability: [`simpleStreamableHttp.ts`](../src/examples/server/simpleStreamableHttp.ts)
48
48
49
49
### Deprecated HTTP + SSE
50
50
51
51
The older HTTP+SSE transport (protocol version 2024‑11‑05) is supported only for backwards compatibility. New implementations should prefer Streamable HTTP.
1. Start from `src/examples/server/simpleStreamableHttp.ts`.
63
+
1. Start from [`simpleStreamableHttp.ts`](../src/examples/server/simpleStreamableHttp.ts).
64
64
2. Remove features you do not need (tasks, advanced logging, OAuth, etc.).
65
65
3. Register your own tools, resources and prompts.
66
66
67
67
For more detailed patterns (stateless vs stateful, JSON response mode, CORS, DNS rebind protection), see the examples above and the MCP spec sections on transports.
68
68
69
+
## Tools, resources, and prompts
70
+
71
+
### Tools
72
+
73
+
Tools let MCP clients ask your server to take actions. They are usually the main way that LLMs call into your application.
74
+
75
+
A typical registration with `registerTool` looks like this:
Tools can return `resource_link` content items to reference large resources without embedding them directly, allowing clients to fetch only what they need.
107
+
108
+
The README’s `list-files` example shows the pattern conceptually; for concrete usage, see the Streamable HTTP examples in `src/examples/server`.
109
+
110
+
### Resources
111
+
112
+
Resources expose data to clients, but should not perform heavy computation or side‑effects. They are ideal for configuration, documents, or other reference data.
Prompts are reusable templates that help humans (or client UIs) talk to models in a consistent way. They are declared on the server and listed through MCP.
138
+
139
+
A minimal prompt:
140
+
141
+
```typescript
142
+
server.registerPrompt(
143
+
'review-code',
144
+
{
145
+
title: 'Code Review',
146
+
description: 'Review code for best practices and potential issues',
Both prompts and resources can support argument completions. On the client side, you use `client.complete()` with a reference to the prompt or resource and the partially‑typed argument.
170
+
171
+
See the MCP spec sections on prompts and resources for complete details, and [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts) for client‑side usage patterns.
172
+
173
+
### Display names and metadata
174
+
175
+
Tools, resources and prompts support a `title` field for human‑readable names. Older APIs can also attach `annotations.title`. To compute the correct display name on the client, use:
176
+
177
+
-`getDisplayName` from `@modelcontextprotocol/sdk/shared/metadataUtils.js`
178
+
69
179
## Multi‑node deployment patterns
70
180
71
-
The SDK supports multi‑node deployments using Streamable HTTP. The high‑level patterns are documented in `src/examples/README.md`:
181
+
The SDK supports multi‑node deployments using Streamable HTTP. The high‑level patterns are documented in [`README.md`](../src/examples/README.md):
72
182
73
183
- Stateless mode (any node can handle any request)
74
184
- Persistent storage mode (shared database for session state)
75
185
- Local state with message routing (message queue + pub/sub)
76
186
77
-
Those deployment diagrams are kept in `src/examples/README.md` so the examples and documentation stay aligned.
187
+
Those deployment diagrams are kept in [`README.md`](../src/examples/README.md) so the examples and documentation stay aligned.
0 commit comments