| 270 | // --------------------------------------------------------------------------- |
| 271 | |
| 272 | export const createMcpConnector = (input: ConnectorInput): McpConnector => { |
| 273 | if (input.transport === "stdio") { |
| 274 | const command = input.command.trim(); |
| 275 | if (!command) { |
| 276 | return Effect.fail( |
| 277 | new McpConnectionError({ |
| 278 | transport: "stdio", |
| 279 | message: "MCP stdio transport requires a command", |
| 280 | }), |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | return Effect.gen(function* () { |
| 285 | // Dynamic import so the underlying module (which evaluates |
| 286 | // `node:child_process`) is only loaded when stdio is actually used. |
| 287 | const { createStdioTransport } = yield* Effect.tryPromise({ |
| 288 | try: () => import("./stdio-connector"), |
| 289 | catch: () => |
| 290 | new McpConnectionError({ |
| 291 | transport: "stdio", |
| 292 | message: "Failed to load stdio transport module", |
| 293 | }), |
| 294 | }); |
| 295 | |
| 296 | return yield* connectClient({ |
| 297 | transport: "stdio", |
| 298 | createTransport: () => |
| 299 | createStdioTransport({ |
| 300 | command, |
| 301 | args: input.args, |
| 302 | env: input.env, |
| 303 | cwd: input.cwd?.trim().length ? input.cwd.trim() : undefined, |
| 304 | }), |
| 305 | }); |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | // Remote transport |
| 310 | const headers = input.headers ?? {}; |
| 311 | const remoteTransport = input.remoteTransport ?? "auto"; |
| 312 | const requestInit = Object.keys(headers).length > 0 ? { headers } : undefined; |
| 313 | const fetch = input.httpClientLayer ? fetchFromHttpClientLayer(input.httpClientLayer) : undefined; |
| 314 | |
| 315 | const endpoint = buildEndpointUrl(input.endpoint, input.queryParams ?? {}); |
| 316 | |
| 317 | const connectStreamableHttp = connectClient({ |
| 318 | transport: "streamable-http", |
| 319 | createTransport: () => |
| 320 | new StreamableHTTPClientTransport(endpoint, { |
| 321 | requestInit, |
| 322 | authProvider: input.authProvider, |
| 323 | fetch, |
| 324 | }), |
| 325 | }); |
| 326 | |
| 327 | const connectSse = connectClient({ |
| 328 | transport: "sse", |
| 329 | createTransport: () => |