@olbrain/generic-api-mcp
Version:
Generic MCP server for wrapping any REST API with configuration-driven approach
67 lines (57 loc) • 2.12 kB
JavaScript
/**
* Generic API MCP Server Entry Point
* Spawns Python MCP server process for stdio communication
*/
const { spawn } = require('child_process');
const path = require('path');
// Path to Python server
const serverPath = path.join(__dirname, 'src', 'server.py');
// Find Python executable (try python3, then python)
function getPythonCommand() {
const isWindows = process.platform === 'win32';
return isWindows ? 'python' : 'python3';
}
// Validate required environment variables
const requiredEnvVars = ['API_BASE_URL', 'API_ENDPOINTS_CONFIG'];
const missingVars = requiredEnvVars.filter(v => !process.env[v]);
if (missingVars.length > 0) {
console.error('Error: Missing required environment variables:', missingVars.join(', '));
console.error('');
console.error('Required environment variables:');
console.error(' - API_BASE_URL: Base URL of your API (e.g., https://api.example.com)');
console.error(' - API_ENDPOINTS_CONFIG: Base64-encoded JSON array of endpoint configurations');
console.error('');
console.error('Optional environment variables (authentication):');
console.error(' - API_AUTH_TYPE: none|bearer|api_key|basic|oauth (default: none)');
console.error(' - BEARER_TOKEN: For bearer authentication');
console.error(' - API_KEY: For API key authentication');
console.error(' - API_KEY_HEADER: Header name for API key (default: X-API-Key)');
console.error(' - USERNAME, PASSWORD: For basic authentication');
console.error(' - CLIENT_ID, CLIENT_SECRET: For OAuth 2.0');
process.exit(1);
}
// Spawn Python MCP server
const pythonCmd = getPythonCommand();
const server = spawn(pythonCmd, [serverPath], {
stdio: ['inherit', 'inherit', 'inherit'],
env: {
...process.env,
PYTHONUNBUFFERED: '1'
}
});
// Handle server exit
server.on('exit', (code, signal) => {
if (code !== null) {
process.exit(code);
} else if (signal !== null) {
process.exit(1);
}
});
// Forward signals to Python process
process.on('SIGINT', () => {
server.kill('SIGINT');
});
process.on('SIGTERM', () => {
server.kill('SIGTERM');
});