Skip to content

Conversation

@olaservo
Copy link
Member

Summary

Fix client-side sampling validation to use CreateMessageResultWithToolsSchema when tools are present in the request, preventing false validation failures for tool_use responses.

Motivation and Context

When a client sampling handler returns tool_use content formatted as an array, validation fails with "Invalid input: expected object, received array". This occurs because the client always validates against CreateMessageResultSchema regardless of whether tools were included in the request.

  • CreateMessageResultSchema expects single content blocks (text, image, or audio only)
  • CreateMessageResultWithToolsSchema supports array content with tool_use/tool_result blocks

This mirrors the server-side fix from PR #1156, which the client-side code missed.

Fixes #1346

How Has This Been Tested?

Added 5 new integration tests in test/integration/test/client/client.test.ts:

  1. Array content with tool_use when request includes tools - Verifies handler can return [{ type: 'tool_use', ... }]
  2. Single content when request includes tools - Verifies backwards compatibility
  3. Single content when request has no tools - Verifies standard non-tool flow
  4. Reject array content when request has no tools - Ensures validation still enforces single content for non-tool requests
  5. Array content when request includes toolChoice - Verifies toolChoice also triggers the correct schema

All 483 tests pass.

Breaking Changes

None

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

…sent

When a sampling request includes tools or toolChoice, the client now validates
the response against CreateMessageResultWithToolsSchema (which supports array
content with tool_use blocks) instead of always using CreateMessageResultSchema.

This fixes validation failures when client handlers return tool_use content
formatted as an array, which was incorrectly rejected with "Invalid input:
expected object, received array".

Fixes modelcontextprotocol#1346

🦉 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@olaservo olaservo requested a review from a team as a code owner December 29, 2025 01:34
@changeset-bot
Copy link

changeset-bot bot commented Dec 29, 2025

⚠️ No Changeset found

Latest commit: eb7aae5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 29, 2025

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1347
npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1347

commit: eb7aae5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-side sampling validation doesn't use CreateMessageResultWithToolsSchema when tools are present

1 participant