mcp-tools.ts42 lines · main
| 1 | import type { ToolSet } from 'ai' |
| 2 | |
| 3 | import { createBrivenMCPClient } from '../briven-mcp' |
| 4 | import { filterToolsByOptInLevel, toolSetValidationSchema } from '../tool-filter' |
| 5 | import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi' |
| 6 | |
| 7 | const UI_EXECUTED_TOOLS = ['execute_sql', 'deploy_edge_function'] |
| 8 | |
| 9 | export const getMcpTools = async ({ |
| 10 | accessToken, |
| 11 | projectRef, |
| 12 | aiOptInLevel, |
| 13 | }: { |
| 14 | accessToken: string |
| 15 | projectRef: string |
| 16 | aiOptInLevel: AiOptInLevel |
| 17 | }) => { |
| 18 | // If platform, fetch MCP client and tools which replace old local tools |
| 19 | const mcpClient = await createBrivenMCPClient({ |
| 20 | accessToken, |
| 21 | projectId: projectRef, |
| 22 | }) |
| 23 | |
| 24 | const availableMcpTools = (await mcpClient.tools()) as ToolSet |
| 25 | // Filter tools based on the (potentially modified) AI opt-in level |
| 26 | const allowedMcpTools = filterToolsByOptInLevel(availableMcpTools, aiOptInLevel) |
| 27 | |
| 28 | // Remove UI-executed tools handled locally |
| 29 | const filteredMcpTools: ToolSet = { ...allowedMcpTools } |
| 30 | UI_EXECUTED_TOOLS.forEach((toolName) => { |
| 31 | delete filteredMcpTools[toolName] |
| 32 | }) |
| 33 | |
| 34 | // Validate that only known tools are provided |
| 35 | const validation = toolSetValidationSchema.safeParse(filteredMcpTools) |
| 36 | if (!validation.success) { |
| 37 | console.error('MCP tools validation error:', validation.error) |
| 38 | throw new Error('Internal error: MCP tools validation failed') |
| 39 | } |
| 40 | |
| 41 | return validation.data |
| 42 | } |