@ai-sdk/anthropic 3.0.0-beta.27 → 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({
@@ -1701,7 +1788,7 @@ var AnthropicMessagesLanguageModel = class {
1701
1788
  providerOptions,
1702
1789
  schema: anthropicProviderOptions
1703
1790
  });
1704
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1791
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1705
1792
  prompt,
1706
1793
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1707
1794
  warnings
@@ -1723,6 +1810,19 @@ var AnthropicMessagesLanguageModel = class {
1723
1810
  ...isThinking && {
1724
1811
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1725
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
+ },
1726
1826
  // prompt:
1727
1827
  system: messagesPrompt.system,
1728
1828
  messages: messagesPrompt.messages
@@ -1764,11 +1864,14 @@ var AnthropicMessagesLanguageModel = class {
1764
1864
  warnings.push({
1765
1865
  type: "unsupported-setting",
1766
1866
  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}.`
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}.`
1768
1868
  });
1769
1869
  }
1770
1870
  baseArgs.max_tokens = maxOutputTokensForModel;
1771
1871
  }
1872
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1873
+ betas.add("mcp-client-2025-04-04");
1874
+ }
1772
1875
  const {
1773
1876
  tools: anthropicTools2,
1774
1877
  toolChoice: anthropicToolChoice,
@@ -1792,7 +1895,7 @@ var AnthropicMessagesLanguageModel = class {
1792
1895
  tool_choice: anthropicToolChoice
1793
1896
  },
1794
1897
  warnings: [...warnings, ...toolWarnings],
1795
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1898
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1796
1899
  usesJsonResponseTool: jsonResponseTool != null
1797
1900
  };
1798
1901
  }
@@ -1857,6 +1960,7 @@ var AnthropicMessagesLanguageModel = class {
1857
1960
  fetch: this.config.fetch
1858
1961
  });
1859
1962
  const content = [];
1963
+ const mcpToolCalls = {};
1860
1964
  for (const part of response.content) {
1861
1965
  switch (part.type) {
1862
1966
  case "text": {
@@ -1936,6 +2040,37 @@ var AnthropicMessagesLanguageModel = class {
1936
2040
  }
1937
2041
  break;
1938
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
+ }
1939
2074
  case "web_fetch_tool_result": {
1940
2075
  if (part.content.type === "web_fetch_result") {
1941
2076
  content.push({
@@ -2116,6 +2251,7 @@ var AnthropicMessagesLanguageModel = class {
2116
2251
  totalTokens: void 0
2117
2252
  };
2118
2253
  const contentBlocks = {};
2254
+ const mcpToolCalls = {};
2119
2255
  let rawUsage = void 0;
2120
2256
  let cacheCreationInputTokens = null;
2121
2257
  let stopSequence = null;
@@ -2142,7 +2278,8 @@ var AnthropicMessagesLanguageModel = class {
2142
2278
  return;
2143
2279
  }
2144
2280
  case "content_block_start": {
2145
- const contentBlockType = value.content_block.type;
2281
+ const part = value.content_block;
2282
+ const contentBlockType = part.type;
2146
2283
  blockType = contentBlockType;
2147
2284
  switch (contentBlockType) {
2148
2285
  case "text": {
@@ -2168,7 +2305,7 @@ var AnthropicMessagesLanguageModel = class {
2168
2305
  id: String(value.index),
2169
2306
  providerMetadata: {
2170
2307
  anthropic: {
2171
- redactedData: value.content_block.data
2308
+ redactedData: part.data
2172
2309
  }
2173
2310
  }
2174
2311
  });
@@ -2177,16 +2314,16 @@ var AnthropicMessagesLanguageModel = class {
2177
2314
  case "tool_use": {
2178
2315
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2179
2316
  type: "tool-call",
2180
- toolCallId: value.content_block.id,
2181
- toolName: value.content_block.name,
2317
+ toolCallId: part.id,
2318
+ toolName: part.name,
2182
2319
  input: "",
2183
2320
  firstDelta: true
2184
2321
  };
2185
2322
  controller.enqueue(
2186
2323
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2187
2324
  type: "tool-input-start",
2188
- id: value.content_block.id,
2189
- toolName: value.content_block.name
2325
+ id: part.id,
2326
+ toolName: part.name
2190
2327
  }
2191
2328
  );
2192
2329
  return;
@@ -2201,19 +2338,19 @@ var AnthropicMessagesLanguageModel = class {
2201
2338
  "text_editor_code_execution",
2202
2339
  // code execution 20250825 bash:
2203
2340
  "bash_code_execution"
2204
- ].includes(value.content_block.name)) {
2341
+ ].includes(part.name)) {
2205
2342
  contentBlocks[value.index] = {
2206
2343
  type: "tool-call",
2207
- toolCallId: value.content_block.id,
2208
- toolName: value.content_block.name,
2344
+ toolCallId: part.id,
2345
+ toolName: part.name,
2209
2346
  input: "",
2210
2347
  providerExecuted: true,
2211
2348
  firstDelta: true
2212
2349
  };
2213
- 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;
2214
2351
  controller.enqueue({
2215
2352
  type: "tool-input-start",
2216
- id: value.content_block.id,
2353
+ id: part.id,
2217
2354
  toolName: mappedToolName,
2218
2355
  providerExecuted: true
2219
2356
  });
@@ -2221,7 +2358,6 @@ var AnthropicMessagesLanguageModel = class {
2221
2358
  return;
2222
2359
  }
2223
2360
  case "web_fetch_tool_result": {
2224
- const part = value.content_block;
2225
2361
  if (part.content.type === "web_fetch_result") {
2226
2362
  controller.enqueue({
2227
2363
  type: "tool-result",
@@ -2259,7 +2395,6 @@ var AnthropicMessagesLanguageModel = class {
2259
2395
  return;
2260
2396
  }
2261
2397
  case "web_search_tool_result": {
2262
- const part = value.content_block;
2263
2398
  if (Array.isArray(part.content)) {
2264
2399
  controller.enqueue({
2265
2400
  type: "tool-result",
@@ -2308,7 +2443,6 @@ var AnthropicMessagesLanguageModel = class {
2308
2443
  }
2309
2444
  // code execution 20250522:
2310
2445
  case "code_execution_tool_result": {
2311
- const part = value.content_block;
2312
2446
  if (part.content.type === "code_execution_result") {
2313
2447
  controller.enqueue({
2314
2448
  type: "tool-result",
@@ -2340,7 +2474,6 @@ var AnthropicMessagesLanguageModel = class {
2340
2474
  // code execution 20250825:
2341
2475
  case "bash_code_execution_tool_result":
2342
2476
  case "text_editor_code_execution_tool_result": {
2343
- const part = value.content_block;
2344
2477
  controller.enqueue({
2345
2478
  type: "tool-result",
2346
2479
  toolCallId: part.tool_use_id,
@@ -2350,6 +2483,37 @@ var AnthropicMessagesLanguageModel = class {
2350
2483
  });
2351
2484
  return;
2352
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
+ }
2353
2517
  default: {
2354
2518
  const _exhaustiveCheck = contentBlockType;
2355
2519
  throw new Error(