feat(xai): add mcpServer tool for MCP server support #11143
+325
−105
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.
Summary
This PR adds MCP server tool support and aligns xAI streaming behavior with the OpenAI provider.
Changes
1. MCP Server Tool Support
xai.tools.mcpServer()for connecting to remote MCP servers via xAI's Responses APIXaiResponsesToolwith all xAI API fieldsmcp_call,response.mcp_call.*events)2. Streaming Refactor (Matches OpenAI Pattern)
The xAI Responses API sends text in two places:
response.output_text.deltaeventsresponse.output_item.doneeventPrevious behavior: Emitted
text-deltafrom both, causing duplicate text in the stream.New behavior (matches OpenAI):
response.output_text.delta→ emittext-delta(streaming content)response.output_item.done→ emittext-end(signal completion, no content)This aligns with
packages/openai/src/responses/openai-responses-language-model.tswhich emitstext-endfrom the done event rather than re-emitting text content.Test plan
prepareResponsesToolsMCP handling (4 new tests)streamText()andOutput.object()Usage