-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathConnectMCP.tsx
More file actions
392 lines (363 loc) 路 13 KB
/
Copy pathConnectMCP.tsx
File metadata and controls
392 lines (363 loc) 路 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { Check, HelpCircle, Loader2, Plus, Server, Trash2 } from 'lucide-react'
import { type FC, useState } from 'react'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
import {
CUSTOM_MCP_ADDED_EVENT,
MANAGED_MCP_ADDED_EVENT,
} from '@/lib/constants/analyticsEvents'
import { connectAppsHelpUrl } from '@/lib/constants/productUrls'
import { useMcpServers } from '@/lib/mcp/mcpServerStorage'
import { useSyncRemoteIntegrations } from '@/lib/mcp/useSyncRemoteIntegrations'
import { track } from '@/lib/metrics/track'
import { sentry } from '@/lib/sentry/sentry'
import { AddCustomMCPDialog } from './AddCustomMCPDialog'
import { AddManagedMCPDialog } from './AddManagedMCPDialog'
import { ApiKeyDialog } from './ApiKeyDialog'
import { AvailableManagedServers } from './AvailableManagedServers'
import { McpServerIcon } from './McpServerIcon'
import { useAddManagedServer } from './useAddManagedServer'
import { useGetMCPServersList } from './useGetMCPServersList'
import { useGetUserMCPIntegrations } from './useGetUserMCPIntegrations'
import { useRemoveManagedServer } from './useRemoveManagedServer'
import { useSubmitApiKey } from './useSubmitApiKey'
const failedToAddMcp = (serverName: string, e: unknown) => {
toast.error(`Failed to add app: ${serverName}`)
sentry.captureException(e)
}
const failedToRemoveMcp = (serverName: string, e: unknown) => {
toast.error(`Failed to remove app: ${serverName}`)
sentry.captureException(e)
}
/**
* @public
*/
export const ConnectMCP: FC = () => {
const { servers: createdServers, addServer, removeServer } = useMcpServers()
const [addingManagedMcp, setAddingManagedMcp] = useState(false)
const [addingCustomMcp, setAddingCustomMcp] = useState(false)
const [deletingServerId, setDeletingServerId] = useState<string | null>(null)
const [apiKeyServer, setApiKeyServer] = useState<{
name: string
description: string
apiKeyUrl: string
} | null>(null)
const { trigger: addManagedServerMutation } = useAddManagedServer()
const { trigger: removeManagedServerMutation } = useRemoveManagedServer()
const { trigger: submitApiKeyMutation, isMutating: isSubmittingApiKey } =
useSubmitApiKey()
const { data: serversList } = useGetMCPServersList()
const {
data: userMCPIntegrations,
isLoading: isUserMCPIntegrationsLoading,
mutate: mutateUserIntegrations,
} = useGetUserMCPIntegrations()
useSyncRemoteIntegrations()
const openAuthUrlForMCP = async (mcpName: string) => {
try {
const response = await addManagedServerMutation({
serverName: mcpName,
})
if (response.apiKeyUrl) {
setApiKeyServer({
name: mcpName,
description: '',
apiKeyUrl: response.apiKeyUrl,
})
return
}
if (!response.oauthUrl) {
failedToAddMcp(mcpName, 'No auth URL returned')
return
}
window.open(response.oauthUrl, '_blank')?.focus()
} catch (e) {
failedToAddMcp(mcpName, e)
}
}
const addManagedServer = async ({
name,
description,
}: {
name: string
description: string
}) => {
try {
const response = await addManagedServerMutation({
serverName: name,
})
if (!response.apiKeyUrl && !response.oauthUrl) {
failedToAddMcp(name, 'No auth URL returned')
return
}
addServer({
id: Date.now().toString(),
displayName: name,
type: 'managed',
managedServerName: name,
managedServerDescription: description,
})
track(MANAGED_MCP_ADDED_EVENT, { server_name: name })
if (response.apiKeyUrl) {
setApiKeyServer({ name, description, apiKeyUrl: response.apiKeyUrl })
return
}
window.open(response.oauthUrl, '_blank')?.focus()
} catch (e) {
failedToAddMcp(name, e)
}
}
const handleSubmitApiKey = async (apiKey: string) => {
if (!apiKeyServer) return
try {
await submitApiKeyMutation({
serverName: apiKeyServer.name,
apiKey,
apiKeyUrl: apiKeyServer.apiKeyUrl,
})
toast.success(`${apiKeyServer.name} connected successfully`)
setApiKeyServer(null)
mutateUserIntegrations()
} catch (e) {
toast.error(
`Failed to connect ${apiKeyServer.name}: ${e instanceof Error ? e.message : 'Unknown error'}`,
)
sentry.captureException(e)
}
}
const deleteManagedServer = async ({
id,
name,
}: {
id: string
name: string
}) => {
setDeletingServerId(id)
try {
const response = await removeManagedServerMutation({
serverName: name,
})
if (response.success) {
removeServer(id)
} else {
failedToRemoveMcp(name, 'Success not returned from server')
}
} catch (e) {
failedToRemoveMcp(name, e)
} finally {
setDeletingServerId(null)
}
}
const addCustomServer = (config: {
name: string
url: string
description: string
}) => {
addServer({
id: Date.now().toString(),
displayName: config.name,
type: 'custom',
config: {
url: config.url,
description: config.description,
},
})
track(CUSTOM_MCP_ADDED_EVENT)
}
const availableServers = serversList?.servers.filter((eachServer) => {
const serverName = eachServer.name
if (
createdServers.find((server) => server.managedServerName === serverName)
) {
return false
}
return true
})
const unauthenticatedServers: { name: string; description: string }[] = []
if (!isUserMCPIntegrationsLoading) {
for (const server of createdServers) {
if (server.type !== 'managed' || !server.managedServerName) continue
const integration = userMCPIntegrations?.integrations?.find(
(i) => i.name === server.managedServerName,
)
if (!integration?.is_authenticated) {
unauthenticatedServers.push({
name: server.managedServerName,
description: server.managedServerDescription ?? '',
})
}
}
}
return (
<div className="fade-in slide-in-from-bottom-5 animate-in space-y-6 duration-500">
{/* Header */}
<div className="rounded-xl border border-border bg-card p-6 shadow-sm transition-all hover:shadow-md">
<div className="flex items-start gap-4">
<div className="flex h-12 w-12 shrink-0 items-center justify-center rounded-xl bg-[var(--accent-orange)]/10">
<Server className="h-6 w-6 text-[var(--accent-orange)]" />
</div>
<div className="flex-1">
<div className="mb-1 flex items-center gap-2">
<h2 className="font-semibold text-xl">Connected Apps</h2>
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<a
href={connectAppsHelpUrl}
target="_blank"
rel="noopener noreferrer"
className="rounded-full p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
<HelpCircle className="h-4 w-4" />
</a>
</TooltipTrigger>
<TooltipContent>Learn more about Connect Apps</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
<p className="mb-6 text-muted-foreground text-sm">
Connect BrowserOS assistant to apps to send email, schedule
calendar events, write docs, and more
</p>
<div className="flex flex-wrap gap-3">
<Button
variant="outline"
onClick={() => setAddingManagedMcp(true)}
className="border-[var(--accent-orange)] bg-[var(--accent-orange)]/10 text-[var(--accent-orange)] hover:bg-[var(--accent-orange)]/20"
>
<Plus className="h-4 w-4" />
<span>Add built-in app</span>
</Button>
<Button
variant="outline"
onClick={() => setAddingCustomMcp(true)}
>
<Plus className="h-4 w-4" />
<span>Add custom app</span>
</Button>
</div>
</div>
</div>
</div>
{/* Created Servers */}
{createdServers.length > 0 && (
<div className="rounded-xl border border-border bg-card p-6 shadow-sm transition-all hover:shadow-md">
<h3 className="mb-4 font-semibold text-lg">Your Connected Apps</h3>
<div className="space-y-3">
{createdServers.map((server) => (
<div
key={server.id}
className="flex items-center gap-4 rounded-lg border border-border bg-background p-4 transition-all hover:border-[var(--accent-orange)]/50 hover:shadow-sm"
>
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-[var(--accent-orange)]/10">
<McpServerIcon
serverName={server.managedServerName ?? ''}
size={20}
className="text-[var(--accent-orange)]"
/>
</div>
<div className="flex-1">
<div className="mb-1 flex items-center gap-2">
<span className="font-semibold">{server.displayName}</span>
<span
className={`rounded px-2 py-0.5 font-medium text-xs ${
server.type === 'managed'
? 'bg-[var(--accent-orange)]/10 text-[var(--accent-orange)]'
: 'bg-muted text-muted-foreground'
}`}
>
{server.type === 'managed' ? 'Built-in' : 'Custom'}
</span>
</div>
<p className="text-muted-foreground text-sm">
{server.managedServerDescription ||
server.config?.description ||
server.config?.url}
</p>
</div>
{server.type === 'managed' &&
(isUserMCPIntegrationsLoading ? (
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
) : userMCPIntegrations?.integrations?.find(
(i) => i.name === server.managedServerName,
)?.is_authenticated ? (
<span className="flex items-center gap-1 rounded-full bg-green-500/10 px-2 py-1 font-medium text-green-600 text-xs">
<Check className="h-3 w-3" />
Authenticated
</span>
) : (
<Button
variant="outline"
size="sm"
onClick={() =>
server.managedServerName &&
openAuthUrlForMCP(server.managedServerName)
}
>
Authenticate
</Button>
))}
<Button
variant="ghost"
size="icon"
disabled={deletingServerId === server.id}
onClick={() => {
if (server.type === 'managed' && server.managedServerName) {
deleteManagedServer({
id: server.id,
name: server.managedServerName,
})
} else {
removeServer(server.id)
}
}}
className="text-muted-foreground hover:bg-destructive/10 hover:text-destructive"
title="Remove server"
>
{deletingServerId === server.id ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Trash2 className="h-4 w-4" />
)}
</Button>
</div>
))}
</div>
</div>
)}
<AvailableManagedServers
availableServers={availableServers}
onAddServer={addManagedServer}
isLoading={false}
/>
<AddManagedMCPDialog
open={addingManagedMcp}
onOpenChange={setAddingManagedMcp}
serversList={availableServers}
unauthenticatedServers={unauthenticatedServers}
onAddServer={addManagedServer}
onAuthenticate={openAuthUrlForMCP}
/>
<AddCustomMCPDialog
open={addingCustomMcp}
onOpenChange={setAddingCustomMcp}
onAddServer={addCustomServer}
/>
<ApiKeyDialog
open={!!apiKeyServer}
onOpenChange={(open) => {
if (!open) setApiKeyServer(null)
}}
serverName={apiKeyServer?.name ?? ''}
onSubmit={handleSubmitApiKey}
isSubmitting={isSubmittingApiKey}
/>
</div>
)
}