Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor MCP copy handler and improve type consistency (#40650)

Pedro Rodriguesf29fc05eb590
diff --git a/apps/docs/features/ui/McpConfigPanel.tsx b/apps/docs/features/ui/McpConfigPanel.tsx
index fa954c0c8a0..d847dfa48e2 100644
--- a/apps/docs/features/ui/McpConfigPanel.tsx
+++ b/apps/docs/features/ui/McpConfigPanel.tsx
@@ -19,7 +19,11 @@ import {
ScrollArea,
} from 'ui'
import { Admonition } from 'ui-patterns'
-import { McpConfigPanel as McpConfigPanelBase, type McpClient } from 'ui-patterns/McpUrlBuilder'
+import {
+ createMcpCopyHandler,
+ McpConfigPanel as McpConfigPanelBase,
+ type McpClient,
+} from 'ui-patterns/McpUrlBuilder'
import ShimmeringLoader from 'ui-patterns/ShimmeringLoader'
import { useDebounce } from '~/hooks/useDebounce'
import { useIntersectionObserver } from '~/hooks/useIntersectionObserver'
@@ -268,34 +272,22 @@ export function McpConfigPanel() {
const isPlatform = selectedPlatform === 'hosted'
const project = isPlatform ? selectedProject : null
- const handleCopy = (type?: 'url' | 'json' | 'command') => {
- let connectionType: string
- switch (type) {
- case 'command':
- connectionType = 'Command Line'
- break
- case 'json':
- connectionType = 'JSON'
- break
- case 'url':
- default:
- connectionType = 'MCP URL'
- break
- }
-
- sendTelemetryEvent({
- action: 'connection_string_copied',
- properties: {
- connectionTab: 'MCP',
- selectedItem: selectedClient?.label,
- connectionType,
+ const handleCopy = useMemo(
+ () =>
+ createMcpCopyHandler({
+ selectedClient,
source: 'docs',
- },
- groups: {
- ...(project?.ref && { project: project.ref }),
- } as any,
- })
- }
+ onTrack: (event) => {
+ sendTelemetryEvent({
+ action: event.action,
+ properties: event.properties,
+ groups: (event.groups || {}) as any,
+ })
+ },
+ projectRef: project?.ref,
+ }),
+ [selectedClient, sendTelemetryEvent, project?.ref]
+ )
const handleInstall = () => {
if (selectedClient?.label) {
diff --git a/apps/studio/components/interfaces/Connect/McpTabContent.tsx b/apps/studio/components/interfaces/Connect/McpTabContent.tsx
index 941e2e3e8c9..ea33579fbbe 100644
--- a/apps/studio/components/interfaces/Connect/McpTabContent.tsx
+++ b/apps/studio/components/interfaces/Connect/McpTabContent.tsx
@@ -4,8 +4,8 @@ import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import { BASE_PATH } from 'lib/constants'
import { useTrack } from 'lib/telemetry/track'
import { useTheme } from 'next-themes'
-import { useState } from 'react'
-import { McpConfigPanel, type McpClient } from 'ui-patterns/McpUrlBuilder'
+import { useMemo, useState } from 'react'
+import { createMcpCopyHandler, McpConfigPanel, type McpClient } from 'ui-patterns/McpUrlBuilder'
import type { projectKeys } from './Connect.types'
export const McpTabContent = ({ projectKeys }: { projectKeys: projectKeys }) => {
@@ -42,28 +42,16 @@ const McpTabContentInnerLoaded = ({
const track = useTrack()
const [selectedClient, setSelectedClient] = useState<McpClient | null>(null)
- const handleCopy = (type?: 'url' | 'json' | 'command') => {
- let connectionType: string
- switch (type) {
- case 'command':
- connectionType = 'Command Line'
- break
- case 'json':
- connectionType = 'JSON'
- break
- case 'url':
- default:
- connectionType = 'MCP URL'
- break
- }
-
- track('connection_string_copied', {
- connectionTab: 'MCP',
- selectedItem: selectedClient?.label,
- connectionType,
- source: 'studio',
- })
- }
+ const handleCopy = useMemo(
+ () =>
+ createMcpCopyHandler({
+ selectedClient,
+ source: 'studio',
+ onTrack: (event) => track(event.action, event.properties, event.groups),
+ projectRef,
+ }),
+ [selectedClient, track, projectRef]
+ )
const handleInstall = () => {
if (selectedClient?.label) {
diff --git a/packages/ui-patterns/src/McpUrlBuilder/McpConfigPanel.tsx b/packages/ui-patterns/src/McpUrlBuilder/McpConfigPanel.tsx
index 11ab2f94d13..6697da68b1c 100644
--- a/packages/ui-patterns/src/McpUrlBuilder/McpConfigPanel.tsx
+++ b/packages/ui-patterns/src/McpUrlBuilder/McpConfigPanel.tsx
@@ -7,7 +7,7 @@ import { ClientSelectDropdown } from './components/ClientSelectDropdown'
import { McpConfigurationDisplay } from './components/McpConfigurationDisplay'
import { McpConfigurationOptions } from './components/McpConfigurationOptions'
import { FEATURE_GROUPS_NON_PLATFORM, FEATURE_GROUPS_PLATFORM, MCP_CLIENTS } from './constants'
-import type { McpClient } from './types'
+import type { McpClient, McpOnCopyCallback } from './types'
import { getMcpUrl } from './utils/getMcpUrl'
import { InfoTooltip } from '../info-tooltip'
@@ -17,7 +17,7 @@ export interface McpConfigPanelProps {
projectRef?: string
initialSelectedClient?: McpClient
onClientSelect?: (client: McpClient) => void
- onCopyCallback?: (type?: 'url' | 'json' | 'command') => void
+ onCopyCallback: (type?: McpOnCopyCallback) => void
onInstallCallback?: () => void
theme?: 'light' | 'dark'
className?: string
diff --git a/packages/ui-patterns/src/McpUrlBuilder/components/McpConfigurationDisplay.tsx b/packages/ui-patterns/src/McpUrlBuilder/components/McpConfigurationDisplay.tsx
index 81a39fba1b4..e7ba4ef1ccd 100644
--- a/packages/ui-patterns/src/McpUrlBuilder/components/McpConfigurationDisplay.tsx
+++ b/packages/ui-patterns/src/McpUrlBuilder/components/McpConfigurationDisplay.tsx
@@ -4,7 +4,7 @@ import { ExternalLink } from 'lucide-react'
import Image from 'next/image'
import { Button, cn } from 'ui'
import { CodeBlock } from 'ui/src/components/CodeBlock'
-import type { McpClient, McpClientConfig } from '../types'
+import type { McpClient, McpClientConfig, McpOnCopyCallback } from '../types'
import { getMcpButtonData } from '../utils/getMcpButtonData'
interface McpConfigurationDisplayProps {
@@ -13,7 +13,7 @@ interface McpConfigurationDisplayProps {
className?: string
theme?: 'light' | 'dark'
basePath: string
- onCopyCallback?: (type?: 'url' | 'json' | 'command') => void
+ onCopyCallback: (type?: McpOnCopyCallback) => void
onInstallCallback?: () => void
}
@@ -79,7 +79,7 @@ export function McpConfigurationDisplay({
language="json"
className="max-h-64 overflow-y-auto"
focusable={false}
- onCopyCallback={() => onCopyCallback?.('json')}
+ onCopyCallback={() => onCopyCallback?.('config')}
/>
{selectedClient.alternateInstructions &&
diff --git a/packages/ui-patterns/src/McpUrlBuilder/index.ts b/packages/ui-patterns/src/McpUrlBuilder/index.ts
index a15b032d884..61699d25573 100644
--- a/packages/ui-patterns/src/McpUrlBuilder/index.ts
+++ b/packages/ui-patterns/src/McpUrlBuilder/index.ts
@@ -10,10 +10,12 @@ export {
MCP_CLIENTS,
} from './constants'
export { getMcpUrl } from './utils/getMcpUrl'
+export { createMcpCopyHandler, type McpCopyType } from './utils/createMcpCopyHandler'
export { McpConfigPanel, type McpConfigPanelProps } from './McpConfigPanel'
export type {
McpClient,
McpClientBaseConfig as McpClientConfig,
McpFeatureGroup,
+ McpOnCopyCallback,
McpUrlBuilderConfig,
} from './types'
diff --git a/packages/ui-patterns/src/McpUrlBuilder/types.ts b/packages/ui-patterns/src/McpUrlBuilder/types.ts
index 02f867a6002..3a17c3e26e8 100644
--- a/packages/ui-patterns/src/McpUrlBuilder/types.ts
+++ b/packages/ui-patterns/src/McpUrlBuilder/types.ts
@@ -4,6 +4,8 @@ export interface McpFeatureGroup {
description: string
}
+export type McpOnCopyCallback = 'url' | 'config' | 'command'
+
export interface McpClient {
key: string
label: string
@@ -15,11 +17,11 @@ export interface McpClient {
transformConfig?: (config: McpClientBaseConfig) => McpClientConfig
primaryInstructions?: (
config: McpClientConfig,
- onCopy?: (type?: 'url' | 'json' | 'command') => void
+ onCopy: (type?: McpOnCopyCallback) => void
) => React.ReactNode
alternateInstructions?: (
config: McpClientConfig,
- onCopy?: (type?: 'url' | 'json' | 'command') => void
+ onCopy: (type?: McpOnCopyCallback) => void
) => React.ReactNode
}
diff --git a/packages/ui-patterns/src/McpUrlBuilder/utils/createMcpCopyHandler.ts b/packages/ui-patterns/src/McpUrlBuilder/utils/createMcpCopyHandler.ts
new file mode 100644
index 00000000000..b3204deddbc
--- /dev/null
+++ b/packages/ui-patterns/src/McpUrlBuilder/utils/createMcpCopyHandler.ts
@@ -0,0 +1,76 @@
+import type { McpClient, McpOnCopyCallback } from '../types'
+
+export type McpCopyType = McpOnCopyCallback
+
+export interface McpCopyHandlerParams {
+ selectedClient: McpClient | null
+ source: 'studio' | 'docs'
+ onTrack: (event: {
+ action: 'connection_string_copied'
+ properties: {
+ connectionTab: 'MCP'
+ selectedItem?: string
+ connectionType: string
+ source: 'studio' | 'docs'
+ }
+ groups?: {
+ project?: string
+ organization?: string
+ [key: string]: string | undefined
+ }
+ }) => void
+ projectRef?: string
+}
+
+/**
+ * Creates a copy handler for MCP configuration copying events.
+ * Centralizes the telemetry logic for tracking MCP URL, config, and command copies.
+ *
+ * @param params - Configuration for the copy handler
+ * @returns A function that handles copy events and sends appropriate telemetry
+ *
+ * @example
+ * ```ts
+ * const handleCopy = createMcpCopyHandler({
+ * selectedClient,
+ * source: 'studio',
+ * onTrack: track,
+ * projectRef: project?.ref
+ * })
+ *
+ * // Usage in component
+ * handleCopy('config') // Tracks copying of config file
+ * handleCopy('command') // Tracks copying of command line
+ * handleCopy('url') // Tracks copying of MCP URL
+ * ```
+ */
+export function createMcpCopyHandler(params: McpCopyHandlerParams) {
+ const { selectedClient, source, onTrack, projectRef } = params
+
+ return (type?: McpCopyType) => {
+ let connectionType: string
+ switch (type) {
+ case 'command':
+ connectionType = 'Command Line'
+ break
+ case 'config':
+ connectionType = 'Config File'
+ break
+ case 'url':
+ default:
+ connectionType = 'MCP URL'
+ break
+ }
+
+ onTrack({
+ action: 'connection_string_copied',
+ properties: {
+ connectionTab: 'MCP',
+ selectedItem: selectedClient?.label,
+ connectionType,
+ source,
+ },
+ ...(projectRef && { groups: { project: projectRef } }),
+ })
+ }
+}