@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.
@@ -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;
@@ -1208,20 +1256,20 @@ async function convertToAnthropicMessagesPrompt({
1208
1256
  return {
1209
1257
  type: "text",
1210
1258
  text: contentPart.text,
1211
- cache_control: void 0
1259
+ cache_control: cacheControl
1212
1260
  };
1213
- case "media": {
1214
- if (contentPart.mediaType.startsWith("image/")) {
1215
- return {
1216
- type: "image",
1217
- source: {
1218
- type: "base64",
1219
- media_type: contentPart.mediaType,
1220
- data: contentPart.data
1221
- },
1222
- cache_control: void 0
1223
- };
1224
- }
1261
+ case "image-data": {
1262
+ return {
1263
+ type: "image",
1264
+ source: {
1265
+ type: "base64",
1266
+ media_type: contentPart.mediaType,
1267
+ data: contentPart.data
1268
+ },
1269
+ cache_control: cacheControl
1270
+ };
1271
+ }
1272
+ case "file-data": {
1225
1273
  if (contentPart.mediaType === "application/pdf") {
1226
1274
  betas.add("pdfs-2024-09-25");
1227
1275
  return {
@@ -1231,15 +1279,24 @@ async function convertToAnthropicMessagesPrompt({
1231
1279
  media_type: contentPart.mediaType,
1232
1280
  data: contentPart.data
1233
1281
  },
1234
- cache_control: void 0
1282
+ cache_control: cacheControl
1235
1283
  };
1236
1284
  }
1237
- throw new import_provider2.UnsupportedFunctionalityError({
1238
- functionality: `media type: ${contentPart.mediaType}`
1285
+ warnings.push({
1286
+ type: "other",
1287
+ message: `unsupported tool content part type: ${contentPart.type} with media type: ${contentPart.mediaType}`
1239
1288
  });
1289
+ return void 0;
1290
+ }
1291
+ default: {
1292
+ warnings.push({
1293
+ type: "other",
1294
+ message: `unsupported tool content part type: ${contentPart.type}`
1295
+ });
1296
+ return void 0;
1240
1297
  }
1241
1298
  }
1242
- });
1299
+ }).filter(import_provider_utils10.isNonNullable);
1243
1300
  break;
1244
1301
  case "text":
1245
1302
  case "error-text":
@@ -1275,6 +1332,7 @@ async function convertToAnthropicMessagesPrompt({
1275
1332
  }
1276
1333
  case "assistant": {
1277
1334
  const anthropicContent = [];
1335
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1278
1336
  for (let j = 0; j < block.messages.length; j++) {
1279
1337
  const message = block.messages[j];
1280
1338
  const isLastMessage = j === block.messages.length - 1;
@@ -1340,7 +1398,29 @@ async function convertToAnthropicMessagesPrompt({
1340
1398
  }
1341
1399
  case "tool-call": {
1342
1400
  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")) {
1401
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1402
+ if (isMcpToolUse) {
1403
+ mcpToolUseIds.add(part.toolCallId);
1404
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1405
+ if (serverName == null || typeof serverName !== "string") {
1406
+ warnings.push({
1407
+ type: "other",
1408
+ message: "mcp tool use server name is required and must be a string"
1409
+ });
1410
+ break;
1411
+ }
1412
+ anthropicContent.push({
1413
+ type: "mcp_tool_use",
1414
+ id: part.toolCallId,
1415
+ name: part.toolName,
1416
+ input: part.input,
1417
+ server_name: serverName,
1418
+ cache_control: cacheControl
1419
+ });
1420
+ } else if (
1421
+ // code execution 20250825:
1422
+ 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")
1423
+ ) {
1344
1424
  anthropicContent.push({
1345
1425
  type: "server_tool_use",
1346
1426
  id: part.toolCallId,
@@ -1376,7 +1456,23 @@ async function convertToAnthropicMessagesPrompt({
1376
1456
  break;
1377
1457
  }
1378
1458
  case "tool-result": {
1379
- if (part.toolName === "code_execution") {
1459
+ if (mcpToolUseIds.has(part.toolCallId)) {
1460
+ const output = part.output;
1461
+ if (output.type !== "json" && output.type !== "error-json") {
1462
+ warnings.push({
1463
+ type: "other",
1464
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1465
+ });
1466
+ break;
1467
+ }
1468
+ anthropicContent.push({
1469
+ type: "mcp_tool_result",
1470
+ tool_use_id: part.toolCallId,
1471
+ is_error: output.type === "error-json",
1472
+ content: output.value,
1473
+ cache_control: cacheControl
1474
+ });
1475
+ } else if (part.toolName === "code_execution") {
1380
1476
  const output = part.output;
1381
1477
  if (output.type !== "json") {
1382
1478
  warnings.push({
@@ -1691,7 +1787,7 @@ var AnthropicMessagesLanguageModel = class {
1691
1787
  providerOptions,
1692
1788
  schema: anthropicProviderOptions
1693
1789
  });
1694
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1790
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1695
1791
  prompt,
1696
1792
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1697
1793
  warnings
@@ -1713,6 +1809,19 @@ var AnthropicMessagesLanguageModel = class {
1713
1809
  ...isThinking && {
1714
1810
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1715
1811
  },
1812
+ // mcp servers:
1813
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1814
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1815
+ type: server.type,
1816
+ name: server.name,
1817
+ url: server.url,
1818
+ authorization_token: server.authorizationToken,
1819
+ tool_configuration: server.toolConfiguration ? {
1820
+ allowed_tools: server.toolConfiguration.allowedTools,
1821
+ enabled: server.toolConfiguration.enabled
1822
+ } : void 0
1823
+ }))
1824
+ },
1716
1825
  // prompt:
1717
1826
  system: messagesPrompt.system,
1718
1827
  messages: messagesPrompt.messages
@@ -1754,11 +1863,14 @@ var AnthropicMessagesLanguageModel = class {
1754
1863
  warnings.push({
1755
1864
  type: "unsupported-setting",
1756
1865
  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}.`
1866
+ 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
1867
  });
1759
1868
  }
1760
1869
  baseArgs.max_tokens = maxOutputTokensForModel;
1761
1870
  }
1871
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1872
+ betas.add("mcp-client-2025-04-04");
1873
+ }
1762
1874
  const {
1763
1875
  tools: anthropicTools2,
1764
1876
  toolChoice: anthropicToolChoice,
@@ -1782,7 +1894,7 @@ var AnthropicMessagesLanguageModel = class {
1782
1894
  tool_choice: anthropicToolChoice
1783
1895
  },
1784
1896
  warnings: [...warnings, ...toolWarnings],
1785
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1897
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1786
1898
  usesJsonResponseTool: jsonResponseTool != null
1787
1899
  };
1788
1900
  }
@@ -1847,6 +1959,7 @@ var AnthropicMessagesLanguageModel = class {
1847
1959
  fetch: this.config.fetch
1848
1960
  });
1849
1961
  const content = [];
1962
+ const mcpToolCalls = {};
1850
1963
  for (const part of response.content) {
1851
1964
  switch (part.type) {
1852
1965
  case "text": {
@@ -1926,6 +2039,37 @@ var AnthropicMessagesLanguageModel = class {
1926
2039
  }
1927
2040
  break;
1928
2041
  }
2042
+ case "mcp_tool_use": {
2043
+ mcpToolCalls[part.id] = {
2044
+ type: "tool-call",
2045
+ toolCallId: part.id,
2046
+ toolName: part.name,
2047
+ input: JSON.stringify(part.input),
2048
+ providerExecuted: true,
2049
+ dynamic: true,
2050
+ providerMetadata: {
2051
+ anthropic: {
2052
+ type: "mcp-tool-use",
2053
+ serverName: part.server_name
2054
+ }
2055
+ }
2056
+ };
2057
+ content.push(mcpToolCalls[part.id]);
2058
+ break;
2059
+ }
2060
+ case "mcp_tool_result": {
2061
+ content.push({
2062
+ type: "tool-result",
2063
+ toolCallId: part.tool_use_id,
2064
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2065
+ isError: part.is_error,
2066
+ result: part.content,
2067
+ providerExecuted: true,
2068
+ dynamic: true,
2069
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2070
+ });
2071
+ break;
2072
+ }
1929
2073
  case "web_fetch_tool_result": {
1930
2074
  if (part.content.type === "web_fetch_result") {
1931
2075
  content.push({
@@ -2106,6 +2250,7 @@ var AnthropicMessagesLanguageModel = class {
2106
2250
  totalTokens: void 0
2107
2251
  };
2108
2252
  const contentBlocks = {};
2253
+ const mcpToolCalls = {};
2109
2254
  let rawUsage = void 0;
2110
2255
  let cacheCreationInputTokens = null;
2111
2256
  let stopSequence = null;
@@ -2132,7 +2277,8 @@ var AnthropicMessagesLanguageModel = class {
2132
2277
  return;
2133
2278
  }
2134
2279
  case "content_block_start": {
2135
- const contentBlockType = value.content_block.type;
2280
+ const part = value.content_block;
2281
+ const contentBlockType = part.type;
2136
2282
  blockType = contentBlockType;
2137
2283
  switch (contentBlockType) {
2138
2284
  case "text": {
@@ -2158,7 +2304,7 @@ var AnthropicMessagesLanguageModel = class {
2158
2304
  id: String(value.index),
2159
2305
  providerMetadata: {
2160
2306
  anthropic: {
2161
- redactedData: value.content_block.data
2307
+ redactedData: part.data
2162
2308
  }
2163
2309
  }
2164
2310
  });
@@ -2167,16 +2313,16 @@ var AnthropicMessagesLanguageModel = class {
2167
2313
  case "tool_use": {
2168
2314
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2169
2315
  type: "tool-call",
2170
- toolCallId: value.content_block.id,
2171
- toolName: value.content_block.name,
2316
+ toolCallId: part.id,
2317
+ toolName: part.name,
2172
2318
  input: "",
2173
2319
  firstDelta: true
2174
2320
  };
2175
2321
  controller.enqueue(
2176
2322
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2177
2323
  type: "tool-input-start",
2178
- id: value.content_block.id,
2179
- toolName: value.content_block.name
2324
+ id: part.id,
2325
+ toolName: part.name
2180
2326
  }
2181
2327
  );
2182
2328
  return;
@@ -2191,19 +2337,19 @@ var AnthropicMessagesLanguageModel = class {
2191
2337
  "text_editor_code_execution",
2192
2338
  // code execution 20250825 bash:
2193
2339
  "bash_code_execution"
2194
- ].includes(value.content_block.name)) {
2340
+ ].includes(part.name)) {
2195
2341
  contentBlocks[value.index] = {
2196
2342
  type: "tool-call",
2197
- toolCallId: value.content_block.id,
2198
- toolName: value.content_block.name,
2343
+ toolCallId: part.id,
2344
+ toolName: part.name,
2199
2345
  input: "",
2200
2346
  providerExecuted: true,
2201
2347
  firstDelta: true
2202
2348
  };
2203
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2349
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2204
2350
  controller.enqueue({
2205
2351
  type: "tool-input-start",
2206
- id: value.content_block.id,
2352
+ id: part.id,
2207
2353
  toolName: mappedToolName,
2208
2354
  providerExecuted: true
2209
2355
  });
@@ -2211,7 +2357,6 @@ var AnthropicMessagesLanguageModel = class {
2211
2357
  return;
2212
2358
  }
2213
2359
  case "web_fetch_tool_result": {
2214
- const part = value.content_block;
2215
2360
  if (part.content.type === "web_fetch_result") {
2216
2361
  controller.enqueue({
2217
2362
  type: "tool-result",
@@ -2249,7 +2394,6 @@ var AnthropicMessagesLanguageModel = class {
2249
2394
  return;
2250
2395
  }
2251
2396
  case "web_search_tool_result": {
2252
- const part = value.content_block;
2253
2397
  if (Array.isArray(part.content)) {
2254
2398
  controller.enqueue({
2255
2399
  type: "tool-result",
@@ -2298,7 +2442,6 @@ var AnthropicMessagesLanguageModel = class {
2298
2442
  }
2299
2443
  // code execution 20250522:
2300
2444
  case "code_execution_tool_result": {
2301
- const part = value.content_block;
2302
2445
  if (part.content.type === "code_execution_result") {
2303
2446
  controller.enqueue({
2304
2447
  type: "tool-result",
@@ -2330,7 +2473,6 @@ var AnthropicMessagesLanguageModel = class {
2330
2473
  // code execution 20250825:
2331
2474
  case "bash_code_execution_tool_result":
2332
2475
  case "text_editor_code_execution_tool_result": {
2333
- const part = value.content_block;
2334
2476
  controller.enqueue({
2335
2477
  type: "tool-result",
2336
2478
  toolCallId: part.tool_use_id,
@@ -2340,6 +2482,37 @@ var AnthropicMessagesLanguageModel = class {
2340
2482
  });
2341
2483
  return;
2342
2484
  }
2485
+ case "mcp_tool_use": {
2486
+ mcpToolCalls[part.id] = {
2487
+ type: "tool-call",
2488
+ toolCallId: part.id,
2489
+ toolName: part.name,
2490
+ input: JSON.stringify(part.input),
2491
+ providerExecuted: true,
2492
+ dynamic: true,
2493
+ providerMetadata: {
2494
+ anthropic: {
2495
+ type: "mcp-tool-use",
2496
+ serverName: part.server_name
2497
+ }
2498
+ }
2499
+ };
2500
+ controller.enqueue(mcpToolCalls[part.id]);
2501
+ return;
2502
+ }
2503
+ case "mcp_tool_result": {
2504
+ controller.enqueue({
2505
+ type: "tool-result",
2506
+ toolCallId: part.tool_use_id,
2507
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2508
+ isError: part.is_error,
2509
+ result: part.content,
2510
+ providerExecuted: true,
2511
+ dynamic: true,
2512
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2513
+ });
2514
+ return;
2515
+ }
2343
2516
  default: {
2344
2517
  const _exhaustiveCheck = contentBlockType;
2345
2518
  throw new Error(