Skip to content

Commit 9879d7a

Browse files
authored
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"
2 parents 0b0ea5b + a45e4fe commit 9879d7a

File tree

3 files changed

+27
-54
lines changed

3 files changed

+27
-54
lines changed

src/examples/server/mcpServerOutputSchema.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ server.registerTool(
4343
void country;
4444
// Simulate weather API call
4545
const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
46-
const conditionCandidates = [
47-
"sunny",
48-
"cloudy",
49-
"rainy",
50-
"stormy",
51-
"snowy",
52-
] as const;
53-
const conditions = conditionCandidates[Math.floor(Math.random() * conditionCandidates.length)];
46+
const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)];
5447

5548
const structuredContent = {
5649
temperature: {
@@ -84,4 +77,4 @@ async function main() {
8477
main().catch((error) => {
8578
console.error("Server error:", error);
8679
process.exit(1);
87-
});
80+
});

src/server/mcp.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ describe("tool()", () => {
13121312
resultType: "structured",
13131313
// Missing required 'timestamp' field
13141314
someExtraField: "unexpected" // Extra field not in schema
1315-
} as unknown as { processedInput: string; resultType: string; timestamp: string }, // Type assertion to bypass TypeScript validation for testing purposes
1315+
},
13161316
})
13171317
);
13181318

src/server/mcp.ts

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class McpServer {
169169
}
170170

171171
const args = parseResult.data;
172-
const cb = tool.callback as ToolCallback<ZodRawShape, ZodRawShape>;
172+
const cb = tool.callback as ToolCallback<ZodRawShape>;
173173
try {
174174
result = await Promise.resolve(cb(args, extra));
175175
} catch (error) {
@@ -184,7 +184,7 @@ export class McpServer {
184184
};
185185
}
186186
} else {
187-
const cb = tool.callback as ToolCallback<undefined, ZodRawShape>;
187+
const cb = tool.callback as ToolCallback<undefined>;
188188
try {
189189
result = await Promise.resolve(cb(extra));
190190
} catch (error) {
@@ -772,7 +772,7 @@ export class McpServer {
772772
inputSchema: ZodRawShape | undefined,
773773
outputSchema: ZodRawShape | undefined,
774774
annotations: ToolAnnotations | undefined,
775-
callback: ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>
775+
callback: ToolCallback<ZodRawShape | undefined>
776776
): RegisteredTool {
777777
const registeredTool: RegisteredTool = {
778778
title,
@@ -929,7 +929,7 @@ export class McpServer {
929929
outputSchema?: OutputArgs;
930930
annotations?: ToolAnnotations;
931931
},
932-
cb: ToolCallback<InputArgs, OutputArgs>
932+
cb: ToolCallback<InputArgs>
933933
): RegisteredTool {
934934
if (this._registeredTools[name]) {
935935
throw new Error(`Tool ${name} is already registered`);
@@ -944,7 +944,7 @@ export class McpServer {
944944
inputSchema,
945945
outputSchema,
946946
annotations,
947-
cb as ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>
947+
cb as ToolCallback<ZodRawShape | undefined>
948948
);
949949
}
950950

@@ -1138,16 +1138,6 @@ export class ResourceTemplate {
11381138
}
11391139
}
11401140

1141-
/**
1142-
* Type helper to create a strongly-typed CallToolResult with structuredContent
1143-
*/
1144-
type TypedCallToolResult<OutputArgs extends undefined | ZodRawShape> =
1145-
OutputArgs extends ZodRawShape
1146-
? CallToolResult & {
1147-
structuredContent?: z.objectOutputType<OutputArgs, ZodTypeAny>;
1148-
}
1149-
: CallToolResult;
1150-
11511141
/**
11521142
* Callback for a tool handler registered with Server.tool().
11531143
*
@@ -1158,46 +1148,36 @@ type TypedCallToolResult<OutputArgs extends undefined | ZodRawShape> =
11581148
* - `content` if the tool does not have an outputSchema
11591149
* - Both fields are optional but typically one should be provided
11601150
*/
1161-
export type ToolCallback<
1162-
InputArgs extends undefined | ZodRawShape = undefined,
1163-
OutputArgs extends undefined | ZodRawShape = undefined
1164-
> = InputArgs extends ZodRawShape
1151+
export type ToolCallback<Args extends undefined | ZodRawShape = undefined> =
1152+
Args extends ZodRawShape
11651153
? (
1166-
args: z.objectOutputType<InputArgs, ZodTypeAny>,
1167-
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
1168-
) =>
1169-
| TypedCallToolResult<OutputArgs>
1170-
| Promise<TypedCallToolResult<OutputArgs>>
1171-
: (
1172-
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
1173-
) =>
1174-
| TypedCallToolResult<OutputArgs>
1175-
| Promise<TypedCallToolResult<OutputArgs>>;
1154+
args: z.objectOutputType<Args, ZodTypeAny>,
1155+
extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
1156+
) => CallToolResult | Promise<CallToolResult>
1157+
: (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
11761158

11771159
export type RegisteredTool = {
11781160
title?: string;
11791161
description?: string;
11801162
inputSchema?: AnyZodObject;
11811163
outputSchema?: AnyZodObject;
11821164
annotations?: ToolAnnotations;
1183-
callback: ToolCallback<ZodRawShape | undefined, ZodRawShape | undefined>;
1165+
callback: ToolCallback<undefined | ZodRawShape>;
11841166
enabled: boolean;
11851167
enable(): void;
11861168
disable(): void;
1187-
update<
1188-
InputArgs extends ZodRawShape,
1189-
OutputArgs extends ZodRawShape
1190-
>(updates: {
1191-
name?: string | null;
1192-
title?: string;
1193-
description?: string;
1194-
paramsSchema?: InputArgs;
1195-
outputSchema?: OutputArgs;
1196-
annotations?: ToolAnnotations;
1197-
callback?: ToolCallback<InputArgs, OutputArgs>
1198-
enabled?: boolean
1199-
}): void;
1200-
remove(): void;
1169+
update<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(
1170+
updates: {
1171+
name?: string | null,
1172+
title?: string,
1173+
description?: string,
1174+
paramsSchema?: InputArgs,
1175+
outputSchema?: OutputArgs,
1176+
annotations?: ToolAnnotations,
1177+
callback?: ToolCallback<InputArgs>,
1178+
enabled?: boolean
1179+
}): void
1180+
remove(): void
12011181
};
12021182

12031183
const EMPTY_OBJECT_JSON_SCHEMA = {

0 commit comments

Comments
 (0)