@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.
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  } from "@ai-sdk/provider-utils";
11
11
 
12
12
  // src/version.ts
13
- var VERSION = true ? "3.0.0-beta.26" : "0.0.0-test";
13
+ var VERSION = true ? "3.0.0-beta.28" : "0.0.0-test";
14
14
 
15
15
  // src/anthropic-messages-language-model.ts
16
16
  import {
@@ -112,6 +112,24 @@ var anthropicMessagesResponseSchema = lazySchema2(
112
112
  name: z2.string(),
113
113
  input: z2.record(z2.string(), z2.unknown()).nullish()
114
114
  }),
115
+ z2.object({
116
+ type: z2.literal("mcp_tool_use"),
117
+ id: z2.string(),
118
+ name: z2.string(),
119
+ input: z2.unknown(),
120
+ server_name: z2.string()
121
+ }),
122
+ z2.object({
123
+ type: z2.literal("mcp_tool_result"),
124
+ tool_use_id: z2.string(),
125
+ is_error: z2.boolean(),
126
+ content: z2.array(
127
+ z2.union([
128
+ z2.string(),
129
+ z2.object({ type: z2.literal("text"), text: z2.string() })
130
+ ])
131
+ )
132
+ }),
115
133
  z2.object({
116
134
  type: z2.literal("web_fetch_tool_result"),
117
135
  tool_use_id: z2.string(),
@@ -284,6 +302,24 @@ var anthropicMessagesChunkSchema = lazySchema2(
284
302
  name: z2.string(),
285
303
  input: z2.record(z2.string(), z2.unknown()).nullish()
286
304
  }),
305
+ z2.object({
306
+ type: z2.literal("mcp_tool_use"),
307
+ id: z2.string(),
308
+ name: z2.string(),
309
+ input: z2.unknown(),
310
+ server_name: z2.string()
311
+ }),
312
+ z2.object({
313
+ type: z2.literal("mcp_tool_result"),
314
+ tool_use_id: z2.string(),
315
+ is_error: z2.boolean(),
316
+ content: z2.array(
317
+ z2.union([
318
+ z2.string(),
319
+ z2.object({ type: z2.literal("text"), text: z2.string() })
320
+ ])
321
+ )
322
+ }),
287
323
  z2.object({
288
324
  type: z2.literal("web_fetch_tool_result"),
289
325
  tool_use_id: z2.string(),
@@ -536,7 +572,19 @@ var anthropicProviderOptions = z3.object({
536
572
  cacheControl: z3.object({
537
573
  type: z3.literal("ephemeral"),
538
574
  ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional()
539
- }).optional()
575
+ }).optional(),
576
+ mcpServers: z3.array(
577
+ z3.object({
578
+ type: z3.literal("url"),
579
+ name: z3.string(),
580
+ url: z3.string(),
581
+ authorizationToken: z3.string().nullish(),
582
+ toolConfiguration: z3.object({
583
+ enabled: z3.boolean().nullish(),
584
+ allowedTools: z3.array(z3.string()).nullish()
585
+ }).nullish()
586
+ })
587
+ ).optional()
540
588
  });
541
589
 
542
590
  // src/anthropic-prepare-tools.ts
@@ -1081,7 +1129,7 @@ async function convertToAnthropicMessagesPrompt({
1081
1129
  sendReasoning,
1082
1130
  warnings
1083
1131
  }) {
1084
- var _a, _b, _c, _d, _e, _f;
1132
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1085
1133
  const betas = /* @__PURE__ */ new Set();
1086
1134
  const blocks = groupIntoBlocks(prompt);
1087
1135
  let system = void 0;
@@ -1299,6 +1347,7 @@ async function convertToAnthropicMessagesPrompt({
1299
1347
  }
1300
1348
  case "assistant": {
1301
1349
  const anthropicContent = [];
1350
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1302
1351
  for (let j = 0; j < block.messages.length; j++) {
1303
1352
  const message = block.messages[j];
1304
1353
  const isLastMessage = j === block.messages.length - 1;
@@ -1364,7 +1413,29 @@ async function convertToAnthropicMessagesPrompt({
1364
1413
  }
1365
1414
  case "tool-call": {
1366
1415
  if (part.providerExecuted) {
1367
- 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")) {
1416
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1417
+ if (isMcpToolUse) {
1418
+ mcpToolUseIds.add(part.toolCallId);
1419
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1420
+ if (serverName == null || typeof serverName !== "string") {
1421
+ warnings.push({
1422
+ type: "other",
1423
+ message: "mcp tool use server name is required and must be a string"
1424
+ });
1425
+ break;
1426
+ }
1427
+ anthropicContent.push({
1428
+ type: "mcp_tool_use",
1429
+ id: part.toolCallId,
1430
+ name: part.toolName,
1431
+ input: part.input,
1432
+ server_name: serverName,
1433
+ cache_control: cacheControl
1434
+ });
1435
+ } else if (
1436
+ // code execution 20250825:
1437
+ 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")
1438
+ ) {
1368
1439
  anthropicContent.push({
1369
1440
  type: "server_tool_use",
1370
1441
  id: part.toolCallId,
@@ -1400,7 +1471,23 @@ async function convertToAnthropicMessagesPrompt({
1400
1471
  break;
1401
1472
  }
1402
1473
  case "tool-result": {
1403
- if (part.toolName === "code_execution") {
1474
+ if (mcpToolUseIds.has(part.toolCallId)) {
1475
+ const output = part.output;
1476
+ if (output.type !== "json" && output.type !== "error-json") {
1477
+ warnings.push({
1478
+ type: "other",
1479
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1480
+ });
1481
+ break;
1482
+ }
1483
+ anthropicContent.push({
1484
+ type: "mcp_tool_result",
1485
+ tool_use_id: part.toolCallId,
1486
+ is_error: output.type === "error-json",
1487
+ content: output.value,
1488
+ cache_control: cacheControl
1489
+ });
1490
+ } else if (part.toolName === "code_execution") {
1404
1491
  const output = part.output;
1405
1492
  if (output.type !== "json") {
1406
1493
  warnings.push({
@@ -1656,8 +1743,7 @@ var AnthropicMessagesLanguageModel = class {
1656
1743
  }
1657
1744
  async getArgs({
1658
1745
  prompt,
1659
- maxOutputTokens = 4096,
1660
- // 4096: max model output tokens TODO update default in v5
1746
+ maxOutputTokens,
1661
1747
  temperature,
1662
1748
  topP,
1663
1749
  topK,
@@ -1716,18 +1802,20 @@ var AnthropicMessagesLanguageModel = class {
1716
1802
  providerOptions,
1717
1803
  schema: anthropicProviderOptions
1718
1804
  });
1719
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1805
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1720
1806
  prompt,
1721
1807
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1722
1808
  warnings
1723
1809
  });
1724
1810
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1725
1811
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1812
+ const maxOutputTokensForModel = getMaxOutputTokensForModel(this.modelId);
1813
+ const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1726
1814
  const baseArgs = {
1727
1815
  // model id:
1728
1816
  model: this.modelId,
1729
1817
  // standardized settings:
1730
- max_tokens: maxOutputTokens,
1818
+ max_tokens: maxTokens,
1731
1819
  temperature,
1732
1820
  top_k: topK,
1733
1821
  top_p: topP,
@@ -1736,6 +1824,19 @@ var AnthropicMessagesLanguageModel = class {
1736
1824
  ...isThinking && {
1737
1825
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1738
1826
  },
1827
+ // mcp servers:
1828
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1829
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1830
+ type: server.type,
1831
+ name: server.name,
1832
+ url: server.url,
1833
+ authorization_token: server.authorizationToken,
1834
+ tool_configuration: server.toolConfiguration ? {
1835
+ allowed_tools: server.toolConfiguration.allowedTools,
1836
+ enabled: server.toolConfiguration.enabled
1837
+ } : void 0
1838
+ }))
1839
+ },
1739
1840
  // prompt:
1740
1841
  system: messagesPrompt.system,
1741
1842
  messages: messagesPrompt.messages
@@ -1770,7 +1871,20 @@ var AnthropicMessagesLanguageModel = class {
1770
1871
  details: "topP is not supported when thinking is enabled"
1771
1872
  });
1772
1873
  }
1773
- baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
1874
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
1875
+ }
1876
+ if (baseArgs.max_tokens > maxOutputTokensForModel) {
1877
+ if (maxOutputTokens != null) {
1878
+ warnings.push({
1879
+ type: "unsupported-setting",
1880
+ setting: "maxOutputTokens",
1881
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1882
+ });
1883
+ }
1884
+ baseArgs.max_tokens = maxOutputTokensForModel;
1885
+ }
1886
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1887
+ betas.add("mcp-client-2025-04-04");
1774
1888
  }
1775
1889
  const {
1776
1890
  tools: anthropicTools2,
@@ -1795,7 +1909,7 @@ var AnthropicMessagesLanguageModel = class {
1795
1909
  tool_choice: anthropicToolChoice
1796
1910
  },
1797
1911
  warnings: [...warnings, ...toolWarnings],
1798
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1912
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1799
1913
  usesJsonResponseTool: jsonResponseTool != null
1800
1914
  };
1801
1915
  }
@@ -1860,6 +1974,7 @@ var AnthropicMessagesLanguageModel = class {
1860
1974
  fetch: this.config.fetch
1861
1975
  });
1862
1976
  const content = [];
1977
+ const mcpToolCalls = {};
1863
1978
  for (const part of response.content) {
1864
1979
  switch (part.type) {
1865
1980
  case "text": {
@@ -1939,6 +2054,37 @@ var AnthropicMessagesLanguageModel = class {
1939
2054
  }
1940
2055
  break;
1941
2056
  }
2057
+ case "mcp_tool_use": {
2058
+ mcpToolCalls[part.id] = {
2059
+ type: "tool-call",
2060
+ toolCallId: part.id,
2061
+ toolName: part.name,
2062
+ input: JSON.stringify(part.input),
2063
+ providerExecuted: true,
2064
+ dynamic: true,
2065
+ providerMetadata: {
2066
+ anthropic: {
2067
+ type: "mcp-tool-use",
2068
+ serverName: part.server_name
2069
+ }
2070
+ }
2071
+ };
2072
+ content.push(mcpToolCalls[part.id]);
2073
+ break;
2074
+ }
2075
+ case "mcp_tool_result": {
2076
+ content.push({
2077
+ type: "tool-result",
2078
+ toolCallId: part.tool_use_id,
2079
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2080
+ isError: part.is_error,
2081
+ result: part.content,
2082
+ providerExecuted: true,
2083
+ dynamic: true,
2084
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2085
+ });
2086
+ break;
2087
+ }
1942
2088
  case "web_fetch_tool_result": {
1943
2089
  if (part.content.type === "web_fetch_result") {
1944
2090
  content.push({
@@ -2119,6 +2265,7 @@ var AnthropicMessagesLanguageModel = class {
2119
2265
  totalTokens: void 0
2120
2266
  };
2121
2267
  const contentBlocks = {};
2268
+ const mcpToolCalls = {};
2122
2269
  let rawUsage = void 0;
2123
2270
  let cacheCreationInputTokens = null;
2124
2271
  let stopSequence = null;
@@ -2145,7 +2292,8 @@ var AnthropicMessagesLanguageModel = class {
2145
2292
  return;
2146
2293
  }
2147
2294
  case "content_block_start": {
2148
- const contentBlockType = value.content_block.type;
2295
+ const part = value.content_block;
2296
+ const contentBlockType = part.type;
2149
2297
  blockType = contentBlockType;
2150
2298
  switch (contentBlockType) {
2151
2299
  case "text": {
@@ -2171,7 +2319,7 @@ var AnthropicMessagesLanguageModel = class {
2171
2319
  id: String(value.index),
2172
2320
  providerMetadata: {
2173
2321
  anthropic: {
2174
- redactedData: value.content_block.data
2322
+ redactedData: part.data
2175
2323
  }
2176
2324
  }
2177
2325
  });
@@ -2180,16 +2328,16 @@ var AnthropicMessagesLanguageModel = class {
2180
2328
  case "tool_use": {
2181
2329
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2182
2330
  type: "tool-call",
2183
- toolCallId: value.content_block.id,
2184
- toolName: value.content_block.name,
2331
+ toolCallId: part.id,
2332
+ toolName: part.name,
2185
2333
  input: "",
2186
2334
  firstDelta: true
2187
2335
  };
2188
2336
  controller.enqueue(
2189
2337
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2190
2338
  type: "tool-input-start",
2191
- id: value.content_block.id,
2192
- toolName: value.content_block.name
2339
+ id: part.id,
2340
+ toolName: part.name
2193
2341
  }
2194
2342
  );
2195
2343
  return;
@@ -2204,19 +2352,19 @@ var AnthropicMessagesLanguageModel = class {
2204
2352
  "text_editor_code_execution",
2205
2353
  // code execution 20250825 bash:
2206
2354
  "bash_code_execution"
2207
- ].includes(value.content_block.name)) {
2355
+ ].includes(part.name)) {
2208
2356
  contentBlocks[value.index] = {
2209
2357
  type: "tool-call",
2210
- toolCallId: value.content_block.id,
2211
- toolName: value.content_block.name,
2358
+ toolCallId: part.id,
2359
+ toolName: part.name,
2212
2360
  input: "",
2213
2361
  providerExecuted: true,
2214
2362
  firstDelta: true
2215
2363
  };
2216
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2364
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2217
2365
  controller.enqueue({
2218
2366
  type: "tool-input-start",
2219
- id: value.content_block.id,
2367
+ id: part.id,
2220
2368
  toolName: mappedToolName,
2221
2369
  providerExecuted: true
2222
2370
  });
@@ -2224,7 +2372,6 @@ var AnthropicMessagesLanguageModel = class {
2224
2372
  return;
2225
2373
  }
2226
2374
  case "web_fetch_tool_result": {
2227
- const part = value.content_block;
2228
2375
  if (part.content.type === "web_fetch_result") {
2229
2376
  controller.enqueue({
2230
2377
  type: "tool-result",
@@ -2262,7 +2409,6 @@ var AnthropicMessagesLanguageModel = class {
2262
2409
  return;
2263
2410
  }
2264
2411
  case "web_search_tool_result": {
2265
- const part = value.content_block;
2266
2412
  if (Array.isArray(part.content)) {
2267
2413
  controller.enqueue({
2268
2414
  type: "tool-result",
@@ -2311,7 +2457,6 @@ var AnthropicMessagesLanguageModel = class {
2311
2457
  }
2312
2458
  // code execution 20250522:
2313
2459
  case "code_execution_tool_result": {
2314
- const part = value.content_block;
2315
2460
  if (part.content.type === "code_execution_result") {
2316
2461
  controller.enqueue({
2317
2462
  type: "tool-result",
@@ -2343,7 +2488,6 @@ var AnthropicMessagesLanguageModel = class {
2343
2488
  // code execution 20250825:
2344
2489
  case "bash_code_execution_tool_result":
2345
2490
  case "text_editor_code_execution_tool_result": {
2346
- const part = value.content_block;
2347
2491
  controller.enqueue({
2348
2492
  type: "tool-result",
2349
2493
  toolCallId: part.tool_use_id,
@@ -2353,6 +2497,37 @@ var AnthropicMessagesLanguageModel = class {
2353
2497
  });
2354
2498
  return;
2355
2499
  }
2500
+ case "mcp_tool_use": {
2501
+ mcpToolCalls[part.id] = {
2502
+ type: "tool-call",
2503
+ toolCallId: part.id,
2504
+ toolName: part.name,
2505
+ input: JSON.stringify(part.input),
2506
+ providerExecuted: true,
2507
+ dynamic: true,
2508
+ providerMetadata: {
2509
+ anthropic: {
2510
+ type: "mcp-tool-use",
2511
+ serverName: part.server_name
2512
+ }
2513
+ }
2514
+ };
2515
+ controller.enqueue(mcpToolCalls[part.id]);
2516
+ return;
2517
+ }
2518
+ case "mcp_tool_result": {
2519
+ controller.enqueue({
2520
+ type: "tool-result",
2521
+ toolCallId: part.tool_use_id,
2522
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2523
+ isError: part.is_error,
2524
+ result: part.content,
2525
+ providerExecuted: true,
2526
+ dynamic: true,
2527
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2528
+ });
2529
+ return;
2530
+ }
2356
2531
  default: {
2357
2532
  const _exhaustiveCheck = contentBlockType;
2358
2533
  throw new Error(
@@ -2550,6 +2725,17 @@ var AnthropicMessagesLanguageModel = class {
2550
2725
  };
2551
2726
  }
2552
2727
  };
2728
+ function getMaxOutputTokensForModel(modelId) {
2729
+ if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2730
+ return 64e3;
2731
+ } else if (modelId.includes("claude-opus-4-")) {
2732
+ return 32e3;
2733
+ } else if (modelId.includes("claude-3-5-haiku")) {
2734
+ return 8192;
2735
+ } else {
2736
+ return 4096;
2737
+ }
2738
+ }
2553
2739
 
2554
2740
  // src/tool/bash_20241022.ts
2555
2741
  import {