#!/usr/bin/env node
/**
* Setup script to configure Cursor with the GitHub MCP Server
*/
import { config } from "dotenv";
import { existsSync, writeFileSync, mkdirSync, readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { homedir } from "os";
// Load environment variables
config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log("π§ Setting up GitHub MCP Server for Cursor\n");
// Get the current project path
const projectPath = __dirname;
const serverPath = join(projectPath, "dist", "server.js");
console.log("π Project path:", projectPath);
console.log("π Server path:", serverPath);
// Check if server exists
if (!existsSync(serverPath)) {
console.error("β Server not found! Please run 'npm run build' first.");
process.exit(1);
}
// Check if .env exists
const envPath = join(projectPath, ".env");
if (!existsSync(envPath)) {
console.error("β .env file not found! Please run 'npm run setup' first.");
process.exit(1);
}
// Read .env file
const envContent = readFileSync(envPath, "utf8");
const githubToken = envContent.match(/GITHUB_TOKEN=(.+)/)?.[1];
const githubUsername = envContent.match(/GITHUB_USERNAME=(.+)/)?.[1];
if (!githubToken) {
console.error("β GITHUB_TOKEN not found in .env file!");
process.exit(1);
}
console.log("β
Found GitHub token and username");
// Cursor MCP configuration paths for different OS
const os = process.platform;
let cursorConfigPath;
switch (os) {
case "win32":
cursorConfigPath = join(
homedir(),
"AppData",
"Roaming",
"Cursor",
"User",
"globalStorage",
"cursor.mcp"
);
break;
case "darwin":
cursorConfigPath = join(
homedir(),
"Library",
"Application Support",
"Cursor",
"User",
"globalStorage",
"cursor.mcp"
);
break;
case "linux":
cursorConfigPath = join(
homedir(),
".config",
"Cursor",
"User",
"globalStorage",
"cursor.mcp"
);
break;
default:
console.error("β Unsupported operating system:", os);
process.exit(1);
}
console.log("π Cursor config directory:", cursorConfigPath);
// Create directory if it doesn't exist
if (!existsSync(cursorConfigPath)) {
console.log("π Creating Cursor MCP directory...");
mkdirSync(cursorConfigPath, { recursive: true });
}
const configFilePath = join(cursorConfigPath, "config.json");
// MCP configuration
const mcpConfig = {
mcpServers: {
"github-mcp": {
command: "node",
args: [serverPath],
cwd: projectPath,
env: {
GITHUB_TOKEN: githubToken,
...(githubUsername && { GITHUB_USERNAME: githubUsername }),
},
},
},
};
// Check if config file already exists
let existingConfig = {};
if (existsSync(configFilePath)) {
try {
const existingContent = readFileSync(configFilePath, "utf8");
existingConfig = JSON.parse(existingContent);
console.log("π Found existing Cursor MCP configuration");
} catch (error) {
console.log("β οΈ Existing config file is invalid, will overwrite");
}
}
// Merge configurations
const finalConfig = {
...existingConfig,
mcpServers: {
...existingConfig.mcpServers,
...mcpConfig.mcpServers,
},
};
// Write configuration
try {
writeFileSync(configFilePath, JSON.stringify(finalConfig, null, 2));
console.log("β
Cursor MCP configuration updated successfully!");
console.log("π Config file:", configFilePath);
} catch (error) {
console.error("β Failed to write configuration:", error.message);
process.exit(1);
}
console.log("\nπ Setup Complete!");
console.log("\nπ Configuration Summary:");
console.log("β’ Server: github-mcp");
console.log("β’ Command: node");
console.log("β’ Path:", serverPath);
console.log("β’ Working Directory:", projectPath);
console.log("β’ GitHub Token: β
Configured");
if (githubUsername) {
console.log("β’ GitHub Username: β
", githubUsername);
}
console.log("\nπ Next Steps:");
console.log("1. Restart Cursor IDE");
console.log("2. Open the MCP panel in Cursor");
console.log("3. You should see 'github-mcp' server listed");
console.log("4. Start using GitHub tools in Cursor!");
console.log("\nπ― Example Usage in Cursor:");
console.log("β’ Ask: 'Show me information about the VS Code repository'");
console.log("β’ Ask: 'What are the open issues in React?'");
console.log("β’ Ask: 'Get my GitHub statistics'");
console.log("β’ Ask: 'Search for TypeScript repositories'");
console.log("\nπ For more details, see cursor-setup.md");
console.log(
"π οΈ For troubleshooting, run 'npm run simple-test' to verify server works"
);