dddvchang-mcp-proxy
Version:
Smart MCP proxy with automatic JetBrains IDE discovery, WebSocket support, and intelligent connection naming
117 lines (116 loc) ⢠3.8 kB
JavaScript
import * as http from 'http';
/**
* Check if a port has MCP service running
*/
async function checkMCPPort(port) {
return new Promise((resolve) => {
const options = {
hostname: '127.0.0.1',
port: port,
path: '/api/mcp',
method: 'GET',
timeout: 1000
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
if (res.statusCode === 200) {
try {
const info = JSON.parse(data);
resolve({
port,
ideName: info.ideName || `JetBrains IDE`,
projectPath: info.projectPath,
isActive: true
});
}
catch {
resolve({
port,
ideName: `JetBrains IDE`,
isActive: true
});
}
}
else {
resolve(null);
}
});
});
req.on('error', () => resolve(null));
req.on('timeout', () => {
req.destroy();
resolve(null);
});
req.end();
});
}
/**
* Scan ports for MCP services
*/
async function scanMCPServers(startPort = 63320, endPort = 63340) {
console.log(`š Scanning ports ${startPort}-${endPort} for MCP servers...\n`);
const foundServers = [];
const promises = [];
for (let port = startPort; port <= endPort; port++) {
promises.push(checkMCPPort(port));
}
const results = await Promise.all(promises);
for (const result of results) {
if (result) {
foundServers.push(result);
}
}
return foundServers;
}
/**
* Generate claude mcp add commands
*/
function generateClaudeCommands(servers) {
const commands = [];
servers.forEach((server, index) => {
const name = `jetbrains-${server.port}`;
const command = `claude mcp add proxy --name "${name}" --proxy-command "npx" --proxy-args "@dddvchang/mcp-proxy" --proxy-env "MCP_SERVER_URL=http://127.0.0.1:${server.port}/api/mcp"`;
commands.push(command);
});
return commands;
}
/**
* Main function
*/
export async function runMCPFind() {
try {
const servers = await scanMCPServers();
if (servers.length === 0) {
console.log('ā No MCP servers found.');
console.log('\nš” Make sure:');
console.log(' 1. JetBrains IDE is running');
console.log(' 2. MCP Server Plugin is installed and enabled');
console.log(' 3. A project is open in the IDE');
return;
}
console.log(`ā
Found ${servers.length} MCP server(s):\n`);
servers.forEach((server) => {
console.log(`š Port ${server.port}: ${server.ideName}`);
if (server.projectPath) {
console.log(` Project: ${server.projectPath}`);
}
console.log('');
});
console.log('š To add these servers to Claude, run the following commands:\n');
const commands = generateClaudeCommands(servers);
commands.forEach((cmd) => {
console.log(cmd);
console.log('');
});
console.log('š” Or run all at once:');
console.log('');
console.log(commands.join(' && '));
}
catch (error) {
console.error('ā Error scanning for MCP servers:', error);
process.exit(1);
}
}