snigop
Version:
Snigop - A sniffer gopher CLI tool
152 lines • 6.38 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MCPServer = void 0;
const child_process_1 = require("child_process");
const process = __importStar(require("process"));
class MCPServer {
constructor() {
this.process = null;
this.start = async () => {
console.log('Starting Playwright MCP server...');
this.process = (0, child_process_1.spawn)('npx', ['@playwright/mcp@latest'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd()
});
return new Promise((resolve, reject) => {
if (!this.process) {
reject(new Error('Failed to create MCP process'));
return;
}
let hasResolved = false;
this.process.stdout?.on('data', (data) => {
const output = data.toString();
console.log(`MCP Server stdout: ${output}`);
// MCP servers often start without explicit "ready" messages
if (!hasResolved) {
hasResolved = true;
setTimeout(() => resolve(this.process), 2000); // Give it 2 seconds to fully start
}
});
this.process.stderr?.on('data', (data) => {
console.log(`MCP Server stderr: ${data}`);
// npm warnings are normal, don't treat as errors
if (!hasResolved && data.toString().includes('installed')) {
hasResolved = true;
setTimeout(() => resolve(this.process), 3000); // Give it 3 seconds after installation
}
});
this.process.on('error', (error) => {
if (!hasResolved) {
reject(new Error(`Failed to start MCP server: ${error.message}`));
}
});
// Fallback: assume server is ready after 5 seconds
setTimeout(() => {
if (!hasResolved) {
hasResolved = true;
resolve(this.process);
}
}, 5000);
});
};
this.connectAndListTools = async () => {
if (!this.process) {
throw new Error('MCP server not started');
}
console.log('Connecting to MCP server...');
// Send tools/list request to MCP server
const request = {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
};
this.process.stdin?.write(JSON.stringify(request) + '\n');
return new Promise((resolve, reject) => {
if (!this.process) {
reject(new Error('MCP server not available'));
return;
}
let responseData = '';
const onData = (data) => {
responseData += data.toString();
try {
const lines = responseData.split('\n').filter(line => line.trim());
for (const line of lines) {
const response = JSON.parse(line);
if (response.id === 1 && response.result) {
console.log('Available tools:');
response.result.tools.forEach((tool) => {
console.log(`- ${tool.name}: ${tool.description}`);
});
this.process.stdout?.off('data', onData);
resolve();
return;
}
}
}
catch {
// Continue reading if JSON is incomplete
}
};
this.process.stdout?.on('data', onData);
setTimeout(() => {
if (this.process) {
this.process.stdout?.off('data', onData);
}
reject(new Error('Timeout waiting for tools list response'));
}, 5000);
});
};
this.shutdown = () => {
if (!this.process) {
return;
}
console.log('Shutting down MCP server...');
if (!this.process.killed) {
this.process.kill('SIGTERM');
setTimeout(() => {
if (this.process && !this.process.killed) {
this.process.kill('SIGKILL');
}
}, 3000);
}
this.process = null;
};
}
}
exports.MCPServer = MCPServer;
//# sourceMappingURL=mcpServer.js.map