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