UNPKG

@devinrcai/mcp-image-generator

Version:

MCP server for generating images using ModelScope API - works with Claude Desktop and other MCP clients

190 lines (174 loc) • 5.6 kB
#!/usr/bin/env node import fs from 'fs/promises'; import path from 'path'; import { exec } from 'child_process'; import { promisify } from 'util'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; const execAsync = promisify(exec); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const PACKAGE_NAME = 'mcp-image-generator'; async function getPackageInfo() { try { const packageJsonPath = path.join(__dirname, '..', 'package.json'); const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')); return packageJson; } catch (error) { return { name: PACKAGE_NAME, version: '1.0.0' }; } } async function showHelp() { const pkg = await getPackageInfo(); console.log(` šŸŽØ ${pkg.name} v${pkg.version} MCP server for generating images using ModelScope API Usage: npx ${pkg.name} [options] Options: --help, -h Show this help message --version, -v Show version --setup Generate Claude Desktop configuration --start Start the MCP server directly --config Show configuration examples Examples: npx ${pkg.name} --setup # Generate Claude Desktop config npx ${pkg.name} --start # Start MCP server npx ${pkg.name} --config # Show all configuration options For more information, visit: https://github.com/your-username/mcp-image-generator `); } async function showVersion() { const pkg = await getPackageInfo(); console.log(`${pkg.version}`); } async function generateClaudeConfig() { try { const { stdout } = await execAsync('npm root -g'); const globalNodeModules = stdout.trim(); const serverPath = path.join(globalNodeModules, PACKAGE_NAME, 'dist', 'index.js'); const config = { mcpServers: { "image-generator": { command: "node", args: [serverPath], env: { NODE_ENV: "production" } } } }; console.log(` šŸ”§ Claude Desktop Configuration Add this to your Claude Desktop config file: Location: ~/Library/Application Support/Claude/claude_desktop_config.json ${JSON.stringify(config, null, 2)} šŸš€ Quick Setup Commands: 1. Create config directory (if not exists): mkdir -p "~/Library/Application Support/Claude" 2. Add to your Claude config: echo '${JSON.stringify(config, null, 2)}' > "~/Library/Application Support/Claude/claude_desktop_config.json" 3. Restart Claude Desktop šŸ“‹ Available Tools after setup: - set-api-key: Set your ModelScope API key - generate-image: Generate images from text - generate-placeholder-html: Create HTML placeholders - Resource: images://generated (view all generated images) `); } catch (error) { console.error('Error generating config:', error); } } async function showConfig() { console.log(` šŸ“– Configuration Examples 1. šŸ–„ļø Claude Desktop (Recommended): npx ${PACKAGE_NAME} --setup 2. šŸ“ Manual Configuration: Add to your MCP client config: { "mcpServers": { "image-generator": { "command": "npx", "args": ["${PACKAGE_NAME}", "--start"] } } } 3. 🐳 Docker (Advanced): docker run -v $(pwd)/images:/app/generated-images \\ your-registry/${PACKAGE_NAME}:latest 4. šŸ”§ Development Mode: git clone https://github.com/your-username/mcp-image-generator cd mcp-image-generator npm install && npm run build npm run start šŸ”‘ Don't forget to get your ModelScope API key: 1. Visit: https://modelscope.cn 2. Register/Login 3. Get API Token from dashboard 4. Use set-api-key tool in your MCP client `); } async function startServer() { console.log('šŸš€ Starting MCP Image Generator Server...'); // Import and start the MCP server try { const serverPath = path.join(__dirname, 'index.js'); const { spawn } = await import('child_process'); const serverProcess = spawn('node', [serverPath], { stdio: 'inherit' }); serverProcess.on('error', (error) => { console.error('Failed to start server:', error); process.exit(1); }); serverProcess.on('exit', (code) => { console.log(`Server exited with code ${code}`); process.exit(code || 0); }); // Handle graceful shutdown process.on('SIGINT', () => { console.log('\nšŸ‘‹ Shutting down MCP server...'); serverProcess.kill('SIGINT'); }); process.on('SIGTERM', () => { serverProcess.kill('SIGTERM'); }); } catch (error) { console.error('Error starting server:', error); process.exit(1); } } async function main() { const args = process.argv.slice(2); if (args.length === 0 || args.includes('--help') || args.includes('-h')) { await showHelp(); return; } if (args.includes('--version') || args.includes('-v')) { await showVersion(); return; } if (args.includes('--setup')) { await generateClaudeConfig(); return; } if (args.includes('--config')) { await showConfig(); return; } if (args.includes('--start')) { await startServer(); return; } // Default action await showHelp(); } main().catch((error) => { console.error('CLI Error:', error); process.exit(1); }); //# sourceMappingURL=cli.js.map