refactor: enhance MCPSamplingLanguageModel to support tool calls and … #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request significantly enhances the AI SDK MCP Sampling Provider examples and core implementation to support advanced tool calling, structured object generation, and improved streaming compatibility. The changes introduce new example scripts demonstrating structured data and streaming with tool calls, and update the core language model to better align with the AI SDK v2 protocol, including improved handling of tool calls/results and streaming events.
New Example Scripts and Features:
generate_object_example.tsto demonstrate generating structured objects (e.g., recipes) using the MCP Sampling Provider and AI SDK'sgenerateObjectfunction.stream_text_example.tsto showcase streaming text generation and tool call handling withstreamText, including a calculator tool example.Enhancements to Existing Examples:
generate_text_example.tsto include a new tool ("test-tool-calls") that demonstrates tool injection and execution (e.g., calculator) with AI SDK'sgenerateText. Also removed the unused agent definition for clarity. [1] [2] [3]Core Language Model Improvements:
MCPSamplingLanguageModelto:doStream) to emit events (text-start,text-delta,text-end,tool-call,response-metadata,finish) in the correct AI SDK order, ensuring compatibility with consumers expecting v2 protocol streams.Documentation and Clarity:
Summary of Most Important Changes:
New Example Scripts:
generate_object_example.tsfor structured object generation with AI SDK and MCP Sampling Provider.stream_text_example.tsfor streaming text and tool call handling examples.Enhanced Tool Call Support:
generate_text_example.tswith a "test-tool-calls" tool to demonstrate tool injection and execution with AI SDK.MCPSamplingLanguageModel, mapping them to AI SDK v2 protocol. [1] [2]Streaming Protocol Compliance:
doStreaminMCPSamplingLanguageModelto emit events in the correct AI SDK v2 order and format, including tool calls and response metadata.Developer Experience: