google-mcp-server
Version:
Google Suite MCP Server for Cursor and other MCP clients
167 lines (134 loc) • 4.8 kB
JavaScript
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// Get the directory where this script is located
const scriptDir = __dirname;
const packageDir = path.dirname(scriptDir);
// Function to find Python executable
function findPython() {
const pythonCommands = ['python3', 'python', 'py'];
for (const cmd of pythonCommands) {
try {
const result = require('child_process').execSync(`${cmd} --version`, { encoding: 'utf8' });
if (result) {
return cmd;
}
} catch (error) {
// Continue to next command
}
}
throw new Error('Python not found. Please install Python 3.7+ and ensure it\'s in your PATH.');
}
// Function to check if setup is needed
function checkSetup() {
const credentialsPath = path.join(packageDir, 'credentials.json');
const tokenPath = path.join(packageDir, 'google_shared_token.json');
const missingFiles = [];
if (!fs.existsSync(credentialsPath)) {
missingFiles.push('credentials.json');
}
if (!fs.existsSync(tokenPath)) {
missingFiles.push('google_shared_token.json');
}
return missingFiles;
}
// Function to run setup
function runSetup() {
console.log('Setting up Google MCP Server...');
const python = findPython();
const setupScript = path.join(packageDir, 'setup_unified_auth.py');
if (!fs.existsSync(setupScript)) {
console.error('Setup script not found. Please ensure the package is properly installed.');
process.exit(1);
}
const setupProcess = spawn(python, [setupScript], {
stdio: 'inherit',
cwd: packageDir
});
setupProcess.on('close', (code) => {
if (code === 0) {
console.log('Setup completed successfully!');
startServer();
} else {
console.error(`Setup failed with code ${code}`);
process.exit(code);
}
});
}
// Function to start the MCP server
function startServer() {
console.log('Starting Google MCP Server...');
const python = findPython();
const serverScript = path.join(packageDir, 'start_mcp_server.py');
if (!fs.existsSync(serverScript)) {
console.error('Server script not found. Please ensure the package is properly installed.');
process.exit(1);
}
const serverProcess = spawn(python, [serverScript], {
stdio: 'inherit',
cwd: packageDir
});
serverProcess.on('close', (code) => {
if (code !== 0) {
console.error(`Server exited with code ${code}`);
}
process.exit(code);
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\nShutting down server...');
serverProcess.kill('SIGINT');
});
process.on('SIGTERM', () => {
console.log('\nShutting down server...');
serverProcess.kill('SIGTERM');
});
}
// Main function
function main() {
const args = process.argv.slice(2);
// Check for help flag
if (args.includes('--help') || args.includes('-h')) {
console.log(`
Google MCP Server
Usage:
npx google-mcp-server [options]
Options:
--setup, -s Run setup (download credentials and authenticate)
--help, -h Show this help message
Examples:
npx google-mcp-server --setup # Run setup first time
npx google-mcp-server # Start the server
For more information, visit: https://github.com/yourusername/google-mcp-server
`);
return;
}
// Check for setup flag
if (args.includes('--setup') || args.includes('-s')) {
runSetup();
return;
}
// Check if setup is needed
const missingFiles = checkSetup();
if (missingFiles.length > 0) {
console.log('Google MCP Server Setup Required');
console.log('================================');
console.log('');
console.log('Missing files:', missingFiles.join(', '));
console.log('');
console.log('To set up the server, run:');
console.log(' npx google-mcp-server --setup');
console.log('');
console.log('This will:');
console.log(' 1. Download credentials from Google Cloud Console');
console.log(' 2. Authenticate with Google services');
console.log(' 3. Create necessary token files');
console.log('');
process.exit(1);
}
// Start the server
startServer();
}
// Run main function
main();