mcp-tools.ts 1.3 KB

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