[v1.x backport] Use correct schema for client sampling validation when tools are present (#1407)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ola Hungerford 2026-01-20 04:15:11 -07:00 • committed by GitHub
commit 12ae856cee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,6 +40,7 @@ import {
CreateTaskResultSchema,
CreateMessageRequestSchema,
CreateMessageResultSchema,
CreateMessageResultWithToolsSchema,
ToolListChangedNotificationSchema,
PromptListChangedNotificationSchema,
ResourceListChangedNotificationSchema,
@ -452,8 +453,10 @@ export class Client<
return taskValidationResult.data;
}
// For non-task requests, validate against CreateMessageResultSchema
const validationResult = safeParse(CreateMessageResultSchema, result);
// For non-task requests, validate against appropriate schema based on tools presence
const hasTools = params.tools || params.toolChoice;
const resultSchema = hasTools ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema;
const validationResult = safeParse(resultSchema, result);
if (!validationResult.success) {
const errorMessage =
validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);