Skip to content

chore: Upgrade AI SDK from 4.x to 6.x#761

Merged
dcramer merged 12 commits intomainfrom
revert-mcp-sdk-upgrade
Feb 3, 2026
Merged

chore: Upgrade AI SDK from 4.x to 6.x#761
dcramer merged 12 commits intomainfrom
revert-mcp-sdk-upgrade

Conversation

@dcramer
Copy link
Member

@dcramer dcramer commented Jan 31, 2026

Upgrade to AI SDK 6.x to support MCP protocol version 2025-11-25.

This upgrade enables compatibility with agents@0.3.5+ which is required for the latest MCP protocol features.

Package Changes

Package Before After
ai ^4.3.16 ^6.0.64
agents ^0.2.23 ^0.3.6
@ai-sdk/anthropic ^1.2.12 ^3.0.33
@ai-sdk/openai ^1.3.22 ^3.0.23
@ai-sdk/react ^1.2.12 ^3.0.66
@ai-sdk/mcp (new) ^1.0.16
@modelcontextprotocol/sdk ^1.21.0 ^1.25.3

API Migrations

  • MCP client imports: experimental_createMCPClient moved from "ai" to "@ai-sdk/mcp"
  • Type rename: LanguageModelV1LanguageModel (now a union type)
  • Step control: maxSteps: NstopWhen: stepCountIs(N)
  • Tool API: parametersinputSchema, execute(params)execute(input, options)
  • Tool call capture: toolCall.argstoolCall.input

This branch includes a revert of #759 as the base, then applies the full AI SDK 6.x migration on top.

Upgrade to AI SDK 6.x to support MCP protocol version 2025-11-25.

Package changes:
- ai: ^4.3.16 → ^6.0.64
- agents: ^0.2.23 → ^0.3.6
- @ai-sdk/anthropic: ^1.2.12 → ^3.0.33
- @ai-sdk/openai: ^1.3.22 → ^3.0.23
- @ai-sdk/react: ^1.2.12 → ^3.0.66
- @ai-sdk/mcp: (new) ^1.0.16
- @modelcontextprotocol/sdk: ^1.21.0 → ^1.25.3

API migrations:
- MCP client imports: "ai" → "@ai-sdk/mcp"
- Type rename: LanguageModelV1 → LanguageModel
- generateText/streamText: maxSteps → stopWhen(stepCountIs(N))
- tool() API: parameters → inputSchema, execute(params) → execute(input)
- Tool call capture: toolCall.args → toolCall.input

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The AI SDK 6.x migration renamed toolCall.args to toolCall.input.
This was correctly updated in callEmbeddedAgent.ts but missed in
the test client's agent.ts, causing logTool to receive undefined
instead of the actual tool arguments.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Migrate the cloudflare chat UI to be compatible with AI SDK 5+/6.x:

**Client-side changes:**
- Replace `Message` type with `UIMessage` (requires `parts` array)
- Migrate `useChat` to new API:
  - Use `DefaultChatTransport` with `api` option
  - Manage input state manually (removed from hook)
  - `append` → `sendMessage`
  - `reload` → `regenerate`
  - `handleInputChange`/`handleSubmit` → manual handlers
- Update all components to use `UIMessage` type
- Import from `@ai-sdk/react` instead of `ai/react`

**Server-side changes:**
- `maxTokens` → `maxOutputTokens`
- `toDataStreamResponse` → `toUIMessageStreamResponse`

**Dependency fixes:**
- Add pnpm override for `@modelcontextprotocol/sdk: ^1.25.3` to fix
  version mismatch between direct dep (1.25.3) and `agents` package
  (1.25.2) which caused TypeScript type conflicts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Additional fixes for UIMessage type changes in AI SDK 5+/6.x:

- Remove `content` property usage (UIMessage always uses `parts` array)
- Replace `data` with `metadata` for custom message metadata
- Remove `createdAt` property (no longer in UIMessage interface)
- Update tool part handling to support AI SDK 6 format where tool
  type is `tool-${toolName}` with properties directly on the part
- Remove legacy content fallback in processMessages

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Refactored the isValidMessage callback to use early return pattern,
which fixes the TypeScript error where msg.parts was narrowed to
type 'never' in the else branch. Since UIMessage.parts is a required
property, the previous if/else structure was flagged by TypeScript
as having unreachable code.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Addresses Cursor Bugbot feedback:
1. Legacy tool-invocation parts from persisted messages (AI SDK 4/5 format)
   now render correctly instead of being silently ignored
2. Tool output conversion now properly handles MCP-format objects with
   content arrays, strings, and other objects - avoiding [object Object]

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Legacy AI SDK 4/5 tool-invocation parts have structure:
{ type: "tool-invocation", toolInvocation: {...} }

The previous fix incorrectly cast the entire part to ChatToolInvocation
instead of accessing part.toolInvocation. This caused undefined values
for toolName, toolCallId, args, state, and result.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@dcramer dcramer marked this pull request as ready for review January 31, 2026 16:42
Added migration logic to convert persisted messages from AI SDK 4.x
format (using `content` string) to AI SDK 6 format (using `parts` array).
This prevents silent data loss when users upgrade, as their chat history
will be automatically migrated instead of being filtered out.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…igration

1. Fixed state mapping for AI SDK 6 tool parts to correctly handle
   'partial-call' state during streaming, preventing message persistence
   failures and incorrect UI rendering.

2. Updated migration function to preserve metadata from both legacy
   'data' property and new 'metadata' property, preventing loss of
   custom properties like type, hasSlashCommands, simulateStreaming.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@sentry-warden sentry-warden bot left a comment

Choose a reason for hiding this comment

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

testing-guidelines

testing-guidelines: Found 2 issues (2 medium)


⏱ 433.7s · 2.1M in / 34.8k out · $4.38

The AI SDK 6.x has two different message formats:
- UIMessage (client/UI format) uses a `parts` array
- ModelMessage (API format) uses a `content` property

The chat endpoint was passing UIMessage[] directly to streamText(),
which expects ModelMessage[]. Added convertToModelMessages() to
properly convert the format before streaming.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

dcramer and others added 2 commits February 3, 2026 09:35
…parts

Two bug fixes:

1. useChat transport not re-initializing when endpoint mode changes:
   The useChat hook captures the initial transport and doesn't reset
   when the prop changes. Added effect to clear messages when mode
   switches, ensuring the new transport is used for subsequent messages.

2. isValidMessage not validating AI SDK 6.x tool format:
   AI SDK 6.x uses "tool-<toolname>" format (e.g., "tool-whoami")
   instead of "tool-invocation". Updated validation to filter out
   incomplete tool calls (input-streaming, input-available,
   approval-requested states) from persisted messages.

Also consolidated the auth logout and mode change effects into one.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Legacy AI SDK 4.x tool-invocation parts have structure:
{ type: "tool-invocation", toolInvocation: { state, result, ... } }

The validation was incorrectly accessing state and result directly on
the part object instead of through the nested toolInvocation property.
This caused all legacy tool-invocation parts to pass validation
regardless of their actual state.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@dcramer dcramer merged commit e7acd09 into main Feb 3, 2026
18 checks passed
@dcramer dcramer deleted the revert-mcp-sdk-upgrade branch February 3, 2026 17:58
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.

1 participant