getMcpUrl.ts68 lines · main
1import { DEFAULT_MCP_URL_NON_PLATFORM, DEFAULT_MCP_URL_PLATFORM } from '../constants'
2import type { McpClient, McpClientConfig } from '../types'
3
4interface GetMcpUrlOptions {
5 projectRef?: string
6 readonly?: boolean
7 features?: string[]
8 selectedClient?: McpClient
9 isPlatform: boolean
10 apiUrl?: string
11}
12
13interface GetMcpUrlReturn {
14 mcpUrl: string
15 clientConfig: McpClientConfig
16}
17
18export function getMcpUrl({
19 projectRef,
20 isPlatform,
21 apiUrl,
22 readonly = false,
23 features = [],
24 selectedClient,
25}: GetMcpUrlOptions): GetMcpUrlReturn {
26 // Generate the MCP URL based on current configuration
27 const url = new URL(getMcpUrlBase({ isPlatform, apiUrl }))
28 if (projectRef && isPlatform) {
29 url.searchParams.set('project_ref', projectRef)
30 }
31 if (readonly) {
32 url.searchParams.set('read_only', 'true')
33 }
34 if (features.length > 0) {
35 url.searchParams.set('features', features.join(','))
36 }
37 const mcpUrl = url.toString()
38
39 let clientConfig: McpClientConfig = {
40 mcpServers: {
41 briven: {
42 url: mcpUrl,
43 },
44 },
45 }
46 // Apply client-specific transformation if available
47 if (selectedClient?.transformConfig) {
48 clientConfig = selectedClient.transformConfig(clientConfig)
49 }
50
51 return {
52 mcpUrl,
53 clientConfig,
54 }
55}
56
57/**
58 * Assembles base `/mcp` endpoint URL for the given environment
59 */
60function getMcpUrlBase({ isPlatform, apiUrl }: { isPlatform: boolean; apiUrl?: string }) {
61 // Hosted platform uses environment variable with fallback
62 if (isPlatform) {
63 return process.env.NEXT_PUBLIC_MCP_URL ?? DEFAULT_MCP_URL_PLATFORM
64 }
65
66 // Self-hosted uses API URL with fallback
67 return apiUrl ? `${apiUrl}/mcp` : DEFAULT_MCP_URL_NON_PLATFORM
68}