You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Client-side sampling validation doesn't use CreateMessageResultWithToolsSchema when tools are present #1346
Describe the bug
When a client handles a sampling/createMessage request that includes tools, the client SDK validates the handler's response using CreateMessageResultSchema instead of CreateMessageResultWithToolsSchema. This causes valid tool_use responses (which have array content) to be rejected with a validation error.
The server-side was fixed in PR #1156, but it appears that the client-side validation in Client.setRequestHandler was missed.
To Reproduce
Create a client that declares sampling: { tools: {} } capability
Set up a sampling handler that returns tool_use content when the LLM wants to use tools
Connect to a server that sends a sampling/createMessage request with tools
When the LLM responds with tool_use, the client rejects the response with validation error
Expected behavior
When params.tools is present and non-empty in the sampling request, the client should validate the response using CreateMessageResultWithToolsSchema (which accepts array content with tool_use blocks) instead of CreateMessageResultSchema (which only accepts single object content).
Location of bug: packages/client/src/client/client.ts lines 461-469
Current code (SDK 1.25.1):
// For non-task requests, validate against CreateMessageResultSchemaconstvalidationResult=safeParse(CreateMessageResultSchema,result);if(!validationResult.success){consterrorMessage=validationResult.errorinstanceofError ? validationResult.error.message : String(validationResult.error);thrownewMcpError(ErrorCode.InvalidParams,`Invalid sampling result: ${errorMessage}`);}returnvalidationResult.data;
Suggested fix:
// For non-task requests, validate against appropriate schema// Use CreateMessageResultWithToolsSchema when tools are present (allows array content with tool_use)consthasTools=params.tools&¶ms.tools.length>0;constresultSchema=hasTools ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema;constvalidationResult=safeParse(resultSchema,result);if(!validationResult.success){consterrorMessage=validationResult.errorinstanceofError ? validationResult.error.message : String(validationResult.error);thrownewMcpError(ErrorCode.InvalidParams,`Invalid sampling result: ${errorMessage}`);}returnvalidationResult.data;
Also requires adding CreateMessageResultWithToolsSchema to the imports at the top of the file.
Activity
fix: use correct schema for client sampling validation when tools pre…
fix: use correct schema for client sampling validation when tools pre…