@ai-sdk/anthropic 3.0.0-beta.27 → 3.0.0-beta.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -112,6 +112,24 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
112
112
  name: import_v42.z.string(),
113
113
  input: import_v42.z.record(import_v42.z.string(), import_v42.z.unknown()).nullish()
114
114
  }),
115
+ import_v42.z.object({
116
+ type: import_v42.z.literal("mcp_tool_use"),
117
+ id: import_v42.z.string(),
118
+ name: import_v42.z.string(),
119
+ input: import_v42.z.unknown(),
120
+ server_name: import_v42.z.string()
121
+ }),
122
+ import_v42.z.object({
123
+ type: import_v42.z.literal("mcp_tool_result"),
124
+ tool_use_id: import_v42.z.string(),
125
+ is_error: import_v42.z.boolean(),
126
+ content: import_v42.z.array(
127
+ import_v42.z.union([
128
+ import_v42.z.string(),
129
+ import_v42.z.object({ type: import_v42.z.literal("text"), text: import_v42.z.string() })
130
+ ])
131
+ )
132
+ }),
115
133
  import_v42.z.object({
116
134
  type: import_v42.z.literal("web_fetch_tool_result"),
117
135
  tool_use_id: import_v42.z.string(),
@@ -284,6 +302,24 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
284
302
  name: import_v42.z.string(),
285
303
  input: import_v42.z.record(import_v42.z.string(), import_v42.z.unknown()).nullish()
286
304
  }),
305
+ import_v42.z.object({
306
+ type: import_v42.z.literal("mcp_tool_use"),
307
+ id: import_v42.z.string(),
308
+ name: import_v42.z.string(),
309
+ input: import_v42.z.unknown(),
310
+ server_name: import_v42.z.string()
311
+ }),
312
+ import_v42.z.object({
313
+ type: import_v42.z.literal("mcp_tool_result"),
314
+ tool_use_id: import_v42.z.string(),
315
+ is_error: import_v42.z.boolean(),
316
+ content: import_v42.z.array(
317
+ import_v42.z.union([
318
+ import_v42.z.string(),
319
+ import_v42.z.object({ type: import_v42.z.literal("text"), text: import_v42.z.string() })
320
+ ])
321
+ )
322
+ }),
287
323
  import_v42.z.object({
288
324
  type: import_v42.z.literal("web_fetch_tool_result"),
289
325
  tool_use_id: import_v42.z.string(),
@@ -536,7 +572,19 @@ var anthropicProviderOptions = import_v43.z.object({
536
572
  cacheControl: import_v43.z.object({
537
573
  type: import_v43.z.literal("ephemeral"),
538
574
  ttl: import_v43.z.union([import_v43.z.literal("5m"), import_v43.z.literal("1h")]).optional()
539
- }).optional()
575
+ }).optional(),
576
+ mcpServers: import_v43.z.array(
577
+ import_v43.z.object({
578
+ type: import_v43.z.literal("url"),
579
+ name: import_v43.z.string(),
580
+ url: import_v43.z.string(),
581
+ authorizationToken: import_v43.z.string().nullish(),
582
+ toolConfiguration: import_v43.z.object({
583
+ enabled: import_v43.z.boolean().nullish(),
584
+ allowedTools: import_v43.z.array(import_v43.z.string()).nullish()
585
+ }).nullish()
586
+ })
587
+ ).optional()
540
588
  });
541
589
 
542
590
  // src/anthropic-prepare-tools.ts
@@ -1057,7 +1105,7 @@ async function convertToAnthropicMessagesPrompt({
1057
1105
  sendReasoning,
1058
1106
  warnings
1059
1107
  }) {
1060
- var _a, _b, _c, _d, _e, _f;
1108
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1061
1109
  const betas = /* @__PURE__ */ new Set();
1062
1110
  const blocks = groupIntoBlocks(prompt);
1063
1111
  let system = void 0;
@@ -1275,6 +1323,7 @@ async function convertToAnthropicMessagesPrompt({
1275
1323
  }
1276
1324
  case "assistant": {
1277
1325
  const anthropicContent = [];
1326
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1278
1327
  for (let j = 0; j < block.messages.length; j++) {
1279
1328
  const message = block.messages[j];
1280
1329
  const isLastMessage = j === block.messages.length - 1;
@@ -1340,7 +1389,29 @@ async function convertToAnthropicMessagesPrompt({
1340
1389
  }
1341
1390
  case "tool-call": {
1342
1391
  if (part.providerExecuted) {
1343
- if (part.toolName === "code_execution" && part.input != null && typeof part.input === "object" && "type" in part.input && typeof part.input.type === "string" && (part.input.type === "bash_code_execution" || part.input.type === "text_editor_code_execution")) {
1392
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1393
+ if (isMcpToolUse) {
1394
+ mcpToolUseIds.add(part.toolCallId);
1395
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1396
+ if (serverName == null || typeof serverName !== "string") {
1397
+ warnings.push({
1398
+ type: "other",
1399
+ message: "mcp tool use server name is required and must be a string"
1400
+ });
1401
+ break;
1402
+ }
1403
+ anthropicContent.push({
1404
+ type: "mcp_tool_use",
1405
+ id: part.toolCallId,
1406
+ name: part.toolName,
1407
+ input: part.input,
1408
+ server_name: serverName,
1409
+ cache_control: cacheControl
1410
+ });
1411
+ } else if (
1412
+ // code execution 20250825:
1413
+ part.toolName === "code_execution" && part.input != null && typeof part.input === "object" && "type" in part.input && typeof part.input.type === "string" && (part.input.type === "bash_code_execution" || part.input.type === "text_editor_code_execution")
1414
+ ) {
1344
1415
  anthropicContent.push({
1345
1416
  type: "server_tool_use",
1346
1417
  id: part.toolCallId,
@@ -1376,7 +1447,23 @@ async function convertToAnthropicMessagesPrompt({
1376
1447
  break;
1377
1448
  }
1378
1449
  case "tool-result": {
1379
- if (part.toolName === "code_execution") {
1450
+ if (mcpToolUseIds.has(part.toolCallId)) {
1451
+ const output = part.output;
1452
+ if (output.type !== "json" && output.type !== "error-json") {
1453
+ warnings.push({
1454
+ type: "other",
1455
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1456
+ });
1457
+ break;
1458
+ }
1459
+ anthropicContent.push({
1460
+ type: "mcp_tool_result",
1461
+ tool_use_id: part.toolCallId,
1462
+ is_error: output.type === "error-json",
1463
+ content: output.value,
1464
+ cache_control: cacheControl
1465
+ });
1466
+ } else if (part.toolName === "code_execution") {
1380
1467
  const output = part.output;
1381
1468
  if (output.type !== "json") {
1382
1469
  warnings.push({
@@ -1691,7 +1778,7 @@ var AnthropicMessagesLanguageModel = class {
1691
1778
  providerOptions,
1692
1779
  schema: anthropicProviderOptions
1693
1780
  });
1694
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1781
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1695
1782
  prompt,
1696
1783
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1697
1784
  warnings
@@ -1713,6 +1800,19 @@ var AnthropicMessagesLanguageModel = class {
1713
1800
  ...isThinking && {
1714
1801
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1715
1802
  },
1803
+ // mcp servers:
1804
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1805
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1806
+ type: server.type,
1807
+ name: server.name,
1808
+ url: server.url,
1809
+ authorization_token: server.authorizationToken,
1810
+ tool_configuration: server.toolConfiguration ? {
1811
+ allowed_tools: server.toolConfiguration.allowedTools,
1812
+ enabled: server.toolConfiguration.enabled
1813
+ } : void 0
1814
+ }))
1815
+ },
1716
1816
  // prompt:
1717
1817
  system: messagesPrompt.system,
1718
1818
  messages: messagesPrompt.messages
@@ -1754,11 +1854,14 @@ var AnthropicMessagesLanguageModel = class {
1754
1854
  warnings.push({
1755
1855
  type: "unsupported-setting",
1756
1856
  setting: "maxOutputTokens",
1757
- details: `${maxTokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1857
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1758
1858
  });
1759
1859
  }
1760
1860
  baseArgs.max_tokens = maxOutputTokensForModel;
1761
1861
  }
1862
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1863
+ betas.add("mcp-client-2025-04-04");
1864
+ }
1762
1865
  const {
1763
1866
  tools: anthropicTools2,
1764
1867
  toolChoice: anthropicToolChoice,
@@ -1782,7 +1885,7 @@ var AnthropicMessagesLanguageModel = class {
1782
1885
  tool_choice: anthropicToolChoice
1783
1886
  },
1784
1887
  warnings: [...warnings, ...toolWarnings],
1785
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1888
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1786
1889
  usesJsonResponseTool: jsonResponseTool != null
1787
1890
  };
1788
1891
  }
@@ -1847,6 +1950,7 @@ var AnthropicMessagesLanguageModel = class {
1847
1950
  fetch: this.config.fetch
1848
1951
  });
1849
1952
  const content = [];
1953
+ const mcpToolCalls = {};
1850
1954
  for (const part of response.content) {
1851
1955
  switch (part.type) {
1852
1956
  case "text": {
@@ -1926,6 +2030,37 @@ var AnthropicMessagesLanguageModel = class {
1926
2030
  }
1927
2031
  break;
1928
2032
  }
2033
+ case "mcp_tool_use": {
2034
+ mcpToolCalls[part.id] = {
2035
+ type: "tool-call",
2036
+ toolCallId: part.id,
2037
+ toolName: part.name,
2038
+ input: JSON.stringify(part.input),
2039
+ providerExecuted: true,
2040
+ dynamic: true,
2041
+ providerMetadata: {
2042
+ anthropic: {
2043
+ type: "mcp-tool-use",
2044
+ serverName: part.server_name
2045
+ }
2046
+ }
2047
+ };
2048
+ content.push(mcpToolCalls[part.id]);
2049
+ break;
2050
+ }
2051
+ case "mcp_tool_result": {
2052
+ content.push({
2053
+ type: "tool-result",
2054
+ toolCallId: part.tool_use_id,
2055
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2056
+ isError: part.is_error,
2057
+ result: part.content,
2058
+ providerExecuted: true,
2059
+ dynamic: true,
2060
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2061
+ });
2062
+ break;
2063
+ }
1929
2064
  case "web_fetch_tool_result": {
1930
2065
  if (part.content.type === "web_fetch_result") {
1931
2066
  content.push({
@@ -2106,6 +2241,7 @@ var AnthropicMessagesLanguageModel = class {
2106
2241
  totalTokens: void 0
2107
2242
  };
2108
2243
  const contentBlocks = {};
2244
+ const mcpToolCalls = {};
2109
2245
  let rawUsage = void 0;
2110
2246
  let cacheCreationInputTokens = null;
2111
2247
  let stopSequence = null;
@@ -2132,7 +2268,8 @@ var AnthropicMessagesLanguageModel = class {
2132
2268
  return;
2133
2269
  }
2134
2270
  case "content_block_start": {
2135
- const contentBlockType = value.content_block.type;
2271
+ const part = value.content_block;
2272
+ const contentBlockType = part.type;
2136
2273
  blockType = contentBlockType;
2137
2274
  switch (contentBlockType) {
2138
2275
  case "text": {
@@ -2158,7 +2295,7 @@ var AnthropicMessagesLanguageModel = class {
2158
2295
  id: String(value.index),
2159
2296
  providerMetadata: {
2160
2297
  anthropic: {
2161
- redactedData: value.content_block.data
2298
+ redactedData: part.data
2162
2299
  }
2163
2300
  }
2164
2301
  });
@@ -2167,16 +2304,16 @@ var AnthropicMessagesLanguageModel = class {
2167
2304
  case "tool_use": {
2168
2305
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2169
2306
  type: "tool-call",
2170
- toolCallId: value.content_block.id,
2171
- toolName: value.content_block.name,
2307
+ toolCallId: part.id,
2308
+ toolName: part.name,
2172
2309
  input: "",
2173
2310
  firstDelta: true
2174
2311
  };
2175
2312
  controller.enqueue(
2176
2313
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2177
2314
  type: "tool-input-start",
2178
- id: value.content_block.id,
2179
- toolName: value.content_block.name
2315
+ id: part.id,
2316
+ toolName: part.name
2180
2317
  }
2181
2318
  );
2182
2319
  return;
@@ -2191,19 +2328,19 @@ var AnthropicMessagesLanguageModel = class {
2191
2328
  "text_editor_code_execution",
2192
2329
  // code execution 20250825 bash:
2193
2330
  "bash_code_execution"
2194
- ].includes(value.content_block.name)) {
2331
+ ].includes(part.name)) {
2195
2332
  contentBlocks[value.index] = {
2196
2333
  type: "tool-call",
2197
- toolCallId: value.content_block.id,
2198
- toolName: value.content_block.name,
2334
+ toolCallId: part.id,
2335
+ toolName: part.name,
2199
2336
  input: "",
2200
2337
  providerExecuted: true,
2201
2338
  firstDelta: true
2202
2339
  };
2203
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2340
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2204
2341
  controller.enqueue({
2205
2342
  type: "tool-input-start",
2206
- id: value.content_block.id,
2343
+ id: part.id,
2207
2344
  toolName: mappedToolName,
2208
2345
  providerExecuted: true
2209
2346
  });
@@ -2211,7 +2348,6 @@ var AnthropicMessagesLanguageModel = class {
2211
2348
  return;
2212
2349
  }
2213
2350
  case "web_fetch_tool_result": {
2214
- const part = value.content_block;
2215
2351
  if (part.content.type === "web_fetch_result") {
2216
2352
  controller.enqueue({
2217
2353
  type: "tool-result",
@@ -2249,7 +2385,6 @@ var AnthropicMessagesLanguageModel = class {
2249
2385
  return;
2250
2386
  }
2251
2387
  case "web_search_tool_result": {
2252
- const part = value.content_block;
2253
2388
  if (Array.isArray(part.content)) {
2254
2389
  controller.enqueue({
2255
2390
  type: "tool-result",
@@ -2298,7 +2433,6 @@ var AnthropicMessagesLanguageModel = class {
2298
2433
  }
2299
2434
  // code execution 20250522:
2300
2435
  case "code_execution_tool_result": {
2301
- const part = value.content_block;
2302
2436
  if (part.content.type === "code_execution_result") {
2303
2437
  controller.enqueue({
2304
2438
  type: "tool-result",
@@ -2330,7 +2464,6 @@ var AnthropicMessagesLanguageModel = class {
2330
2464
  // code execution 20250825:
2331
2465
  case "bash_code_execution_tool_result":
2332
2466
  case "text_editor_code_execution_tool_result": {
2333
- const part = value.content_block;
2334
2467
  controller.enqueue({
2335
2468
  type: "tool-result",
2336
2469
  toolCallId: part.tool_use_id,
@@ -2340,6 +2473,37 @@ var AnthropicMessagesLanguageModel = class {
2340
2473
  });
2341
2474
  return;
2342
2475
  }
2476
+ case "mcp_tool_use": {
2477
+ mcpToolCalls[part.id] = {
2478
+ type: "tool-call",
2479
+ toolCallId: part.id,
2480
+ toolName: part.name,
2481
+ input: JSON.stringify(part.input),
2482
+ providerExecuted: true,
2483
+ dynamic: true,
2484
+ providerMetadata: {
2485
+ anthropic: {
2486
+ type: "mcp-tool-use",
2487
+ serverName: part.server_name
2488
+ }
2489
+ }
2490
+ };
2491
+ controller.enqueue(mcpToolCalls[part.id]);
2492
+ return;
2493
+ }
2494
+ case "mcp_tool_result": {
2495
+ controller.enqueue({
2496
+ type: "tool-result",
2497
+ toolCallId: part.tool_use_id,
2498
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2499
+ isError: part.is_error,
2500
+ result: part.content,
2501
+ providerExecuted: true,
2502
+ dynamic: true,
2503
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2504
+ });
2505
+ return;
2506
+ }
2343
2507
  default: {
2344
2508
  const _exhaustiveCheck = contentBlockType;
2345
2509
  throw new Error(