Skip to content

Conversation

@olaservo
Copy link
Member

Summary

Backport of #1347 to v1.x branch for inclusion in a v1.x.x point release.

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/client/index.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 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

🦉 Generated with Claude Code

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

…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 January 19, 2026 17:31
@changeset-bot
Copy link

changeset-bot bot commented Jan 19, 2026

⚠️ No Changeset found

Latest commit: 9a89eba

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.

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 Jan 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/sdk@1407

commit: 9a89eba

Copy link
Member

@pcarleton pcarleton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks ola!!

@pcarleton pcarleton merged commit 12ae856 into modelcontextprotocol:v1.x Jan 20, 2026
6 checks passed
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.

2 participants