chore: infer tool params (#241)

Moves the `schema.parse` call to the calling side of the handler, so we
don't have to duplicate it everywhere.
This commit is contained in:
Simon Knott
2025-04-22 13:24:38 +02:00
committed by GitHub
parent 9578a5b2af
commit c80f7cf222
20 changed files with 212 additions and 273 deletions

View File

@@ -15,17 +15,19 @@
*/
import type { ImageContent, TextContent } from '@modelcontextprotocol/sdk/types';
import type { JsonSchema7Type } from 'zod-to-json-schema';
import type { z } from 'zod';
import type { Context } from '../context';
import type * as playwright from 'playwright';
export type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files' | 'install';
export type ToolSchema = {
export type ToolSchema<Input extends InputType> = {
name: string;
description: string;
inputSchema: JsonSchema7Type;
inputSchema: Input;
};
type InputType = z.Schema;
export type FileUploadModalState = {
type: 'fileChooser';
description: string;
@@ -50,11 +52,15 @@ export type ToolResult = {
resultOverride?: ToolActionResult;
};
export type Tool = {
export type Tool<Input extends InputType = InputType> = {
capability: ToolCapability;
schema: ToolSchema;
schema: ToolSchema<Input>;
clearsModalState?: ModalState['type'];
handle: (context: Context, params?: Record<string, any>) => Promise<ToolResult>;
handle: (context: Context, params: z.output<Input>) => Promise<ToolResult>;
};
export type ToolFactory = (snapshot: boolean) => Tool;
export type ToolFactory = (snapshot: boolean) => Tool<any>;
export function defineTool<Input extends InputType>(tool: Tool<Input>): Tool<Input> {
return tool;
}