okx-mcp-server
Version:
Model Context Protocol (MCP) server for gasless DEX swapping powered by OKX DEX API + Biconomy MEE
63 lines (50 loc) • 1.92 kB
JavaScript
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import { spawn } from 'child_process';
import { createRequire } from 'module';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);
// Parse command line arguments
const args = process.argv.slice(2);
const httpMode = args.includes('--http') || args.includes('-h');
// Users need to provide both PRIVATE_KEY and OKX credentials
const requiredEnvVars = ['PRIVATE_KEY', 'OKX_API_KEY', 'OKX_SECRET_KEY', 'OKX_PASSPHRASE'];
const missingVars = requiredEnvVars.filter(varName => !process.env[varName]);
if (missingVars.length > 0) {
console.error(`❌ Missing required environment variables: ${missingVars.join(', ')}`);
console.error('Please set these in your MCP configuration.');
process.exit(1);
}
// Remove console.log that interferes with MCP JSON protocol
// console.log(`Starting OKX MCP Server in ${httpMode ? 'HTTP' : 'stdio'} mode...`);
// Determine which file to execute
const scriptPath = resolve(__dirname, '../build', httpMode ? 'http-server.js' : 'index.js');
try {
// Check if the built files exist
require.resolve(scriptPath);
// Execute the server
const server = spawn('node', [scriptPath], {
stdio: 'inherit',
shell: false
});
server.on('error', (err) => {
console.error('Failed to start server:', err);
process.exit(1);
});
// Handle clean shutdown
const cleanup = () => {
if (!server.killed) {
server.kill();
}
};
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
process.on('exit', cleanup);
} catch (error) {
console.error('Error: Server files not found. The package may not be built correctly.');
console.error('Please try reinstalling the package or contact the maintainers.');
console.error(error);
process.exit(1);
}