| 386 | } |
| 387 | |
| 388 | export async function tools() { |
| 389 | const result: Record<string, Tool> = {} |
| 390 | const s = await state() |
| 391 | const clientsSnapshot = await clients() |
| 392 | |
| 393 | for (const [clientName, client] of Object.entries(clientsSnapshot)) { |
| 394 | // Only include tools from connected MCPs (skip disabled ones) |
| 395 | if (s.status[clientName]?.status !== "connected") { |
| 396 | continue |
| 397 | } |
| 398 | |
| 399 | const tools = await client.tools().catch((e) => { |
| 400 | log.error("failed to get tools", { clientName, error: e.message }) |
| 401 | const failedStatus = { |
| 402 | status: "failed" as const, |
| 403 | error: e instanceof Error ? e.message : String(e), |
| 404 | } |
| 405 | s.status[clientName] = failedStatus |
| 406 | delete s.clients[clientName] |
| 407 | }) |
| 408 | if (!tools) { |
| 409 | continue |
| 410 | } |
| 411 | for (const [toolName, tool] of Object.entries(tools)) { |
| 412 | const sanitizedClientName = clientName.replace(/[^a-zA-Z0-9_-]/g, "_") |
| 413 | const sanitizedToolName = toolName.replace(/[^a-zA-Z0-9_-]/g, "_") |
| 414 | result[sanitizedClientName + "_" + sanitizedToolName] = tool |
| 415 | } |
| 416 | } |
| 417 | return result |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Start OAuth authentication flow for an MCP server. |