feat(ai-mcp): send server instructions with createMCPServer #2271
main fromrefs/pull/2271/head·opened View on GitHubcreateMCPServer takes an instructions option. The server sends it to the client as the MCP instructions field.
🎯 Changes#
Why we need it: we serve data tools to MCP hosts from a createMCPServer server, and we want to tell the host's model how the tools relate. MCP has a core field for this, instructions, returned in the initialize result (spec 2025) and the server/discover result (spec 2026-07-28). createMCPServer builds the SDK McpServer with only { name, version } and its capabilities. The SDK keeps instructions in a private field that it sets at construction, so the setup hook cannot set it. Today there is no way to send it.
- New optional
instructions?: stringonMCPServerOptions.buildMcpServerpasses it tonew McpServer(info, { capabilities, instructions }). - Without
instructions, the server sends none, as before. - Docs: "Send instructions to the client" in
docs/mcp/server.md, and the option in thecreateMCPServerdoc comment. - Minor changeset for
@tanstack/ai-mcp.
Not in this PR: SDK PR modelcontextprotocol/typescript-sdk#3066 will add extensions to the McpServer options, and createMCPServer will then need to pass that too.
I found no existing issue or PR for server-side instructions. #1567 is about the client side only.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Testing#
Commands run in packages/ai-mcp:
pnpm vitest run: 54 files, 698 tests passed.pnpm test:types: passed.pnpm test:oxlint: no errors, and no warnings in the changed files.- Repo root:
pnpm test:docs(no broken links) andkiira check docs/mcp/server.md(10 snippets passed). - Not run locally: the root
pnpm test:prand the rest of the E2E suite (onlymcp-typed.spec.tsran). CI runs them. For this reason thetest:prbox is not ticked.
Manual test:
- In
packages/ai-mcp, runpnpm vitest run tests/server/create-server.test.ts -t instructions. - Make sure both tests pass. The first checks
client.getInstructions()on spec 2026 (server/discover), spec 2025 stateless, and spec 2025 memory sessions. The second checks that an omitted option givesundefined. - Remove the
instructions,line inserverOptionsincreate-server.ts, then run step 1 again. The first test fails withexpected undefined to be 'Call echo first.'.
E2E: testing/e2e/tests/mcp-typed.spec.ts has a new test, "a client reads the instructions that the server sends". The e2e stateless server is built with instructions, and a createMCPClient over HTTP reads them from client.instructions. Run with pnpm --filter @tanstack/ai-e2e exec playwright test tests/mcp-typed.spec.ts: 9 passed. With the instructions, line removed from serverOptions, the new test fails (instructions is null) and the other 8 pass.
Risk / rollback#
Low. instructions is opt-in. Without it the server builds as before. Revert the PR to undo.
Public API change#
Before
// No way to send MCP `instructions` from createMCPServer.
const server = createMCPServer({ name: 'notes', version: '1.0.0' })
After
const server = createMCPServer({
name: 'notes',
version: '1.0.0',
instructions: 'Call `search_notes` before `read_note`.',
})- 1
tanstack-source added labelsfeature
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 6fe2901ef47a54887656a102b88fa2f59a6d9b1f
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR