@ai-sdk/mcp 2.0.0-beta.6 → 2.0.0-beta.67

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.
@@ -62,6 +62,14 @@ interface MCPTransport {
62
62
  * Event handler for received messages
63
63
  */
64
64
  onmessage?: (message: JSONRPCMessage) => void;
65
+ /**
66
+ * The protocol version negotiated during initialization.
67
+ */
68
+ protocolVersion?: string;
69
+ /**
70
+ * Set the protocol version negotiated during initialization.
71
+ */
72
+ setProtocolVersion?(version: string): void;
65
73
  }
66
74
 
67
75
  interface StdioConfig {
@@ -1,289 +1,287 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name2 in all)
8
- __defProp(target, name2, { get: all[name2], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/tool/mcp-stdio/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- Experimental_StdioMCPTransport: () => StdioMCPTransport
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
-
27
1
  // src/tool/json-rpc-message.ts
28
- var import_v42 = require("zod/v4");
2
+ import { parseJSON } from "@ai-sdk/provider-utils";
3
+ import { z as z2 } from "zod/v4";
29
4
 
30
5
  // src/tool/types.ts
31
- var import_v4 = require("zod/v4");
32
- var ToolMetaSchema = import_v4.z.optional(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown()));
33
- var ClientOrServerImplementationSchema = import_v4.z.looseObject({
34
- name: import_v4.z.string(),
35
- version: import_v4.z.string()
6
+ import { z } from "zod/v4";
7
+ var ToolMetaSchema = z.optional(z.record(z.string(), z.unknown()));
8
+ var ClientOrServerImplementationSchema = z.looseObject({
9
+ name: z.string(),
10
+ version: z.string(),
11
+ title: z.optional(z.string())
36
12
  });
37
- var BaseParamsSchema = import_v4.z.looseObject({
38
- _meta: import_v4.z.optional(import_v4.z.object({}).loose())
13
+ var BaseParamsSchema = z.looseObject({
14
+ _meta: z.optional(z.object({}).loose())
39
15
  });
40
16
  var ResultSchema = BaseParamsSchema;
41
- var RequestSchema = import_v4.z.object({
42
- method: import_v4.z.string(),
43
- params: import_v4.z.optional(BaseParamsSchema)
17
+ var RequestSchema = z.object({
18
+ method: z.string(),
19
+ params: z.optional(BaseParamsSchema)
44
20
  });
45
- var ElicitationCapabilitySchema = import_v4.z.object({
46
- applyDefaults: import_v4.z.optional(import_v4.z.boolean())
21
+ var ElicitationCapabilitySchema = z.object({
22
+ applyDefaults: z.optional(z.boolean())
47
23
  }).loose();
48
- var ServerCapabilitiesSchema = import_v4.z.looseObject({
49
- experimental: import_v4.z.optional(import_v4.z.object({}).loose()),
50
- logging: import_v4.z.optional(import_v4.z.object({}).loose()),
51
- prompts: import_v4.z.optional(
52
- import_v4.z.looseObject({
53
- listChanged: import_v4.z.optional(import_v4.z.boolean())
24
+ var ServerCapabilitiesSchema = z.looseObject({
25
+ experimental: z.optional(z.object({}).loose()),
26
+ logging: z.optional(z.object({}).loose()),
27
+ prompts: z.optional(
28
+ z.looseObject({
29
+ listChanged: z.optional(z.boolean())
54
30
  })
55
31
  ),
56
- resources: import_v4.z.optional(
57
- import_v4.z.looseObject({
58
- subscribe: import_v4.z.optional(import_v4.z.boolean()),
59
- listChanged: import_v4.z.optional(import_v4.z.boolean())
32
+ resources: z.optional(
33
+ z.looseObject({
34
+ subscribe: z.optional(z.boolean()),
35
+ listChanged: z.optional(z.boolean())
60
36
  })
61
37
  ),
62
- tools: import_v4.z.optional(
63
- import_v4.z.looseObject({
64
- listChanged: import_v4.z.optional(import_v4.z.boolean())
38
+ tools: z.optional(
39
+ z.looseObject({
40
+ listChanged: z.optional(z.boolean())
65
41
  })
66
42
  ),
67
- elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
43
+ elicitation: z.optional(ElicitationCapabilitySchema)
68
44
  });
69
- var ClientCapabilitiesSchema = import_v4.z.object({
70
- elicitation: import_v4.z.optional(ElicitationCapabilitySchema)
45
+ var ClientCapabilitiesSchema = z.object({
46
+ elicitation: z.optional(ElicitationCapabilitySchema)
71
47
  }).loose();
72
48
  var InitializeResultSchema = ResultSchema.extend({
73
- protocolVersion: import_v4.z.string(),
49
+ protocolVersion: z.string(),
74
50
  capabilities: ServerCapabilitiesSchema,
75
51
  serverInfo: ClientOrServerImplementationSchema,
76
- instructions: import_v4.z.optional(import_v4.z.string())
52
+ instructions: z.optional(z.string())
77
53
  });
78
54
  var PaginatedResultSchema = ResultSchema.extend({
79
- nextCursor: import_v4.z.optional(import_v4.z.string())
55
+ nextCursor: z.optional(z.string())
80
56
  });
81
- var ToolSchema = import_v4.z.object({
82
- name: import_v4.z.string(),
57
+ var ToolSchema = z.object({
58
+ name: z.string(),
83
59
  /**
84
60
  * @see https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool
85
61
  */
86
- title: import_v4.z.optional(import_v4.z.string()),
87
- description: import_v4.z.optional(import_v4.z.string()),
88
- inputSchema: import_v4.z.object({
89
- type: import_v4.z.literal("object"),
90
- properties: import_v4.z.optional(import_v4.z.object({}).loose())
62
+ title: z.optional(z.string()),
63
+ description: z.optional(z.string()),
64
+ inputSchema: z.object({
65
+ type: z.literal("object"),
66
+ properties: z.optional(z.object({}).loose())
91
67
  }).loose(),
92
68
  /**
93
69
  * @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema
94
70
  */
95
- outputSchema: import_v4.z.optional(import_v4.z.object({}).loose()),
96
- annotations: import_v4.z.optional(
97
- import_v4.z.object({
98
- title: import_v4.z.optional(import_v4.z.string())
71
+ outputSchema: z.optional(z.object({}).loose()),
72
+ annotations: z.optional(
73
+ z.object({
74
+ title: z.optional(z.string())
99
75
  }).loose()
100
76
  ),
101
77
  _meta: ToolMetaSchema
102
78
  }).loose();
103
79
  var ListToolsResultSchema = PaginatedResultSchema.extend({
104
- tools: import_v4.z.array(ToolSchema)
80
+ tools: z.array(ToolSchema)
105
81
  });
106
- var TextContentSchema = import_v4.z.object({
107
- type: import_v4.z.literal("text"),
108
- text: import_v4.z.string()
82
+ var TextContentSchema = z.object({
83
+ type: z.literal("text"),
84
+ text: z.string()
109
85
  }).loose();
110
- var ImageContentSchema = import_v4.z.object({
111
- type: import_v4.z.literal("image"),
112
- data: import_v4.z.base64(),
113
- mimeType: import_v4.z.string()
86
+ var ImageContentSchema = z.object({
87
+ type: z.literal("image"),
88
+ data: z.base64(),
89
+ mimeType: z.string()
114
90
  }).loose();
115
- var ResourceSchema = import_v4.z.object({
116
- uri: import_v4.z.string(),
117
- name: import_v4.z.string(),
118
- title: import_v4.z.optional(import_v4.z.string()),
119
- description: import_v4.z.optional(import_v4.z.string()),
120
- mimeType: import_v4.z.optional(import_v4.z.string()),
121
- size: import_v4.z.optional(import_v4.z.number())
91
+ var ResourceSchema = z.object({
92
+ uri: z.string(),
93
+ name: z.string(),
94
+ title: z.optional(z.string()),
95
+ description: z.optional(z.string()),
96
+ mimeType: z.optional(z.string()),
97
+ size: z.optional(z.number())
122
98
  }).loose();
123
99
  var ListResourcesResultSchema = PaginatedResultSchema.extend({
124
- resources: import_v4.z.array(ResourceSchema)
100
+ resources: z.array(ResourceSchema)
125
101
  });
126
- var ResourceContentsSchema = import_v4.z.object({
102
+ var ResourceContentsSchema = z.object({
127
103
  /**
128
104
  * The URI of this resource.
129
105
  */
130
- uri: import_v4.z.string(),
106
+ uri: z.string(),
131
107
  /**
132
108
  * Optional display name of the resource content.
133
109
  */
134
- name: import_v4.z.optional(import_v4.z.string()),
110
+ name: z.optional(z.string()),
135
111
  /**
136
112
  * Optional human readable title.
137
113
  */
138
- title: import_v4.z.optional(import_v4.z.string()),
114
+ title: z.optional(z.string()),
139
115
  /**
140
116
  * The MIME type of this resource, if known.
141
117
  */
142
- mimeType: import_v4.z.optional(import_v4.z.string())
118
+ mimeType: z.optional(z.string())
143
119
  }).loose();
144
120
  var TextResourceContentsSchema = ResourceContentsSchema.extend({
145
- text: import_v4.z.string()
121
+ text: z.string()
146
122
  });
147
123
  var BlobResourceContentsSchema = ResourceContentsSchema.extend({
148
- blob: import_v4.z.base64()
124
+ blob: z.base64()
149
125
  });
150
- var EmbeddedResourceSchema = import_v4.z.object({
151
- type: import_v4.z.literal("resource"),
152
- resource: import_v4.z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
126
+ var EmbeddedResourceSchema = z.object({
127
+ type: z.literal("resource"),
128
+ resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
129
+ }).loose();
130
+ var ResourceLinkContentSchema = z.object({
131
+ type: z.literal("resource_link"),
132
+ uri: z.string(),
133
+ name: z.string(),
134
+ description: z.optional(z.string()),
135
+ mimeType: z.optional(z.string())
153
136
  }).loose();
154
137
  var CallToolResultSchema = ResultSchema.extend({
155
- content: import_v4.z.array(
156
- import_v4.z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
138
+ content: z.array(
139
+ z.union([
140
+ TextContentSchema,
141
+ ImageContentSchema,
142
+ EmbeddedResourceSchema,
143
+ ResourceLinkContentSchema
144
+ ])
157
145
  ),
158
146
  /**
159
147
  * @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content
160
148
  */
161
- structuredContent: import_v4.z.optional(import_v4.z.unknown()),
162
- isError: import_v4.z.boolean().default(false).optional()
149
+ structuredContent: z.optional(z.unknown()),
150
+ isError: z.boolean().default(false).optional()
163
151
  }).or(
164
152
  ResultSchema.extend({
165
- toolResult: import_v4.z.unknown()
153
+ toolResult: z.unknown()
166
154
  })
167
155
  );
168
- var ResourceTemplateSchema = import_v4.z.object({
169
- uriTemplate: import_v4.z.string(),
170
- name: import_v4.z.string(),
171
- title: import_v4.z.optional(import_v4.z.string()),
172
- description: import_v4.z.optional(import_v4.z.string()),
173
- mimeType: import_v4.z.optional(import_v4.z.string())
156
+ var ResourceTemplateSchema = z.object({
157
+ uriTemplate: z.string(),
158
+ name: z.string(),
159
+ title: z.optional(z.string()),
160
+ description: z.optional(z.string()),
161
+ mimeType: z.optional(z.string())
174
162
  }).loose();
175
163
  var ListResourceTemplatesResultSchema = ResultSchema.extend({
176
- resourceTemplates: import_v4.z.array(ResourceTemplateSchema)
164
+ resourceTemplates: z.array(ResourceTemplateSchema)
177
165
  });
178
166
  var ReadResourceResultSchema = ResultSchema.extend({
179
- contents: import_v4.z.array(
180
- import_v4.z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
167
+ contents: z.array(
168
+ z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
181
169
  )
182
170
  });
183
- var PromptArgumentSchema = import_v4.z.object({
184
- name: import_v4.z.string(),
185
- description: import_v4.z.optional(import_v4.z.string()),
186
- required: import_v4.z.optional(import_v4.z.boolean())
171
+ var PromptArgumentSchema = z.object({
172
+ name: z.string(),
173
+ description: z.optional(z.string()),
174
+ required: z.optional(z.boolean())
187
175
  }).loose();
188
- var PromptSchema = import_v4.z.object({
189
- name: import_v4.z.string(),
190
- title: import_v4.z.optional(import_v4.z.string()),
191
- description: import_v4.z.optional(import_v4.z.string()),
192
- arguments: import_v4.z.optional(import_v4.z.array(PromptArgumentSchema))
176
+ var PromptSchema = z.object({
177
+ name: z.string(),
178
+ title: z.optional(z.string()),
179
+ description: z.optional(z.string()),
180
+ arguments: z.optional(z.array(PromptArgumentSchema))
193
181
  }).loose();
194
182
  var ListPromptsResultSchema = PaginatedResultSchema.extend({
195
- prompts: import_v4.z.array(PromptSchema)
183
+ prompts: z.array(PromptSchema)
196
184
  });
197
- var PromptMessageSchema = import_v4.z.object({
198
- role: import_v4.z.union([import_v4.z.literal("user"), import_v4.z.literal("assistant")]),
199
- content: import_v4.z.union([
185
+ var PromptMessageSchema = z.object({
186
+ role: z.union([z.literal("user"), z.literal("assistant")]),
187
+ content: z.union([
200
188
  TextContentSchema,
201
189
  ImageContentSchema,
202
- EmbeddedResourceSchema
190
+ EmbeddedResourceSchema,
191
+ ResourceLinkContentSchema
203
192
  ])
204
193
  }).loose();
205
194
  var GetPromptResultSchema = ResultSchema.extend({
206
- description: import_v4.z.optional(import_v4.z.string()),
207
- messages: import_v4.z.array(PromptMessageSchema)
195
+ description: z.optional(z.string()),
196
+ messages: z.array(PromptMessageSchema)
208
197
  });
209
198
  var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
210
- message: import_v4.z.string(),
211
- requestedSchema: import_v4.z.unknown()
199
+ message: z.string(),
200
+ requestedSchema: z.unknown()
212
201
  });
213
202
  var ElicitationRequestSchema = RequestSchema.extend({
214
- method: import_v4.z.literal("elicitation/create"),
203
+ method: z.literal("elicitation/create"),
215
204
  params: ElicitationRequestParamsSchema
216
205
  });
217
206
  var ElicitResultSchema = ResultSchema.extend({
218
- action: import_v4.z.union([
219
- import_v4.z.literal("accept"),
220
- import_v4.z.literal("decline"),
221
- import_v4.z.literal("cancel")
207
+ action: z.union([
208
+ z.literal("accept"),
209
+ z.literal("decline"),
210
+ z.literal("cancel")
222
211
  ]),
223
- content: import_v4.z.optional(import_v4.z.record(import_v4.z.string(), import_v4.z.unknown()))
212
+ content: z.optional(z.record(z.string(), z.unknown()))
224
213
  });
225
214
 
226
215
  // src/tool/json-rpc-message.ts
227
216
  var JSONRPC_VERSION = "2.0";
228
- var JSONRPCRequestSchema = import_v42.z.object({
229
- jsonrpc: import_v42.z.literal(JSONRPC_VERSION),
230
- id: import_v42.z.union([import_v42.z.string(), import_v42.z.number().int()])
217
+ var JSONRPCRequestSchema = z2.object({
218
+ jsonrpc: z2.literal(JSONRPC_VERSION),
219
+ id: z2.union([z2.string(), z2.number().int()])
231
220
  }).merge(RequestSchema).strict();
232
- var JSONRPCResponseSchema = import_v42.z.object({
233
- jsonrpc: import_v42.z.literal(JSONRPC_VERSION),
234
- id: import_v42.z.union([import_v42.z.string(), import_v42.z.number().int()]),
221
+ var JSONRPCResponseSchema = z2.object({
222
+ jsonrpc: z2.literal(JSONRPC_VERSION),
223
+ id: z2.union([z2.string(), z2.number().int()]),
235
224
  result: ResultSchema
236
225
  }).strict();
237
- var JSONRPCErrorSchema = import_v42.z.object({
238
- jsonrpc: import_v42.z.literal(JSONRPC_VERSION),
239
- id: import_v42.z.union([import_v42.z.string(), import_v42.z.number().int()]),
240
- error: import_v42.z.object({
241
- code: import_v42.z.number().int(),
242
- message: import_v42.z.string(),
243
- data: import_v42.z.optional(import_v42.z.unknown())
226
+ var JSONRPCErrorSchema = z2.object({
227
+ jsonrpc: z2.literal(JSONRPC_VERSION),
228
+ id: z2.union([z2.string(), z2.number().int()]),
229
+ error: z2.object({
230
+ code: z2.number().int(),
231
+ message: z2.string(),
232
+ data: z2.optional(z2.unknown())
244
233
  })
245
234
  }).strict();
246
- var JSONRPCNotificationSchema = import_v42.z.object({
247
- jsonrpc: import_v42.z.literal(JSONRPC_VERSION)
235
+ var JSONRPCNotificationSchema = z2.object({
236
+ jsonrpc: z2.literal(JSONRPC_VERSION)
248
237
  }).merge(
249
- import_v42.z.object({
250
- method: import_v42.z.string(),
251
- params: import_v42.z.optional(BaseParamsSchema)
238
+ z2.object({
239
+ method: z2.string(),
240
+ params: z2.optional(BaseParamsSchema)
252
241
  })
253
242
  ).strict();
254
- var JSONRPCMessageSchema = import_v42.z.union([
243
+ var JSONRPCMessageSchema = z2.union([
255
244
  JSONRPCRequestSchema,
256
245
  JSONRPCNotificationSchema,
257
246
  JSONRPCResponseSchema,
258
247
  JSONRPCErrorSchema
259
248
  ]);
249
+ async function parseJSONRPCMessage(text) {
250
+ return JSONRPCMessageSchema.parse(await parseJSON({ text }));
251
+ }
260
252
 
261
253
  // src/error/mcp-client-error.ts
262
- var import_provider = require("@ai-sdk/provider");
254
+ import { AISDKError } from "@ai-sdk/provider";
263
255
  var name = "AI_MCPClientError";
264
256
  var marker = `vercel.ai.error.${name}`;
265
257
  var symbol = Symbol.for(marker);
266
258
  var _a, _b;
267
- var MCPClientError = class extends (_b = import_provider.AISDKError, _a = symbol, _b) {
259
+ var MCPClientError = class extends (_b = AISDKError, _a = symbol, _b) {
268
260
  constructor({
269
261
  name: name2 = "MCPClientError",
270
262
  message,
271
263
  cause,
272
264
  data,
273
- code
265
+ code,
266
+ statusCode,
267
+ url,
268
+ responseBody
274
269
  }) {
275
270
  super({ name: name2, message, cause });
276
271
  this[_a] = true;
277
272
  this.data = data;
278
273
  this.code = code;
274
+ this.statusCode = statusCode;
275
+ this.url = url;
276
+ this.responseBody = responseBody;
279
277
  }
280
278
  static isInstance(error) {
281
- return import_provider.AISDKError.hasMarker(error, marker);
279
+ return AISDKError.hasMarker(error, marker);
282
280
  }
283
281
  };
284
282
 
285
283
  // src/tool/mcp-stdio/create-child-process.ts
286
- var import_node_child_process = require("child_process");
284
+ import { spawn } from "child_process";
287
285
 
288
286
  // src/tool/mcp-stdio/get-environment.ts
289
287
  function getEnvironment(customEnv) {
@@ -317,7 +315,7 @@ function getEnvironment(customEnv) {
317
315
  // src/tool/mcp-stdio/create-child-process.ts
318
316
  function createChildProcess(config, signal) {
319
317
  var _a2, _b2;
320
- return (0, import_node_child_process.spawn)(config.command, (_a2 = config.args) != null ? _a2 : [], {
318
+ return spawn(config.command, (_a2 = config.args) != null ? _a2 : [], {
321
319
  env: getEnvironment(config.env),
322
320
  stdio: ["pipe", "pipe", (_b2 = config.stderr) != null ? _b2 : "inherit"],
323
321
  shell: false,
@@ -374,7 +372,7 @@ var StdioMCPTransport = class {
374
372
  });
375
373
  (_b2 = this.process.stdout) == null ? void 0 : _b2.on("data", (chunk) => {
376
374
  this.readBuffer.append(chunk);
377
- this.processReadBuffer();
375
+ void this.processReadBuffer();
378
376
  });
379
377
  (_c = this.process.stdout) == null ? void 0 : _c.on("error", (error) => {
380
378
  var _a3;
@@ -386,14 +384,15 @@ var StdioMCPTransport = class {
386
384
  }
387
385
  });
388
386
  }
389
- processReadBuffer() {
387
+ async processReadBuffer() {
390
388
  var _a2, _b2;
391
389
  while (true) {
390
+ const line = this.readBuffer.readLine();
391
+ if (line === null) {
392
+ break;
393
+ }
392
394
  try {
393
- const message = this.readBuffer.readMessage();
394
- if (message === null) {
395
- break;
396
- }
395
+ const message = await deserializeMessage(line);
397
396
  (_a2 = this.onmessage) == null ? void 0 : _a2.call(this, message);
398
397
  } catch (error) {
399
398
  (_b2 = this.onerror) == null ? void 0 : _b2.call(this, error);
@@ -426,7 +425,7 @@ var ReadBuffer = class {
426
425
  append(chunk) {
427
426
  this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
428
427
  }
429
- readMessage() {
428
+ readLine() {
430
429
  if (!this.buffer) return null;
431
430
  const index = this.buffer.indexOf("\n");
432
431
  if (index === -1) {
@@ -434,7 +433,7 @@ var ReadBuffer = class {
434
433
  }
435
434
  const line = this.buffer.toString("utf8", 0, index);
436
435
  this.buffer = this.buffer.subarray(index + 1);
437
- return deserializeMessage(line);
436
+ return line;
438
437
  }
439
438
  clear() {
440
439
  this.buffer = void 0;
@@ -443,11 +442,10 @@ var ReadBuffer = class {
443
442
  function serializeMessage(message) {
444
443
  return JSON.stringify(message) + "\n";
445
444
  }
446
- function deserializeMessage(line) {
447
- return JSONRPCMessageSchema.parse(JSON.parse(line));
445
+ async function deserializeMessage(line) {
446
+ return parseJSONRPCMessage(line);
448
447
  }
449
- // Annotate the CommonJS export names for ESM import in node:
450
- 0 && (module.exports = {
451
- Experimental_StdioMCPTransport
452
- });
448
+ export {
449
+ StdioMCPTransport as Experimental_StdioMCPTransport
450
+ };
453
451
  //# sourceMappingURL=index.js.map