Skip to content

Commit 0b2ef19

Browse files
committed
fix: Preserve ZodRawShape in RegisteredTool type
The RegisteredTool type now preserves the original ZodRawShape instead of converting it to ZodType<object>. This allows users to access shape properties directly, which is important for introspection and testing. The conversion to ZodType<object> is now done only when needed: - During JSON schema generation (for API responses) - During validation (for parsing input/output) This is a non-breaking fix that maintains backward compatibility.
1 parent a30ef44 commit 0b2ef19

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/server/mcp.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class McpServer {
107107
title: tool.title,
108108
description: tool.description,
109109
inputSchema: tool.inputSchema
110-
? (zodToJsonSchema(tool.inputSchema, {
110+
? (zodToJsonSchema(getZodSchemaObject(tool.inputSchema) ?? z.object({}), {
111111
strictUnions: true,
112112
pipeStrategy: 'input'
113113
}) as Tool['inputSchema'])
@@ -117,7 +117,7 @@ export class McpServer {
117117
};
118118

119119
if (tool.outputSchema) {
120-
toolDefinition.outputSchema = zodToJsonSchema(tool.outputSchema, {
120+
toolDefinition.outputSchema = zodToJsonSchema(getZodSchemaObject(tool.outputSchema) ?? z.object({}), {
121121
strictUnions: true,
122122
pipeStrategy: 'output'
123123
}) as Tool['outputSchema'];
@@ -144,7 +144,11 @@ export class McpServer {
144144

145145
if (tool.inputSchema) {
146146
const cb = tool.callback as ToolCallback<ZodRawShape>;
147-
const parseResult = await tool.inputSchema.safeParseAsync(request.params.arguments);
147+
const inputSchemaObject = getZodSchemaObject(tool.inputSchema);
148+
if (!inputSchemaObject) {
149+
throw new McpError(ErrorCode.InternalError, `Tool ${request.params.name} has invalid input schema`);
150+
}
151+
const parseResult = await inputSchemaObject.safeParseAsync(request.params.arguments);
148152
if (!parseResult.success) {
149153
throw new McpError(
150154
ErrorCode.InvalidParams,
@@ -169,7 +173,11 @@ export class McpServer {
169173
}
170174

171175
// if the tool has an output schema, validate structured content
172-
const parseResult = await tool.outputSchema.safeParseAsync(result.structuredContent);
176+
const outputSchemaObject = getZodSchemaObject(tool.outputSchema);
177+
if (!outputSchemaObject) {
178+
throw new McpError(ErrorCode.InternalError, `Tool ${request.params.name} has invalid output schema`);
179+
}
180+
const parseResult = await outputSchemaObject.safeParseAsync(result.structuredContent);
173181
if (!parseResult.success) {
174182
throw new McpError(
175183
ErrorCode.InvalidParams,
@@ -671,8 +679,8 @@ export class McpServer {
671679
const registeredTool: RegisteredTool = {
672680
title,
673681
description,
674-
inputSchema: getZodSchemaObject(inputSchema),
675-
outputSchema: getZodSchemaObject(outputSchema),
682+
inputSchema,
683+
outputSchema,
676684
annotations,
677685
_meta,
678686
callback,
@@ -690,7 +698,7 @@ export class McpServer {
690698
}
691699
if (typeof updates.title !== 'undefined') registeredTool.title = updates.title;
692700
if (typeof updates.description !== 'undefined') registeredTool.description = updates.description;
693-
if (typeof updates.paramsSchema !== 'undefined') registeredTool.inputSchema = z.object(updates.paramsSchema);
701+
if (typeof updates.paramsSchema !== 'undefined') registeredTool.inputSchema = updates.paramsSchema;
694702
if (typeof updates.callback !== 'undefined') registeredTool.callback = updates.callback;
695703
if (typeof updates.annotations !== 'undefined') registeredTool.annotations = updates.annotations;
696704
if (typeof updates._meta !== 'undefined') registeredTool._meta = updates._meta;
@@ -1054,8 +1062,8 @@ export type ToolCallback<Args extends undefined | ZodRawShape | ZodType<object>
10541062
export type RegisteredTool = {
10551063
title?: string;
10561064
description?: string;
1057-
inputSchema?: ZodType<object>;
1058-
outputSchema?: ZodType<object>;
1065+
inputSchema?: ZodRawShape | ZodType<object>;
1066+
outputSchema?: ZodRawShape | ZodType<object>;
10591067
annotations?: ToolAnnotations;
10601068
_meta?: Record<string, unknown>;
10611069
callback: ToolCallback<undefined | ZodRawShape>;

0 commit comments

Comments
 (0)