Support old tools/call result structure, if explicitly opted in

This commit is contained in:
Justin Spahr-Summers 2024-11-07 13:09:20 +00:00
commit 1f3dbef66c
No known key found for this signature in database
3 changed files with 14 additions and 3 deletions

View file

@ -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,
);
}

View file

@ -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<T extends AnyZodObject>(
request<T extends ZodType<object>>(
request: SendRequestT,
resultSchema: T,
onprogress?: ProgressCallback,

View file

@ -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<typeof ToolSchema>;
export type ListToolsRequest = z.infer<typeof ListToolsRequestSchema>;
export type ListToolsResult = z.infer<typeof ListToolsResultSchema>;
export type CallToolResult = z.infer<typeof CallToolResultSchema>;
export type CompatibilityCallToolResult = z.infer<typeof CompatibilityCallToolResultSchema>;
export type CallToolRequest = z.infer<typeof CallToolRequestSchema>;
export type ToolListChangedNotification = z.infer<
typeof ToolListChangedNotificationSchema