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