@ai-sdk/anthropic 3.0.0-beta.26 → 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({
@@ -1632,8 +1719,7 @@ var AnthropicMessagesLanguageModel = class {
1632
1719
  }
1633
1720
  async getArgs({
1634
1721
  prompt,
1635
- maxOutputTokens = 4096,
1636
- // 4096: max model output tokens TODO update default in v5
1722
+ maxOutputTokens,
1637
1723
  temperature,
1638
1724
  topP,
1639
1725
  topK,
@@ -1692,18 +1778,20 @@ var AnthropicMessagesLanguageModel = class {
1692
1778
  providerOptions,
1693
1779
  schema: anthropicProviderOptions
1694
1780
  });
1695
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1781
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1696
1782
  prompt,
1697
1783
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1698
1784
  warnings
1699
1785
  });
1700
1786
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1701
1787
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1788
+ const maxOutputTokensForModel = getMaxOutputTokensForModel(this.modelId);
1789
+ const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1702
1790
  const baseArgs = {
1703
1791
  // model id:
1704
1792
  model: this.modelId,
1705
1793
  // standardized settings:
1706
- max_tokens: maxOutputTokens,
1794
+ max_tokens: maxTokens,
1707
1795
  temperature,
1708
1796
  top_k: topK,
1709
1797
  top_p: topP,
@@ -1712,6 +1800,19 @@ var AnthropicMessagesLanguageModel = class {
1712
1800
  ...isThinking && {
1713
1801
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1714
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
+ },
1715
1816
  // prompt:
1716
1817
  system: messagesPrompt.system,
1717
1818
  messages: messagesPrompt.messages
@@ -1746,7 +1847,20 @@ var AnthropicMessagesLanguageModel = class {
1746
1847
  details: "topP is not supported when thinking is enabled"
1747
1848
  });
1748
1849
  }
1749
- baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
1850
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
1851
+ }
1852
+ if (baseArgs.max_tokens > maxOutputTokensForModel) {
1853
+ if (maxOutputTokens != null) {
1854
+ warnings.push({
1855
+ type: "unsupported-setting",
1856
+ setting: "maxOutputTokens",
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}.`
1858
+ });
1859
+ }
1860
+ baseArgs.max_tokens = maxOutputTokensForModel;
1861
+ }
1862
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1863
+ betas.add("mcp-client-2025-04-04");
1750
1864
  }
1751
1865
  const {
1752
1866
  tools: anthropicTools2,
@@ -1771,7 +1885,7 @@ var AnthropicMessagesLanguageModel = class {
1771
1885
  tool_choice: anthropicToolChoice
1772
1886
  },
1773
1887
  warnings: [...warnings, ...toolWarnings],
1774
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1888
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1775
1889
  usesJsonResponseTool: jsonResponseTool != null
1776
1890
  };
1777
1891
  }
@@ -1836,6 +1950,7 @@ var AnthropicMessagesLanguageModel = class {
1836
1950
  fetch: this.config.fetch
1837
1951
  });
1838
1952
  const content = [];
1953
+ const mcpToolCalls = {};
1839
1954
  for (const part of response.content) {
1840
1955
  switch (part.type) {
1841
1956
  case "text": {
@@ -1915,6 +2030,37 @@ var AnthropicMessagesLanguageModel = class {
1915
2030
  }
1916
2031
  break;
1917
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
+ }
1918
2064
  case "web_fetch_tool_result": {
1919
2065
  if (part.content.type === "web_fetch_result") {
1920
2066
  content.push({
@@ -2095,6 +2241,7 @@ var AnthropicMessagesLanguageModel = class {
2095
2241
  totalTokens: void 0
2096
2242
  };
2097
2243
  const contentBlocks = {};
2244
+ const mcpToolCalls = {};
2098
2245
  let rawUsage = void 0;
2099
2246
  let cacheCreationInputTokens = null;
2100
2247
  let stopSequence = null;
@@ -2121,7 +2268,8 @@ var AnthropicMessagesLanguageModel = class {
2121
2268
  return;
2122
2269
  }
2123
2270
  case "content_block_start": {
2124
- const contentBlockType = value.content_block.type;
2271
+ const part = value.content_block;
2272
+ const contentBlockType = part.type;
2125
2273
  blockType = contentBlockType;
2126
2274
  switch (contentBlockType) {
2127
2275
  case "text": {
@@ -2147,7 +2295,7 @@ var AnthropicMessagesLanguageModel = class {
2147
2295
  id: String(value.index),
2148
2296
  providerMetadata: {
2149
2297
  anthropic: {
2150
- redactedData: value.content_block.data
2298
+ redactedData: part.data
2151
2299
  }
2152
2300
  }
2153
2301
  });
@@ -2156,16 +2304,16 @@ var AnthropicMessagesLanguageModel = class {
2156
2304
  case "tool_use": {
2157
2305
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2158
2306
  type: "tool-call",
2159
- toolCallId: value.content_block.id,
2160
- toolName: value.content_block.name,
2307
+ toolCallId: part.id,
2308
+ toolName: part.name,
2161
2309
  input: "",
2162
2310
  firstDelta: true
2163
2311
  };
2164
2312
  controller.enqueue(
2165
2313
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2166
2314
  type: "tool-input-start",
2167
- id: value.content_block.id,
2168
- toolName: value.content_block.name
2315
+ id: part.id,
2316
+ toolName: part.name
2169
2317
  }
2170
2318
  );
2171
2319
  return;
@@ -2180,19 +2328,19 @@ var AnthropicMessagesLanguageModel = class {
2180
2328
  "text_editor_code_execution",
2181
2329
  // code execution 20250825 bash:
2182
2330
  "bash_code_execution"
2183
- ].includes(value.content_block.name)) {
2331
+ ].includes(part.name)) {
2184
2332
  contentBlocks[value.index] = {
2185
2333
  type: "tool-call",
2186
- toolCallId: value.content_block.id,
2187
- toolName: value.content_block.name,
2334
+ toolCallId: part.id,
2335
+ toolName: part.name,
2188
2336
  input: "",
2189
2337
  providerExecuted: true,
2190
2338
  firstDelta: true
2191
2339
  };
2192
- 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;
2193
2341
  controller.enqueue({
2194
2342
  type: "tool-input-start",
2195
- id: value.content_block.id,
2343
+ id: part.id,
2196
2344
  toolName: mappedToolName,
2197
2345
  providerExecuted: true
2198
2346
  });
@@ -2200,7 +2348,6 @@ var AnthropicMessagesLanguageModel = class {
2200
2348
  return;
2201
2349
  }
2202
2350
  case "web_fetch_tool_result": {
2203
- const part = value.content_block;
2204
2351
  if (part.content.type === "web_fetch_result") {
2205
2352
  controller.enqueue({
2206
2353
  type: "tool-result",
@@ -2238,7 +2385,6 @@ var AnthropicMessagesLanguageModel = class {
2238
2385
  return;
2239
2386
  }
2240
2387
  case "web_search_tool_result": {
2241
- const part = value.content_block;
2242
2388
  if (Array.isArray(part.content)) {
2243
2389
  controller.enqueue({
2244
2390
  type: "tool-result",
@@ -2287,7 +2433,6 @@ var AnthropicMessagesLanguageModel = class {
2287
2433
  }
2288
2434
  // code execution 20250522:
2289
2435
  case "code_execution_tool_result": {
2290
- const part = value.content_block;
2291
2436
  if (part.content.type === "code_execution_result") {
2292
2437
  controller.enqueue({
2293
2438
  type: "tool-result",
@@ -2319,7 +2464,6 @@ var AnthropicMessagesLanguageModel = class {
2319
2464
  // code execution 20250825:
2320
2465
  case "bash_code_execution_tool_result":
2321
2466
  case "text_editor_code_execution_tool_result": {
2322
- const part = value.content_block;
2323
2467
  controller.enqueue({
2324
2468
  type: "tool-result",
2325
2469
  toolCallId: part.tool_use_id,
@@ -2329,6 +2473,37 @@ var AnthropicMessagesLanguageModel = class {
2329
2473
  });
2330
2474
  return;
2331
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
+ }
2332
2507
  default: {
2333
2508
  const _exhaustiveCheck = contentBlockType;
2334
2509
  throw new Error(
@@ -2526,6 +2701,17 @@ var AnthropicMessagesLanguageModel = class {
2526
2701
  };
2527
2702
  }
2528
2703
  };
2704
+ function getMaxOutputTokensForModel(modelId) {
2705
+ if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2706
+ return 64e3;
2707
+ } else if (modelId.includes("claude-opus-4-")) {
2708
+ return 32e3;
2709
+ } else if (modelId.includes("claude-3-5-haiku")) {
2710
+ return 8192;
2711
+ } else {
2712
+ return 4096;
2713
+ }
2714
+ }
2529
2715
 
2530
2716
  // src/tool/bash_20241022.ts
2531
2717
  var import_provider_utils12 = require("@ai-sdk/provider-utils");