@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.
@@ -98,6 +98,24 @@ var anthropicMessagesResponseSchema = lazySchema2(
98
98
  name: z2.string(),
99
99
  input: z2.record(z2.string(), z2.unknown()).nullish()
100
100
  }),
101
+ z2.object({
102
+ type: z2.literal("mcp_tool_use"),
103
+ id: z2.string(),
104
+ name: z2.string(),
105
+ input: z2.unknown(),
106
+ server_name: z2.string()
107
+ }),
108
+ z2.object({
109
+ type: z2.literal("mcp_tool_result"),
110
+ tool_use_id: z2.string(),
111
+ is_error: z2.boolean(),
112
+ content: z2.array(
113
+ z2.union([
114
+ z2.string(),
115
+ z2.object({ type: z2.literal("text"), text: z2.string() })
116
+ ])
117
+ )
118
+ }),
101
119
  z2.object({
102
120
  type: z2.literal("web_fetch_tool_result"),
103
121
  tool_use_id: z2.string(),
@@ -270,6 +288,24 @@ var anthropicMessagesChunkSchema = lazySchema2(
270
288
  name: z2.string(),
271
289
  input: z2.record(z2.string(), z2.unknown()).nullish()
272
290
  }),
291
+ z2.object({
292
+ type: z2.literal("mcp_tool_use"),
293
+ id: z2.string(),
294
+ name: z2.string(),
295
+ input: z2.unknown(),
296
+ server_name: z2.string()
297
+ }),
298
+ z2.object({
299
+ type: z2.literal("mcp_tool_result"),
300
+ tool_use_id: z2.string(),
301
+ is_error: z2.boolean(),
302
+ content: z2.array(
303
+ z2.union([
304
+ z2.string(),
305
+ z2.object({ type: z2.literal("text"), text: z2.string() })
306
+ ])
307
+ )
308
+ }),
273
309
  z2.object({
274
310
  type: z2.literal("web_fetch_tool_result"),
275
311
  tool_use_id: z2.string(),
@@ -522,7 +558,19 @@ var anthropicProviderOptions = z3.object({
522
558
  cacheControl: z3.object({
523
559
  type: z3.literal("ephemeral"),
524
560
  ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional()
525
- }).optional()
561
+ }).optional(),
562
+ mcpServers: z3.array(
563
+ z3.object({
564
+ type: z3.literal("url"),
565
+ name: z3.string(),
566
+ url: z3.string(),
567
+ authorizationToken: z3.string().nullish(),
568
+ toolConfiguration: z3.object({
569
+ enabled: z3.boolean().nullish(),
570
+ allowedTools: z3.array(z3.string()).nullish()
571
+ }).nullish()
572
+ })
573
+ ).optional()
526
574
  });
527
575
 
528
576
  // src/anthropic-prepare-tools.ts
@@ -1067,7 +1115,7 @@ async function convertToAnthropicMessagesPrompt({
1067
1115
  sendReasoning,
1068
1116
  warnings
1069
1117
  }) {
1070
- var _a, _b, _c, _d, _e, _f;
1118
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1071
1119
  const betas = /* @__PURE__ */ new Set();
1072
1120
  const blocks = groupIntoBlocks(prompt);
1073
1121
  let system = void 0;
@@ -1285,6 +1333,7 @@ async function convertToAnthropicMessagesPrompt({
1285
1333
  }
1286
1334
  case "assistant": {
1287
1335
  const anthropicContent = [];
1336
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1288
1337
  for (let j = 0; j < block.messages.length; j++) {
1289
1338
  const message = block.messages[j];
1290
1339
  const isLastMessage = j === block.messages.length - 1;
@@ -1350,7 +1399,29 @@ async function convertToAnthropicMessagesPrompt({
1350
1399
  }
1351
1400
  case "tool-call": {
1352
1401
  if (part.providerExecuted) {
1353
- 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")) {
1402
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1403
+ if (isMcpToolUse) {
1404
+ mcpToolUseIds.add(part.toolCallId);
1405
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1406
+ if (serverName == null || typeof serverName !== "string") {
1407
+ warnings.push({
1408
+ type: "other",
1409
+ message: "mcp tool use server name is required and must be a string"
1410
+ });
1411
+ break;
1412
+ }
1413
+ anthropicContent.push({
1414
+ type: "mcp_tool_use",
1415
+ id: part.toolCallId,
1416
+ name: part.toolName,
1417
+ input: part.input,
1418
+ server_name: serverName,
1419
+ cache_control: cacheControl
1420
+ });
1421
+ } else if (
1422
+ // code execution 20250825:
1423
+ 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")
1424
+ ) {
1354
1425
  anthropicContent.push({
1355
1426
  type: "server_tool_use",
1356
1427
  id: part.toolCallId,
@@ -1386,7 +1457,23 @@ async function convertToAnthropicMessagesPrompt({
1386
1457
  break;
1387
1458
  }
1388
1459
  case "tool-result": {
1389
- if (part.toolName === "code_execution") {
1460
+ if (mcpToolUseIds.has(part.toolCallId)) {
1461
+ const output = part.output;
1462
+ if (output.type !== "json" && output.type !== "error-json") {
1463
+ warnings.push({
1464
+ type: "other",
1465
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1466
+ });
1467
+ break;
1468
+ }
1469
+ anthropicContent.push({
1470
+ type: "mcp_tool_result",
1471
+ tool_use_id: part.toolCallId,
1472
+ is_error: output.type === "error-json",
1473
+ content: output.value,
1474
+ cache_control: cacheControl
1475
+ });
1476
+ } else if (part.toolName === "code_execution") {
1390
1477
  const output = part.output;
1391
1478
  if (output.type !== "json") {
1392
1479
  warnings.push({
@@ -1642,8 +1729,7 @@ var AnthropicMessagesLanguageModel = class {
1642
1729
  }
1643
1730
  async getArgs({
1644
1731
  prompt,
1645
- maxOutputTokens = 4096,
1646
- // 4096: max model output tokens TODO update default in v5
1732
+ maxOutputTokens,
1647
1733
  temperature,
1648
1734
  topP,
1649
1735
  topK,
@@ -1702,18 +1788,20 @@ var AnthropicMessagesLanguageModel = class {
1702
1788
  providerOptions,
1703
1789
  schema: anthropicProviderOptions
1704
1790
  });
1705
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1791
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1706
1792
  prompt,
1707
1793
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1708
1794
  warnings
1709
1795
  });
1710
1796
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1711
1797
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1798
+ const maxOutputTokensForModel = getMaxOutputTokensForModel(this.modelId);
1799
+ const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1712
1800
  const baseArgs = {
1713
1801
  // model id:
1714
1802
  model: this.modelId,
1715
1803
  // standardized settings:
1716
- max_tokens: maxOutputTokens,
1804
+ max_tokens: maxTokens,
1717
1805
  temperature,
1718
1806
  top_k: topK,
1719
1807
  top_p: topP,
@@ -1722,6 +1810,19 @@ var AnthropicMessagesLanguageModel = class {
1722
1810
  ...isThinking && {
1723
1811
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1724
1812
  },
1813
+ // mcp servers:
1814
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1815
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1816
+ type: server.type,
1817
+ name: server.name,
1818
+ url: server.url,
1819
+ authorization_token: server.authorizationToken,
1820
+ tool_configuration: server.toolConfiguration ? {
1821
+ allowed_tools: server.toolConfiguration.allowedTools,
1822
+ enabled: server.toolConfiguration.enabled
1823
+ } : void 0
1824
+ }))
1825
+ },
1725
1826
  // prompt:
1726
1827
  system: messagesPrompt.system,
1727
1828
  messages: messagesPrompt.messages
@@ -1756,7 +1857,20 @@ var AnthropicMessagesLanguageModel = class {
1756
1857
  details: "topP is not supported when thinking is enabled"
1757
1858
  });
1758
1859
  }
1759
- baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
1860
+ baseArgs.max_tokens = maxTokens + thinkingBudget;
1861
+ }
1862
+ if (baseArgs.max_tokens > maxOutputTokensForModel) {
1863
+ if (maxOutputTokens != null) {
1864
+ warnings.push({
1865
+ type: "unsupported-setting",
1866
+ setting: "maxOutputTokens",
1867
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1868
+ });
1869
+ }
1870
+ baseArgs.max_tokens = maxOutputTokensForModel;
1871
+ }
1872
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1873
+ betas.add("mcp-client-2025-04-04");
1760
1874
  }
1761
1875
  const {
1762
1876
  tools: anthropicTools2,
@@ -1781,7 +1895,7 @@ var AnthropicMessagesLanguageModel = class {
1781
1895
  tool_choice: anthropicToolChoice
1782
1896
  },
1783
1897
  warnings: [...warnings, ...toolWarnings],
1784
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1898
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1785
1899
  usesJsonResponseTool: jsonResponseTool != null
1786
1900
  };
1787
1901
  }
@@ -1846,6 +1960,7 @@ var AnthropicMessagesLanguageModel = class {
1846
1960
  fetch: this.config.fetch
1847
1961
  });
1848
1962
  const content = [];
1963
+ const mcpToolCalls = {};
1849
1964
  for (const part of response.content) {
1850
1965
  switch (part.type) {
1851
1966
  case "text": {
@@ -1925,6 +2040,37 @@ var AnthropicMessagesLanguageModel = class {
1925
2040
  }
1926
2041
  break;
1927
2042
  }
2043
+ case "mcp_tool_use": {
2044
+ mcpToolCalls[part.id] = {
2045
+ type: "tool-call",
2046
+ toolCallId: part.id,
2047
+ toolName: part.name,
2048
+ input: JSON.stringify(part.input),
2049
+ providerExecuted: true,
2050
+ dynamic: true,
2051
+ providerMetadata: {
2052
+ anthropic: {
2053
+ type: "mcp-tool-use",
2054
+ serverName: part.server_name
2055
+ }
2056
+ }
2057
+ };
2058
+ content.push(mcpToolCalls[part.id]);
2059
+ break;
2060
+ }
2061
+ case "mcp_tool_result": {
2062
+ content.push({
2063
+ type: "tool-result",
2064
+ toolCallId: part.tool_use_id,
2065
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2066
+ isError: part.is_error,
2067
+ result: part.content,
2068
+ providerExecuted: true,
2069
+ dynamic: true,
2070
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2071
+ });
2072
+ break;
2073
+ }
1928
2074
  case "web_fetch_tool_result": {
1929
2075
  if (part.content.type === "web_fetch_result") {
1930
2076
  content.push({
@@ -2105,6 +2251,7 @@ var AnthropicMessagesLanguageModel = class {
2105
2251
  totalTokens: void 0
2106
2252
  };
2107
2253
  const contentBlocks = {};
2254
+ const mcpToolCalls = {};
2108
2255
  let rawUsage = void 0;
2109
2256
  let cacheCreationInputTokens = null;
2110
2257
  let stopSequence = null;
@@ -2131,7 +2278,8 @@ var AnthropicMessagesLanguageModel = class {
2131
2278
  return;
2132
2279
  }
2133
2280
  case "content_block_start": {
2134
- const contentBlockType = value.content_block.type;
2281
+ const part = value.content_block;
2282
+ const contentBlockType = part.type;
2135
2283
  blockType = contentBlockType;
2136
2284
  switch (contentBlockType) {
2137
2285
  case "text": {
@@ -2157,7 +2305,7 @@ var AnthropicMessagesLanguageModel = class {
2157
2305
  id: String(value.index),
2158
2306
  providerMetadata: {
2159
2307
  anthropic: {
2160
- redactedData: value.content_block.data
2308
+ redactedData: part.data
2161
2309
  }
2162
2310
  }
2163
2311
  });
@@ -2166,16 +2314,16 @@ var AnthropicMessagesLanguageModel = class {
2166
2314
  case "tool_use": {
2167
2315
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2168
2316
  type: "tool-call",
2169
- toolCallId: value.content_block.id,
2170
- toolName: value.content_block.name,
2317
+ toolCallId: part.id,
2318
+ toolName: part.name,
2171
2319
  input: "",
2172
2320
  firstDelta: true
2173
2321
  };
2174
2322
  controller.enqueue(
2175
2323
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2176
2324
  type: "tool-input-start",
2177
- id: value.content_block.id,
2178
- toolName: value.content_block.name
2325
+ id: part.id,
2326
+ toolName: part.name
2179
2327
  }
2180
2328
  );
2181
2329
  return;
@@ -2190,19 +2338,19 @@ var AnthropicMessagesLanguageModel = class {
2190
2338
  "text_editor_code_execution",
2191
2339
  // code execution 20250825 bash:
2192
2340
  "bash_code_execution"
2193
- ].includes(value.content_block.name)) {
2341
+ ].includes(part.name)) {
2194
2342
  contentBlocks[value.index] = {
2195
2343
  type: "tool-call",
2196
- toolCallId: value.content_block.id,
2197
- toolName: value.content_block.name,
2344
+ toolCallId: part.id,
2345
+ toolName: part.name,
2198
2346
  input: "",
2199
2347
  providerExecuted: true,
2200
2348
  firstDelta: true
2201
2349
  };
2202
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2350
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2203
2351
  controller.enqueue({
2204
2352
  type: "tool-input-start",
2205
- id: value.content_block.id,
2353
+ id: part.id,
2206
2354
  toolName: mappedToolName,
2207
2355
  providerExecuted: true
2208
2356
  });
@@ -2210,7 +2358,6 @@ var AnthropicMessagesLanguageModel = class {
2210
2358
  return;
2211
2359
  }
2212
2360
  case "web_fetch_tool_result": {
2213
- const part = value.content_block;
2214
2361
  if (part.content.type === "web_fetch_result") {
2215
2362
  controller.enqueue({
2216
2363
  type: "tool-result",
@@ -2248,7 +2395,6 @@ var AnthropicMessagesLanguageModel = class {
2248
2395
  return;
2249
2396
  }
2250
2397
  case "web_search_tool_result": {
2251
- const part = value.content_block;
2252
2398
  if (Array.isArray(part.content)) {
2253
2399
  controller.enqueue({
2254
2400
  type: "tool-result",
@@ -2297,7 +2443,6 @@ var AnthropicMessagesLanguageModel = class {
2297
2443
  }
2298
2444
  // code execution 20250522:
2299
2445
  case "code_execution_tool_result": {
2300
- const part = value.content_block;
2301
2446
  if (part.content.type === "code_execution_result") {
2302
2447
  controller.enqueue({
2303
2448
  type: "tool-result",
@@ -2329,7 +2474,6 @@ var AnthropicMessagesLanguageModel = class {
2329
2474
  // code execution 20250825:
2330
2475
  case "bash_code_execution_tool_result":
2331
2476
  case "text_editor_code_execution_tool_result": {
2332
- const part = value.content_block;
2333
2477
  controller.enqueue({
2334
2478
  type: "tool-result",
2335
2479
  toolCallId: part.tool_use_id,
@@ -2339,6 +2483,37 @@ var AnthropicMessagesLanguageModel = class {
2339
2483
  });
2340
2484
  return;
2341
2485
  }
2486
+ case "mcp_tool_use": {
2487
+ mcpToolCalls[part.id] = {
2488
+ type: "tool-call",
2489
+ toolCallId: part.id,
2490
+ toolName: part.name,
2491
+ input: JSON.stringify(part.input),
2492
+ providerExecuted: true,
2493
+ dynamic: true,
2494
+ providerMetadata: {
2495
+ anthropic: {
2496
+ type: "mcp-tool-use",
2497
+ serverName: part.server_name
2498
+ }
2499
+ }
2500
+ };
2501
+ controller.enqueue(mcpToolCalls[part.id]);
2502
+ return;
2503
+ }
2504
+ case "mcp_tool_result": {
2505
+ controller.enqueue({
2506
+ type: "tool-result",
2507
+ toolCallId: part.tool_use_id,
2508
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2509
+ isError: part.is_error,
2510
+ result: part.content,
2511
+ providerExecuted: true,
2512
+ dynamic: true,
2513
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2514
+ });
2515
+ return;
2516
+ }
2342
2517
  default: {
2343
2518
  const _exhaustiveCheck = contentBlockType;
2344
2519
  throw new Error(
@@ -2536,6 +2711,17 @@ var AnthropicMessagesLanguageModel = class {
2536
2711
  };
2537
2712
  }
2538
2713
  };
2714
+ function getMaxOutputTokensForModel(modelId) {
2715
+ if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2716
+ return 64e3;
2717
+ } else if (modelId.includes("claude-opus-4-")) {
2718
+ return 32e3;
2719
+ } else if (modelId.includes("claude-3-5-haiku")) {
2720
+ return 8192;
2721
+ } else {
2722
+ return 4096;
2723
+ }
2724
+ }
2539
2725
 
2540
2726
  // src/tool/bash_20241022.ts
2541
2727
  import {