Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai-mcp): run a tool's own input schema before it executes #2309

OpenPHLIH(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2309/headopened View on GitHub

Showing 8 changed files with 22 additions and 2 deletions (loading 5).

x
0 of 8 viewed, 8 left
5 additions and 0 deletions.changeset/ai-mcp-input-schema.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai-mcp': patch
+---
+​
+`createMCPServer` runs a tool's own `inputSchema` before the tool executes, so a transform and a default apply to the input the tool receives, as they already did under `chat()`. The MCP SDK checks the JSON Schema view of the schema, which cannot carry either. An input that the schema refuses now answers as a tool error (`isError`) naming the failed paths, instead of running the tool on the raw arguments.
1 addition and 0 deletionsdocs/mcp/server.mdModified
View file
@@ -93,6 +93,7 @@
​
- You create the server once. `handleMcp` calls `server.fetch` for each request.
- The server speaks MCP over HTTP. It finds the tool by name, parses the input with `inputSchema`, and runs your `.server()` function.
+- The input reaches the tool through its own `inputSchema`, as it does under `chat()`: a transform or a default applies, and an input that the schema refuses answers as a tool error (`isError`) naming the failed paths.
- By default the server keeps no session. As a result, any instance can answer any request, and a serverless host works with no extra setup.
​
To call this server from your own `chat()` route, see [MCP Server Tools](../tools/mcp).
16 additions and 2 deletionspackages/ai-mcp/src/server/create-server.tsModified
View file
@@ -33,6 +33,7 @@
inputDeclinedMessage,
} from './context'
import type { SampleRequest, ToolInputRequest } from './context'
+import { ToolInputError, parseToolInput } from './input'
import { isServedOverStdio, rememberServerOptions } from './registry'
import { parseToolOutput } from './output'
import type { MCPResourceContext, MCPResourceList } from './definitions'
@@ -551,8 +552,21 @@