UNPKG

@brianveltman/sonatype-mcp

Version:

Model Context Protocol server for Sonatype Nexus Repository Manager

157 lines 6.04 kB
#!/usr/bin/env node console.error('=== MCP Server Starting ==='); console.error('process.argv:', process.argv); console.error('process.cwd():', process.cwd()); console.error('import.meta.url:', import.meta.url); process.on('uncaughtException', (error) => { console.error('Uncaught Exception during startup:', error.message); if (error.message.includes('Cannot find package')) { console.error(''); console.error('ERROR: Missing dependencies. If running via npx, this may be a temporary issue.'); console.error('Please ensure all dependencies are installed correctly.'); console.error('Try running: npm install -g @brianveltman/sonatype-mcp'); console.error(''); } process.exit(1); }); process.on('unhandledRejection', (reason) => { console.error('Unhandled Rejection during startup:', reason); process.exit(1); }); import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js'; import { createConfig, displayUsage } from './config/environment.js'; import { NexusClient } from './services/nexus-client.js'; import { createTools, getToolByName } from './tools/index.js'; import { formatMCPError } from './utils/errors.js'; class NexusMCPServer { server; nexusClient = null; tools = []; config = null; constructor() { this.server = new Server({ name: 'mcp-sonatype', version: '1.4.0', }, { capabilities: { tools: {}, }, }); } initializeWithConfig() { try { this.config = createConfig(); this.nexusClient = new NexusClient(this.config); this.tools = createTools(this.nexusClient, this.config); this.setupHandlers(); } catch (error) { console.error('Failed to initialize configuration:', error); throw error; } } setupHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: this.tools.map(tool => ({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema, })), }; }); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; const tool = getToolByName(this.tools, name); if (!tool) { throw new Error(`Tool not found: ${name}`); } try { console.error(`Executing tool: ${name}`); const result = await tool.handler(args || {}); console.error(`Tool execution completed: ${name}`); return result; } catch (error) { console.error(`Tool execution failed: ${name}`, error); const mcpError = formatMCPError(error); return { content: [ { type: 'text', text: `Error executing ${name}: ${mcpError.message}` } ], isError: true }; } }); } async start() { try { if (process.argv.includes('--help') || process.argv.includes('-h')) { displayUsage(); process.exit(0); } console.error('Starting Nexus MCP Server...'); this.initializeWithConfig(); if (!this.config || !this.nexusClient) { throw new Error('Failed to initialize server configuration'); } console.error(`Server: ${this.config.server.name} v${this.config.server.version}`); console.error(`Nexus URL: ${this.config.nexus.baseUrl}`); console.error(`Username: ${this.config.nexus.username}`); console.error(`Read-only mode: ${this.config.server.readOnly}`); console.error(`Available tools: ${this.tools.length}`); console.error('Testing connection to Nexus...'); try { const isConnected = await this.nexusClient.testConnection(); if (!isConnected) { console.error('Warning: Could not connect to Nexus server'); } else { console.error('Successfully connected to Nexus'); } } catch (error) { console.error('Warning: Could not test connection to Nexus:', error); } const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('Nexus MCP Server started successfully'); } catch (error) { console.error('Failed to start server:', error); throw error; } } async stop() { try { console.error('Stopping Nexus MCP Server...'); await this.server.close(); console.error('Server stopped'); } catch (error) { console.error('Error stopping server:', error); } } } process.on('SIGINT', async () => { console.error('\nReceived SIGINT, shutting down gracefully...'); process.exit(0); }); process.on('SIGTERM', async () => { console.error('\nReceived SIGTERM, shutting down gracefully...'); process.exit(0); }); console.error('Module loaded, starting server...'); console.error('process.argv:', process.argv); console.error('import.meta.url:', import.meta.url); const server = new NexusMCPServer(); server.start().catch((error) => { console.error('Failed to start server:', error); process.exit(1); }); //# sourceMappingURL=index.js.map