Skip to content

Commit 282e9d1

Browse files
committed
refactor(app): fold error prefix into validateStandardSchema; clarify registerAppTool runtime caveat
1 parent 0b67f68 commit 282e9d1

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

src/app.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -403,34 +403,23 @@ export class App extends ProtocolWithEvents<
403403
if (!registeredTool.enabled) {
404404
throw new Error(`Tool ${name} is disabled`);
405405
}
406-
let parsedArgs = rawArgs;
407-
if (config.inputSchema) {
408-
try {
409-
parsedArgs = await validateStandardSchema(
406+
const parsedArgs = config.inputSchema
407+
? await validateStandardSchema(
410408
config.inputSchema,
411409
rawArgs,
412-
);
413-
} catch (e) {
414-
throw new Error(
415-
`Invalid input for tool ${name}: ${(e as Error).message}`,
416-
);
417-
}
418-
}
410+
`Invalid input for tool ${name}: `,
411+
)
412+
: rawArgs;
419413
const result = await (cb as AppToolCallback<StandardSchemaWithJSON>)(
420414
parsedArgs,
421415
extra,
422416
);
423417
if (config.outputSchema) {
424-
try {
425-
result.structuredContent = (await validateStandardSchema(
426-
config.outputSchema,
427-
result.structuredContent,
428-
)) as CallToolResult["structuredContent"];
429-
} catch (e) {
430-
throw new Error(
431-
`Invalid output for tool ${name}: ${(e as Error).message}`,
432-
);
433-
}
418+
result.structuredContent = (await validateStandardSchema(
419+
config.outputSchema,
420+
result.structuredContent,
421+
`Invalid output for tool ${name}: `,
422+
)) as CallToolResult["structuredContent"];
434423
}
435424
return result;
436425
},

src/server/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,10 @@ export function registerAppTool<
225225
inputSchema?: InputArgs;
226226
outputSchema?: OutputArgs;
227227
},
228-
// ToolCallback in sdk@1.x is constrained to zod; widen here so callers using
229-
// other Standard Schema libs type-check. Runtime is fully delegated to
230-
// McpServer.registerTool, which in 1.29.0 already handles anything zod-shaped
231-
// (and zod ≥3.25 is a Standard Schema). Non-zod callers get correct arg
232-
// inference here; the cast below bridges the 1.x SDK type until v2.
228+
// The widened constraint signals the v2 API shape, but NOTE: McpServer in
229+
// sdk@1.x still calls zod internals at runtime, so non-zod schemas will fail
230+
// here until we depend on sdk v2. Zod (which all current callers use) is
231+
// unaffected. The cast below bridges the 1.x type signature.
233232
cb: ToolCallback<
234233
InputArgs extends undefined | ZodRawShapeCompat | AnySchema
235234
? InputArgs

src/standard-schema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ export function standardSchemaToJsonSchema(
4747

4848
/**
4949
* Validate a value against a Standard Schema. Returns the parsed value on
50-
* success or throws with a formatted issue list on failure.
50+
* success or throws with a formatted issue list (optionally prefixed).
5151
*/
5252
export async function validateStandardSchema<S extends StandardSchemaV1>(
5353
schema: S,
5454
value: unknown,
55+
errorPrefix = "",
5556
): Promise<StandardSchemaV1.InferOutput<S>> {
5657
const result = await schema["~standard"].validate(value);
5758
if (result.issues) {
@@ -63,7 +64,7 @@ export async function validateStandardSchema<S extends StandardSchemaV1>(
6364
return path ? `${path}: ${i.message}` : i.message;
6465
})
6566
.join("; ");
66-
throw new Error(msg);
67+
throw new Error(errorPrefix + msg);
6768
}
6869
return result.value;
6970
}

0 commit comments

Comments
 (0)