-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMcpDialog.tsx
More file actions
473 lines (442 loc) · 16.4 KB
/
Copy pathCustomMcpDialog.tsx
File metadata and controls
473 lines (442 loc) · 16.4 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
* Custom MCP Server Dialog
*
* Dialog for adding/editing custom MCP servers.
* Supports both command-based (npx/npm) and HTTP-based servers.
*/
import { useState, useEffect, useMemo } from 'react';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from './ui/dialog';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Label } from './ui/label';
import { RadioGroup, RadioGroupItem } from './ui/radio-group';
import { useTranslation } from 'react-i18next';
import type { CustomMcpServer } from '../../shared/types';
import { Terminal, Globe, X, GitBranch as Github, ExternalLink } from 'lucide-react';
interface CustomMcpDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
server: CustomMcpServer | null; // null = create new, non-null = edit
existingIds: string[]; // Existing server IDs for validation
onSave: (server: CustomMcpServer) => void;
}
export function CustomMcpDialog({
open,
onOpenChange,
server,
existingIds,
onSave
}: CustomMcpDialogProps) {
const { t } = useTranslation(['settings', 'common']);
const isEditing = server !== null;
const [formData, setFormData] = useState<CustomMcpServer>({
id: '',
name: '',
type: 'command',
command: 'npx',
args: [],
url: '',
headers: {},
description: '',
});
const [argsInput, setArgsInput] = useState('');
const [headerKey, setHeaderKey] = useState('');
const [headerValue, setHeaderValue] = useState('');
const [bearerToken, setBearerToken] = useState('');
const [showAdvancedHeaders, setShowAdvancedHeaders] = useState(false);
const [error, setError] = useState<string | null>(null);
// Known provider patterns for helpful hints
const urlHint = useMemo(() => {
const url = formData.url?.toLowerCase() || '';
if (url.includes('github')) {
return {
provider: 'GitHub',
icon: Github,
message: t('mcp.hints.github'),
link: 'https://github.com/settings/tokens',
linkText: t('mcp.hints.createGithubPat'),
};
}
if (url.includes('google') || url.includes('googleapis')) {
return {
provider: 'Google',
icon: Globe,
message: t('mcp.hints.google'),
link: 'https://console.cloud.google.com/apis/credentials',
linkText: t('mcp.hints.createGoogleToken'),
};
}
if (url.includes('anthropic')) {
return {
provider: 'Anthropic',
icon: Globe,
message: t('mcp.hints.anthropic'),
link: 'https://console.anthropic.com/settings/keys',
linkText: t('mcp.hints.createAnthropicKey'),
};
}
if (url.includes('openai')) {
return {
provider: 'OpenAI',
icon: Globe,
message: t('mcp.hints.openai'),
link: 'https://platform.openai.com/api-keys',
linkText: t('mcp.hints.createOpenaiKey'),
};
}
return null;
}, [formData.url, t]);
// Reset form when dialog opens/closes or server changes
useEffect(() => {
if (open && server) {
setFormData(server);
setArgsInput(server.args?.join(' ') || '');
// Extract bearer token from existing Authorization header
const authHeader = server.headers?.['Authorization'] || server.headers?.['authorization'] || '';
if (authHeader.toLowerCase().startsWith('bearer ')) {
setBearerToken(authHeader.substring(7));
} else {
setBearerToken('');
}
// Show advanced headers if there are non-Authorization headers
const hasOtherHeaders = Object.keys(server.headers || {}).some(
k => k.toLowerCase() !== 'authorization'
);
setShowAdvancedHeaders(hasOtherHeaders);
setError(null);
} else if (open) {
setFormData({
id: '',
name: '',
type: 'command',
command: 'npx',
args: [],
url: '',
headers: {},
description: '',
});
setArgsInput('');
setBearerToken('');
setShowAdvancedHeaders(false);
setError(null);
}
setHeaderKey('');
setHeaderValue('');
}, [open, server]);
// Generate ID from name
const generateId = (name: string): string => {
return name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
};
const handleSave = () => {
// Validate
if (!formData.name.trim()) {
setError(t('mcp.errorNameRequired'));
return;
}
const generatedId = isEditing ? formData.id : generateId(formData.name);
// Check for duplicate ID (only when creating new)
if (!isEditing && existingIds.includes(generatedId)) {
setError(t('mcp.errorIdExists'));
return;
}
if (formData.type === 'command' && !formData.command?.trim()) {
setError(t('mcp.errorCommandRequired'));
return;
}
if (formData.type === 'http' && !formData.url?.trim()) {
setError(t('mcp.errorUrlRequired'));
return;
}
// Build headers, merging bearer token if provided
const finalHeaders: Record<string, string> = {};
if (formData.type === 'http') {
// Start with existing headers (excluding old Authorization if we have a new bearer token)
if (formData.headers) {
for (const [key, value] of Object.entries(formData.headers)) {
if (bearerToken && key.toLowerCase() === 'authorization') {
continue; // Skip old auth header if we have a new bearer token
}
finalHeaders[key] = value;
}
}
// Add bearer token as Authorization header
if (bearerToken.trim()) {
finalHeaders['Authorization'] = `Bearer ${bearerToken.trim()}`;
}
}
const serverToSave: CustomMcpServer = {
id: generatedId,
name: formData.name.trim(),
type: formData.type,
description: formData.description?.trim() || undefined,
...(formData.type === 'command'
? {
command: formData.command,
args: argsInput.split(' ').filter(Boolean),
}
: {
url: formData.url,
headers: Object.keys(finalHeaders).length > 0 ? finalHeaders : undefined,
}),
};
onSave(serverToSave);
onOpenChange(false);
};
const addHeader = () => {
if (headerKey.trim() && headerValue.trim()) {
setFormData(prev => ({
...prev,
headers: { ...prev.headers, [headerKey.trim()]: headerValue.trim() },
}));
setHeaderKey('');
setHeaderValue('');
}
};
const removeHeader = (key: string) => {
setFormData(prev => {
const newHeaders = { ...prev.headers };
delete newHeaders[key];
return { ...prev, headers: newHeaders };
});
};
const isValid = formData.name.trim() && (
(formData.type === 'command' && formData.command?.trim()) ||
(formData.type === 'http' && formData.url?.trim())
);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>
{isEditing ? t('mcp.editCustomServer') : t('mcp.addCustomServer')}
</DialogTitle>
<DialogDescription>
{t('mcp.customServerDescription')}
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
{/* Error message */}
{error && (
<div className="text-sm text-destructive bg-destructive/10 px-3 py-2 rounded">
{error}
</div>
)}
{/* Server Type */}
<div className="space-y-2">
<Label>{t('mcp.serverType')}</Label>
<RadioGroup
value={formData.type}
onValueChange={(value: 'command' | 'http') =>
setFormData(prev => ({ ...prev, type: value }))
}
className="flex gap-4"
>
<div className="flex items-center gap-2">
<RadioGroupItem value="command" id="type-command" />
<Label htmlFor="type-command" className="flex items-center gap-1.5 cursor-pointer">
<Terminal className="h-3.5 w-3.5" />
{t('mcp.typeCommand')}
</Label>
</div>
<div className="flex items-center gap-2">
<RadioGroupItem value="http" id="type-http" />
<Label htmlFor="type-http" className="flex items-center gap-1.5 cursor-pointer">
<Globe className="h-3.5 w-3.5" />
{t('mcp.typeHttp')}
</Label>
</div>
</RadioGroup>
</div>
{/* Name */}
<div className="space-y-2">
<Label htmlFor="name">{t('mcp.serverName')}</Label>
<Input
id="name"
value={formData.name}
onChange={(e) => {
setFormData(prev => ({ ...prev, name: e.target.value }));
setError(null);
}}
placeholder={t('mcp.serverNamePlaceholder')}
/>
{!isEditing && formData.name && (
<p className="text-xs text-muted-foreground">
ID: {generateId(formData.name) || '...'}
</p>
)}
</div>
{/* Description (optional) */}
<div className="space-y-2">
<Label htmlFor="description">
{t('mcp.serverDescription')} <span className="text-muted-foreground">({t('common:optional')})</span>
</Label>
<Input
id="description"
value={formData.description || ''}
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
placeholder={t('mcp.serverDescriptionPlaceholder')}
/>
</div>
{/* Command-based fields */}
{formData.type === 'command' && (
<>
<div className="space-y-2">
<Label htmlFor="command">{t('mcp.command')}</Label>
<Input
id="command"
value={formData.command || ''}
onChange={(e) => {
setFormData(prev => ({ ...prev, command: e.target.value }));
setError(null);
}}
placeholder="npx"
/>
</div>
<div className="space-y-2">
<Label htmlFor="args">{t('mcp.args')}</Label>
<Input
id="args"
value={argsInput}
onChange={(e) => setArgsInput(e.target.value)}
placeholder="-y @myorg/my-mcp-server"
/>
<p className="text-xs text-muted-foreground">{t('mcp.argsHint')}</p>
</div>
</>
)}
{/* HTTP-based fields */}
{formData.type === 'http' && (
<>
<div className="space-y-2">
<Label htmlFor="url">{t('mcp.url')}</Label>
<Input
id="url"
value={formData.url || ''}
onChange={(e) => {
setFormData(prev => ({ ...prev, url: e.target.value }));
setError(null);
}}
placeholder="https://mcp.example.com/mcp"
/>
</div>
{/* URL-based hint for known providers */}
{urlHint && (
<div className="flex items-start gap-2 p-3 bg-muted/50 rounded-lg border border-border">
<urlHint.icon className="h-4 w-4 text-muted-foreground mt-0.5 flex-shrink-0" />
<div className="flex-1 min-w-0">
<p className="text-sm text-muted-foreground">{urlHint.message}</p>
<a
href={urlHint.link}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-sm text-primary hover:underline mt-1"
onClick={(e) => {
e.preventDefault();
window.electronAPI?.openExternal(urlHint.link);
}}
>
{urlHint.linkText}
<ExternalLink className="h-3 w-3" />
</a>
</div>
</div>
)}
{/* Authentication Token (simplified) */}
<div className="space-y-2">
<Label htmlFor="bearerToken">
{t('mcp.authToken')} <span className="text-muted-foreground">({t('common:optional')})</span>
</Label>
<Input
id="bearerToken"
value={bearerToken}
onChange={(e) => setBearerToken(e.target.value)}
placeholder={t('mcp.authTokenPlaceholder')}
type="password"
/>
<p className="text-xs text-muted-foreground">{t('mcp.authTokenHint')}</p>
</div>
{/* Advanced Headers (collapsible) */}
<div className="space-y-2">
<button
type="button"
onClick={() => setShowAdvancedHeaders(!showAdvancedHeaders)}
className="flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<span className={`transition-transform ${showAdvancedHeaders ? 'rotate-90' : ''}`}>▶</span>
{t('mcp.advancedHeaders')}
</button>
{showAdvancedHeaders && (
<div className="pl-4 space-y-2">
<div className="flex gap-2">
<Input
value={headerKey}
onChange={(e) => setHeaderKey(e.target.value)}
placeholder={t('mcp.headerName')}
className="flex-1"
/>
<Input
value={headerValue}
onChange={(e) => setHeaderValue(e.target.value)}
placeholder={t('mcp.headerValue')}
className="flex-1"
type="password"
/>
<Button
type="button"
variant="outline"
size="sm"
onClick={addHeader}
disabled={!headerKey.trim() || !headerValue.trim()}
>
{t('common:add')}
</Button>
</div>
{/* Show non-Authorization headers */}
{Object.entries(formData.headers || {}).filter(([key]) => key.toLowerCase() !== 'authorization').length > 0 && (
<div className="space-y-1 mt-2">
{Object.entries(formData.headers || {})
.filter(([key]) => key.toLowerCase() !== 'authorization')
.map(([key, value]) => (
<div key={key} className="flex items-center justify-between text-sm bg-muted px-2 py-1 rounded">
<span>
<span className="font-medium">{key}:</span>{' '}
<span className="text-muted-foreground">
{value.length > 20 ? `${value.substring(0, 20)}...` : value}
</span>
</span>
<button
type="button"
onClick={() => removeHeader(key)}
className="text-muted-foreground hover:text-destructive transition-colors"
>
<X className="h-3 w-3" />
</button>
</div>
))}
</div>
)}
</div>
)}
</div>
</>
)}
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}>
{t('common:cancel')}
</Button>
<Button onClick={handleSave} disabled={!isValid}>
{isEditing ? t('common:save') : t('mcp.addServer')}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}