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

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
@@ -915,7 +963,8 @@ import {
915
963
  import {
916
964
  convertToBase64,
917
965
  parseProviderOptions,
918
- validateTypes as validateTypes2
966
+ validateTypes as validateTypes2,
967
+ isNonNullable
919
968
  } from "@ai-sdk/provider-utils";
920
969
 
921
970
  // src/tool/code-execution_20250522.ts
@@ -1067,7 +1116,7 @@ async function convertToAnthropicMessagesPrompt({
1067
1116
  sendReasoning,
1068
1117
  warnings
1069
1118
  }) {
1070
- var _a, _b, _c, _d, _e, _f;
1119
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1071
1120
  const betas = /* @__PURE__ */ new Set();
1072
1121
  const blocks = groupIntoBlocks(prompt);
1073
1122
  let system = void 0;
@@ -1218,20 +1267,20 @@ async function convertToAnthropicMessagesPrompt({
1218
1267
  return {
1219
1268
  type: "text",
1220
1269
  text: contentPart.text,
1221
- cache_control: void 0
1270
+ cache_control: cacheControl
1222
1271
  };
1223
- case "media": {
1224
- if (contentPart.mediaType.startsWith("image/")) {
1225
- return {
1226
- type: "image",
1227
- source: {
1228
- type: "base64",
1229
- media_type: contentPart.mediaType,
1230
- data: contentPart.data
1231
- },
1232
- cache_control: void 0
1233
- };
1234
- }
1272
+ case "image-data": {
1273
+ return {
1274
+ type: "image",
1275
+ source: {
1276
+ type: "base64",
1277
+ media_type: contentPart.mediaType,
1278
+ data: contentPart.data
1279
+ },
1280
+ cache_control: cacheControl
1281
+ };
1282
+ }
1283
+ case "file-data": {
1235
1284
  if (contentPart.mediaType === "application/pdf") {
1236
1285
  betas.add("pdfs-2024-09-25");
1237
1286
  return {
@@ -1241,15 +1290,24 @@ async function convertToAnthropicMessagesPrompt({
1241
1290
  media_type: contentPart.mediaType,
1242
1291
  data: contentPart.data
1243
1292
  },
1244
- cache_control: void 0
1293
+ cache_control: cacheControl
1245
1294
  };
1246
1295
  }
1247
- throw new UnsupportedFunctionalityError2({
1248
- functionality: `media type: ${contentPart.mediaType}`
1296
+ warnings.push({
1297
+ type: "other",
1298
+ message: `unsupported tool content part type: ${contentPart.type} with media type: ${contentPart.mediaType}`
1249
1299
  });
1300
+ return void 0;
1301
+ }
1302
+ default: {
1303
+ warnings.push({
1304
+ type: "other",
1305
+ message: `unsupported tool content part type: ${contentPart.type}`
1306
+ });
1307
+ return void 0;
1250
1308
  }
1251
1309
  }
1252
- });
1310
+ }).filter(isNonNullable);
1253
1311
  break;
1254
1312
  case "text":
1255
1313
  case "error-text":
@@ -1285,6 +1343,7 @@ async function convertToAnthropicMessagesPrompt({
1285
1343
  }
1286
1344
  case "assistant": {
1287
1345
  const anthropicContent = [];
1346
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1288
1347
  for (let j = 0; j < block.messages.length; j++) {
1289
1348
  const message = block.messages[j];
1290
1349
  const isLastMessage = j === block.messages.length - 1;
@@ -1350,7 +1409,29 @@ async function convertToAnthropicMessagesPrompt({
1350
1409
  }
1351
1410
  case "tool-call": {
1352
1411
  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")) {
1412
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1413
+ if (isMcpToolUse) {
1414
+ mcpToolUseIds.add(part.toolCallId);
1415
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1416
+ if (serverName == null || typeof serverName !== "string") {
1417
+ warnings.push({
1418
+ type: "other",
1419
+ message: "mcp tool use server name is required and must be a string"
1420
+ });
1421
+ break;
1422
+ }
1423
+ anthropicContent.push({
1424
+ type: "mcp_tool_use",
1425
+ id: part.toolCallId,
1426
+ name: part.toolName,
1427
+ input: part.input,
1428
+ server_name: serverName,
1429
+ cache_control: cacheControl
1430
+ });
1431
+ } else if (
1432
+ // code execution 20250825:
1433
+ 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")
1434
+ ) {
1354
1435
  anthropicContent.push({
1355
1436
  type: "server_tool_use",
1356
1437
  id: part.toolCallId,
@@ -1386,7 +1467,23 @@ async function convertToAnthropicMessagesPrompt({
1386
1467
  break;
1387
1468
  }
1388
1469
  case "tool-result": {
1389
- if (part.toolName === "code_execution") {
1470
+ if (mcpToolUseIds.has(part.toolCallId)) {
1471
+ const output = part.output;
1472
+ if (output.type !== "json" && output.type !== "error-json") {
1473
+ warnings.push({
1474
+ type: "other",
1475
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1476
+ });
1477
+ break;
1478
+ }
1479
+ anthropicContent.push({
1480
+ type: "mcp_tool_result",
1481
+ tool_use_id: part.toolCallId,
1482
+ is_error: output.type === "error-json",
1483
+ content: output.value,
1484
+ cache_control: cacheControl
1485
+ });
1486
+ } else if (part.toolName === "code_execution") {
1390
1487
  const output = part.output;
1391
1488
  if (output.type !== "json") {
1392
1489
  warnings.push({
@@ -1701,7 +1798,7 @@ var AnthropicMessagesLanguageModel = class {
1701
1798
  providerOptions,
1702
1799
  schema: anthropicProviderOptions
1703
1800
  });
1704
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1801
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1705
1802
  prompt,
1706
1803
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1707
1804
  warnings
@@ -1723,6 +1820,19 @@ var AnthropicMessagesLanguageModel = class {
1723
1820
  ...isThinking && {
1724
1821
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1725
1822
  },
1823
+ // mcp servers:
1824
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1825
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1826
+ type: server.type,
1827
+ name: server.name,
1828
+ url: server.url,
1829
+ authorization_token: server.authorizationToken,
1830
+ tool_configuration: server.toolConfiguration ? {
1831
+ allowed_tools: server.toolConfiguration.allowedTools,
1832
+ enabled: server.toolConfiguration.enabled
1833
+ } : void 0
1834
+ }))
1835
+ },
1726
1836
  // prompt:
1727
1837
  system: messagesPrompt.system,
1728
1838
  messages: messagesPrompt.messages
@@ -1764,11 +1874,14 @@ var AnthropicMessagesLanguageModel = class {
1764
1874
  warnings.push({
1765
1875
  type: "unsupported-setting",
1766
1876
  setting: "maxOutputTokens",
1767
- details: `${maxTokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1877
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1768
1878
  });
1769
1879
  }
1770
1880
  baseArgs.max_tokens = maxOutputTokensForModel;
1771
1881
  }
1882
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1883
+ betas.add("mcp-client-2025-04-04");
1884
+ }
1772
1885
  const {
1773
1886
  tools: anthropicTools2,
1774
1887
  toolChoice: anthropicToolChoice,
@@ -1792,7 +1905,7 @@ var AnthropicMessagesLanguageModel = class {
1792
1905
  tool_choice: anthropicToolChoice
1793
1906
  },
1794
1907
  warnings: [...warnings, ...toolWarnings],
1795
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1908
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1796
1909
  usesJsonResponseTool: jsonResponseTool != null
1797
1910
  };
1798
1911
  }
@@ -1857,6 +1970,7 @@ var AnthropicMessagesLanguageModel = class {
1857
1970
  fetch: this.config.fetch
1858
1971
  });
1859
1972
  const content = [];
1973
+ const mcpToolCalls = {};
1860
1974
  for (const part of response.content) {
1861
1975
  switch (part.type) {
1862
1976
  case "text": {
@@ -1936,6 +2050,37 @@ var AnthropicMessagesLanguageModel = class {
1936
2050
  }
1937
2051
  break;
1938
2052
  }
2053
+ case "mcp_tool_use": {
2054
+ mcpToolCalls[part.id] = {
2055
+ type: "tool-call",
2056
+ toolCallId: part.id,
2057
+ toolName: part.name,
2058
+ input: JSON.stringify(part.input),
2059
+ providerExecuted: true,
2060
+ dynamic: true,
2061
+ providerMetadata: {
2062
+ anthropic: {
2063
+ type: "mcp-tool-use",
2064
+ serverName: part.server_name
2065
+ }
2066
+ }
2067
+ };
2068
+ content.push(mcpToolCalls[part.id]);
2069
+ break;
2070
+ }
2071
+ case "mcp_tool_result": {
2072
+ content.push({
2073
+ type: "tool-result",
2074
+ toolCallId: part.tool_use_id,
2075
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2076
+ isError: part.is_error,
2077
+ result: part.content,
2078
+ providerExecuted: true,
2079
+ dynamic: true,
2080
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2081
+ });
2082
+ break;
2083
+ }
1939
2084
  case "web_fetch_tool_result": {
1940
2085
  if (part.content.type === "web_fetch_result") {
1941
2086
  content.push({
@@ -2116,6 +2261,7 @@ var AnthropicMessagesLanguageModel = class {
2116
2261
  totalTokens: void 0
2117
2262
  };
2118
2263
  const contentBlocks = {};
2264
+ const mcpToolCalls = {};
2119
2265
  let rawUsage = void 0;
2120
2266
  let cacheCreationInputTokens = null;
2121
2267
  let stopSequence = null;
@@ -2142,7 +2288,8 @@ var AnthropicMessagesLanguageModel = class {
2142
2288
  return;
2143
2289
  }
2144
2290
  case "content_block_start": {
2145
- const contentBlockType = value.content_block.type;
2291
+ const part = value.content_block;
2292
+ const contentBlockType = part.type;
2146
2293
  blockType = contentBlockType;
2147
2294
  switch (contentBlockType) {
2148
2295
  case "text": {
@@ -2168,7 +2315,7 @@ var AnthropicMessagesLanguageModel = class {
2168
2315
  id: String(value.index),
2169
2316
  providerMetadata: {
2170
2317
  anthropic: {
2171
- redactedData: value.content_block.data
2318
+ redactedData: part.data
2172
2319
  }
2173
2320
  }
2174
2321
  });
@@ -2177,16 +2324,16 @@ var AnthropicMessagesLanguageModel = class {
2177
2324
  case "tool_use": {
2178
2325
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2179
2326
  type: "tool-call",
2180
- toolCallId: value.content_block.id,
2181
- toolName: value.content_block.name,
2327
+ toolCallId: part.id,
2328
+ toolName: part.name,
2182
2329
  input: "",
2183
2330
  firstDelta: true
2184
2331
  };
2185
2332
  controller.enqueue(
2186
2333
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2187
2334
  type: "tool-input-start",
2188
- id: value.content_block.id,
2189
- toolName: value.content_block.name
2335
+ id: part.id,
2336
+ toolName: part.name
2190
2337
  }
2191
2338
  );
2192
2339
  return;
@@ -2201,19 +2348,19 @@ var AnthropicMessagesLanguageModel = class {
2201
2348
  "text_editor_code_execution",
2202
2349
  // code execution 20250825 bash:
2203
2350
  "bash_code_execution"
2204
- ].includes(value.content_block.name)) {
2351
+ ].includes(part.name)) {
2205
2352
  contentBlocks[value.index] = {
2206
2353
  type: "tool-call",
2207
- toolCallId: value.content_block.id,
2208
- toolName: value.content_block.name,
2354
+ toolCallId: part.id,
2355
+ toolName: part.name,
2209
2356
  input: "",
2210
2357
  providerExecuted: true,
2211
2358
  firstDelta: true
2212
2359
  };
2213
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2360
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2214
2361
  controller.enqueue({
2215
2362
  type: "tool-input-start",
2216
- id: value.content_block.id,
2363
+ id: part.id,
2217
2364
  toolName: mappedToolName,
2218
2365
  providerExecuted: true
2219
2366
  });
@@ -2221,7 +2368,6 @@ var AnthropicMessagesLanguageModel = class {
2221
2368
  return;
2222
2369
  }
2223
2370
  case "web_fetch_tool_result": {
2224
- const part = value.content_block;
2225
2371
  if (part.content.type === "web_fetch_result") {
2226
2372
  controller.enqueue({
2227
2373
  type: "tool-result",
@@ -2259,7 +2405,6 @@ var AnthropicMessagesLanguageModel = class {
2259
2405
  return;
2260
2406
  }
2261
2407
  case "web_search_tool_result": {
2262
- const part = value.content_block;
2263
2408
  if (Array.isArray(part.content)) {
2264
2409
  controller.enqueue({
2265
2410
  type: "tool-result",
@@ -2308,7 +2453,6 @@ var AnthropicMessagesLanguageModel = class {
2308
2453
  }
2309
2454
  // code execution 20250522:
2310
2455
  case "code_execution_tool_result": {
2311
- const part = value.content_block;
2312
2456
  if (part.content.type === "code_execution_result") {
2313
2457
  controller.enqueue({
2314
2458
  type: "tool-result",
@@ -2340,7 +2484,6 @@ var AnthropicMessagesLanguageModel = class {
2340
2484
  // code execution 20250825:
2341
2485
  case "bash_code_execution_tool_result":
2342
2486
  case "text_editor_code_execution_tool_result": {
2343
- const part = value.content_block;
2344
2487
  controller.enqueue({
2345
2488
  type: "tool-result",
2346
2489
  toolCallId: part.tool_use_id,
@@ -2350,6 +2493,37 @@ var AnthropicMessagesLanguageModel = class {
2350
2493
  });
2351
2494
  return;
2352
2495
  }
2496
+ case "mcp_tool_use": {
2497
+ mcpToolCalls[part.id] = {
2498
+ type: "tool-call",
2499
+ toolCallId: part.id,
2500
+ toolName: part.name,
2501
+ input: JSON.stringify(part.input),
2502
+ providerExecuted: true,
2503
+ dynamic: true,
2504
+ providerMetadata: {
2505
+ anthropic: {
2506
+ type: "mcp-tool-use",
2507
+ serverName: part.server_name
2508
+ }
2509
+ }
2510
+ };
2511
+ controller.enqueue(mcpToolCalls[part.id]);
2512
+ return;
2513
+ }
2514
+ case "mcp_tool_result": {
2515
+ controller.enqueue({
2516
+ type: "tool-result",
2517
+ toolCallId: part.tool_use_id,
2518
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2519
+ isError: part.is_error,
2520
+ result: part.content,
2521
+ providerExecuted: true,
2522
+ dynamic: true,
2523
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2524
+ });
2525
+ return;
2526
+ }
2353
2527
  default: {
2354
2528
  const _exhaustiveCheck = contentBlockType;
2355
2529
  throw new Error(