-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.js
More file actions
384 lines (320 loc) Β· 12.4 KB
/
Copy pathdebug.js
File metadata and controls
384 lines (320 loc) Β· 12.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
#!/usr/bin/env node
import { promisify } from "util";
import { exec } from "child_process";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from 'url';
const execAsync = promisify(exec);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class MCPServerDebugger {
constructor() {
this.issues = [];
this.warnings = [];
this.success = [];
}
log(type, message, details = null) {
const timestamp = new Date().toISOString();
const logEntry = { timestamp, message, details };
switch (type) {
case 'success':
this.success.push(logEntry);
console.log(`β
${message}`);
if (details) console.log(` ${details}`);
break;
case 'warning':
this.warnings.push(logEntry);
console.log(`β οΈ ${message}`);
if (details) console.log(` ${details}`);
break;
case 'error':
this.issues.push(logEntry);
console.log(`β ${message}`);
if (details) console.log(` ${details}`);
break;
case 'info':
console.log(`βΉοΈ ${message}`);
if (details) console.log(` ${details}`);
break;
}
}
async checkNodeVersion() {
try {
const { stdout } = await execAsync("node --version");
const version = stdout.trim();
const majorVersion = parseInt(version.slice(1).split('.')[0]);
if (majorVersion >= 18) {
this.log('success', `Node.js version: ${version}`);
} else {
this.log('error', `Node.js version too old: ${version}`, 'Requires Node.js 18+');
}
} catch (error) {
this.log('error', 'Node.js not found', error.message);
}
}
async checkProjectStructure() {
const projectDir = path.join(__dirname, '..');
const requiredFiles = [
'package.json',
'src/index.js',
'config.json'
];
this.log('info', `Checking project structure in: ${projectDir}`);
for (const file of requiredFiles) {
const filePath = path.join(projectDir, file);
try {
const stats = await fs.stat(filePath);
this.log('success', `Found ${file}`, `Size: ${stats.size} bytes`);
} catch (error) {
this.log('error', `Missing ${file}`, error.message);
}
}
}
async checkDependencies() {
try {
const packageJsonPath = path.join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
this.log('info', 'Checking dependencies...');
// Check if MCP SDK is installed
const nodeModulesPath = path.join(__dirname, '..', 'node_modules', '@modelcontextprotocol', 'sdk');
try {
await fs.stat(nodeModulesPath);
this.log('success', 'MCP SDK dependency found');
} catch (error) {
this.log('error', 'MCP SDK dependency missing', 'Run: npm install');
}
} catch (error) {
this.log('error', 'Could not check dependencies', error.message);
}
}
async checkWSLAvailability() {
try {
const { stdout } = await execAsync("wsl --version");
this.log('success', 'WSL is available', stdout.trim().split('\n')[0]);
} catch (error) {
this.log('error', 'WSL not available', error.message);
return false;
}
try {
const { stdout } = await execAsync("wsl -l -v");
this.log('info', 'WSL distributions:', '\n' + stdout);
// Parse distributions
const lines = stdout.split('\n').filter(line => line.trim());
let foundDistributions = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
const cleanLine = line.replace(/[\x00-\x1f\x7f-\x9f]/g, '');
const parts = cleanLine.split(/\s+/);
if (parts.length >= 3) {
const name = parts[0].replace(/^\*\s*/, '');
const state = parts[1];
const version = parts[2];
foundDistributions.push({ name, state, version });
}
}
}
if (foundDistributions.length === 0) {
this.log('error', 'No WSL distributions found', 'Install a Linux distribution: wsl --install -d Ubuntu');
return false;
}
foundDistributions.forEach(dist => {
if (dist.state === 'Running') {
this.log('success', `Distribution ${dist.name} is running`, `WSL${dist.version}`);
} else {
this.log('warning', `Distribution ${dist.name} is stopped`, `WSL${dist.version}`);
}
});
return foundDistributions;
} catch (error) {
this.log('error', 'Could not list WSL distributions', error.message);
return false;
}
}
async testWSLConnection(distributions) {
if (!distributions || distributions.length === 0) {
this.log('error', 'No distributions to test');
return;
}
for (const dist of distributions) {
try {
const { stdout } = await execAsync(`wsl -d ${dist.name} -- echo "Hello from ${dist.name}"`);
if (stdout.includes(`Hello from ${dist.name}`)) {
this.log('success', `WSL connection test passed for ${dist.name}`);
} else {
this.log('warning', `WSL connection test gave unexpected output for ${dist.name}`, stdout);
}
} catch (error) {
this.log('error', `WSL connection test failed for ${dist.name}`, error.message);
}
}
}
async checkConfiguration() {
try {
const configPath = path.join(__dirname, '..', 'config.json');
const configContent = await fs.readFile(configPath, 'utf8');
const config = JSON.parse(configContent);
this.log('success', 'Configuration file is valid JSON');
this.log('info', 'Configuration content:', JSON.stringify(config, null, 2));
if (config.wslDistribution) {
this.log('info', `Configured WSL distribution: ${config.wslDistribution}`);
} else {
this.log('warning', 'No WSL distribution configured');
}
return config;
} catch (error) {
this.log('error', 'Configuration file issue', error.message);
return null;
}
}
async testMCPServerImports() {
try {
this.log('info', 'Testing MCP Server imports...');
// Test if we can import the MCP SDK
const { Server } = await import("@modelcontextprotocol/sdk/server/index.js");
const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
this.log('success', 'MCP SDK imports successful');
// Test if we can create a server instance
const server = new Server(
{ name: "test-server", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
this.log('success', 'MCP Server instance created successfully');
} catch (error) {
this.log('error', 'MCP Server import/creation failed', error.message);
}
}
async testServerStartup() {
this.log('info', 'Testing server startup (dry run)...');
try {
// Import our server module
const serverPath = path.join(__dirname, '..', 'src', 'index.js');
// Check if the file can be read
const serverContent = await fs.readFile(serverPath, 'utf8');
this.log('success', 'Server file readable', `${serverContent.length} characters`);
// Try to parse it as a module (syntax check)
try {
// This is a basic syntax check - we can't actually run it without stdio setup
this.log('info', 'Server file syntax appears valid');
} catch (syntaxError) {
this.log('error', 'Server file syntax error', syntaxError.message);
}
} catch (error) {
this.log('error', 'Server file issue', error.message);
}
}
async checkClaudeDesktopConfig() {
const platform = process.platform;
let configPath;
if (platform === "win32") {
configPath = path.join(process.env.APPDATA, "Claude", "claude_desktop_config.json");
} else if (platform === "darwin") {
configPath = path.join(process.env.HOME, "Library", "Application Support", "Claude", "claude_desktop_config.json");
} else {
configPath = path.join(process.env.HOME, ".config", "Claude", "claude_desktop_config.json");
}
this.log('info', `Checking Claude Desktop config: ${configPath}`);
try {
const configContent = await fs.readFile(configPath, 'utf8');
const config = JSON.parse(configContent);
this.log('success', 'Claude Desktop config found and valid');
if (config.mcpServers) {
const serverNames = Object.keys(config.mcpServers);
this.log('info', `Configured MCP servers: ${serverNames.join(', ')}`);
if (config.mcpServers['linux-bash']) {
const linuxBashConfig = config.mcpServers['linux-bash'];
this.log('success', 'linux-bash MCP server found in config');
this.log('info', 'Server command:', linuxBashConfig.command);
this.log('info', 'Server args:', linuxBashConfig.args?.join(' ') || 'None');
if (linuxBashConfig.env && linuxBashConfig.env.WSL_DISTRIBUTION) {
this.log('info', `WSL_DISTRIBUTION env var: ${linuxBashConfig.env.WSL_DISTRIBUTION}`);
} else {
this.log('warning', 'No WSL_DISTRIBUTION environment variable set');
}
// Check if the server file path exists
if (linuxBashConfig.args && linuxBashConfig.args.length > 0) {
const serverPath = linuxBashConfig.args[0];
try {
await fs.stat(serverPath);
this.log('success', 'Server file path exists', serverPath);
} catch (error) {
this.log('error', 'Server file path does not exist', serverPath);
}
}
} else {
this.log('warning', 'linux-bash MCP server not found in config');
}
} else {
this.log('warning', 'No MCP servers configured');
}
} catch (error) {
this.log('error', 'Claude Desktop config issue', error.message);
}
}
async runDiagnostics() {
console.log("π Linux Bash MCP Server Diagnostics\n");
console.log("=" .repeat(50));
await this.checkNodeVersion();
console.log("");
await this.checkProjectStructure();
console.log("");
await this.checkDependencies();
console.log("");
const distributions = await this.checkWSLAvailability();
console.log("");
if (distributions) {
await this.testWSLConnection(distributions);
console.log("");
}
await this.checkConfiguration();
console.log("");
await this.testMCPServerImports();
console.log("");
await this.testServerStartup();
console.log("");
await this.checkClaudeDesktopConfig();
console.log("");
// Summary
console.log("=" .repeat(50));
console.log("π DIAGNOSTIC SUMMARY");
console.log("=" .repeat(50));
console.log(`β
Successful checks: ${this.success.length}`);
console.log(`β οΈ Warnings: ${this.warnings.length}`);
console.log(`β Errors: ${this.issues.length}`);
console.log("");
if (this.issues.length > 0) {
console.log("π¨ CRITICAL ISSUES TO FIX:");
this.issues.forEach((issue, index) => {
console.log(`${index + 1}. ${issue.message}`);
if (issue.details) console.log(` ${issue.details}`);
});
console.log("");
}
if (this.warnings.length > 0) {
console.log("β οΈ WARNINGS TO REVIEW:");
this.warnings.forEach((warning, index) => {
console.log(`${index + 1}. ${warning.message}`);
if (warning.details) console.log(` ${warning.details}`);
});
console.log("");
}
// Recommendations
console.log("π‘ RECOMMENDATIONS:");
if (this.issues.length === 0 && this.warnings.length === 0) {
console.log("β
All checks passed! The MCP server should be working correctly.");
console.log(" If it's still not working, try restarting Claude Desktop.");
} else {
console.log("1. Fix all critical issues first");
console.log("2. Review and address warnings");
console.log("3. Run diagnostics again to verify fixes");
console.log("4. Restart Claude Desktop after making changes");
}
console.log("\nπ For more help, see README.md or run 'npm run setup'");
}
}
// Run diagnostics
const debugger = new MCPServerDebugger();
debugger.runDiagnostics().catch(error => {
console.error("β Diagnostics failed:", error);
process.exit(1);
});