MCPcopy Create free account
hub / github.com/modelcontextprotocol/typescript-sdk / validateToolInput

Method validateToolInput

src/server/mcp.ts:258–282  ·  view source on GitHub ↗

* Validates tool input arguments against the tool's input schema.

(tool: Tool, args: Args, toolName: string)

Source from the content-addressed store, hash-verified

256 * Validates tool input arguments against the tool's input schema.
257 */
258 private async validateToolInput<
259 Tool extends RegisteredTool,
260 Args extends Tool['inputSchema'] extends infer InputSchema
261 ? InputSchema extends AnySchema
262 ? SchemaOutput<InputSchema>
263 : undefined
264 : undefined
265 >(tool: Tool, args: Args, toolName: string): Promise<Args> {
266 if (!tool.inputSchema) {
267 return undefined as Args;
268 }
269
270 // Try to normalize to object schema first (for raw shapes and object schemas)
271 // If that fails, use the schema directly (for union/intersection/etc)
272 const inputObj = normalizeObjectSchema(tool.inputSchema);
273 const schemaToParse = inputObj ?? (tool.inputSchema as AnySchema);
274 const parseResult = await safeParseAsync(schemaToParse, args);
275 if (!parseResult.success) {
276 const error = 'error' in parseResult ? parseResult.error : 'Unknown error';
277 const errorMessage = getParseErrorMessage(error);
278 throw new McpError(ErrorCode.InvalidParams, `Input validation error: Invalid arguments for tool ${toolName}: ${errorMessage}`);
279 }
280
281 return parseResult.data as unknown as Args;
282 }
283
284 /**
285 * Validates tool output against the tool's output schema.

Callers 2

Calls 3

normalizeObjectSchemaFunction · 0.85
safeParseAsyncFunction · 0.85
getParseErrorMessageFunction · 0.85

Tested by

no test coverage detected