#!/usr/bin/env node
/**
* GIT MCP Server - Entry Point
*
* Professional MCP server for Git operations with multi-provider support.
* Supports GitHub and Gitea providers with unified interface.
*
* Usage:
* - Local development: node dist/index.js
* - NPM package: npx @andrebuzeli/git-mcp@latest
* - Direct execution: git-mcp (after npm install -g)
*
* Environment Variables:
* - GITHUB_TOKEN: GitHub personal access token
* - GITHUB_USERNAME: GitHub username
* - GITEA_URL: Gitea instance URL
* - GITEA_TOKEN: Gitea access token
* - GITEA_USERNAME: Gitea username
*/
import { GitMCPServer } from './server.js';
import { getProviderConfig } from './config.js';
/**
* Validate Node.js version compatibility
*/
function validateNodeVersion(): void {
const nodeVersion = process.version;
const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0]);
if (majorVersion < 18) {
console.error(`Error: Node.js version ${nodeVersion} is not supported.`);
console.error('GIT MCP Server requires Node.js 18.0.0 or higher.');
console.error('Please upgrade your Node.js installation.');
process.exit(1);
}
}
/**
* Display startup information and configuration status
*/
function displayStartupInfo(): void {
console.error('='.repeat(60));
console.error('π GIT MCP Server v1.0.0');
console.error('Professional MCP server for Git operations');
console.error('='.repeat(60));
// Display provider configuration status
const config = getProviderConfig();
const githubConfigured = !!config.github?.token;
const giteaConfigured = !!config.gitea?.token;
console.error('\nπ Provider Configuration:');
console.error(` GitHub: ${githubConfigured ? 'β
Configured' : 'β Not configured'}`);
if (githubConfigured && config.github?.username) {
console.error(` Username: ${config.github.username}`);
}
console.error(` Gitea: ${giteaConfigured ? 'β
Configured' : 'β Not configured'}`);
if (giteaConfigured && config.gitea?.url) {
console.error(` URL: ${config.gitea.url}`);
if (config.gitea?.username) {
console.error(` Username: ${config.gitea.username}`);
}
}
if (!githubConfigured && !giteaConfigured) {
console.error('\nβ οΈ Warning: No providers configured!');
console.error(' Remote operations will not be available.');
console.error(' Please set environment variables for at least one provider.');
console.error('\n GitHub Configuration:');
console.error(' export GITHUB_TOKEN="your_github_token"');
console.error(' export GITHUB_USERNAME="your_github_username"');
console.error('\n Gitea Configuration:');
console.error(' export GITEA_URL="https://your-gitea-instance.com"');
console.error(' export GITEA_TOKEN="your_gitea_token"');
console.error(' export GITEA_USERNAME="your_gitea_username"');
}
console.error('\nπ§ Available Tools: 18 Git tools ready');
console.error('π Resources: Server status and tools listing');
console.error('');
}
/**
* Handle graceful shutdown
*/
function setupGracefulShutdown(): void {
const shutdown = (signal: string) => {
console.error(`\nπ΄ Received ${signal}. Shutting down GIT MCP Server gracefully...`);
process.exit(0);
};
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('π₯ Uncaught Exception:', error);
console.error('Stack trace:', error.stack);
process.exit(1);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('π₯ Unhandled Promise Rejection at:', promise);
console.error('Reason:', reason);
process.exit(1);
});
}
/**
* Main server startup function
*/
async function main(): Promise<void> {
try {
// Validate environment
validateNodeVersion();
// Setup graceful shutdown handlers
setupGracefulShutdown();
// Display startup information
displayStartupInfo();
// Initialize and start server
console.error('π Initializing GIT MCP Server...');
const server = new GitMCPServer();
console.error('π Starting MCP server transport...');
await server.start();
// Server is now running and will handle MCP protocol communication
// The process will continue running until terminated
} catch (error) {
console.error('\nπ₯ Failed to start GIT MCP Server:');
if (error instanceof Error) {
console.error(` Error: ${error.message}`);
if (error.stack) {
console.error(` Stack: ${error.stack}`);
}
} else {
console.error(` Unknown error: ${error}`);
}
console.error('\nπ Troubleshooting:');
console.error(' 1. Check that Node.js 18+ is installed');
console.error(' 2. Verify environment variables are set correctly');
console.error(' 3. Ensure network connectivity for provider APIs');
console.error(' 4. Check that the MCP client is configured properly');
process.exit(1);
}
}
// Only run if this file is executed directly (not imported)
if (require.main === module) {
main().catch((error) => {
console.error('\nπ₯ Unhandled error in main process:');
console.error(error);
process.exit(1);
});
}
// Export for programmatic usage
export { GitMCPServer } from './server.js';
export { getProviderConfig } from './config.js';