Skip to content

Conversation

@felixweinberger
Copy link
Contributor

Summary

Introduce method overloading for create_message to preserve backwards compatibility while supporting the new tools feature from SEP-1577.

  • When called without tools, returns CreateMessageResult with single content (backwards compatible)
  • When called with tools, returns CreateMessageResultWithTools which allows array content

Motivation and Context

SEP-1577 changes CreateMessageResult.content from single to single | list. This would be a breaking change for existing SDK users. To avoid requiring a major version bump (v2.0), this PR uses @overload decorators to return different types based on whether tools are provided.

Existing code that doesn't use tools continues to work unchanged:

result = await session.create_message(messages=..., max_tokens=100)
# result.content is single content - no changes needed
if result.content.type == "text":
    return result.content.text

How Has This Been Tested?

  • Type tests pass with pyright
  • 469 tests pass
  • Updated test for tool use to use CreateMessageResultWithTools
  • Added test for basic CreateMessageResult

Breaking Changes

None for v1.x users. Existing code that doesn't use tools continues to work unchanged.

Note: v2.0 will align CreateMessageResult with the schema (array content).

Types of changes

  • New feature (non-breaking change which adds functionality)

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

Introduce method overloading for create_message to preserve backwards
compatibility while supporting the new tools feature from SEP-1577.

When called without tools, create_message returns CreateMessageResult
with single content (backwards compatible). When called with tools,
it returns CreateMessageResultWithTools which allows array content.

This allows existing code that doesn't use tools to continue working
without any changes, while new code using tools gets the appropriate
type that handles array content.

Changes:
- Add SamplingContent type alias for basic content types (no tool use)
- Add CreateMessageResultWithTools for tool-enabled responses
- Add @overload signatures to create_message()
- Update tests to use appropriate result types
- Revert examples to use direct content access (no content_as_list)
@felixweinberger felixweinberger mentioned this pull request Dec 2, 2025
6 tasks
@felixweinberger felixweinberger marked this pull request as ready for review December 2, 2025 11:47
@felixweinberger
Copy link
Contributor Author

Also added this to the list of things to clean up for v2: #1253

pcarleton
pcarleton previously approved these changes Dec 2, 2025
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: types.ModelPreferences | None = None,
tools: list[types.Tool] | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

note: this confused me (why not leave it out?), but its a kwarg, and is needed to discriminate between the overloads

Verify that:
- create_message without tools returns CreateMessageResult with single content
- CreateMessageResultWithTools supports content_as_list for array content
@felixweinberger felixweinberger enabled auto-merge (squash) December 2, 2025 13:09
@felixweinberger felixweinberger merged commit fa851d9 into main Dec 2, 2025
38 of 39 checks passed
@felixweinberger felixweinberger deleted the fweinberger/backwards-compat-sampling branch December 2, 2025 13:17
Copy link
Contributor

@bhosmer-ant bhosmer-ant left a comment

Choose a reason for hiding this comment

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

thank you for this!

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.

4 participants