| 455 | } |
| 456 | |
| 457 | export const invokeMcpTool = ( |
| 458 | input: InvokeMcpToolInput, |
| 459 | ): Effect.Effect< |
| 460 | unknown, |
| 461 | McpConnectionError | McpInvocationError | McpOAuthReauthorizationRequired |
| 462 | > => |
| 463 | Effect.gen(function* () { |
| 464 | const args = argsRecord(input.args); |
| 465 | const use = (connection: McpConnection) => |
| 466 | useConnection(connection, input.toolName, args, input.elicit, input.onToolListChanged); |
| 467 | |
| 468 | if (input.connectionPool && input.connectionPoolKey) { |
| 469 | return yield* input.connectionPool.withConnection( |
| 470 | input.connectionPoolKey, |
| 471 | input.connector.pipe( |
| 472 | Effect.withSpan("plugin.mcp.connection.acquire", { |
| 473 | attributes: { "plugin.mcp.transport": input.transport }, |
| 474 | }), |
| 475 | ), |
| 476 | use, |
| 477 | ); |
| 478 | } |
| 479 | |
| 480 | const connection = yield* Effect.acquireRelease( |
| 481 | input.connector.pipe( |
| 482 | Effect.withSpan("plugin.mcp.connection.acquire", { |
| 483 | attributes: { "plugin.mcp.transport": input.transport }, |
| 484 | }), |
| 485 | ), |
| 486 | (conn) => |
| 487 | Effect.ignore( |
| 488 | Effect.tryPromise({ |
| 489 | try: () => conn.close(), |
| 490 | catch: () => |
| 491 | new McpConnectionError({ |
| 492 | transport: input.transport, |
| 493 | message: "Failed to close MCP connection", |
| 494 | }), |
| 495 | }), |
| 496 | ), |
| 497 | ); |
| 498 | |
| 499 | return yield* use(connection); |
| 500 | }).pipe( |
| 501 | Effect.scoped, |
| 502 | Effect.withSpan("plugin.mcp.invoke", { |
| 503 | attributes: { |
| 504 | "mcp.tool.name": input.toolName, |
| 505 | "plugin.mcp.tool_id": input.toolId, |
| 506 | "plugin.mcp.transport": input.transport, |
| 507 | }, |
| 508 | }), |
| 509 | ); |