ai 4.1.63 → 4.1.65
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 +15 -0
- package/mcp-stdio/create-child-process.ts +56 -0
- package/mcp-stdio/dist/index.d.mts +169 -0
- package/mcp-stdio/dist/index.d.ts +169 -0
- package/mcp-stdio/dist/index.js +348 -0
- package/mcp-stdio/dist/index.js.map +1 -0
- package/mcp-stdio/dist/index.mjs +333 -0
- package/mcp-stdio/dist/index.mjs.map +1 -0
- package/mcp-stdio/index.ts +4 -0
- package/mcp-stdio/mcp-stdio-transport.test.ts +262 -0
- package/mcp-stdio/mcp-stdio-transport.ts +157 -0
- package/package.json +5 -4
| @@ -0,0 +1,333 @@ | |
| 1 | 
            +
            // core/tool/mcp/json-rpc-message.ts
         | 
| 2 | 
            +
            import { z as z2 } from "zod";
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            // core/tool/mcp/types.ts
         | 
| 5 | 
            +
            import { z } from "zod";
         | 
| 6 | 
            +
            var ClientOrServerImplementationSchema = z.object({
         | 
| 7 | 
            +
              name: z.string(),
         | 
| 8 | 
            +
              version: z.string()
         | 
| 9 | 
            +
            }).passthrough();
         | 
| 10 | 
            +
            var BaseParamsSchema = z.object({
         | 
| 11 | 
            +
              _meta: z.optional(z.object({}).passthrough())
         | 
| 12 | 
            +
            }).passthrough();
         | 
| 13 | 
            +
            var ResultSchema = BaseParamsSchema;
         | 
| 14 | 
            +
            var RequestSchema = z.object({
         | 
| 15 | 
            +
              method: z.string(),
         | 
| 16 | 
            +
              params: z.optional(BaseParamsSchema)
         | 
| 17 | 
            +
            });
         | 
| 18 | 
            +
            var ServerCapabilitiesSchema = z.object({
         | 
| 19 | 
            +
              experimental: z.optional(z.object({}).passthrough()),
         | 
| 20 | 
            +
              logging: z.optional(z.object({}).passthrough()),
         | 
| 21 | 
            +
              prompts: z.optional(
         | 
| 22 | 
            +
                z.object({
         | 
| 23 | 
            +
                  listChanged: z.optional(z.boolean())
         | 
| 24 | 
            +
                }).passthrough()
         | 
| 25 | 
            +
              ),
         | 
| 26 | 
            +
              resources: z.optional(
         | 
| 27 | 
            +
                z.object({
         | 
| 28 | 
            +
                  subscribe: z.optional(z.boolean()),
         | 
| 29 | 
            +
                  listChanged: z.optional(z.boolean())
         | 
| 30 | 
            +
                }).passthrough()
         | 
| 31 | 
            +
              ),
         | 
| 32 | 
            +
              tools: z.optional(
         | 
| 33 | 
            +
                z.object({
         | 
| 34 | 
            +
                  listChanged: z.optional(z.boolean())
         | 
| 35 | 
            +
                }).passthrough()
         | 
| 36 | 
            +
              )
         | 
| 37 | 
            +
            }).passthrough();
         | 
| 38 | 
            +
            var InitializeResultSchema = ResultSchema.extend({
         | 
| 39 | 
            +
              protocolVersion: z.string(),
         | 
| 40 | 
            +
              capabilities: ServerCapabilitiesSchema,
         | 
| 41 | 
            +
              serverInfo: ClientOrServerImplementationSchema,
         | 
| 42 | 
            +
              instructions: z.optional(z.string())
         | 
| 43 | 
            +
            });
         | 
| 44 | 
            +
            var PaginatedResultSchema = ResultSchema.extend({
         | 
| 45 | 
            +
              nextCursor: z.optional(z.string())
         | 
| 46 | 
            +
            });
         | 
| 47 | 
            +
            var ToolSchema = z.object({
         | 
| 48 | 
            +
              name: z.string(),
         | 
| 49 | 
            +
              description: z.optional(z.string()),
         | 
| 50 | 
            +
              inputSchema: z.object({
         | 
| 51 | 
            +
                type: z.literal("object"),
         | 
| 52 | 
            +
                properties: z.optional(z.object({}).passthrough())
         | 
| 53 | 
            +
              }).passthrough()
         | 
| 54 | 
            +
            }).passthrough();
         | 
| 55 | 
            +
            var ListToolsResultSchema = PaginatedResultSchema.extend({
         | 
| 56 | 
            +
              tools: z.array(ToolSchema)
         | 
| 57 | 
            +
            });
         | 
| 58 | 
            +
            var TextContentSchema = z.object({
         | 
| 59 | 
            +
              type: z.literal("text"),
         | 
| 60 | 
            +
              text: z.string()
         | 
| 61 | 
            +
            }).passthrough();
         | 
| 62 | 
            +
            var ImageContentSchema = z.object({
         | 
| 63 | 
            +
              type: z.literal("image"),
         | 
| 64 | 
            +
              data: z.string().base64(),
         | 
| 65 | 
            +
              mimeType: z.string()
         | 
| 66 | 
            +
            }).passthrough();
         | 
| 67 | 
            +
            var ResourceContentsSchema = z.object({
         | 
| 68 | 
            +
              /**
         | 
| 69 | 
            +
               * The URI of this resource.
         | 
| 70 | 
            +
               */
         | 
| 71 | 
            +
              uri: z.string(),
         | 
| 72 | 
            +
              /**
         | 
| 73 | 
            +
               * The MIME type of this resource, if known.
         | 
| 74 | 
            +
               */
         | 
| 75 | 
            +
              mimeType: z.optional(z.string())
         | 
| 76 | 
            +
            }).passthrough();
         | 
| 77 | 
            +
            var TextResourceContentsSchema = ResourceContentsSchema.extend({
         | 
| 78 | 
            +
              text: z.string()
         | 
| 79 | 
            +
            });
         | 
| 80 | 
            +
            var BlobResourceContentsSchema = ResourceContentsSchema.extend({
         | 
| 81 | 
            +
              blob: z.string().base64()
         | 
| 82 | 
            +
            });
         | 
| 83 | 
            +
            var EmbeddedResourceSchema = z.object({
         | 
| 84 | 
            +
              type: z.literal("resource"),
         | 
| 85 | 
            +
              resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
         | 
| 86 | 
            +
            }).passthrough();
         | 
| 87 | 
            +
            var CallToolResultSchema = ResultSchema.extend({
         | 
| 88 | 
            +
              content: z.array(
         | 
| 89 | 
            +
                z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
         | 
| 90 | 
            +
              ),
         | 
| 91 | 
            +
              isError: z.boolean().default(false).optional()
         | 
| 92 | 
            +
            }).or(
         | 
| 93 | 
            +
              ResultSchema.extend({
         | 
| 94 | 
            +
                toolResult: z.unknown()
         | 
| 95 | 
            +
              })
         | 
| 96 | 
            +
            );
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            // core/tool/mcp/json-rpc-message.ts
         | 
| 99 | 
            +
            var JSONRPC_VERSION = "2.0";
         | 
| 100 | 
            +
            var JSONRPCRequestSchema = z2.object({
         | 
| 101 | 
            +
              jsonrpc: z2.literal(JSONRPC_VERSION),
         | 
| 102 | 
            +
              id: z2.union([z2.string(), z2.number().int()])
         | 
| 103 | 
            +
            }).merge(RequestSchema).strict();
         | 
| 104 | 
            +
            var JSONRPCResponseSchema = z2.object({
         | 
| 105 | 
            +
              jsonrpc: z2.literal(JSONRPC_VERSION),
         | 
| 106 | 
            +
              id: z2.union([z2.string(), z2.number().int()]),
         | 
| 107 | 
            +
              result: ResultSchema
         | 
| 108 | 
            +
            }).strict();
         | 
| 109 | 
            +
            var JSONRPCErrorSchema = z2.object({
         | 
| 110 | 
            +
              jsonrpc: z2.literal(JSONRPC_VERSION),
         | 
| 111 | 
            +
              id: z2.union([z2.string(), z2.number().int()]),
         | 
| 112 | 
            +
              error: z2.object({
         | 
| 113 | 
            +
                code: z2.number().int(),
         | 
| 114 | 
            +
                message: z2.string(),
         | 
| 115 | 
            +
                data: z2.optional(z2.unknown())
         | 
| 116 | 
            +
              })
         | 
| 117 | 
            +
            }).strict();
         | 
| 118 | 
            +
            var JSONRPCNotificationSchema = z2.object({
         | 
| 119 | 
            +
              jsonrpc: z2.literal(JSONRPC_VERSION)
         | 
| 120 | 
            +
            }).merge(
         | 
| 121 | 
            +
              z2.object({
         | 
| 122 | 
            +
                method: z2.string(),
         | 
| 123 | 
            +
                params: z2.optional(BaseParamsSchema)
         | 
| 124 | 
            +
              })
         | 
| 125 | 
            +
            ).strict();
         | 
| 126 | 
            +
            var JSONRPCMessageSchema = z2.union([
         | 
| 127 | 
            +
              JSONRPCRequestSchema,
         | 
| 128 | 
            +
              JSONRPCNotificationSchema,
         | 
| 129 | 
            +
              JSONRPCResponseSchema,
         | 
| 130 | 
            +
              JSONRPCErrorSchema
         | 
| 131 | 
            +
            ]);
         | 
| 132 | 
            +
             | 
| 133 | 
            +
            // errors/index.ts
         | 
| 134 | 
            +
            import {
         | 
| 135 | 
            +
              AISDKError as AISDKError2,
         | 
| 136 | 
            +
              APICallError,
         | 
| 137 | 
            +
              EmptyResponseBodyError,
         | 
| 138 | 
            +
              InvalidPromptError,
         | 
| 139 | 
            +
              InvalidResponseDataError,
         | 
| 140 | 
            +
              JSONParseError,
         | 
| 141 | 
            +
              LoadAPIKeyError,
         | 
| 142 | 
            +
              NoContentGeneratedError,
         | 
| 143 | 
            +
              NoSuchModelError,
         | 
| 144 | 
            +
              TypeValidationError,
         | 
| 145 | 
            +
              UnsupportedFunctionalityError
         | 
| 146 | 
            +
            } from "@ai-sdk/provider";
         | 
| 147 | 
            +
             | 
| 148 | 
            +
            // errors/mcp-client-error.ts
         | 
| 149 | 
            +
            import { AISDKError } from "@ai-sdk/provider";
         | 
| 150 | 
            +
            var name = "AI_MCPClientError";
         | 
| 151 | 
            +
            var marker = `vercel.ai.error.${name}`;
         | 
| 152 | 
            +
            var symbol = Symbol.for(marker);
         | 
| 153 | 
            +
            var _a;
         | 
| 154 | 
            +
            var MCPClientError = class extends AISDKError {
         | 
| 155 | 
            +
              constructor({
         | 
| 156 | 
            +
                name: name2 = "MCPClientError",
         | 
| 157 | 
            +
                message,
         | 
| 158 | 
            +
                cause
         | 
| 159 | 
            +
              }) {
         | 
| 160 | 
            +
                super({ name: name2, message, cause });
         | 
| 161 | 
            +
                this[_a] = true;
         | 
| 162 | 
            +
              }
         | 
| 163 | 
            +
              static isInstance(error) {
         | 
| 164 | 
            +
                return AISDKError.hasMarker(error, marker);
         | 
| 165 | 
            +
              }
         | 
| 166 | 
            +
            };
         | 
| 167 | 
            +
            _a = symbol;
         | 
| 168 | 
            +
             | 
| 169 | 
            +
            // mcp-stdio/create-child-process.ts
         | 
| 170 | 
            +
            import { spawn } from "child_process";
         | 
| 171 | 
            +
            async function createChildProcess(config, signal) {
         | 
| 172 | 
            +
              var _a2, _b, _c;
         | 
| 173 | 
            +
              return spawn(config.command, (_a2 = config.args) != null ? _a2 : [], {
         | 
| 174 | 
            +
                env: (_b = config.env) != null ? _b : getDefaultEnvironment(),
         | 
| 175 | 
            +
                stdio: ["pipe", "pipe", (_c = config.stderr) != null ? _c : "inherit"],
         | 
| 176 | 
            +
                shell: false,
         | 
| 177 | 
            +
                signal,
         | 
| 178 | 
            +
                windowsHide: globalThis.process.platform === "win32" && isElectron(),
         | 
| 179 | 
            +
                cwd: config.cwd
         | 
| 180 | 
            +
              });
         | 
| 181 | 
            +
            }
         | 
| 182 | 
            +
            function getDefaultEnvironment() {
         | 
| 183 | 
            +
              const DEFAULT_INHERITED_ENV_VARS = globalThis.process.platform === "win32" ? [
         | 
| 184 | 
            +
                "APPDATA",
         | 
| 185 | 
            +
                "HOMEDRIVE",
         | 
| 186 | 
            +
                "HOMEPATH",
         | 
| 187 | 
            +
                "LOCALAPPDATA",
         | 
| 188 | 
            +
                "PATH",
         | 
| 189 | 
            +
                "PROCESSOR_ARCHITECTURE",
         | 
| 190 | 
            +
                "SYSTEMDRIVE",
         | 
| 191 | 
            +
                "SYSTEMROOT",
         | 
| 192 | 
            +
                "TEMP",
         | 
| 193 | 
            +
                "USERNAME",
         | 
| 194 | 
            +
                "USERPROFILE"
         | 
| 195 | 
            +
              ] : ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"];
         | 
| 196 | 
            +
              const env = {};
         | 
| 197 | 
            +
              for (const key of DEFAULT_INHERITED_ENV_VARS) {
         | 
| 198 | 
            +
                const value = globalThis.process.env[key];
         | 
| 199 | 
            +
                if (value === void 0) {
         | 
| 200 | 
            +
                  continue;
         | 
| 201 | 
            +
                }
         | 
| 202 | 
            +
                if (value.startsWith("()")) {
         | 
| 203 | 
            +
                  continue;
         | 
| 204 | 
            +
                }
         | 
| 205 | 
            +
                env[key] = value;
         | 
| 206 | 
            +
              }
         | 
| 207 | 
            +
              return env;
         | 
| 208 | 
            +
            }
         | 
| 209 | 
            +
            function isElectron() {
         | 
| 210 | 
            +
              return "type" in globalThis.process;
         | 
| 211 | 
            +
            }
         | 
| 212 | 
            +
             | 
| 213 | 
            +
            // mcp-stdio/mcp-stdio-transport.ts
         | 
| 214 | 
            +
            var StdioMCPTransport = class {
         | 
| 215 | 
            +
              constructor(server) {
         | 
| 216 | 
            +
                this.abortController = new AbortController();
         | 
| 217 | 
            +
                this.readBuffer = new ReadBuffer();
         | 
| 218 | 
            +
                this.serverParams = server;
         | 
| 219 | 
            +
              }
         | 
| 220 | 
            +
              async start() {
         | 
| 221 | 
            +
                if (this.process) {
         | 
| 222 | 
            +
                  throw new MCPClientError({
         | 
| 223 | 
            +
                    message: "StdioMCPTransport already started."
         | 
| 224 | 
            +
                  });
         | 
| 225 | 
            +
                }
         | 
| 226 | 
            +
                return new Promise(async (resolve, reject) => {
         | 
| 227 | 
            +
                  var _a2, _b, _c, _d;
         | 
| 228 | 
            +
                  try {
         | 
| 229 | 
            +
                    const process = await createChildProcess(
         | 
| 230 | 
            +
                      this.serverParams,
         | 
| 231 | 
            +
                      this.abortController.signal
         | 
| 232 | 
            +
                    );
         | 
| 233 | 
            +
                    this.process = process;
         | 
| 234 | 
            +
                    this.process.on("error", (error) => {
         | 
| 235 | 
            +
                      var _a3, _b2;
         | 
| 236 | 
            +
                      if (error.name === "AbortError") {
         | 
| 237 | 
            +
                        (_a3 = this.onclose) == null ? void 0 : _a3.call(this);
         | 
| 238 | 
            +
                        return;
         | 
| 239 | 
            +
                      }
         | 
| 240 | 
            +
                      reject(error);
         | 
| 241 | 
            +
                      (_b2 = this.onerror) == null ? void 0 : _b2.call(this, error);
         | 
| 242 | 
            +
                    });
         | 
| 243 | 
            +
                    this.process.on("spawn", () => {
         | 
| 244 | 
            +
                      resolve();
         | 
| 245 | 
            +
                    });
         | 
| 246 | 
            +
                    this.process.on("close", (_code) => {
         | 
| 247 | 
            +
                      var _a3;
         | 
| 248 | 
            +
                      this.process = void 0;
         | 
| 249 | 
            +
                      (_a3 = this.onclose) == null ? void 0 : _a3.call(this);
         | 
| 250 | 
            +
                    });
         | 
| 251 | 
            +
                    (_a2 = this.process.stdin) == null ? void 0 : _a2.on("error", (error) => {
         | 
| 252 | 
            +
                      var _a3;
         | 
| 253 | 
            +
                      (_a3 = this.onerror) == null ? void 0 : _a3.call(this, error);
         | 
| 254 | 
            +
                    });
         | 
| 255 | 
            +
                    (_b = this.process.stdout) == null ? void 0 : _b.on("data", (chunk) => {
         | 
| 256 | 
            +
                      this.readBuffer.append(chunk);
         | 
| 257 | 
            +
                      this.processReadBuffer();
         | 
| 258 | 
            +
                    });
         | 
| 259 | 
            +
                    (_c = this.process.stdout) == null ? void 0 : _c.on("error", (error) => {
         | 
| 260 | 
            +
                      var _a3;
         | 
| 261 | 
            +
                      (_a3 = this.onerror) == null ? void 0 : _a3.call(this, error);
         | 
| 262 | 
            +
                    });
         | 
| 263 | 
            +
                  } catch (error) {
         | 
| 264 | 
            +
                    reject(error);
         | 
| 265 | 
            +
                    (_d = this.onerror) == null ? void 0 : _d.call(this, error);
         | 
| 266 | 
            +
                  }
         | 
| 267 | 
            +
                });
         | 
| 268 | 
            +
              }
         | 
| 269 | 
            +
              processReadBuffer() {
         | 
| 270 | 
            +
                var _a2, _b;
         | 
| 271 | 
            +
                while (true) {
         | 
| 272 | 
            +
                  try {
         | 
| 273 | 
            +
                    const message = this.readBuffer.readMessage();
         | 
| 274 | 
            +
                    if (message === null) {
         | 
| 275 | 
            +
                      break;
         | 
| 276 | 
            +
                    }
         | 
| 277 | 
            +
                    (_a2 = this.onmessage) == null ? void 0 : _a2.call(this, message);
         | 
| 278 | 
            +
                  } catch (error) {
         | 
| 279 | 
            +
                    (_b = this.onerror) == null ? void 0 : _b.call(this, error);
         | 
| 280 | 
            +
                  }
         | 
| 281 | 
            +
                }
         | 
| 282 | 
            +
              }
         | 
| 283 | 
            +
              async close() {
         | 
| 284 | 
            +
                this.abortController.abort();
         | 
| 285 | 
            +
                this.process = void 0;
         | 
| 286 | 
            +
                this.readBuffer.clear();
         | 
| 287 | 
            +
              }
         | 
| 288 | 
            +
              send(message) {
         | 
| 289 | 
            +
                return new Promise((resolve) => {
         | 
| 290 | 
            +
                  var _a2;
         | 
| 291 | 
            +
                  if (!((_a2 = this.process) == null ? void 0 : _a2.stdin)) {
         | 
| 292 | 
            +
                    throw new MCPClientError({
         | 
| 293 | 
            +
                      message: "StdioClientTransport not connected"
         | 
| 294 | 
            +
                    });
         | 
| 295 | 
            +
                  }
         | 
| 296 | 
            +
                  const json = serializeMessage(message);
         | 
| 297 | 
            +
                  if (this.process.stdin.write(json)) {
         | 
| 298 | 
            +
                    resolve();
         | 
| 299 | 
            +
                  } else {
         | 
| 300 | 
            +
                    this.process.stdin.once("drain", resolve);
         | 
| 301 | 
            +
                  }
         | 
| 302 | 
            +
                });
         | 
| 303 | 
            +
              }
         | 
| 304 | 
            +
            };
         | 
| 305 | 
            +
            var ReadBuffer = class {
         | 
| 306 | 
            +
              append(chunk) {
         | 
| 307 | 
            +
                this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
         | 
| 308 | 
            +
              }
         | 
| 309 | 
            +
              readMessage() {
         | 
| 310 | 
            +
                if (!this.buffer)
         | 
| 311 | 
            +
                  return null;
         | 
| 312 | 
            +
                const index = this.buffer.indexOf("\n");
         | 
| 313 | 
            +
                if (index === -1) {
         | 
| 314 | 
            +
                  return null;
         | 
| 315 | 
            +
                }
         | 
| 316 | 
            +
                const line = this.buffer.toString("utf8", 0, index);
         | 
| 317 | 
            +
                this.buffer = this.buffer.subarray(index + 1);
         | 
| 318 | 
            +
                return deserializeMessage(line);
         | 
| 319 | 
            +
              }
         | 
| 320 | 
            +
              clear() {
         | 
| 321 | 
            +
                this.buffer = void 0;
         | 
| 322 | 
            +
              }
         | 
| 323 | 
            +
            };
         | 
| 324 | 
            +
            function serializeMessage(message) {
         | 
| 325 | 
            +
              return JSON.stringify(message) + "\n";
         | 
| 326 | 
            +
            }
         | 
| 327 | 
            +
            function deserializeMessage(line) {
         | 
| 328 | 
            +
              return JSONRPCMessageSchema.parse(JSON.parse(line));
         | 
| 329 | 
            +
            }
         | 
| 330 | 
            +
            export {
         | 
| 331 | 
            +
              StdioMCPTransport as Experimental_StdioMCPTransport
         | 
| 332 | 
            +
            };
         | 
| 333 | 
            +
            //# sourceMappingURL=index.mjs.map
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            {"version":3,"sources":["../../core/tool/mcp/json-rpc-message.ts","../../core/tool/mcp/types.ts","../../errors/index.ts","../../errors/mcp-client-error.ts","../create-child-process.ts","../mcp-stdio-transport.ts"],"sourcesContent":["import { z } from 'zod';\nimport { BaseParamsSchema, RequestSchema, ResultSchema } from './types';\n\nconst JSONRPC_VERSION = '2.0';\n\nconst JSONRPCRequestSchema = z\n  .object({\n    jsonrpc: z.literal(JSONRPC_VERSION),\n    id: z.union([z.string(), z.number().int()]),\n  })\n  .merge(RequestSchema)\n  .strict();\n\nexport type JSONRPCRequest = z.infer<typeof JSONRPCRequestSchema>;\n\nconst JSONRPCResponseSchema = z\n  .object({\n    jsonrpc: z.literal(JSONRPC_VERSION),\n    id: z.union([z.string(), z.number().int()]),\n    result: ResultSchema,\n  })\n  .strict();\n\nexport type JSONRPCResponse = z.infer<typeof JSONRPCResponseSchema>;\n\nconst JSONRPCErrorSchema = z\n  .object({\n    jsonrpc: z.literal(JSONRPC_VERSION),\n    id: z.union([z.string(), z.number().int()]),\n    error: z.object({\n      code: z.number().int(),\n      message: z.string(),\n      data: z.optional(z.unknown()),\n    }),\n  })\n  .strict();\n\nexport type JSONRPCError = z.infer<typeof JSONRPCErrorSchema>;\n\nconst JSONRPCNotificationSchema = z\n  .object({\n    jsonrpc: z.literal(JSONRPC_VERSION),\n  })\n  .merge(\n    z.object({\n      method: z.string(),\n      params: z.optional(BaseParamsSchema),\n    }),\n  )\n  .strict();\n\nexport type JSONRPCNotification = z.infer<typeof JSONRPCNotificationSchema>;\n\nexport const JSONRPCMessageSchema = z.union([\n  JSONRPCRequestSchema,\n  JSONRPCNotificationSchema,\n  JSONRPCResponseSchema,\n  JSONRPCErrorSchema,\n]);\n\nexport type JSONRPCMessage = z.infer<typeof JSONRPCMessageSchema>;\n","import { z } from 'zod';\nimport {\n  inferParameters,\n  Tool,\n  ToolExecutionOptions,\n  ToolParameters,\n} from '../tool';\n\nexport const LATEST_PROTOCOL_VERSION = '2024-11-05';\nexport const SUPPORTED_PROTOCOL_VERSIONS = [\n  LATEST_PROTOCOL_VERSION,\n  '2024-10-07',\n];\n\nexport type ToolSchemas =\n  | Record<string, { parameters: ToolParameters }>\n  | 'automatic'\n  | undefined;\n\nexport type McpToolSet<TOOL_SCHEMAS extends ToolSchemas = 'automatic'> =\n  TOOL_SCHEMAS extends Record<string, { parameters: ToolParameters }>\n    ? {\n        [K in keyof TOOL_SCHEMAS]: Tool<\n          TOOL_SCHEMAS[K]['parameters'],\n          CallToolResult\n        > & {\n          execute: (\n            args: inferParameters<TOOL_SCHEMAS[K]['parameters']>,\n            options: ToolExecutionOptions,\n          ) => PromiseLike<CallToolResult>;\n        };\n      }\n    : {\n        [k: string]: Tool<z.ZodUnknown, CallToolResult> & {\n          execute: (\n            args: unknown,\n            options: ToolExecutionOptions,\n          ) => PromiseLike<CallToolResult>;\n        };\n      };\n\nconst ClientOrServerImplementationSchema = z\n  .object({\n    name: z.string(),\n    version: z.string(),\n  })\n  .passthrough();\nexport type Configuration = z.infer<typeof ClientOrServerImplementationSchema>;\n\nexport const BaseParamsSchema = z\n  .object({\n    _meta: z.optional(z.object({}).passthrough()),\n  })\n  .passthrough();\ntype BaseParams = z.infer<typeof BaseParamsSchema>;\nexport const ResultSchema = BaseParamsSchema;\n\nexport const RequestSchema = z.object({\n  method: z.string(),\n  params: z.optional(BaseParamsSchema),\n});\nexport type Request = z.infer<typeof RequestSchema>;\nexport type RequestOptions = {\n  signal?: AbortSignal;\n  timeout?: number;\n  maxTotalTimeout?: number;\n};\n\nexport type Notification = z.infer<typeof RequestSchema>;\n\nconst ServerCapabilitiesSchema = z\n  .object({\n    experimental: z.optional(z.object({}).passthrough()),\n    logging: z.optional(z.object({}).passthrough()),\n    prompts: z.optional(\n      z\n        .object({\n          listChanged: z.optional(z.boolean()),\n        })\n        .passthrough(),\n    ),\n    resources: z.optional(\n      z\n        .object({\n          subscribe: z.optional(z.boolean()),\n          listChanged: z.optional(z.boolean()),\n        })\n        .passthrough(),\n    ),\n    tools: z.optional(\n      z\n        .object({\n          listChanged: z.optional(z.boolean()),\n        })\n        .passthrough(),\n    ),\n  })\n  .passthrough();\nexport type ServerCapabilities = z.infer<typeof ServerCapabilitiesSchema>;\n\nexport const InitializeResultSchema = ResultSchema.extend({\n  protocolVersion: z.string(),\n  capabilities: ServerCapabilitiesSchema,\n  serverInfo: ClientOrServerImplementationSchema,\n  instructions: z.optional(z.string()),\n});\nexport type InitializeResult = z.infer<typeof InitializeResultSchema>;\n\nexport type PaginatedRequest = Request & {\n  params?: BaseParams & {\n    cursor?: string;\n  };\n};\n\nconst PaginatedResultSchema = ResultSchema.extend({\n  nextCursor: z.optional(z.string()),\n});\n\nconst ToolSchema = z\n  .object({\n    name: z.string(),\n    description: z.optional(z.string()),\n    inputSchema: z\n      .object({\n        type: z.literal('object'),\n        properties: z.optional(z.object({}).passthrough()),\n      })\n      .passthrough(),\n  })\n  .passthrough();\nexport type MCPTool = z.infer<typeof ToolSchema>;\nexport const ListToolsResultSchema = PaginatedResultSchema.extend({\n  tools: z.array(ToolSchema),\n});\nexport type ListToolsResult = z.infer<typeof ListToolsResultSchema>;\n\nconst TextContentSchema = z\n  .object({\n    type: z.literal('text'),\n    text: z.string(),\n  })\n  .passthrough();\nconst ImageContentSchema = z\n  .object({\n    type: z.literal('image'),\n    data: z.string().base64(),\n    mimeType: z.string(),\n  })\n  .passthrough();\nconst ResourceContentsSchema = z\n  .object({\n    /**\n     * The URI of this resource.\n     */\n    uri: z.string(),\n    /**\n     * The MIME type of this resource, if known.\n     */\n    mimeType: z.optional(z.string()),\n  })\n  .passthrough();\nconst TextResourceContentsSchema = ResourceContentsSchema.extend({\n  text: z.string(),\n});\nconst BlobResourceContentsSchema = ResourceContentsSchema.extend({\n  blob: z.string().base64(),\n});\nconst EmbeddedResourceSchema = z\n  .object({\n    type: z.literal('resource'),\n    resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema]),\n  })\n  .passthrough();\n\nexport const CallToolResultSchema = ResultSchema.extend({\n  content: z.array(\n    z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema]),\n  ),\n  isError: z.boolean().default(false).optional(),\n}).or(\n  ResultSchema.extend({\n    toolResult: z.unknown(),\n  }),\n);\nexport type CallToolResult = z.infer<typeof CallToolResultSchema>;\n","export {\n  AISDKError,\n  APICallError,\n  EmptyResponseBodyError,\n  InvalidPromptError,\n  InvalidResponseDataError,\n  JSONParseError,\n  LoadAPIKeyError,\n  NoContentGeneratedError,\n  NoSuchModelError,\n  TypeValidationError,\n  UnsupportedFunctionalityError,\n} from '@ai-sdk/provider';\n\nexport { InvalidArgumentError } from './invalid-argument-error';\nexport { InvalidStreamPartError } from './invalid-stream-part-error';\nexport { InvalidToolArgumentsError } from './invalid-tool-arguments-error';\nexport { NoImageGeneratedError } from './no-image-generated-error';\nexport { NoObjectGeneratedError } from './no-object-generated-error';\nexport { NoOutputSpecifiedError } from './no-output-specified-error';\nexport { NoSuchToolError } from './no-such-tool-error';\nexport { ToolCallRepairError } from './tool-call-repair-error';\nexport { ToolExecutionError } from './tool-execution-error';\nexport { MCPClientError } from './mcp-client-error';\n\nexport { InvalidDataContentError } from '../core/prompt/invalid-data-content-error';\nexport { InvalidMessageRoleError } from '../core/prompt/invalid-message-role-error';\nexport { MessageConversionError } from '../core/prompt/message-conversion-error';\nexport { DownloadError } from '../util/download-error';\nexport { RetryError } from '../util/retry-error';\n","import { AISDKError } from '@ai-sdk/provider';\n\nconst name = 'AI_MCPClientError';\nconst marker = `vercel.ai.error.${name}`;\nconst symbol = Symbol.for(marker);\n\n/**\n * An error occurred with the MCP client.\n */\nexport class MCPClientError extends AISDKError {\n  private readonly [symbol] = true;\n\n  constructor({\n    name = 'MCPClientError',\n    message,\n    cause,\n  }: {\n    name?: string;\n    message: string;\n    cause?: unknown;\n  }) {\n    super({ name, message, cause });\n  }\n\n  static isInstance(error: unknown): error is MCPClientError {\n    return AISDKError.hasMarker(error, marker);\n  }\n}\n","import { ChildProcess, spawn } from 'node:child_process';\nimport { StdioConfig } from './mcp-stdio-transport';\n\nexport async function createChildProcess(\n  config: StdioConfig,\n  signal: AbortSignal,\n): Promise<ChildProcess> {\n  return spawn(config.command, config.args ?? [], {\n    env: config.env ?? getDefaultEnvironment(),\n    stdio: ['pipe', 'pipe', config.stderr ?? 'inherit'],\n    shell: false,\n    signal,\n    windowsHide: globalThis.process.platform === 'win32' && isElectron(),\n    cwd: config.cwd,\n  });\n}\n\nfunction getDefaultEnvironment(): Record<string, string> {\n  const DEFAULT_INHERITED_ENV_VARS =\n    globalThis.process.platform === 'win32'\n      ? [\n          'APPDATA',\n          'HOMEDRIVE',\n          'HOMEPATH',\n          'LOCALAPPDATA',\n          'PATH',\n          'PROCESSOR_ARCHITECTURE',\n          'SYSTEMDRIVE',\n          'SYSTEMROOT',\n          'TEMP',\n          'USERNAME',\n          'USERPROFILE',\n        ]\n      : ['HOME', 'LOGNAME', 'PATH', 'SHELL', 'TERM', 'USER'];\n\n  const env: Record<string, string> = {};\n\n  for (const key of DEFAULT_INHERITED_ENV_VARS) {\n    const value = globalThis.process.env[key];\n    if (value === undefined) {\n      continue;\n    }\n\n    if (value.startsWith('()')) {\n      continue;\n    }\n\n    env[key] = value;\n  }\n\n  return env;\n}\n\nfunction isElectron() {\n  return 'type' in globalThis.process;\n}\n","import type { ChildProcess, IOType } from 'node:child_process';\nimport { Stream } from 'node:stream';\nimport {\n  JSONRPCMessage,\n  JSONRPCMessageSchema,\n} from '../core/tool/mcp/json-rpc-message';\nimport { MCPTransport } from '../core/tool/mcp/mcp-transport';\nimport { MCPClientError } from '../errors';\nimport { createChildProcess } from './create-child-process';\n\nexport interface StdioConfig {\n  command: string;\n  args?: string[];\n  env?: Record<string, string>;\n  stderr?: IOType | Stream | number;\n  cwd?: string;\n}\n\nexport class StdioMCPTransport implements MCPTransport {\n  private process?: ChildProcess;\n  private abortController: AbortController = new AbortController();\n  private readBuffer: ReadBuffer = new ReadBuffer();\n  private serverParams: StdioConfig;\n\n  onclose?: () => void;\n  onerror?: (error: unknown) => void;\n  onmessage?: (message: JSONRPCMessage) => void;\n\n  constructor(server: StdioConfig) {\n    this.serverParams = server;\n  }\n\n  async start(): Promise<void> {\n    if (this.process) {\n      throw new MCPClientError({\n        message: 'StdioMCPTransport already started.',\n      });\n    }\n\n    return new Promise(async (resolve, reject) => {\n      try {\n        const process = await createChildProcess(\n          this.serverParams,\n          this.abortController.signal,\n        );\n\n        this.process = process;\n\n        this.process.on('error', error => {\n          if (error.name === 'AbortError') {\n            this.onclose?.();\n            return;\n          }\n\n          reject(error);\n          this.onerror?.(error);\n        });\n\n        this.process.on('spawn', () => {\n          resolve();\n        });\n\n        this.process.on('close', _code => {\n          this.process = undefined;\n          this.onclose?.();\n        });\n\n        this.process.stdin?.on('error', error => {\n          this.onerror?.(error);\n        });\n\n        this.process.stdout?.on('data', chunk => {\n          this.readBuffer.append(chunk);\n          this.processReadBuffer();\n        });\n\n        this.process.stdout?.on('error', error => {\n          this.onerror?.(error);\n        });\n      } catch (error) {\n        reject(error);\n        this.onerror?.(error);\n      }\n    });\n  }\n\n  private processReadBuffer() {\n    while (true) {\n      try {\n        const message = this.readBuffer.readMessage();\n        if (message === null) {\n          break;\n        }\n\n        this.onmessage?.(message);\n      } catch (error) {\n        this.onerror?.(error as Error);\n      }\n    }\n  }\n\n  async close(): Promise<void> {\n    this.abortController.abort();\n    this.process = undefined;\n    this.readBuffer.clear();\n  }\n\n  send(message: JSONRPCMessage): Promise<void> {\n    return new Promise(resolve => {\n      if (!this.process?.stdin) {\n        throw new MCPClientError({\n          message: 'StdioClientTransport not connected',\n        });\n      }\n\n      const json = serializeMessage(message);\n      if (this.process.stdin.write(json)) {\n        resolve();\n      } else {\n        this.process.stdin.once('drain', resolve);\n      }\n    });\n  }\n}\n\nclass ReadBuffer {\n  private buffer?: Buffer;\n\n  append(chunk: Buffer): void {\n    this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;\n  }\n\n  readMessage(): JSONRPCMessage | null {\n    if (!this.buffer) return null;\n\n    const index = this.buffer.indexOf('\\n');\n    if (index === -1) {\n      return null;\n    }\n\n    const line = this.buffer.toString('utf8', 0, index);\n    this.buffer = this.buffer.subarray(index + 1);\n    return deserializeMessage(line);\n  }\n\n  clear(): void {\n    this.buffer = undefined;\n  }\n}\n\nfunction serializeMessage(message: JSONRPCMessage): string {\n  return JSON.stringify(message) + '\\n';\n}\n\nexport function deserializeMessage(line: string): JSONRPCMessage {\n  return JSONRPCMessageSchema.parse(JSON.parse(line));\n}\n"],"mappings":";AAAA,SAAS,KAAAA,UAAS;;;ACAlB,SAAS,SAAS;AAyClB,IAAM,qCAAqC,EACxC,OAAO;AAAA,EACN,MAAM,EAAE,OAAO;AAAA,EACf,SAAS,EAAE,OAAO;AACpB,CAAC,EACA,YAAY;AAGR,IAAM,mBAAmB,EAC7B,OAAO;AAAA,EACN,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,EAAE,YAAY,CAAC;AAC9C,CAAC,EACA,YAAY;AAER,IAAM,eAAe;AAErB,IAAM,gBAAgB,EAAE,OAAO;AAAA,EACpC,QAAQ,EAAE,OAAO;AAAA,EACjB,QAAQ,EAAE,SAAS,gBAAgB;AACrC,CAAC;AAUD,IAAM,2BAA2B,EAC9B,OAAO;AAAA,EACN,cAAc,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,EAAE,YAAY,CAAC;AAAA,EACnD,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,EAAE,YAAY,CAAC;AAAA,EAC9C,SAAS,EAAE;AAAA,IACT,EACG,OAAO;AAAA,MACN,aAAa,EAAE,SAAS,EAAE,QAAQ,CAAC;AAAA,IACrC,CAAC,EACA,YAAY;AAAA,EACjB;AAA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AAO,qBAAqB,MAAM,KAAK,MAAM,IAAI,CAAC;AACpD;","names":["z","z","AISDKError","name","_a","_a","_b"]}
         | 
| @@ -0,0 +1,262 @@ | |
| 1 | 
            +
            import type { ChildProcess } from 'node:child_process';
         | 
| 2 | 
            +
            import { EventEmitter } from 'node:events';
         | 
| 3 | 
            +
            import { beforeEach, describe, expect, it, vi } from 'vitest';
         | 
| 4 | 
            +
            import { JSONRPCMessage } from '../core/tool/mcp/json-rpc-message';
         | 
| 5 | 
            +
            import { MCPClientError } from '../errors';
         | 
| 6 | 
            +
            import { createChildProcess } from './create-child-process';
         | 
| 7 | 
            +
            import { StdioMCPTransport } from './mcp-stdio-transport';
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            vi.mock('./create-child-process', { spy: true });
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            interface MockChildProcess {
         | 
| 12 | 
            +
              stdin: EventEmitter & { write?: ReturnType<typeof vi.fn> };
         | 
| 13 | 
            +
              stdout: EventEmitter;
         | 
| 14 | 
            +
              stderr: EventEmitter;
         | 
| 15 | 
            +
              on: ReturnType<typeof vi.fn>;
         | 
| 16 | 
            +
              removeAllListeners: ReturnType<typeof vi.fn>;
         | 
| 17 | 
            +
            }
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            describe('StdioMCPTransport', () => {
         | 
| 20 | 
            +
              let transport: StdioMCPTransport;
         | 
| 21 | 
            +
              let mockChildProcess: MockChildProcess;
         | 
| 22 | 
            +
              let mockStdin: EventEmitter & { write?: ReturnType<typeof vi.fn> };
         | 
| 23 | 
            +
              let mockStdout: EventEmitter;
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              beforeEach(() => {
         | 
| 26 | 
            +
                vi.clearAllMocks();
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                mockStdin = new EventEmitter();
         | 
| 29 | 
            +
                mockStdout = new EventEmitter();
         | 
| 30 | 
            +
                mockChildProcess = {
         | 
| 31 | 
            +
                  stdin: mockStdin,
         | 
| 32 | 
            +
                  stdout: mockStdout,
         | 
| 33 | 
            +
                  stderr: new EventEmitter(),
         | 
| 34 | 
            +
                  on: vi.fn(),
         | 
| 35 | 
            +
                  removeAllListeners: vi.fn(),
         | 
| 36 | 
            +
                };
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                vi.mocked(createChildProcess).mockResolvedValue(
         | 
| 39 | 
            +
                  mockChildProcess as unknown as ChildProcess,
         | 
| 40 | 
            +
                );
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                transport = new StdioMCPTransport({
         | 
| 43 | 
            +
                  command: 'test-command',
         | 
| 44 | 
            +
                  args: ['--test'],
         | 
| 45 | 
            +
                });
         | 
| 46 | 
            +
              });
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              afterEach(() => {
         | 
| 49 | 
            +
                transport.close();
         | 
| 50 | 
            +
              });
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              describe('start', () => {
         | 
| 53 | 
            +
                it('should successfully start the transport', async () => {
         | 
| 54 | 
            +
                  const stdinOnSpy = vi.spyOn(mockStdin, 'on');
         | 
| 55 | 
            +
                  const stdoutOnSpy = vi.spyOn(mockStdout, 'on');
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 58 | 
            +
                    (event: string, callback: () => void) => {
         | 
| 59 | 
            +
                      if (event === 'spawn') {
         | 
| 60 | 
            +
                        callback();
         | 
| 61 | 
            +
                      }
         | 
| 62 | 
            +
                    },
         | 
| 63 | 
            +
                  );
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                  const startPromise = transport.start();
         | 
| 66 | 
            +
                  await expect(startPromise).resolves.toBeUndefined();
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                  expect(mockChildProcess.on).toHaveBeenCalledWith(
         | 
| 69 | 
            +
                    'error',
         | 
| 70 | 
            +
                    expect.any(Function),
         | 
| 71 | 
            +
                  );
         | 
| 72 | 
            +
                  expect(mockChildProcess.on).toHaveBeenCalledWith(
         | 
| 73 | 
            +
                    'spawn',
         | 
| 74 | 
            +
                    expect.any(Function),
         | 
| 75 | 
            +
                  );
         | 
| 76 | 
            +
                  expect(mockChildProcess.on).toHaveBeenCalledWith(
         | 
| 77 | 
            +
                    'close',
         | 
| 78 | 
            +
                    expect.any(Function),
         | 
| 79 | 
            +
                  );
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                  expect(stdinOnSpy).toHaveBeenCalledWith('error', expect.any(Function));
         | 
| 82 | 
            +
                  expect(stdoutOnSpy).toHaveBeenCalledWith('error', expect.any(Function));
         | 
| 83 | 
            +
                  expect(stdoutOnSpy).toHaveBeenCalledWith('data', expect.any(Function));
         | 
| 84 | 
            +
                });
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                it('should throw error if already started', async () => {
         | 
| 87 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 88 | 
            +
                    (event: string, callback: () => void) => {
         | 
| 89 | 
            +
                      if (event === 'spawn') {
         | 
| 90 | 
            +
                        callback();
         | 
| 91 | 
            +
                      }
         | 
| 92 | 
            +
                    },
         | 
| 93 | 
            +
                  );
         | 
| 94 | 
            +
                  const firstStart = transport.start();
         | 
| 95 | 
            +
                  await expect(firstStart).resolves.toBeUndefined();
         | 
| 96 | 
            +
                  const secondStart = transport.start();
         | 
| 97 | 
            +
                  await expect(secondStart).rejects.toThrow(MCPClientError);
         | 
| 98 | 
            +
                });
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                it('should handle spawn errors', async () => {
         | 
| 101 | 
            +
                  const error = new Error('Spawn failed');
         | 
| 102 | 
            +
                  const onErrorSpy = vi.fn();
         | 
| 103 | 
            +
                  transport.onerror = onErrorSpy;
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                  // simulate `spawn` failure by emitting error event after returning child process
         | 
| 106 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 107 | 
            +
                    (event: string, callback: (err: Error) => void) => {
         | 
| 108 | 
            +
                      if (event === 'error') {
         | 
| 109 | 
            +
                        callback(error);
         | 
| 110 | 
            +
                      }
         | 
| 111 | 
            +
                    },
         | 
| 112 | 
            +
                  );
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                  const startPromise = transport.start();
         | 
| 115 | 
            +
                  await expect(startPromise).rejects.toThrow('Spawn failed');
         | 
| 116 | 
            +
                  expect(onErrorSpy).toHaveBeenCalledWith(error);
         | 
| 117 | 
            +
                });
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                it('should handle child_process import errors', async () => {
         | 
| 120 | 
            +
                  vi.mocked(createChildProcess).mockRejectedValue(
         | 
| 121 | 
            +
                    new MCPClientError({
         | 
| 122 | 
            +
                      message: 'Failed to load child_process module dynamically',
         | 
| 123 | 
            +
                    }),
         | 
| 124 | 
            +
                  );
         | 
| 125 | 
            +
             | 
| 126 | 
            +
                  const startPromise = transport.start();
         | 
| 127 | 
            +
                  await expect(startPromise).rejects.toThrow(
         | 
| 128 | 
            +
                    'Failed to load child_process module dynamically',
         | 
| 129 | 
            +
                  );
         | 
| 130 | 
            +
                });
         | 
| 131 | 
            +
              });
         | 
| 132 | 
            +
             | 
| 133 | 
            +
              describe('send', () => {
         | 
| 134 | 
            +
                beforeEach(async () => {
         | 
| 135 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 136 | 
            +
                    (event: string, callback: () => void) => {
         | 
| 137 | 
            +
                      if (event === 'spawn') {
         | 
| 138 | 
            +
                        callback();
         | 
| 139 | 
            +
                      }
         | 
| 140 | 
            +
                    },
         | 
| 141 | 
            +
                  );
         | 
| 142 | 
            +
                  await transport.start();
         | 
| 143 | 
            +
                });
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                it('should successfully send a message', async () => {
         | 
| 146 | 
            +
                  const message: JSONRPCMessage = {
         | 
| 147 | 
            +
                    jsonrpc: '2.0',
         | 
| 148 | 
            +
                    id: '1',
         | 
| 149 | 
            +
                    method: 'test',
         | 
| 150 | 
            +
                    params: {},
         | 
| 151 | 
            +
                  };
         | 
| 152 | 
            +
             | 
| 153 | 
            +
                  mockStdin.write = vi.fn().mockReturnValue(true);
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                  await transport.send(message);
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                  expect(mockStdin.write).toHaveBeenCalledWith(
         | 
| 158 | 
            +
                    JSON.stringify(message) + '\n',
         | 
| 159 | 
            +
                  );
         | 
| 160 | 
            +
                });
         | 
| 161 | 
            +
             | 
| 162 | 
            +
                it('should handle write backpressure', async () => {
         | 
| 163 | 
            +
                  const message: JSONRPCMessage = {
         | 
| 164 | 
            +
                    jsonrpc: '2.0',
         | 
| 165 | 
            +
                    id: '1',
         | 
| 166 | 
            +
                    method: 'test',
         | 
| 167 | 
            +
                    params: {},
         | 
| 168 | 
            +
                  };
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                  mockStdin.write = vi.fn().mockReturnValue(false);
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                  const sendPromise = transport.send(message);
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                  mockStdin.emit('drain');
         | 
| 175 | 
            +
             | 
| 176 | 
            +
                  await expect(sendPromise).resolves.toBeUndefined();
         | 
| 177 | 
            +
                });
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                it('should throw error if transport is not connected', async () => {
         | 
| 180 | 
            +
                  await transport.close();
         | 
| 181 | 
            +
             | 
| 182 | 
            +
                  const message: JSONRPCMessage = {
         | 
| 183 | 
            +
                    jsonrpc: '2.0',
         | 
| 184 | 
            +
                    id: '1',
         | 
| 185 | 
            +
                    method: 'test',
         | 
| 186 | 
            +
                    params: {},
         | 
| 187 | 
            +
                  };
         | 
| 188 | 
            +
             | 
| 189 | 
            +
                  await expect(transport.send(message)).rejects.toThrow(MCPClientError);
         | 
| 190 | 
            +
                });
         | 
| 191 | 
            +
              });
         | 
| 192 | 
            +
             | 
| 193 | 
            +
              describe('message handling', () => {
         | 
| 194 | 
            +
                const onMessageSpy = vi.fn();
         | 
| 195 | 
            +
             | 
| 196 | 
            +
                beforeEach(async () => {
         | 
| 197 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 198 | 
            +
                    (event: string, callback: () => void) => {
         | 
| 199 | 
            +
                      if (event === 'spawn') {
         | 
| 200 | 
            +
                        callback();
         | 
| 201 | 
            +
                      }
         | 
| 202 | 
            +
                    },
         | 
| 203 | 
            +
                  );
         | 
| 204 | 
            +
                  transport.onmessage = onMessageSpy;
         | 
| 205 | 
            +
                  await transport.start();
         | 
| 206 | 
            +
                });
         | 
| 207 | 
            +
             | 
| 208 | 
            +
                it('should handle incoming messages correctly', async () => {
         | 
| 209 | 
            +
                  const message: JSONRPCMessage = {
         | 
| 210 | 
            +
                    jsonrpc: '2.0',
         | 
| 211 | 
            +
                    id: '1',
         | 
| 212 | 
            +
                    method: 'test',
         | 
| 213 | 
            +
                    params: {},
         | 
| 214 | 
            +
                  };
         | 
| 215 | 
            +
             | 
| 216 | 
            +
                  mockStdout.emit('data', Buffer.from(JSON.stringify(message) + '\n'));
         | 
| 217 | 
            +
                  expect(onMessageSpy).toHaveBeenCalledWith(message);
         | 
| 218 | 
            +
                });
         | 
| 219 | 
            +
             | 
| 220 | 
            +
                it('should handle partial messages correctly', async () => {
         | 
| 221 | 
            +
                  const message = {
         | 
| 222 | 
            +
                    jsonrpc: '2.0',
         | 
| 223 | 
            +
                    id: '1',
         | 
| 224 | 
            +
                    method: 'test',
         | 
| 225 | 
            +
                    params: {},
         | 
| 226 | 
            +
                  };
         | 
| 227 | 
            +
             | 
| 228 | 
            +
                  const messageStr = JSON.stringify(message);
         | 
| 229 | 
            +
                  mockStdout.emit('data', Buffer.from(messageStr.slice(0, 10)));
         | 
| 230 | 
            +
                  mockStdout.emit('data', Buffer.from(messageStr.slice(10) + '\n'));
         | 
| 231 | 
            +
                  expect(onMessageSpy).toHaveBeenCalledWith(message);
         | 
| 232 | 
            +
                });
         | 
| 233 | 
            +
              });
         | 
| 234 | 
            +
             | 
| 235 | 
            +
              describe('close', () => {
         | 
| 236 | 
            +
                const onCloseSpy = vi.fn();
         | 
| 237 | 
            +
             | 
| 238 | 
            +
                beforeEach(async () => {
         | 
| 239 | 
            +
                  mockChildProcess.on.mockImplementation(
         | 
| 240 | 
            +
                    (event: string, callback: (code?: number) => void) => {
         | 
| 241 | 
            +
                      if (event === 'spawn') {
         | 
| 242 | 
            +
                        callback();
         | 
| 243 | 
            +
                      } else if (event === 'close') {
         | 
| 244 | 
            +
                        callback(0);
         | 
| 245 | 
            +
                      }
         | 
| 246 | 
            +
                    },
         | 
| 247 | 
            +
                  );
         | 
| 248 | 
            +
                  transport.onclose = onCloseSpy;
         | 
| 249 | 
            +
                  await transport.start();
         | 
| 250 | 
            +
                });
         | 
| 251 | 
            +
             | 
| 252 | 
            +
                it('should close the transport successfully', async () => {
         | 
| 253 | 
            +
                  await transport.close();
         | 
| 254 | 
            +
             | 
| 255 | 
            +
                  expect(mockChildProcess.on).toHaveBeenCalledWith(
         | 
| 256 | 
            +
                    'close',
         | 
| 257 | 
            +
                    expect.any(Function),
         | 
| 258 | 
            +
                  );
         | 
| 259 | 
            +
                  expect(onCloseSpy).toHaveBeenCalled();
         | 
| 260 | 
            +
                });
         | 
| 261 | 
            +
              });
         | 
| 262 | 
            +
            });
         |