@qwst_desenvolvimento/mcp-connector-http-apikey
Version:
MCP connector for HTTP servers with API key authentication. Bridges MCP clients to HTTP-based MCP servers using x-api-key header authentication.
99 lines (82 loc) ⢠2.65 kB
JavaScript
/**
* Example usage of MCP Connector HTTP API Key
* This example shows how to set up and use the connector
*/
const { spawn } = require('child_process');
const path = require('path');
// Example server configurations
const examples = {
// Example 1: Local MCP server
local: {
MCP_SERVER_URL: 'http://localhost:3000/mcp',
MCP_API_KEY: 'your-local-api-key',
MCP_DEBUG: 'true',
},
// Example 2: Remote MCP server
remote: {
MCP_SERVER_URL: 'https://api.example.com/mcp',
MCP_API_KEY: 'your-remote-api-key',
MCP_DEBUG: 'false',
},
// Example 3: Custom configuration
custom: {
MCP_SERVER_URL: 'http://my-server.com:8080/mcp',
MCP_API_KEY: 'custom-api-key',
MCP_DEBUG: 'false',
MCP_SHUTDOWN_TIMEOUT: '15000',
},
};
function runExample(exampleName) {
const config = examples[exampleName];
if (!config) {
console.error(`ā Example "${exampleName}" not found`);
console.log('Available examples:', Object.keys(examples).join(', '));
process.exit(1);
}
console.log(`š Running example: ${exampleName}`);
console.log(`š” Server URL: ${config.MCP_SERVER_URL}`);
console.log(`š API Key: ${config.MCP_API_KEY.substring(0, 8)}...`);
// Set environment variables
const env = { ...process.env, ...config };
// Spawn the connector
const connector = spawn('node', [path.join(__dirname, 'mcp-connector-http-apikey.js')], {
env,
stdio: 'inherit',
});
connector.on('error', (error) => {
console.error(`ā Failed to start connector: ${error.message}`);
process.exit(1);
});
connector.on('exit', (code) => {
console.log(`š Connector exited with code ${code}`);
process.exit(code);
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\nš Shutting down gracefully...');
connector.kill('SIGINT');
});
}
// Handle command line arguments
const exampleName = process.argv[2];
if (!exampleName) {
console.log('š Generic MCP Connector - Usage Examples');
console.log('');
console.log('Usage: node example.js <example-name>');
console.log('');
console.log('Available examples:');
console.log(' local - Connect to local MCP server');
console.log(' remote - Connect to remote MCP server');
console.log(' custom - Custom configuration example');
console.log('');
console.log('Example:');
console.log(' node example.js local');
console.log('');
console.log('You can also run the connector directly:');
console.log(' export MCP_SERVER_URL=http://localhost:3000/mcp');
console.log(' export MCP_API_KEY=your-api-key');
console.log(' node generic-mcp-connector.js');
process.exit(0);
}
runExample(exampleName);