|
1 | 1 | import { db } from '@sim/db' |
2 | | -import { workflowMcpServer } from '@sim/db/schema' |
3 | | -import { eq, sql } from 'drizzle-orm' |
| 2 | +import { workflowMcpServer, workflowMcpTool } from '@sim/db/schema' |
| 3 | +import { eq, inArray, sql } from 'drizzle-orm' |
4 | 4 | import type { NextRequest } from 'next/server' |
5 | 5 | import { createLogger } from '@/lib/logs/console/logger' |
6 | 6 | import { getParsedBody, withMcpAuth } from '@/lib/mcp/middleware' |
@@ -38,10 +38,38 @@ export const GET = withMcpAuth('read')( |
38 | 38 | .from(workflowMcpServer) |
39 | 39 | .where(eq(workflowMcpServer.workspaceId, workspaceId)) |
40 | 40 |
|
| 41 | + // Fetch all tools for these servers |
| 42 | + const serverIds = servers.map((s) => s.id) |
| 43 | + const tools = |
| 44 | + serverIds.length > 0 |
| 45 | + ? await db |
| 46 | + .select({ |
| 47 | + serverId: workflowMcpTool.serverId, |
| 48 | + toolName: workflowMcpTool.toolName, |
| 49 | + }) |
| 50 | + .from(workflowMcpTool) |
| 51 | + .where(inArray(workflowMcpTool.serverId, serverIds)) |
| 52 | + : [] |
| 53 | + |
| 54 | + // Group tool names by server |
| 55 | + const toolNamesByServer: Record<string, string[]> = {} |
| 56 | + for (const tool of tools) { |
| 57 | + if (!toolNamesByServer[tool.serverId]) { |
| 58 | + toolNamesByServer[tool.serverId] = [] |
| 59 | + } |
| 60 | + toolNamesByServer[tool.serverId].push(tool.toolName) |
| 61 | + } |
| 62 | + |
| 63 | + // Attach tool names to servers |
| 64 | + const serversWithToolNames = servers.map((server) => ({ |
| 65 | + ...server, |
| 66 | + toolNames: toolNamesByServer[server.id] || [], |
| 67 | + })) |
| 68 | + |
41 | 69 | logger.info( |
42 | 70 | `[${requestId}] Listed ${servers.length} workflow MCP servers for workspace ${workspaceId}` |
43 | 71 | ) |
44 | | - return createMcpSuccessResponse({ servers }) |
| 72 | + return createMcpSuccessResponse({ servers: serversWithToolNames }) |
45 | 73 | } catch (error) { |
46 | 74 | logger.error(`[${requestId}] Error listing workflow MCP servers:`, error) |
47 | 75 | return createMcpErrorResponse( |
|
0 commit comments