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

server-sessions.md

95 lines3.7 KB

1---
2title: MCP Server Sessions
3id: mcp-server-sessions
4order: 13
5description: "Choose how a createMCPServer server serves spec 2025 clients: with no session, with sessions for input and sampling, or not at all."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - createMCPServer
11 - sticky sessions
12 - spec 2025
13 - mcp-session-id
14---
15​
16MCP spec 2025 sessions live in one process. A multi-instance deploy needs sticky routing for those clients.
17​
18The `sessions` option of `createMCPServer` sets how the server treats spec 2025 clients. By default, it keeps no session. Change it only for one of the cases below.
19​
20## Quick setup
21​
22Pick one value for `sessions`:
23​
24| Value | What you get | Use it when |
25| --- | --- | --- |
26| `'stateless'` (default) | No session. Any instance can answer any request. | You deploy to serverless or to many instances. |
27| `'memory'` | Sessions in the process. The server can ask a spec 2025 client for input or a sample. | A tool calls `requestInput` or `sample` for spec 2025 clients. |
28| `'reject'` | Spec 2026 only. | You do not want to serve spec 2025 clients. |
29​
30To keep sessions, set `sessions` to `'memory'`:
31​
32```ts
33import { createMCPServer } from '@tanstack/ai-mcp/server'
34​
35const server = createMCPServer({
36 name: 'weather',
37 version: '1.0.0',
38 sessions: 'memory',
39})
40```
41​
42Now a spec 2025 client opens a session, and a tool can ask that client for input or a sample.
43​
44## How it works
45​
46With the default `'stateless'` value, a new server answers each spec 2025 request, and no session stays in memory. A spec 2026 client sends no session id either, so any instance can serve both.
47​
48Without a session, the server cannot send a request back to a spec 2025 client. For that client:
49​
50- `ctx.context.requestInput` throws an error that names the `sessions` option.
51- `ctx.context.sample` calls the `sample` option of the server. Without that option, it throws.
52​
53A spec 2026 client gets `requestInput` and `sample` with no session.
54​
55`serveMCPStdio` uses `'memory'` when you do not set `sessions`. A stdio process serves one client, so a session costs nothing there.
56​
57## Turn spec 2025 off
58​
59To serve spec 2026 only, set `sessions` to `'reject'`. A spec 2025 request then gets the SDK rejection. A client that speaks only spec 2025 cannot use that server.
60​
61```ts
62import { createMCPServer } from '@tanstack/ai-mcp/server'
63​
64const server = createMCPServer({
65 name: 'weather',
66 version: '1.0.0',
67 sessions: 'reject',
68})
69```
70​
71## Advanced
72​
73### Route each session to one instance
74​
75A spec 2025 session keeps a live connection object. That object stays in the process that opened the session. Another process cannot read it.
76​
77- The client sends the session id on the `mcp-session-id` header.
78- A request with an unknown session id gets 404 `Session not found`.
79​
80On a deploy with more than one instance, a later request can reach an instance that did not open the session. That request gets 404. Use the `mcp-session-id` header as the key for sticky routing on your load balancer.
81​
82### When a session closes
83​
84A session closes in two cases:
85​
86- The client sends `DELETE` with its session id.
87- The session gets no request for 30 minutes.
88​
89The server looks for idle sessions when a request comes in. After a session closes, the client must open a new session.
90​
91The server has no limit on the number of open sessions. Each session stays in memory until it closes, and that can take 30 minutes. On a public server, set `auth`, or limit new sessions at your proxy.
92​
93### Sessions and auth
94​
95When the server has `auth`, the session belongs to the caller that opened it: the `clientId` of the token plus its `sub` claim. A request from another caller gets 404, the same as an unknown id. [MCP Server Auth](./server-auth) shows how the verifier sets these values.