diff --git a/src/client/index.ts b/src/client/index.ts index e256b4ed..c3662b29 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -6,6 +6,7 @@ import { ClientNotification, ClientRequest, ClientResult, + CompatibilityCallToolResultSchema, CompleteRequest, CompleteResultSchema, EmptyResultSchema, @@ -218,11 +219,12 @@ export class Client< async callTool( params: CallToolRequest["params"], + resultSchema: typeof CallToolResultSchema | typeof CompatibilityCallToolResultSchema = CallToolResultSchema, onprogress?: ProgressCallback, ) { return this.request( { method: "tools/call", params }, - CallToolResultSchema, + resultSchema, onprogress, ); } diff --git a/src/shared/protocol.ts b/src/shared/protocol.ts index fdb117ca..22d2503f 100644 --- a/src/shared/protocol.ts +++ b/src/shared/protocol.ts @@ -1,4 +1,4 @@ -import { AnyZodObject, ZodLiteral, ZodObject, z } from "zod"; +import { ZodLiteral, ZodObject, ZodType, z } from "zod"; import { ErrorCode, JSONRPCError, @@ -250,7 +250,7 @@ export class Protocol< * * Do not use this method to emit notifications! Use notification() instead. */ - request( + request>( request: SendRequestT, resultSchema: T, onprogress?: ProgressCallback, diff --git a/src/types.ts b/src/types.ts index caf05785..f932699e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -722,6 +722,13 @@ export const CallToolResultSchema = ResultSchema.extend({ isError: z.boolean(), }); +/** + * CallToolResultSchema extended with backwards compatibility to protocol version 2024-10-07. + */ +export const CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({ + toolResult: z.unknown(), +})); + /** * Used by the client to invoke a tool provided by the server. */ @@ -1054,6 +1061,7 @@ export const ServerResultSchema = z.union([ ListResourceTemplatesResultSchema, ReadResourceResultSchema, CallToolResultSchema, + CompatibilityCallToolResultSchema, ListToolsResultSchema, ]); @@ -1148,6 +1156,7 @@ export type Tool = z.infer; export type ListToolsRequest = z.infer; export type ListToolsResult = z.infer; export type CallToolResult = z.infer; +export type CompatibilityCallToolResult = z.infer; export type CallToolRequest = z.infer; export type ToolListChangedNotification = z.infer< typeof ToolListChangedNotificationSchema