Merge pull request #725 from sushichan044/revert-670-feat/structured-content-with-type-safety

Revert "fix: add type safety for tool output schemas in ToolCallback"
This commit is contained in:
bhosmer-ant 2025-07-03 17:03:31 -04:00 • committed by GitHub
commit 9879d7a98e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 54 deletions

View file

@ -43,14 +43,7 @@ server.registerTool(
void country;
// Simulate weather API call
const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
const conditionCandidates = [
"sunny",
"cloudy",
"rainy",
"stormy",
"snowy",
] as const;
const conditions = conditionCandidates[Math.floor(Math.random() * conditionCandidates.length)];
const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)];
const structuredContent = {
temperature: {
@ -84,4 +77,4 @@ async function main() {
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});
});

View file

@ -1312,7 +1312,7 @@ describe("tool()", () => {
resultType: "structured",
// Missing required 'timestamp' field
someExtraField: "unexpected" // Extra field not in schema
} as unknown as { processedInput: string; resultType: string; timestamp: string }, // Type assertion to bypass TypeScript validation for testing purposes
},
})
);

View file

@ -169,7 +169,7 @@ export class McpServer {
}
const args = parseResult.data;
const cb = tool.callback as ToolCallback<ZodRawShape, ZodRawShape>;
const cb = tool.callback as ToolCallback<ZodRawShape>;
try {
result = await Promise.resolve(cb(args, extra));
} catch (error) {
@ -184,7 +184,7 @@ export class McpServer {
};
}
} else {
const cb = tool.callback as ToolCallback<undefined, ZodRawShape>;
const cb = tool.callback as ToolCallback<undefined>;
try {
result = await Promise.resolve(cb(extra));
} catch (error) {
@ -772,7 +772,7 @@ export class McpServer {
inputSchema: ZodRawShape | undefined,
outputSchema: ZodRawShape | undefined,
annotations: ToolAnnotations | undefined,
callback: ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>
callback: ToolCallback<ZodRawShape | undefined>
): RegisteredTool {
const registeredTool: RegisteredTool = {
title,
@ -929,7 +929,7 @@ export class McpServer {
outputSchema?: OutputArgs;
annotations?: ToolAnnotations;
},
cb: ToolCallback<InputArgs, OutputArgs>
cb: ToolCallback<InputArgs>
): RegisteredTool {
if (this._registeredTools[name]) {
throw new Error(`Tool ${name} is already registered`);
@ -944,7 +944,7 @@ export class McpServer {
inputSchema,
outputSchema,
annotations,
cb as ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>
cb as ToolCallback<ZodRawShape | undefined>
);
}
@ -1138,16 +1138,6 @@ export class ResourceTemplate {
}
}
/**
* Type helper to create a strongly-typed CallToolResult with structuredContent
*/
type TypedCallToolResult<OutputArgs extends undefined | ZodRawShape> =
OutputArgs extends ZodRawShape
? CallToolResult & {
structuredContent?: z.objectOutputType<OutputArgs, ZodTypeAny>;
}
: CallToolResult;
/**
* Callback for a tool handler registered with Server.tool().
*
@ -1158,21 +1148,13 @@ type TypedCallToolResult<OutputArgs extends undefined | ZodRawShape> =
* - `content` if the tool does not have an outputSchema
* - Both fields are optional but typically one should be provided
*/
export type ToolCallback<
InputArgs extends undefined | ZodRawShape = undefined,
OutputArgs extends undefined | ZodRawShape = undefined
> = InputArgs extends ZodRawShape
export type ToolCallback<Args extends undefined | ZodRawShape = undefined> =
Args extends ZodRawShape
? (
args: z.objectOutputType<InputArgs, ZodTypeAny>,
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
) =>
| TypedCallToolResult<OutputArgs>
| Promise<TypedCallToolResult<OutputArgs>>
: (
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
) =>
| TypedCallToolResult<OutputArgs>
| Promise<TypedCallToolResult<OutputArgs>>;
args: z.objectOutputType<Args, ZodTypeAny>,
extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
) => CallToolResult | Promise<CallToolResult>
: (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
export type RegisteredTool = {
title?: string;
@ -1180,24 +1162,22 @@ export type RegisteredTool = {
inputSchema?: AnyZodObject;
outputSchema?: AnyZodObject;
annotations?: ToolAnnotations;
callback: ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>;
callback: ToolCallback<undefined | ZodRawShape>;
enabled: boolean;
enable(): void;
disable(): void;
update<
InputArgs extends ZodRawShape,
OutputArgs extends ZodRawShape
>(updates: {
name?: string | null;
title?: string;
description?: string;
paramsSchema?: InputArgs;
outputSchema?: OutputArgs;
annotations?: ToolAnnotations;
callback?: ToolCallback<InputArgs, OutputArgs>
enabled?: boolean
}): void;
remove(): void;
update<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(
updates: {
name?: string | null,
title?: string,
description?: string,
paramsSchema?: InputArgs,
outputSchema?: OutputArgs,
annotations?: ToolAnnotations,
callback?: ToolCallback<InputArgs>,
enabled?: boolean
}): void
remove(): void
};
const EMPTY_OBJECT_JSON_SCHEMA = {