@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.0-beta.28
4
+
5
+ ### Patch Changes
6
+
7
+ - 81d4308: feat(provider/anthropic): mcp connector support
8
+ - Updated dependencies [81d4308]
9
+ - @ai-sdk/provider@3.0.0-beta.7
10
+ - @ai-sdk/provider-utils@4.0.0-beta.18
11
+
3
12
  ## 3.0.0-beta.27
4
13
 
5
14
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -15,6 +15,16 @@ declare const anthropicProviderOptions: z.ZodObject<{
15
15
  type: z.ZodLiteral<"ephemeral">;
16
16
  ttl: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
17
17
  }, z.core.$strip>>;
18
+ mcpServers: z.ZodOptional<z.ZodArray<z.ZodObject<{
19
+ type: z.ZodLiteral<"url">;
20
+ name: z.ZodString;
21
+ url: z.ZodString;
22
+ authorizationToken: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ toolConfiguration: z.ZodOptional<z.ZodNullable<z.ZodObject<{
24
+ enabled: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
25
+ allowedTools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
26
+ }, z.core.$strip>>>;
27
+ }, z.core.$strip>>>;
18
28
  }, z.core.$strip>;
19
29
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
20
30
 
package/dist/index.d.ts CHANGED
@@ -15,6 +15,16 @@ declare const anthropicProviderOptions: z.ZodObject<{
15
15
  type: z.ZodLiteral<"ephemeral">;
16
16
  ttl: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"5m">, z.ZodLiteral<"1h">]>>;
17
17
  }, z.core.$strip>>;
18
+ mcpServers: z.ZodOptional<z.ZodArray<z.ZodObject<{
19
+ type: z.ZodLiteral<"url">;
20
+ name: z.ZodString;
21
+ url: z.ZodString;
22
+ authorizationToken: z.ZodOptional<z.ZodNullable<z.ZodString>>;
23
+ toolConfiguration: z.ZodOptional<z.ZodNullable<z.ZodObject<{
24
+ enabled: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
25
+ allowedTools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
26
+ }, z.core.$strip>>>;
27
+ }, z.core.$strip>>>;
18
28
  }, z.core.$strip>;
19
29
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
20
30
 
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
31
31
  var import_provider_utils20 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/version.ts
34
- var VERSION = true ? "3.0.0-beta.27" : "0.0.0-test";
34
+ var VERSION = true ? "3.0.0-beta.28" : "0.0.0-test";
35
35
 
36
36
  // src/anthropic-messages-language-model.ts
37
37
  var import_provider3 = require("@ai-sdk/provider");
@@ -119,6 +119,24 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
119
119
  name: import_v42.z.string(),
120
120
  input: import_v42.z.record(import_v42.z.string(), import_v42.z.unknown()).nullish()
121
121
  }),
122
+ import_v42.z.object({
123
+ type: import_v42.z.literal("mcp_tool_use"),
124
+ id: import_v42.z.string(),
125
+ name: import_v42.z.string(),
126
+ input: import_v42.z.unknown(),
127
+ server_name: import_v42.z.string()
128
+ }),
129
+ import_v42.z.object({
130
+ type: import_v42.z.literal("mcp_tool_result"),
131
+ tool_use_id: import_v42.z.string(),
132
+ is_error: import_v42.z.boolean(),
133
+ content: import_v42.z.array(
134
+ import_v42.z.union([
135
+ import_v42.z.string(),
136
+ import_v42.z.object({ type: import_v42.z.literal("text"), text: import_v42.z.string() })
137
+ ])
138
+ )
139
+ }),
122
140
  import_v42.z.object({
123
141
  type: import_v42.z.literal("web_fetch_tool_result"),
124
142
  tool_use_id: import_v42.z.string(),
@@ -291,6 +309,24 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
291
309
  name: import_v42.z.string(),
292
310
  input: import_v42.z.record(import_v42.z.string(), import_v42.z.unknown()).nullish()
293
311
  }),
312
+ import_v42.z.object({
313
+ type: import_v42.z.literal("mcp_tool_use"),
314
+ id: import_v42.z.string(),
315
+ name: import_v42.z.string(),
316
+ input: import_v42.z.unknown(),
317
+ server_name: import_v42.z.string()
318
+ }),
319
+ import_v42.z.object({
320
+ type: import_v42.z.literal("mcp_tool_result"),
321
+ tool_use_id: import_v42.z.string(),
322
+ is_error: import_v42.z.boolean(),
323
+ content: import_v42.z.array(
324
+ import_v42.z.union([
325
+ import_v42.z.string(),
326
+ import_v42.z.object({ type: import_v42.z.literal("text"), text: import_v42.z.string() })
327
+ ])
328
+ )
329
+ }),
294
330
  import_v42.z.object({
295
331
  type: import_v42.z.literal("web_fetch_tool_result"),
296
332
  tool_use_id: import_v42.z.string(),
@@ -543,7 +579,19 @@ var anthropicProviderOptions = import_v43.z.object({
543
579
  cacheControl: import_v43.z.object({
544
580
  type: import_v43.z.literal("ephemeral"),
545
581
  ttl: import_v43.z.union([import_v43.z.literal("5m"), import_v43.z.literal("1h")]).optional()
546
- }).optional()
582
+ }).optional(),
583
+ mcpServers: import_v43.z.array(
584
+ import_v43.z.object({
585
+ type: import_v43.z.literal("url"),
586
+ name: import_v43.z.string(),
587
+ url: import_v43.z.string(),
588
+ authorizationToken: import_v43.z.string().nullish(),
589
+ toolConfiguration: import_v43.z.object({
590
+ enabled: import_v43.z.boolean().nullish(),
591
+ allowedTools: import_v43.z.array(import_v43.z.string()).nullish()
592
+ }).nullish()
593
+ })
594
+ ).optional()
547
595
  });
548
596
 
549
597
  // src/anthropic-prepare-tools.ts
@@ -1064,7 +1112,7 @@ async function convertToAnthropicMessagesPrompt({
1064
1112
  sendReasoning,
1065
1113
  warnings
1066
1114
  }) {
1067
- var _a, _b, _c, _d, _e, _f;
1115
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1068
1116
  const betas = /* @__PURE__ */ new Set();
1069
1117
  const blocks = groupIntoBlocks(prompt);
1070
1118
  let system = void 0;
@@ -1282,6 +1330,7 @@ async function convertToAnthropicMessagesPrompt({
1282
1330
  }
1283
1331
  case "assistant": {
1284
1332
  const anthropicContent = [];
1333
+ const mcpToolUseIds = /* @__PURE__ */ new Set();
1285
1334
  for (let j = 0; j < block.messages.length; j++) {
1286
1335
  const message = block.messages[j];
1287
1336
  const isLastMessage = j === block.messages.length - 1;
@@ -1347,7 +1396,29 @@ async function convertToAnthropicMessagesPrompt({
1347
1396
  }
1348
1397
  case "tool-call": {
1349
1398
  if (part.providerExecuted) {
1350
- 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")) {
1399
+ const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
1400
+ if (isMcpToolUse) {
1401
+ mcpToolUseIds.add(part.toolCallId);
1402
+ const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
1403
+ if (serverName == null || typeof serverName !== "string") {
1404
+ warnings.push({
1405
+ type: "other",
1406
+ message: "mcp tool use server name is required and must be a string"
1407
+ });
1408
+ break;
1409
+ }
1410
+ anthropicContent.push({
1411
+ type: "mcp_tool_use",
1412
+ id: part.toolCallId,
1413
+ name: part.toolName,
1414
+ input: part.input,
1415
+ server_name: serverName,
1416
+ cache_control: cacheControl
1417
+ });
1418
+ } else if (
1419
+ // code execution 20250825:
1420
+ 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")
1421
+ ) {
1351
1422
  anthropicContent.push({
1352
1423
  type: "server_tool_use",
1353
1424
  id: part.toolCallId,
@@ -1383,7 +1454,23 @@ async function convertToAnthropicMessagesPrompt({
1383
1454
  break;
1384
1455
  }
1385
1456
  case "tool-result": {
1386
- if (part.toolName === "code_execution") {
1457
+ if (mcpToolUseIds.has(part.toolCallId)) {
1458
+ const output = part.output;
1459
+ if (output.type !== "json" && output.type !== "error-json") {
1460
+ warnings.push({
1461
+ type: "other",
1462
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
1463
+ });
1464
+ break;
1465
+ }
1466
+ anthropicContent.push({
1467
+ type: "mcp_tool_result",
1468
+ tool_use_id: part.toolCallId,
1469
+ is_error: output.type === "error-json",
1470
+ content: output.value,
1471
+ cache_control: cacheControl
1472
+ });
1473
+ } else if (part.toolName === "code_execution") {
1387
1474
  const output = part.output;
1388
1475
  if (output.type !== "json") {
1389
1476
  warnings.push({
@@ -1698,7 +1785,7 @@ var AnthropicMessagesLanguageModel = class {
1698
1785
  providerOptions,
1699
1786
  schema: anthropicProviderOptions
1700
1787
  });
1701
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1788
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1702
1789
  prompt,
1703
1790
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1704
1791
  warnings
@@ -1720,6 +1807,19 @@ var AnthropicMessagesLanguageModel = class {
1720
1807
  ...isThinking && {
1721
1808
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1722
1809
  },
1810
+ // mcp servers:
1811
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1812
+ mcp_servers: anthropicOptions.mcpServers.map((server) => ({
1813
+ type: server.type,
1814
+ name: server.name,
1815
+ url: server.url,
1816
+ authorization_token: server.authorizationToken,
1817
+ tool_configuration: server.toolConfiguration ? {
1818
+ allowed_tools: server.toolConfiguration.allowedTools,
1819
+ enabled: server.toolConfiguration.enabled
1820
+ } : void 0
1821
+ }))
1822
+ },
1723
1823
  // prompt:
1724
1824
  system: messagesPrompt.system,
1725
1825
  messages: messagesPrompt.messages
@@ -1761,11 +1861,14 @@ var AnthropicMessagesLanguageModel = class {
1761
1861
  warnings.push({
1762
1862
  type: "unsupported-setting",
1763
1863
  setting: "maxOutputTokens",
1764
- details: `${maxTokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1864
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1765
1865
  });
1766
1866
  }
1767
1867
  baseArgs.max_tokens = maxOutputTokensForModel;
1768
1868
  }
1869
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1870
+ betas.add("mcp-client-2025-04-04");
1871
+ }
1769
1872
  const {
1770
1873
  tools: anthropicTools2,
1771
1874
  toolChoice: anthropicToolChoice,
@@ -1789,7 +1892,7 @@ var AnthropicMessagesLanguageModel = class {
1789
1892
  tool_choice: anthropicToolChoice
1790
1893
  },
1791
1894
  warnings: [...warnings, ...toolWarnings],
1792
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1895
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1793
1896
  usesJsonResponseTool: jsonResponseTool != null
1794
1897
  };
1795
1898
  }
@@ -1854,6 +1957,7 @@ var AnthropicMessagesLanguageModel = class {
1854
1957
  fetch: this.config.fetch
1855
1958
  });
1856
1959
  const content = [];
1960
+ const mcpToolCalls = {};
1857
1961
  for (const part of response.content) {
1858
1962
  switch (part.type) {
1859
1963
  case "text": {
@@ -1933,6 +2037,37 @@ var AnthropicMessagesLanguageModel = class {
1933
2037
  }
1934
2038
  break;
1935
2039
  }
2040
+ case "mcp_tool_use": {
2041
+ mcpToolCalls[part.id] = {
2042
+ type: "tool-call",
2043
+ toolCallId: part.id,
2044
+ toolName: part.name,
2045
+ input: JSON.stringify(part.input),
2046
+ providerExecuted: true,
2047
+ dynamic: true,
2048
+ providerMetadata: {
2049
+ anthropic: {
2050
+ type: "mcp-tool-use",
2051
+ serverName: part.server_name
2052
+ }
2053
+ }
2054
+ };
2055
+ content.push(mcpToolCalls[part.id]);
2056
+ break;
2057
+ }
2058
+ case "mcp_tool_result": {
2059
+ content.push({
2060
+ type: "tool-result",
2061
+ toolCallId: part.tool_use_id,
2062
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2063
+ isError: part.is_error,
2064
+ result: part.content,
2065
+ providerExecuted: true,
2066
+ dynamic: true,
2067
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2068
+ });
2069
+ break;
2070
+ }
1936
2071
  case "web_fetch_tool_result": {
1937
2072
  if (part.content.type === "web_fetch_result") {
1938
2073
  content.push({
@@ -2113,6 +2248,7 @@ var AnthropicMessagesLanguageModel = class {
2113
2248
  totalTokens: void 0
2114
2249
  };
2115
2250
  const contentBlocks = {};
2251
+ const mcpToolCalls = {};
2116
2252
  let rawUsage = void 0;
2117
2253
  let cacheCreationInputTokens = null;
2118
2254
  let stopSequence = null;
@@ -2139,7 +2275,8 @@ var AnthropicMessagesLanguageModel = class {
2139
2275
  return;
2140
2276
  }
2141
2277
  case "content_block_start": {
2142
- const contentBlockType = value.content_block.type;
2278
+ const part = value.content_block;
2279
+ const contentBlockType = part.type;
2143
2280
  blockType = contentBlockType;
2144
2281
  switch (contentBlockType) {
2145
2282
  case "text": {
@@ -2165,7 +2302,7 @@ var AnthropicMessagesLanguageModel = class {
2165
2302
  id: String(value.index),
2166
2303
  providerMetadata: {
2167
2304
  anthropic: {
2168
- redactedData: value.content_block.data
2305
+ redactedData: part.data
2169
2306
  }
2170
2307
  }
2171
2308
  });
@@ -2174,16 +2311,16 @@ var AnthropicMessagesLanguageModel = class {
2174
2311
  case "tool_use": {
2175
2312
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2176
2313
  type: "tool-call",
2177
- toolCallId: value.content_block.id,
2178
- toolName: value.content_block.name,
2314
+ toolCallId: part.id,
2315
+ toolName: part.name,
2179
2316
  input: "",
2180
2317
  firstDelta: true
2181
2318
  };
2182
2319
  controller.enqueue(
2183
2320
  usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2184
2321
  type: "tool-input-start",
2185
- id: value.content_block.id,
2186
- toolName: value.content_block.name
2322
+ id: part.id,
2323
+ toolName: part.name
2187
2324
  }
2188
2325
  );
2189
2326
  return;
@@ -2198,19 +2335,19 @@ var AnthropicMessagesLanguageModel = class {
2198
2335
  "text_editor_code_execution",
2199
2336
  // code execution 20250825 bash:
2200
2337
  "bash_code_execution"
2201
- ].includes(value.content_block.name)) {
2338
+ ].includes(part.name)) {
2202
2339
  contentBlocks[value.index] = {
2203
2340
  type: "tool-call",
2204
- toolCallId: value.content_block.id,
2205
- toolName: value.content_block.name,
2341
+ toolCallId: part.id,
2342
+ toolName: part.name,
2206
2343
  input: "",
2207
2344
  providerExecuted: true,
2208
2345
  firstDelta: true
2209
2346
  };
2210
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2347
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2211
2348
  controller.enqueue({
2212
2349
  type: "tool-input-start",
2213
- id: value.content_block.id,
2350
+ id: part.id,
2214
2351
  toolName: mappedToolName,
2215
2352
  providerExecuted: true
2216
2353
  });
@@ -2218,7 +2355,6 @@ var AnthropicMessagesLanguageModel = class {
2218
2355
  return;
2219
2356
  }
2220
2357
  case "web_fetch_tool_result": {
2221
- const part = value.content_block;
2222
2358
  if (part.content.type === "web_fetch_result") {
2223
2359
  controller.enqueue({
2224
2360
  type: "tool-result",
@@ -2256,7 +2392,6 @@ var AnthropicMessagesLanguageModel = class {
2256
2392
  return;
2257
2393
  }
2258
2394
  case "web_search_tool_result": {
2259
- const part = value.content_block;
2260
2395
  if (Array.isArray(part.content)) {
2261
2396
  controller.enqueue({
2262
2397
  type: "tool-result",
@@ -2305,7 +2440,6 @@ var AnthropicMessagesLanguageModel = class {
2305
2440
  }
2306
2441
  // code execution 20250522:
2307
2442
  case "code_execution_tool_result": {
2308
- const part = value.content_block;
2309
2443
  if (part.content.type === "code_execution_result") {
2310
2444
  controller.enqueue({
2311
2445
  type: "tool-result",
@@ -2337,7 +2471,6 @@ var AnthropicMessagesLanguageModel = class {
2337
2471
  // code execution 20250825:
2338
2472
  case "bash_code_execution_tool_result":
2339
2473
  case "text_editor_code_execution_tool_result": {
2340
- const part = value.content_block;
2341
2474
  controller.enqueue({
2342
2475
  type: "tool-result",
2343
2476
  toolCallId: part.tool_use_id,
@@ -2347,6 +2480,37 @@ var AnthropicMessagesLanguageModel = class {
2347
2480
  });
2348
2481
  return;
2349
2482
  }
2483
+ case "mcp_tool_use": {
2484
+ mcpToolCalls[part.id] = {
2485
+ type: "tool-call",
2486
+ toolCallId: part.id,
2487
+ toolName: part.name,
2488
+ input: JSON.stringify(part.input),
2489
+ providerExecuted: true,
2490
+ dynamic: true,
2491
+ providerMetadata: {
2492
+ anthropic: {
2493
+ type: "mcp-tool-use",
2494
+ serverName: part.server_name
2495
+ }
2496
+ }
2497
+ };
2498
+ controller.enqueue(mcpToolCalls[part.id]);
2499
+ return;
2500
+ }
2501
+ case "mcp_tool_result": {
2502
+ controller.enqueue({
2503
+ type: "tool-result",
2504
+ toolCallId: part.tool_use_id,
2505
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2506
+ isError: part.is_error,
2507
+ result: part.content,
2508
+ providerExecuted: true,
2509
+ dynamic: true,
2510
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2511
+ });
2512
+ return;
2513
+ }
2350
2514
  default: {
2351
2515
  const _exhaustiveCheck = contentBlockType;
2352
2516
  throw new Error(