ai-debug-local-mcp
Version:
🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh
180 lines • 5.36 kB
JavaScript
/**
* STDIO MCP Transport - V2 Native Implementation
*
* Direct stdio transport for V2 server, eliminating the need for bridges.
* Provides native MCP protocol support over stdin/stdout for Claude Code compatibility.
*/
import { EventEmitter } from 'events';
import * as readline from 'readline';
export class StdioMcpTransport extends EventEmitter {
config;
isRunning = false;
rl;
messageBuffer = '';
requestId = 0;
constructor(config = {}) {
super();
this.config = {
timeout: config.timeout || 30000,
bufferSize: config.bufferSize || 1024 * 1024, // 1MB
...config
};
}
/**
* Start the stdio transport
*/
async start() {
if (this.isRunning) {
return;
}
console.error('🔗 V2 STDIO MCP Transport starting...');
this.setupStdioHandling();
this.setupErrorHandling();
this.isRunning = true;
console.error('✅ V2 STDIO MCP Transport ready');
this.emit('ready');
}
/**
* Setup stdin/stdout handling for MCP protocol
*/
setupStdioHandling() {
// Create readline interface for line-by-line processing
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// Handle incoming MCP messages
this.rl.on('line', (line) => {
this.handleIncomingMessage(line.trim());
});
// Handle stdin end
this.rl.on('close', () => {
console.error('🔚 STDIO closed, shutting down');
this.stop();
});
// Set up stdout for responses (only if it's a real TTY stream)
if (process.stdout.setEncoding) {
process.stdout.setEncoding('utf8');
}
}
/**
* Handle incoming MCP message
*/
async handleIncomingMessage(message) {
if (!message) {
return;
}
try {
const request = JSON.parse(message);
console.error(`📨 MCP Request: ${request.method} (id: ${request.id})`);
// Emit the request for the server to handle
this.emit('request', request, (response) => {
this.sendResponse(response);
});
}
catch (error) {
console.error('❌ Failed to parse MCP message:', error);
// Send JSON-RPC error response
const errorResponse = {
jsonrpc: '2.0',
id: null,
error: {
code: -32700,
message: 'Parse error',
data: error.message
}
};
this.sendResponse(errorResponse);
}
}
/**
* Send response back via stdout
*/
sendResponse(response) {
try {
const responseStr = JSON.stringify(response);
process.stdout.write(responseStr + '\n');
console.error(`📤 MCP Response sent (id: ${response.id})`);
}
catch (error) {
console.error('❌ Failed to send response:', error);
}
}
/**
* Setup error handling
*/
setupErrorHandling() {
process.stdin.on('error', (error) => {
console.error('❌ STDIN error:', error);
this.emit('error', error);
});
process.stdout.on('error', (error) => {
console.error('❌ STDOUT error:', error);
this.emit('error', error);
});
// Handle process signals
process.on('SIGINT', () => {
console.error('🛑 SIGINT received, shutting down gracefully');
this.stop();
});
process.on('SIGTERM', () => {
console.error('🛑 SIGTERM received, shutting down gracefully');
this.stop();
});
}
/**
* Send notification (no response expected)
*/
sendNotification(method, params) {
const notification = {
jsonrpc: '2.0',
method,
params
};
try {
const notificationStr = JSON.stringify(notification);
process.stdout.write(notificationStr + '\n');
console.error(`📢 MCP Notification sent: ${method}`);
}
catch (error) {
console.error('❌ Failed to send notification:', error);
}
}
/**
* Check if transport is running
*/
isActive() {
return this.isRunning;
}
/**
* Stop the transport
*/
async stop() {
if (!this.isRunning) {
return;
}
console.error('🛑 V2 STDIO MCP Transport stopping...');
this.isRunning = false;
if (this.rl) {
this.rl.close();
this.rl = undefined;
}
this.emit('stopped');
console.error('✅ V2 STDIO MCP Transport stopped');
// Exit process gracefully
process.exit(0);
}
/**
* Get transport statistics
*/
getStats() {
return {
isRunning: this.isRunning,
bufferSize: this.config.bufferSize,
timeout: this.config.timeout,
uptime: process.uptime()
};
}
}
//# sourceMappingURL=stdio-mcp-transport.js.map