upfynai-code 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,552 +1,535 @@
1
- import express from 'express';
2
- import { promises as fs } from 'fs';
3
- import path from 'path';
4
- import os from 'os';
5
- import { fileURLToPath } from 'url';
6
- import { dirname } from 'path';
7
- import { spawn } from 'child_process';
8
-
9
- const router = express.Router();
10
- const __filename = fileURLToPath(import.meta.url);
11
- const __dirname = dirname(__filename);
12
-
13
- // Claude CLI command routes
14
-
15
- // GET /api/mcp/cli/list - List MCP servers using Claude CLI
16
- router.get('/cli/list', async (req, res) => {
17
- try {
18
- console.log('📋 Listing MCP servers using Claude CLI');
19
-
20
- const { spawn } = await import('child_process');
21
- const { promisify } = await import('util');
22
- const exec = promisify(spawn);
23
-
24
- const process = spawn('claude', ['mcp', 'list'], {
25
- stdio: ['pipe', 'pipe', 'pipe']
26
- });
27
-
28
- let stdout = '';
29
- let stderr = '';
30
-
31
- process.stdout.on('data', (data) => {
32
- stdout += data.toString();
33
- });
34
-
35
- process.stderr.on('data', (data) => {
36
- stderr += data.toString();
37
- });
38
-
39
- process.on('close', (code) => {
40
- if (code === 0) {
41
- res.json({ success: true, output: stdout, servers: parseClaudeListOutput(stdout) });
42
- } else {
43
- // CLI error
44
- res.status(500).json({ error: 'Claude CLI command failed', details: stderr });
45
- }
46
- });
47
-
48
- process.on('error', (error) => {
49
- // CLI run error
50
- res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
51
- });
52
- } catch (error) {
53
- // MCP list error
54
- res.status(500).json({ error: 'Failed to list MCP servers', details: 'An error occurred' });
55
- }
56
- });
57
-
58
- // POST /api/mcp/cli/add - Add MCP server using Claude CLI
59
- router.post('/cli/add', async (req, res) => {
60
- try {
61
- const { name, type = 'stdio', command, args = [], url, headers = {}, env = {}, scope = 'user', projectPath } = req.body;
62
-
63
- console.log(`➕ Adding MCP server using Claude CLI (${scope} scope):`, name);
64
-
65
- const { spawn } = await import('child_process');
66
-
67
- let cliArgs = ['mcp', 'add'];
68
-
69
- // Add scope flag
70
- cliArgs.push('--scope', scope);
71
-
72
- if (type === 'http') {
73
- cliArgs.push('--transport', 'http', name, url);
74
- // Add headers if provided
75
- Object.entries(headers).forEach(([key, value]) => {
76
- cliArgs.push('--header', `${key}: ${value}`);
77
- });
78
- } else if (type === 'sse') {
79
- cliArgs.push('--transport', 'sse', name, url);
80
- // Add headers if provided
81
- Object.entries(headers).forEach(([key, value]) => {
82
- cliArgs.push('--header', `${key}: ${value}`);
83
- });
84
- } else {
85
- // stdio (default): claude mcp add --scope user <name> <command> [args...]
86
- cliArgs.push(name);
87
- // Add environment variables
88
- Object.entries(env).forEach(([key, value]) => {
89
- cliArgs.push('-e', `${key}=${value}`);
90
- });
91
- cliArgs.push(command);
92
- if (args && args.length > 0) {
93
- cliArgs.push(...args);
94
- }
95
- }
96
-
97
- console.log('🔧 Running Claude CLI command:', 'claude', cliArgs.join(' '));
98
-
99
- // For local scope, we need to run the command in the project directory
100
- const spawnOptions = {
101
- stdio: ['pipe', 'pipe', 'pipe']
102
- };
103
-
104
- if (scope === 'local' && projectPath) {
105
- spawnOptions.cwd = projectPath;
106
- console.log('📁 Running in project directory:', projectPath);
107
- }
108
-
109
- const process = spawn('claude', cliArgs, spawnOptions);
110
-
111
- let stdout = '';
112
- let stderr = '';
113
-
114
- process.stdout.on('data', (data) => {
115
- stdout += data.toString();
116
- });
117
-
118
- process.stderr.on('data', (data) => {
119
- stderr += data.toString();
120
- });
121
-
122
- process.on('close', (code) => {
123
- if (code === 0) {
124
- res.json({ success: true, output: stdout, message: `MCP server "${name}" added successfully` });
125
- } else {
126
- // CLI error
127
- res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
128
- }
129
- });
130
-
131
- process.on('error', (error) => {
132
- // CLI run error
133
- res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
134
- });
135
- } catch (error) {
136
- // MCP add error
137
- res.status(500).json({ error: 'Failed to add MCP server', details: 'An error occurred' });
138
- }
139
- });
140
-
141
- // POST /api/mcp/cli/add-json - Add MCP server using JSON format
142
- router.post('/cli/add-json', async (req, res) => {
143
- try {
144
- const { name, jsonConfig, scope = 'user', projectPath } = req.body;
145
-
146
- console.log('➕ Adding MCP server using JSON format:', name);
147
-
148
- // Validate and parse JSON config
149
- let parsedConfig;
150
- try {
151
- parsedConfig = typeof jsonConfig === 'string' ? JSON.parse(jsonConfig) : jsonConfig;
152
- } catch (parseError) {
153
- return res.status(400).json({
154
- error: 'Invalid JSON configuration',
155
- details: parseError.message
156
- });
157
- }
158
-
159
- // Validate required fields
160
- if (!parsedConfig.type) {
161
- return res.status(400).json({
162
- error: 'Invalid configuration',
163
- details: 'Missing required field: type'
164
- });
165
- }
166
-
167
- if (parsedConfig.type === 'stdio' && !parsedConfig.command) {
168
- return res.status(400).json({
169
- error: 'Invalid configuration',
170
- details: 'stdio type requires a command field'
171
- });
172
- }
173
-
174
- if ((parsedConfig.type === 'http' || parsedConfig.type === 'sse') && !parsedConfig.url) {
175
- return res.status(400).json({
176
- error: 'Invalid configuration',
177
- details: `${parsedConfig.type} type requires a url field`
178
- });
179
- }
180
-
181
- const { spawn } = await import('child_process');
182
-
183
- // Build the command: claude mcp add-json --scope <scope> <name> '<json>'
184
- const cliArgs = ['mcp', 'add-json', '--scope', scope, name];
185
-
186
- // Add the JSON config as a properly formatted string
187
- const jsonString = JSON.stringify(parsedConfig);
188
- cliArgs.push(jsonString);
189
-
190
- console.log('🔧 Running Claude CLI command:', 'claude', cliArgs[0], cliArgs[1], cliArgs[2], cliArgs[3], cliArgs[4], jsonString);
191
-
192
- // For local scope, we need to run the command in the project directory
193
- const spawnOptions = {
194
- stdio: ['pipe', 'pipe', 'pipe']
195
- };
196
-
197
- if (scope === 'local' && projectPath) {
198
- spawnOptions.cwd = projectPath;
199
- console.log('📁 Running in project directory:', projectPath);
200
- }
201
-
202
- const process = spawn('claude', cliArgs, spawnOptions);
203
-
204
- let stdout = '';
205
- let stderr = '';
206
-
207
- process.stdout.on('data', (data) => {
208
- stdout += data.toString();
209
- });
210
-
211
- process.stderr.on('data', (data) => {
212
- stderr += data.toString();
213
- });
214
-
215
- process.on('close', (code) => {
216
- if (code === 0) {
217
- res.json({ success: true, output: stdout, message: `MCP server "${name}" added successfully via JSON` });
218
- } else {
219
- // CLI error
220
- res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
221
- }
222
- });
223
-
224
- process.on('error', (error) => {
225
- // CLI run error
226
- res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
227
- });
228
- } catch (error) {
229
- // MCP JSON add error
230
- res.status(500).json({ error: 'Failed to add MCP server', details: 'An error occurred' });
231
- }
232
- });
233
-
234
- // DELETE /api/mcp/cli/remove/:name - Remove MCP server using Claude CLI
235
- router.delete('/cli/remove/:name', async (req, res) => {
236
- try {
237
- const { name } = req.params;
238
- const { scope } = req.query; // Get scope from query params
239
-
240
- // Handle the ID format (remove scope prefix if present)
241
- let actualName = name;
242
- let actualScope = scope;
243
-
244
- // If the name includes a scope prefix like "local:test", extract it
245
- if (name.includes(':')) {
246
- const [prefix, serverName] = name.split(':');
247
- actualName = serverName;
248
- actualScope = actualScope || prefix; // Use prefix as scope if not provided in query
249
- }
250
-
251
- console.log('🗑️ Removing MCP server using Claude CLI:', actualName, 'scope:', actualScope);
252
-
253
- const { spawn } = await import('child_process');
254
-
255
- // Build command args based on scope
256
- let cliArgs = ['mcp', 'remove'];
257
-
258
- // Add scope flag if it's local scope
259
- if (actualScope === 'local') {
260
- cliArgs.push('--scope', 'local');
261
- } else if (actualScope === 'user' || !actualScope) {
262
- // User scope is default, but we can be explicit
263
- cliArgs.push('--scope', 'user');
264
- }
265
-
266
- cliArgs.push(actualName);
267
-
268
- console.log('🔧 Running Claude CLI command:', 'claude', cliArgs.join(' '));
269
-
270
- const process = spawn('claude', cliArgs, {
271
- stdio: ['pipe', 'pipe', 'pipe']
272
- });
273
-
274
- let stdout = '';
275
- let stderr = '';
276
-
277
- process.stdout.on('data', (data) => {
278
- stdout += data.toString();
279
- });
280
-
281
- process.stderr.on('data', (data) => {
282
- stderr += data.toString();
283
- });
284
-
285
- process.on('close', (code) => {
286
- if (code === 0) {
287
- res.json({ success: true, output: stdout, message: `MCP server "${name}" removed successfully` });
288
- } else {
289
- // CLI error
290
- res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
291
- }
292
- });
293
-
294
- process.on('error', (error) => {
295
- // CLI run error
296
- res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
297
- });
298
- } catch (error) {
299
- // MCP remove error
300
- res.status(500).json({ error: 'Failed to remove MCP server', details: 'An error occurred' });
301
- }
302
- });
303
-
304
- // GET /api/mcp/cli/get/:name - Get MCP server details using Claude CLI
305
- router.get('/cli/get/:name', async (req, res) => {
306
- try {
307
- const { name } = req.params;
308
-
309
- // getting MCP server details
310
-
311
- const { spawn } = await import('child_process');
312
-
313
- const process = spawn('claude', ['mcp', 'get', name], {
314
- stdio: ['pipe', 'pipe', 'pipe']
315
- });
316
-
317
- let stdout = '';
318
- let stderr = '';
319
-
320
- process.stdout.on('data', (data) => {
321
- stdout += data.toString();
322
- });
323
-
324
- process.stderr.on('data', (data) => {
325
- stderr += data.toString();
326
- });
327
-
328
- process.on('close', (code) => {
329
- if (code === 0) {
330
- res.json({ success: true, output: stdout, server: parseClaudeGetOutput(stdout) });
331
- } else {
332
- // CLI error
333
- res.status(404).json({ error: 'Claude CLI command failed', details: stderr });
334
- }
335
- });
336
-
337
- process.on('error', (error) => {
338
- // CLI run error
339
- res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
340
- });
341
- } catch (error) {
342
- // MCP CLI details error
343
- res.status(500).json({ error: 'Failed to get MCP server details', details: 'An error occurred' });
344
- }
345
- });
346
-
347
- // GET /api/mcp/config/read - Read MCP servers directly from Claude config files
348
- router.get('/config/read', async (req, res) => {
349
- try {
350
- console.log('📖 Reading MCP servers from Claude config files');
351
-
352
- const homeDir = os.homedir();
353
- const configPaths = [
354
- path.join(homeDir, '.claude.json'),
355
- path.join(homeDir, '.claude', 'settings.json')
356
- ];
357
-
358
- let configData = null;
359
- let configPath = null;
360
-
361
- // Try to read from either config file
362
- for (const filepath of configPaths) {
363
- try {
364
- const fileContent = await fs.readFile(filepath, 'utf8');
365
- configData = JSON.parse(fileContent);
366
- configPath = filepath;
367
- console.log(`✅ Found Claude config at: ${filepath}`);
368
- break;
369
- } catch (error) {
370
- // File doesn't exist or is not valid JSON, try next
371
- console.log(`ℹ️ Config not found or invalid at: ${filepath}`);
372
- }
373
- }
374
-
375
- if (!configData) {
376
- return res.json({
377
- success: false,
378
- message: 'No Claude configuration file found',
379
- servers: []
380
- });
381
- }
382
-
383
- // Extract MCP servers from the config
384
- const servers = [];
385
-
386
- // Check for user-scoped MCP servers (at root level)
387
- if (configData.mcpServers && typeof configData.mcpServers === 'object' && Object.keys(configData.mcpServers).length > 0) {
388
- console.log('🔍 Found user-scoped MCP servers:', Object.keys(configData.mcpServers));
389
- for (const [name, config] of Object.entries(configData.mcpServers)) {
390
- const server = {
391
- id: name,
392
- name: name,
393
- type: 'stdio', // Default type
394
- scope: 'user', // User scope - available across all projects
395
- config: {},
396
- raw: config // Include raw config for full details
397
- };
398
-
399
- // Determine transport type and extract config
400
- if (config.command) {
401
- server.type = 'stdio';
402
- server.config.command = config.command;
403
- server.config.args = config.args || [];
404
- server.config.env = config.env || {};
405
- } else if (config.url) {
406
- server.type = config.transport || 'http';
407
- server.config.url = config.url;
408
- server.config.headers = config.headers || {};
409
- }
410
-
411
- servers.push(server);
412
- }
413
- }
414
-
415
- // Check for local-scoped MCP servers (project-specific)
416
- const currentProjectPath = process.cwd();
417
-
418
- // Check under 'projects' key
419
- if (configData.projects && configData.projects[currentProjectPath]) {
420
- const projectConfig = configData.projects[currentProjectPath];
421
- if (projectConfig.mcpServers && typeof projectConfig.mcpServers === 'object' && Object.keys(projectConfig.mcpServers).length > 0) {
422
- console.log(`🔍 Found local-scoped MCP servers for ${currentProjectPath}:`, Object.keys(projectConfig.mcpServers));
423
- for (const [name, config] of Object.entries(projectConfig.mcpServers)) {
424
- const server = {
425
- id: `local:${name}`, // Prefix with scope for uniqueness
426
- name: name, // Keep original name
427
- type: 'stdio', // Default type
428
- scope: 'local', // Local scope - only for this project
429
- projectPath: currentProjectPath,
430
- config: {},
431
- raw: config // Include raw config for full details
432
- };
433
-
434
- // Determine transport type and extract config
435
- if (config.command) {
436
- server.type = 'stdio';
437
- server.config.command = config.command;
438
- server.config.args = config.args || [];
439
- server.config.env = config.env || {};
440
- } else if (config.url) {
441
- server.type = config.transport || 'http';
442
- server.config.url = config.url;
443
- server.config.headers = config.headers || {};
444
- }
445
-
446
- servers.push(server);
447
- }
448
- }
449
- }
450
-
451
- console.log(`📋 Found ${servers.length} MCP servers in config`);
452
-
453
- res.json({
454
- success: true,
455
- configPath: configPath,
456
- servers: servers
457
- });
458
- } catch (error) {
459
- console.error('Error reading Claude config:', error);
460
- res.status(500).json({
461
- error: 'Failed to read Claude configuration',
462
- details: 'An error occurred'
463
- });
464
- }
465
- });
466
-
467
- // Helper functions to parse Claude CLI output
468
- function parseClaudeListOutput(output) {
469
- const servers = [];
470
- const lines = output.split('\n').filter(line => line.trim());
471
-
472
- for (const line of lines) {
473
- // Skip the header line
474
- if (line.includes('Checking MCP server health')) continue;
475
-
476
- // Parse lines like "test: test test - ✗ Failed to connect"
477
- // or "server-name: command or description - ✓ Connected"
478
- if (line.includes(':')) {
479
- const colonIndex = line.indexOf(':');
480
- const name = line.substring(0, colonIndex).trim();
481
-
482
- // Skip empty names
483
- if (!name) continue;
484
-
485
- // Extract the rest after the name
486
- const rest = line.substring(colonIndex + 1).trim();
487
-
488
- // Try to extract description and status
489
- let description = rest;
490
- let status = 'unknown';
491
- let type = 'stdio'; // default type
492
-
493
- // Check for status indicators
494
- if (rest.includes('✓') || rest.includes('')) {
495
- const statusMatch = rest.match(/(.*?)\s*-\s*([✓✗].*)$/);
496
- if (statusMatch) {
497
- description = statusMatch[1].trim();
498
- status = statusMatch[2].includes('✓') ? 'connected' : 'failed';
499
- }
500
- }
501
-
502
- // Try to determine type from description
503
- if (description.startsWith('http://') || description.startsWith('https://')) {
504
- type = 'http';
505
- }
506
-
507
- servers.push({
508
- name,
509
- type,
510
- status: status || 'active',
511
- description
512
- });
513
- }
514
- }
515
-
516
- console.log('🔍 Parsed Claude CLI servers:', servers);
517
- return servers;
518
- }
519
-
520
- function parseClaudeGetOutput(output) {
521
- // Parse the output from 'claude mcp get <name>' command
522
- // This is a simple parser - might need adjustment based on actual output format
523
- try {
524
- // Try to extract JSON if present
525
- const jsonMatch = output.match(/\{[\s\S]*\}/);
526
- if (jsonMatch) {
527
- return JSON.parse(jsonMatch[0]);
528
- }
529
-
530
- // Otherwise, parse as text
531
- const server = { raw_output: output };
532
- const lines = output.split('\n');
533
-
534
- for (const line of lines) {
535
- if (line.includes('Name:')) {
536
- server.name = line.split(':')[1]?.trim();
537
- } else if (line.includes('Type:')) {
538
- server.type = line.split(':')[1]?.trim();
539
- } else if (line.includes('Command:')) {
540
- server.command = line.split(':')[1]?.trim();
541
- } else if (line.includes('URL:')) {
542
- server.url = line.split(':')[1]?.trim();
543
- }
544
- }
545
-
546
- return server;
547
- } catch (error) {
548
- return { raw_output: output, parse_error: error.message };
549
- }
550
- }
551
-
1
+ import express from 'express';
2
+ import { promises as fs } from 'fs';
3
+ import path from 'path';
4
+ import os from 'os';
5
+ import { fileURLToPath } from 'url';
6
+ import { dirname } from 'path';
7
+ import { spawn } from 'child_process';
8
+
9
+ const router = express.Router();
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = dirname(__filename);
12
+
13
+ // Claude CLI command routes
14
+
15
+ // GET /api/mcp/cli/list - List MCP servers using Claude CLI
16
+ router.get('/cli/list', async (req, res) => {
17
+ try {
18
+
19
+ const { spawn } = await import('child_process');
20
+ const { promisify } = await import('util');
21
+ const exec = promisify(spawn);
22
+
23
+ const process = spawn('claude', ['mcp', 'list'], {
24
+ stdio: ['pipe', 'pipe', 'pipe']
25
+ });
26
+
27
+ let stdout = '';
28
+ let stderr = '';
29
+
30
+ process.stdout.on('data', (data) => {
31
+ stdout += data.toString();
32
+ });
33
+
34
+ process.stderr.on('data', (data) => {
35
+ stderr += data.toString();
36
+ });
37
+
38
+ process.on('close', (code) => {
39
+ if (code === 0) {
40
+ res.json({ success: true, output: stdout, servers: parseClaudeListOutput(stdout) });
41
+ } else {
42
+ // CLI error
43
+ res.status(500).json({ error: 'Claude CLI command failed', details: stderr });
44
+ }
45
+ });
46
+
47
+ process.on('error', (error) => {
48
+ // CLI run error
49
+ res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
50
+ });
51
+ } catch (error) {
52
+ // MCP list error
53
+ res.status(500).json({ error: 'Failed to list MCP servers', details: 'An error occurred' });
54
+ }
55
+ });
56
+
57
+ // POST /api/mcp/cli/add - Add MCP server using Claude CLI
58
+ router.post('/cli/add', async (req, res) => {
59
+ try {
60
+ const { name, type = 'stdio', command, args = [], url, headers = {}, env = {}, scope = 'user', projectPath } = req.body;
61
+
62
+
63
+ const { spawn } = await import('child_process');
64
+
65
+ let cliArgs = ['mcp', 'add'];
66
+
67
+ // Add scope flag
68
+ cliArgs.push('--scope', scope);
69
+
70
+ if (type === 'http') {
71
+ cliArgs.push('--transport', 'http', name, url);
72
+ // Add headers if provided
73
+ Object.entries(headers).forEach(([key, value]) => {
74
+ cliArgs.push('--header', `${key}: ${value}`);
75
+ });
76
+ } else if (type === 'sse') {
77
+ cliArgs.push('--transport', 'sse', name, url);
78
+ // Add headers if provided
79
+ Object.entries(headers).forEach(([key, value]) => {
80
+ cliArgs.push('--header', `${key}: ${value}`);
81
+ });
82
+ } else {
83
+ // stdio (default): claude mcp add --scope user <name> <command> [args...]
84
+ cliArgs.push(name);
85
+ // Add environment variables
86
+ Object.entries(env).forEach(([key, value]) => {
87
+ cliArgs.push('-e', `${key}=${value}`);
88
+ });
89
+ cliArgs.push(command);
90
+ if (args && args.length > 0) {
91
+ cliArgs.push(...args);
92
+ }
93
+ }
94
+
95
+
96
+ // For local scope, we need to run the command in the project directory
97
+ const spawnOptions = {
98
+ stdio: ['pipe', 'pipe', 'pipe']
99
+ };
100
+
101
+ if (scope === 'local' && projectPath) {
102
+ spawnOptions.cwd = projectPath;
103
+ }
104
+
105
+ const process = spawn('claude', cliArgs, spawnOptions);
106
+
107
+ let stdout = '';
108
+ let stderr = '';
109
+
110
+ process.stdout.on('data', (data) => {
111
+ stdout += data.toString();
112
+ });
113
+
114
+ process.stderr.on('data', (data) => {
115
+ stderr += data.toString();
116
+ });
117
+
118
+ process.on('close', (code) => {
119
+ if (code === 0) {
120
+ res.json({ success: true, output: stdout, message: `MCP server "${name}" added successfully` });
121
+ } else {
122
+ // CLI error
123
+ res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
124
+ }
125
+ });
126
+
127
+ process.on('error', (error) => {
128
+ // CLI run error
129
+ res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
130
+ });
131
+ } catch (error) {
132
+ // MCP add error
133
+ res.status(500).json({ error: 'Failed to add MCP server', details: 'An error occurred' });
134
+ }
135
+ });
136
+
137
+ // POST /api/mcp/cli/add-json - Add MCP server using JSON format
138
+ router.post('/cli/add-json', async (req, res) => {
139
+ try {
140
+ const { name, jsonConfig, scope = 'user', projectPath } = req.body;
141
+
142
+
143
+ // Validate and parse JSON config
144
+ let parsedConfig;
145
+ try {
146
+ parsedConfig = typeof jsonConfig === 'string' ? JSON.parse(jsonConfig) : jsonConfig;
147
+ } catch (parseError) {
148
+ return res.status(400).json({
149
+ error: 'Invalid JSON configuration',
150
+ details: parseError.message
151
+ });
152
+ }
153
+
154
+ // Validate required fields
155
+ if (!parsedConfig.type) {
156
+ return res.status(400).json({
157
+ error: 'Invalid configuration',
158
+ details: 'Missing required field: type'
159
+ });
160
+ }
161
+
162
+ if (parsedConfig.type === 'stdio' && !parsedConfig.command) {
163
+ return res.status(400).json({
164
+ error: 'Invalid configuration',
165
+ details: 'stdio type requires a command field'
166
+ });
167
+ }
168
+
169
+ if ((parsedConfig.type === 'http' || parsedConfig.type === 'sse') && !parsedConfig.url) {
170
+ return res.status(400).json({
171
+ error: 'Invalid configuration',
172
+ details: `${parsedConfig.type} type requires a url field`
173
+ });
174
+ }
175
+
176
+ const { spawn } = await import('child_process');
177
+
178
+ // Build the command: claude mcp add-json --scope <scope> <name> '<json>'
179
+ const cliArgs = ['mcp', 'add-json', '--scope', scope, name];
180
+
181
+ // Add the JSON config as a properly formatted string
182
+ const jsonString = JSON.stringify(parsedConfig);
183
+ cliArgs.push(jsonString);
184
+
185
+
186
+ // For local scope, we need to run the command in the project directory
187
+ const spawnOptions = {
188
+ stdio: ['pipe', 'pipe', 'pipe']
189
+ };
190
+
191
+ if (scope === 'local' && projectPath) {
192
+ spawnOptions.cwd = projectPath;
193
+ }
194
+
195
+ const process = spawn('claude', cliArgs, spawnOptions);
196
+
197
+ let stdout = '';
198
+ let stderr = '';
199
+
200
+ process.stdout.on('data', (data) => {
201
+ stdout += data.toString();
202
+ });
203
+
204
+ process.stderr.on('data', (data) => {
205
+ stderr += data.toString();
206
+ });
207
+
208
+ process.on('close', (code) => {
209
+ if (code === 0) {
210
+ res.json({ success: true, output: stdout, message: `MCP server "${name}" added successfully via JSON` });
211
+ } else {
212
+ // CLI error
213
+ res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
214
+ }
215
+ });
216
+
217
+ process.on('error', (error) => {
218
+ // CLI run error
219
+ res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
220
+ });
221
+ } catch (error) {
222
+ // MCP JSON add error
223
+ res.status(500).json({ error: 'Failed to add MCP server', details: 'An error occurred' });
224
+ }
225
+ });
226
+
227
+ // DELETE /api/mcp/cli/remove/:name - Remove MCP server using Claude CLI
228
+ router.delete('/cli/remove/:name', async (req, res) => {
229
+ try {
230
+ const { name } = req.params;
231
+ const { scope } = req.query; // Get scope from query params
232
+
233
+ // Handle the ID format (remove scope prefix if present)
234
+ let actualName = name;
235
+ let actualScope = scope;
236
+
237
+ // If the name includes a scope prefix like "local:test", extract it
238
+ if (name.includes(':')) {
239
+ const [prefix, serverName] = name.split(':');
240
+ actualName = serverName;
241
+ actualScope = actualScope || prefix; // Use prefix as scope if not provided in query
242
+ }
243
+
244
+
245
+ const { spawn } = await import('child_process');
246
+
247
+ // Build command args based on scope
248
+ let cliArgs = ['mcp', 'remove'];
249
+
250
+ // Add scope flag if it's local scope
251
+ if (actualScope === 'local') {
252
+ cliArgs.push('--scope', 'local');
253
+ } else if (actualScope === 'user' || !actualScope) {
254
+ // User scope is default, but we can be explicit
255
+ cliArgs.push('--scope', 'user');
256
+ }
257
+
258
+ cliArgs.push(actualName);
259
+
260
+
261
+ const process = spawn('claude', cliArgs, {
262
+ stdio: ['pipe', 'pipe', 'pipe']
263
+ });
264
+
265
+ let stdout = '';
266
+ let stderr = '';
267
+
268
+ process.stdout.on('data', (data) => {
269
+ stdout += data.toString();
270
+ });
271
+
272
+ process.stderr.on('data', (data) => {
273
+ stderr += data.toString();
274
+ });
275
+
276
+ process.on('close', (code) => {
277
+ if (code === 0) {
278
+ res.json({ success: true, output: stdout, message: `MCP server "${name}" removed successfully` });
279
+ } else {
280
+ // CLI error
281
+ res.status(400).json({ error: 'Claude CLI command failed', details: stderr });
282
+ }
283
+ });
284
+
285
+ process.on('error', (error) => {
286
+ // CLI run error
287
+ res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
288
+ });
289
+ } catch (error) {
290
+ // MCP remove error
291
+ res.status(500).json({ error: 'Failed to remove MCP server', details: 'An error occurred' });
292
+ }
293
+ });
294
+
295
+ // GET /api/mcp/cli/get/:name - Get MCP server details using Claude CLI
296
+ router.get('/cli/get/:name', async (req, res) => {
297
+ try {
298
+ const { name } = req.params;
299
+
300
+ // getting MCP server details
301
+
302
+ const { spawn } = await import('child_process');
303
+
304
+ const process = spawn('claude', ['mcp', 'get', name], {
305
+ stdio: ['pipe', 'pipe', 'pipe']
306
+ });
307
+
308
+ let stdout = '';
309
+ let stderr = '';
310
+
311
+ process.stdout.on('data', (data) => {
312
+ stdout += data.toString();
313
+ });
314
+
315
+ process.stderr.on('data', (data) => {
316
+ stderr += data.toString();
317
+ });
318
+
319
+ process.on('close', (code) => {
320
+ if (code === 0) {
321
+ res.json({ success: true, output: stdout, server: parseClaudeGetOutput(stdout) });
322
+ } else {
323
+ // CLI error
324
+ res.status(404).json({ error: 'Claude CLI command failed', details: stderr });
325
+ }
326
+ });
327
+
328
+ process.on('error', (error) => {
329
+ // CLI run error
330
+ res.status(500).json({ error: 'Failed to run Claude CLI', details: 'An error occurred' });
331
+ });
332
+ } catch (error) {
333
+ // MCP CLI details error
334
+ res.status(500).json({ error: 'Failed to get MCP server details', details: 'An error occurred' });
335
+ }
336
+ });
337
+
338
+ // GET /api/mcp/config/read - Read MCP servers directly from Claude config files
339
+ router.get('/config/read', async (req, res) => {
340
+ try {
341
+
342
+ const homeDir = os.homedir();
343
+ const configPaths = [
344
+ path.join(homeDir, '.claude.json'),
345
+ path.join(homeDir, '.claude', 'settings.json')
346
+ ];
347
+
348
+ let configData = null;
349
+ let configPath = null;
350
+
351
+ // Try to read from either config file
352
+ for (const filepath of configPaths) {
353
+ try {
354
+ const fileContent = await fs.readFile(filepath, 'utf8');
355
+ configData = JSON.parse(fileContent);
356
+ configPath = filepath;
357
+ break;
358
+ } catch (error) {
359
+ // File doesn't exist or is not valid JSON, try next
360
+ }
361
+ }
362
+
363
+ if (!configData) {
364
+ return res.json({
365
+ success: false,
366
+ message: 'No Claude configuration file found',
367
+ servers: []
368
+ });
369
+ }
370
+
371
+ // Extract MCP servers from the config
372
+ const servers = [];
373
+
374
+ // Check for user-scoped MCP servers (at root level)
375
+ if (configData.mcpServers && typeof configData.mcpServers === 'object' && Object.keys(configData.mcpServers).length > 0) {
376
+ for (const [name, config] of Object.entries(configData.mcpServers)) {
377
+ const server = {
378
+ id: name,
379
+ name: name,
380
+ type: 'stdio', // Default type
381
+ scope: 'user', // User scope - available across all projects
382
+ config: {},
383
+ raw: config // Include raw config for full details
384
+ };
385
+
386
+ // Determine transport type and extract config
387
+ if (config.command) {
388
+ server.type = 'stdio';
389
+ server.config.command = config.command;
390
+ server.config.args = config.args || [];
391
+ server.config.env = config.env || {};
392
+ } else if (config.url) {
393
+ server.type = config.transport || 'http';
394
+ server.config.url = config.url;
395
+ server.config.headers = config.headers || {};
396
+ }
397
+
398
+ servers.push(server);
399
+ }
400
+ }
401
+
402
+ // Check for local-scoped MCP servers (project-specific)
403
+ const currentProjectPath = process.cwd();
404
+
405
+ // Check under 'projects' key
406
+ if (configData.projects && configData.projects[currentProjectPath]) {
407
+ const projectConfig = configData.projects[currentProjectPath];
408
+ if (projectConfig.mcpServers && typeof projectConfig.mcpServers === 'object' && Object.keys(projectConfig.mcpServers).length > 0) {
409
+ for (const [name, config] of Object.entries(projectConfig.mcpServers)) {
410
+ const server = {
411
+ id: `local:${name}`, // Prefix with scope for uniqueness
412
+ name: name, // Keep original name
413
+ type: 'stdio', // Default type
414
+ scope: 'local', // Local scope - only for this project
415
+ projectPath: currentProjectPath,
416
+ config: {},
417
+ raw: config // Include raw config for full details
418
+ };
419
+
420
+ // Determine transport type and extract config
421
+ if (config.command) {
422
+ server.type = 'stdio';
423
+ server.config.command = config.command;
424
+ server.config.args = config.args || [];
425
+ server.config.env = config.env || {};
426
+ } else if (config.url) {
427
+ server.type = config.transport || 'http';
428
+ server.config.url = config.url;
429
+ server.config.headers = config.headers || {};
430
+ }
431
+
432
+ servers.push(server);
433
+ }
434
+ }
435
+ }
436
+
437
+
438
+ res.json({
439
+ success: true,
440
+ configPath: configPath,
441
+ servers: servers
442
+ });
443
+ } catch (error) {
444
+ res.status(500).json({
445
+ error: 'Failed to read Claude configuration',
446
+ details: 'An error occurred'
447
+ });
448
+ }
449
+ });
450
+
451
+ // Helper functions to parse Claude CLI output
452
+ function parseClaudeListOutput(output) {
453
+ const servers = [];
454
+ const lines = output.split('\n').filter(line => line.trim());
455
+
456
+ for (const line of lines) {
457
+ // Skip the header line
458
+ if (line.includes('Checking MCP server health')) continue;
459
+
460
+ // Parse lines like "test: test test - ✗ Failed to connect"
461
+ // or "server-name: command or description - Connected"
462
+ if (line.includes(':')) {
463
+ const colonIndex = line.indexOf(':');
464
+ const name = line.substring(0, colonIndex).trim();
465
+
466
+ // Skip empty names
467
+ if (!name) continue;
468
+
469
+ // Extract the rest after the name
470
+ const rest = line.substring(colonIndex + 1).trim();
471
+
472
+ // Try to extract description and status
473
+ let description = rest;
474
+ let status = 'unknown';
475
+ let type = 'stdio'; // default type
476
+
477
+ // Check for status indicators
478
+ if (rest.includes('') || rest.includes('✗')) {
479
+ const statusMatch = rest.match(/(.*?)\s*-\s*([✓✗].*)$/);
480
+ if (statusMatch) {
481
+ description = statusMatch[1].trim();
482
+ status = statusMatch[2].includes('✓') ? 'connected' : 'failed';
483
+ }
484
+ }
485
+
486
+ // Try to determine type from description
487
+ if (description.startsWith('http://') || description.startsWith('https://')) {
488
+ type = 'http';
489
+ }
490
+
491
+ servers.push({
492
+ name,
493
+ type,
494
+ status: status || 'active',
495
+ description
496
+ });
497
+ }
498
+ }
499
+
500
+ return servers;
501
+ }
502
+
503
+ function parseClaudeGetOutput(output) {
504
+ // Parse the output from 'claude mcp get <name>' command
505
+ // This is a simple parser - might need adjustment based on actual output format
506
+ try {
507
+ // Try to extract JSON if present
508
+ const jsonMatch = output.match(/\{[\s\S]*\}/);
509
+ if (jsonMatch) {
510
+ return JSON.parse(jsonMatch[0]);
511
+ }
512
+
513
+ // Otherwise, parse as text
514
+ const server = { raw_output: output };
515
+ const lines = output.split('\n');
516
+
517
+ for (const line of lines) {
518
+ if (line.includes('Name:')) {
519
+ server.name = line.split(':')[1]?.trim();
520
+ } else if (line.includes('Type:')) {
521
+ server.type = line.split(':')[1]?.trim();
522
+ } else if (line.includes('Command:')) {
523
+ server.command = line.split(':')[1]?.trim();
524
+ } else if (line.includes('URL:')) {
525
+ server.url = line.split(':')[1]?.trim();
526
+ }
527
+ }
528
+
529
+ return server;
530
+ } catch (error) {
531
+ return { raw_output: output, parse_error: error.message };
532
+ }
533
+ }
534
+
552
535
  export default router;