|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { workflow, workflowBlocks } from '@sim/db/schema' |
| 3 | +import { eq } from 'drizzle-orm' |
| 4 | +import type { NextRequest } from 'next/server' |
| 5 | +import { createLogger } from '@/lib/logs/console/logger' |
| 6 | +import { withMcpAuth } from '@/lib/mcp/middleware' |
| 7 | +import { createMcpErrorResponse, createMcpSuccessResponse } from '@/lib/mcp/utils' |
| 8 | + |
| 9 | +const logger = createLogger('McpStoredToolsAPI') |
| 10 | + |
| 11 | +export const dynamic = 'force-dynamic' |
| 12 | + |
| 13 | +interface StoredMcpTool { |
| 14 | + workflowId: string |
| 15 | + workflowName: string |
| 16 | + serverId: string |
| 17 | + serverUrl?: string |
| 18 | + toolName: string |
| 19 | + schema?: Record<string, unknown> |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * GET - Get all stored MCP tools from workflows in the workspace |
| 24 | + * |
| 25 | + * Scans all workflows in the workspace and extracts MCP tools that have been |
| 26 | + * added to agent blocks. Returns the stored state of each tool for comparison |
| 27 | + * against current server state. |
| 28 | + */ |
| 29 | +export const GET = withMcpAuth('read')( |
| 30 | + async (request: NextRequest, { userId, workspaceId, requestId }) => { |
| 31 | + try { |
| 32 | + logger.info(`[${requestId}] Fetching stored MCP tools for workspace ${workspaceId}`) |
| 33 | + |
| 34 | + // Get all workflows in workspace |
| 35 | + const workflows = await db |
| 36 | + .select({ |
| 37 | + id: workflow.id, |
| 38 | + name: workflow.name, |
| 39 | + }) |
| 40 | + .from(workflow) |
| 41 | + .where(eq(workflow.workspaceId, workspaceId)) |
| 42 | + |
| 43 | + const workflowMap = new Map(workflows.map((w) => [w.id, w.name])) |
| 44 | + const workflowIds = workflows.map((w) => w.id) |
| 45 | + |
| 46 | + if (workflowIds.length === 0) { |
| 47 | + return createMcpSuccessResponse({ tools: [] }) |
| 48 | + } |
| 49 | + |
| 50 | + // Get all agent blocks from these workflows |
| 51 | + const agentBlocks = await db |
| 52 | + .select({ |
| 53 | + workflowId: workflowBlocks.workflowId, |
| 54 | + subBlocks: workflowBlocks.subBlocks, |
| 55 | + }) |
| 56 | + .from(workflowBlocks) |
| 57 | + .where(eq(workflowBlocks.type, 'agent')) |
| 58 | + |
| 59 | + const storedTools: StoredMcpTool[] = [] |
| 60 | + |
| 61 | + for (const block of agentBlocks) { |
| 62 | + if (!workflowMap.has(block.workflowId)) continue |
| 63 | + |
| 64 | + const subBlocks = block.subBlocks as Record<string, unknown> | null |
| 65 | + if (!subBlocks) continue |
| 66 | + |
| 67 | + const toolsSubBlock = subBlocks.tools as Record<string, unknown> | undefined |
| 68 | + const toolsValue = toolsSubBlock?.value |
| 69 | + |
| 70 | + if (!toolsValue || !Array.isArray(toolsValue)) continue |
| 71 | + |
| 72 | + for (const tool of toolsValue) { |
| 73 | + if (tool.type !== 'mcp') continue |
| 74 | + |
| 75 | + const params = tool.params as Record<string, unknown> | undefined |
| 76 | + if (!params?.serverId || !params?.toolName) continue |
| 77 | + |
| 78 | + storedTools.push({ |
| 79 | + workflowId: block.workflowId, |
| 80 | + workflowName: workflowMap.get(block.workflowId) || 'Untitled', |
| 81 | + serverId: params.serverId as string, |
| 82 | + serverUrl: params.serverUrl as string | undefined, |
| 83 | + toolName: params.toolName as string, |
| 84 | + schema: tool.schema as Record<string, unknown> | undefined, |
| 85 | + }) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + logger.info( |
| 90 | + `[${requestId}] Found ${storedTools.length} stored MCP tools across ${workflows.length} workflows` |
| 91 | + ) |
| 92 | + |
| 93 | + return createMcpSuccessResponse({ tools: storedTools }) |
| 94 | + } catch (error) { |
| 95 | + logger.error(`[${requestId}] Error fetching stored MCP tools:`, error) |
| 96 | + return createMcpErrorResponse( |
| 97 | + error instanceof Error ? error : new Error('Failed to fetch stored MCP tools'), |
| 98 | + 'Failed to fetch stored MCP tools', |
| 99 | + 500 |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | +) |
0 commit comments