| 242 | } |
| 243 | |
| 244 | export const invokeMcpTool = ( |
| 245 | input: InvokeMcpToolInput, |
| 246 | ): Effect.Effect< |
| 247 | unknown, |
| 248 | McpConnectionError | McpInvocationError | McpOAuthReauthorizationRequired |
| 249 | > => |
| 250 | Effect.gen(function* () { |
| 251 | const args = argsRecord(input.args); |
| 252 | const use = (connection: McpConnection) => |
| 253 | useConnection(connection, input.toolName, args, input.elicit, input.onToolListChanged); |
| 254 | |
| 255 | if (input.connectionPool && input.connectionPoolKey) { |
| 256 | return yield* input.connectionPool.withConnection( |
| 257 | input.connectionPoolKey, |
| 258 | input.connector.pipe( |
| 259 | Effect.withSpan("plugin.mcp.connection.acquire", { |
| 260 | attributes: { "plugin.mcp.transport": input.transport }, |
| 261 | }), |
| 262 | ), |
| 263 | use, |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | const connection = yield* Effect.acquireRelease( |
| 268 | input.connector.pipe( |
| 269 | Effect.withSpan("plugin.mcp.connection.acquire", { |
| 270 | attributes: { "plugin.mcp.transport": input.transport }, |
| 271 | }), |
| 272 | ), |
| 273 | (conn) => |
| 274 | Effect.ignore( |
| 275 | Effect.tryPromise({ |
| 276 | try: () => conn.close(), |
| 277 | catch: () => |
| 278 | new McpConnectionError({ |
| 279 | transport: input.transport, |
| 280 | message: "Failed to close MCP connection", |
| 281 | }), |
| 282 | }), |
| 283 | ), |
| 284 | ); |
| 285 | |
| 286 | return yield* use(connection); |
| 287 | }).pipe( |
| 288 | Effect.scoped, |
| 289 | Effect.withSpan("plugin.mcp.invoke", { |
| 290 | attributes: { |
| 291 | "mcp.tool.name": input.toolName, |
| 292 | "plugin.mcp.tool_id": input.toolId, |
| 293 | "plugin.mcp.transport": input.transport, |
| 294 | }, |
| 295 | }), |
| 296 | ); |