@kinetixarts/craft-it-mcp-server
Version:
Craft IT - Model Context Protocol (MCP) compliant Server for AI-Powered Asset Generation using Gemini
103 lines • 4.33 kB
JavaScript
import startServer from "./server/server.js";
import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
import dotenv from 'dotenv';
// Get the current file's directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Check for a local .env file
const localEnvPath = path.join(process.cwd(), '.env');
const packageEnvPath = path.join(__dirname, '../.env');
// Try to load environment variables from various locations
if (fs.existsSync(localEnvPath)) {
dotenv.config({ path: localEnvPath });
if (!process.argv.includes('--stdio')) {
console.error(`Using .env file from current directory: ${localEnvPath}`);
}
}
else if (fs.existsSync(packageEnvPath)) {
dotenv.config({ path: packageEnvPath });
if (!process.argv.includes('--stdio')) {
console.error(`Using .env file from package directory: ${packageEnvPath}`);
}
}
else {
if (!process.argv.includes('--stdio')) {
console.error('No .env file found. Please set GEMINI_API_KEY as an environment variable.');
}
}
// Check for the API key and/or set a default for development
if (!process.env.GEMINI_API_KEY) {
// In development or when running in Cursor, use a placeholder API key
if (process.env.NODE_ENV === 'development' || process.env.RUNNING_IN_CURSOR) {
process.env.GEMINI_API_KEY = 'development_key';
}
else {
if (!process.argv.includes('--stdio')) {
console.error('\x1b[31m%s\x1b[0m', 'Error: GEMINI_API_KEY is not set!');
console.error('Please provide your Gemini API key using one of these methods:');
console.error('1. Create a .env file in the current directory with GEMINI_API_KEY=your_key');
console.error('2. Set the GEMINI_API_KEY environment variable before running the command');
console.error('\nTo get a Gemini API key, visit: https://makersuite.google.com/app/apikey\n');
}
process.exit(1);
}
}
// Print a welcome message only when not running in stdio mode
if (!process.argv.includes('--stdio')) {
console.error('\x1b[36m%s\x1b[0m', '🚀 Starting Craft-IT MCP Asset Generator Server');
}
// Start the server
async function main() {
try {
const server = await startServer();
const PORT = Number(process.env.PORT) || 3001;
const HOST = process.env.HOST || '0.0.0.0';
const TRANSPORT_TYPE = process.argv.includes('--stdio') ? 'stdio' : 'httpStream';
if (TRANSPORT_TYPE === 'stdio') {
server.start({
transportType: "stdio",
});
if (!process.argv.includes('--stdio')) {
console.error('\x1b[32m%s\x1b[0m', 'MCP Server running in stdio mode');
console.error('\x1b[33m%s\x1b[0m', 'This mode is designed for direct integration with AI assistants');
}
}
else {
server.start({
transportType: "httpStream",
httpStream: {
port: PORT,
endpoint: "/stream",
}
});
if (!process.argv.includes('--stdio')) {
console.error('\x1b[32m%s\x1b[0m', `MCP Server running at http://${HOST}:${PORT}`);
console.error('\x1b[33m%s\x1b[0m', `HTTP Stream endpoint: http://${HOST}:${PORT}/stream`);
console.error('');
console.error('\x1b[36m%s\x1b[0m', 'To connect, use the URL above in your MCP client configuration');
console.error('');
console.error('To use stdio mode instead, run with --stdio flag:');
console.error('\x1b[33m%s\x1b[0m', 'npx @modelcontextprotocol/server-craft-it --stdio');
}
}
}
catch (error) {
console.error('\x1b[31m%s\x1b[0m', "Error starting MCP server:", error);
process.exit(1);
}
}
// Handle ctrl+c gracefully
process.on('SIGINT', () => {
if (!process.argv.includes('--stdio')) {
console.error('\n\x1b[33m%s\x1b[0m', 'Shutting down Craft-IT MCP server...');
}
process.exit(0);
});
main().catch((error) => {
console.error('\x1b[31m%s\x1b[0m', "Fatal error in main():", error);
process.exit(1);
});
//# sourceMappingURL=index.js.map