Skip to content

Conversation

@boylec
Copy link

@boylec boylec commented Dec 12, 2025

Summary

This PR adds MCP server tool support and aligns xAI streaming behavior with the OpenAI provider.

Changes

1. MCP Server Tool Support

  • Adds xai.tools.mcpServer() for connecting to remote MCP servers via xAI's Responses API
  • Includes MCP type definition in XaiResponsesTool with all xAI API fields
  • Args validation and camelCase→snake_case transformation
  • Streaming event schemas for MCP calls (mcp_call, response.mcp_call.* events)

2. Streaming Refactor (Matches OpenAI Pattern)

The xAI Responses API sends text in two places:

  1. Incremental deltas via response.output_text.delta events
  2. Complete text in the response.output_item.done event

Previous behavior: Emitted text-delta from both, causing duplicate text in the stream.

New behavior (matches OpenAI):

  • response.output_text.delta → emit text-delta (streaming content)
  • response.output_item.done → emit text-end (signal completion, no content)

This aligns with packages/openai/src/responses/openai-responses-language-model.ts which emits text-end from the done event rather than re-emitting text content.

Test plan

  • Unit tests for prepareResponsesTools MCP handling (4 new tests)
  • Manual testing with real MCP server (Google Places + PostGrid)
  • Verified streaming works with streamText() and Output.object()
  • All 112 xAI package tests pass

Usage

import { xai } from '@ai-sdk/xai'
import { streamText } from 'ai'

const result = streamText({
  model: xai.responses('grok-4-1-fast-reasoning'),
  tools: {
    web_search: xai.tools.webSearch(),
    my_mcp: xai.tools.mcpServer({
      serverUrl: 'https://my-server.com/mcp',
      serverLabel: 'my-tools',
      allowedTools: ['tool1', 'tool2'],
    }),
  },
})

@vercel-ai-sdk vercel-ai-sdk bot added the ai/mcp label Dec 12, 2025
- Adds xai.tools.mcpServer() for connecting to remote MCP servers via xAI's Responses API
- Includes MCP type definition in XaiResponsesTool with all xAI API fields
- Adds args validation and camelCase to snake_case transformation
- Includes streaming event schemas for MCP calls (mcp_call, response.mcp_call.* events)
- Refactors streaming to match OpenAI pattern: emit text-end from done event, not text-delta with full content
@boylec boylec force-pushed the feature/mcp-tool-support branch from 4c38558 to 913a2ab Compare December 12, 2025 21:16
@boylec
Copy link
Author

boylec commented Dec 12, 2025

Please label as ai/provider

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant