-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathMCPImportExport.tsx
More file actions
425 lines (397 loc) · 14.1 KB
/
MCPImportExport.tsx
File metadata and controls
425 lines (397 loc) · 14.1 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
import React, { useEffect, useState } from "react";
import { Download, Upload, FileText, Loader2, Info, Network, Settings2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { SelectComponent } from "@/components/ui/select";
import { api } from "@/lib/api";
interface MCPImportExportProps {
/**
* Callback when import is completed
*/
onImportCompleted: (imported: number, failed: number) => void;
/**
* Callback for error messages
*/
onError: (message: string) => void;
/**
* Callback for success/info messages
*/
onSuccess: (message: string) => void;
}
/**
* Component for importing and exporting MCP server configurations
*/
export const MCPImportExport: React.FC<MCPImportExportProps> = ({
onImportCompleted,
onError,
onSuccess,
}) => {
const [importingDesktop, setImportingDesktop] = useState(false);
const [importingJson, setImportingJson] = useState(false);
const [importScope, setImportScope] = useState("local");
const [mcpServeRunning, setMcpServeRunning] = useState(false);
const [mcpServeChecking, setMcpServeChecking] = useState(false);
const refreshMcpServeStatus = async () => {
try {
const statuses = await api.mcpGetServerStatus();
setMcpServeRunning(Boolean(statuses["claude-code"]?.running));
} catch {
// If status fails, don't block UX; just assume stopped
setMcpServeRunning(false);
}
};
useEffect(() => {
refreshMcpServeStatus();
const handle = window.setInterval(refreshMcpServeStatus, 2000);
return () => window.clearInterval(handle);
}, []);
/**
* Imports servers from Claude Desktop
*/
const handleImportFromDesktop = async () => {
try {
setImportingDesktop(true);
// Always use "user" scope for Claude Desktop imports (was previously "global")
const result = await api.mcpAddFromClaudeDesktop("user");
// Show detailed results if available
if (result.servers && result.servers.length > 0) {
const failedServers = result.servers.filter(s => !s.success);
// Always call onImportCompleted for server list refresh and count-based toast
onImportCompleted(result.imported_count, result.failed_count);
// Only show detailed error messages for failed servers (onImportCompleted already shows success)
if (failedServers.length > 0) {
const failureDetails = failedServers
.map(s => `${s.name}: ${s.error || "Unknown error"}`)
.join("\n");
console.warn("Failed to import some servers:", failureDetails);
// Don't call onError here - onImportCompleted already handles the toast
}
} else {
onImportCompleted(result.imported_count, result.failed_count);
}
} catch (error: any) {
console.error("Failed to import from Claude Desktop:", error);
onError(error.toString() || "Failed to import from Claude Desktop");
} finally {
setImportingDesktop(false);
}
};
/**
* Handles JSON file import
*/
const handleJsonFileSelect = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
try {
setImportingJson(true);
const content = await file.text();
// Parse the JSON to validate it
let jsonData;
try {
jsonData = JSON.parse(content);
} catch (e) {
onError("Invalid JSON file. Please check the format.");
return;
}
// Check if it's a single server or multiple servers
if (jsonData.mcpServers) {
// Multiple servers format
let imported = 0;
let failed = 0;
for (const [name, config] of Object.entries(jsonData.mcpServers)) {
try {
const serverConfig = {
type: "stdio",
command: (config as any).command,
args: (config as any).args || [],
env: (config as any).env || {}
};
const result = await api.mcpAddJson(name, JSON.stringify(serverConfig), importScope);
if (result.success) {
imported++;
} else {
failed++;
}
} catch (e) {
failed++;
}
}
onImportCompleted(imported, failed);
} else if (jsonData.type && jsonData.command) {
// Single server format
const name = prompt("Enter a name for this server:");
if (!name) return;
const result = await api.mcpAddJson(name, content, importScope);
if (result.success) {
onImportCompleted(1, 0);
} else {
onError(result.message);
}
} else {
onError("Unrecognized JSON format. Expected MCP server configuration.");
}
} catch (error) {
console.error("Failed to import JSON:", error);
onError("Failed to import JSON file");
} finally {
setImportingJson(false);
// Reset the input
event.target.value = "";
}
};
/**
* Handles exporting servers (placeholder)
*/
const handleExport = () => {
// TODO: Implement export functionality
onError("Export functionality coming soon!");
};
/**
* Starts Claude Code as MCP server
*/
const handleStartMCPServer = async () => {
try {
setMcpServeChecking(true);
const message = await api.mcpServe();
await refreshMcpServeStatus();
onSuccess(message);
} catch (error) {
console.error("Failed to start MCP server:", error);
onError("Failed to start Claude Code as MCP server");
} finally {
setMcpServeChecking(false);
}
};
const handleStopMCPServer = async () => {
try {
setMcpServeChecking(true);
const message = await api.mcpStop();
await refreshMcpServeStatus();
onSuccess(message);
} catch (error) {
console.error("Failed to stop MCP server:", error);
onError("Failed to stop Claude Code MCP server");
} finally {
setMcpServeChecking(false);
}
};
return (
<div className="p-6 space-y-6">
<div>
<h3 className="text-base font-semibold">Import & Export</h3>
<p className="text-sm text-muted-foreground mt-1">
Import MCP servers from other sources or export your configuration
</p>
</div>
<div className="space-y-4">
{/* Import Scope Selection */}
<Card className="p-4">
<div className="space-y-3">
<div className="flex items-center gap-2 mb-2">
<Settings2 className="h-4 w-4 text-slate-500" />
<Label className="text-sm font-medium">Import Scope</Label>
</div>
<SelectComponent
value={importScope}
onValueChange={(value: string) => setImportScope(value)}
options={[
{ value: "local", label: "Local (this project only)" },
{ value: "project", label: "Project (shared via .mcp.json)" },
{ value: "user", label: "User (all projects)" },
]}
/>
<p className="text-xs text-muted-foreground">
Choose where to save imported servers from JSON files
</p>
</div>
</Card>
{/* Import from Claude Desktop */}
<Card className="p-4 hover:bg-accent/5 transition-colors">
<div className="space-y-3">
<div className="flex items-start gap-3">
<div className="p-2.5 bg-blue-500/10 rounded-lg">
<Download className="h-5 w-5 text-blue-500" />
</div>
<div className="flex-1">
<h4 className="text-sm font-medium">Import from Claude Desktop</h4>
<p className="text-xs text-muted-foreground mt-1">
Automatically imports all MCP servers from Claude Desktop. Installs to user scope (available across all projects).
</p>
</div>
</div>
<Button
onClick={handleImportFromDesktop}
disabled={importingDesktop}
className="w-full gap-2 bg-primary hover:bg-primary/90"
>
{importingDesktop ? (
<>
<Loader2 className="h-4 w-4 animate-spin" />
Importing...
</>
) : (
<>
<Download className="h-4 w-4" />
Import from Claude Desktop
</>
)}
</Button>
</div>
</Card>
{/* Import from JSON */}
<Card className="p-4 hover:bg-accent/5 transition-colors">
<div className="space-y-3">
<div className="flex items-start gap-3">
<div className="p-2.5 bg-purple-500/10 rounded-lg">
<FileText className="h-5 w-5 text-purple-500" />
</div>
<div className="flex-1">
<h4 className="text-sm font-medium">Import from JSON</h4>
<p className="text-xs text-muted-foreground mt-1">
Import server configuration from a JSON file
</p>
</div>
</div>
<div>
<input
type="file"
accept=".json"
onChange={handleJsonFileSelect}
disabled={importingJson}
className="hidden"
id="json-file-input"
/>
<Button
onClick={() => document.getElementById("json-file-input")?.click()}
disabled={importingJson}
className="w-full gap-2"
variant="outline"
>
{importingJson ? (
<>
<Loader2 className="h-4 w-4 animate-spin" />
Importing...
</>
) : (
<>
<FileText className="h-4 w-4" />
Choose JSON File
</>
)}
</Button>
</div>
</div>
</Card>
{/* Export (Coming Soon) */}
<Card className="p-4 opacity-60">
<div className="space-y-3">
<div className="flex items-start gap-3">
<div className="p-2.5 bg-muted rounded-lg">
<Upload className="h-5 w-5 text-muted-foreground" />
</div>
<div className="flex-1">
<h4 className="text-sm font-medium">Export Configuration</h4>
<p className="text-xs text-muted-foreground mt-1">
Export your MCP server configuration
</p>
</div>
</div>
<Button
onClick={handleExport}
disabled={true}
variant="secondary"
className="w-full gap-2"
>
<Upload className="h-4 w-4" />
Export (Coming Soon)
</Button>
</div>
</Card>
{/* Serve as MCP */}
<Card className="p-4 border-primary/20 bg-primary/5 hover:bg-primary/10 transition-colors">
<div className="space-y-3">
<div className="flex items-start gap-3">
<div className="p-2.5 bg-green-500/20 rounded-lg">
<Network className="h-5 w-5 text-green-500" />
</div>
<div className="flex-1">
<div className="flex items-center justify-between gap-3">
<h4 className="text-sm font-medium">Use Claude Code as MCP Server</h4>
<div className="text-xs text-muted-foreground">
{mcpServeChecking ? "Checking…" : mcpServeRunning ? "Running" : "Stopped"}
</div>
</div>
<p className="text-xs text-muted-foreground mt-1">
Start Claude Code as an MCP server that other applications can connect to
</p>
</div>
</div>
{mcpServeRunning ? (
<Button
onClick={handleStopMCPServer}
variant="outline"
className="w-full gap-2 border-red-500/20 hover:bg-red-500/10 hover:text-red-600 hover:border-red-500/50"
disabled={mcpServeChecking}
>
<Network className="h-4 w-4" />
Stop MCP Server
</Button>
) : (
<Button
onClick={handleStartMCPServer}
variant="outline"
className="w-full gap-2 border-green-500/20 hover:bg-green-500/10 hover:text-green-600 hover:border-green-500/50"
disabled={mcpServeChecking}
>
<Network className="h-4 w-4" />
Start MCP Server
</Button>
)}
</div>
</Card>
</div>
{/* Info Box */}
<Card className="p-4 bg-muted/30">
<div className="space-y-3">
<div className="flex items-center gap-2 text-sm font-medium">
<Info className="h-4 w-4 text-primary" />
<span>JSON Format Examples</span>
</div>
<div className="space-y-3 text-xs">
<div>
<p className="font-medium text-muted-foreground mb-1">Single server:</p>
<pre className="bg-background p-3 rounded-lg overflow-x-auto">
{`{
"type": "stdio",
"command": "/path/to/server",
"args": ["--arg1", "value"],
"env": { "KEY": "value" }
}`}
</pre>
</div>
<div>
<p className="font-medium text-muted-foreground mb-1">Multiple servers (.mcp.json format):</p>
<pre className="bg-background p-3 rounded-lg overflow-x-auto">
{`{
"mcpServers": {
"server1": {
"command": "/path/to/server1",
"args": [],
"env": {}
},
"server2": {
"command": "/path/to/server2",
"args": ["--port", "8080"],
"env": { "API_KEY": "..." }
}
}
}`}
</pre>
</div>
</div>
</div>
</Card>
</div>
);
};