UNPKG

@gala-chain/launchpad-mcp-server

Version:

MCP server for Gala Launchpad SDK with 55 tools + 14 slash commands - Production-grade AI agent integration with comprehensive validation, optimized performance, and 80%+ test coverage

211 lines 7.82 kB
"use strict"; /** * Gala Launchpad MCP Server * * Provides MCP tools for AI agents to interact with Gala Launchpad */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LaunchpadMCPServer = void 0; const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js"); const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js"); const types_js_1 = require("@modelcontextprotocol/sdk/types.js"); const launchpad_sdk_1 = require("@gala-chain/launchpad-sdk"); const index_js_2 = require("./tools/index.js"); const index_js_3 = require("./prompts/index.js"); class LaunchpadMCPServer { server; sdk = null; debug; constructor() { this.debug = process.env.DEBUG === 'true'; this.server = new index_js_1.Server({ name: '@gala-chain/launchpad-mcp-server', version: '1.5.0', }, { capabilities: { tools: {}, prompts: { listChanged: true, }, }, }); this.setupToolHandlers(); this.setupPromptHandlers(); this.setupErrorHandlers(); } /** * Initialize SDK with AgentConfig */ async initialize() { try { if (this.debug) { console.error('[MCP Server] Initializing SDK...'); } const { sdk, validation } = await launchpad_sdk_1.AgentConfig.quickSetup({ environment: process.env.ENVIRONMENT || 'development', privateKey: process.env.PRIVATE_KEY, timeout: parseInt(process.env.TIMEOUT || '30000', 10), debug: this.debug, autoValidate: false, config: { // Pass MySQL connection string if provided (for price history feature) ...(process.env.MYSQL_CONNECTION_STRING && { mysqlConnectionString: process.env.MYSQL_CONNECTION_STRING, }), }, }); this.sdk = sdk; if (this.debug) { console.error('[MCP Server] SDK initialized successfully'); if (validation) { console.error(`[MCP Server] Validation - Ready: ${validation.ready}`); console.error(`[MCP Server] Validation - Can Trade: ${validation.capabilities.canTrade}`); } } if (validation && !validation.ready) { console.error('[MCP Server] Warning: SDK not ready', validation.issues); } } catch (error) { console.error('[MCP Server] Failed to initialize SDK:', error); throw error; } } /** * Setup tool handlers */ setupToolHandlers() { // Register tools/list handler this.server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => { if (this.debug) { console.error(`[MCP Server] Listing ${index_js_2.tools.length} tools`); } return { tools: index_js_2.tools.map((tool) => ({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema, })), }; }); // Register tools/call handler this.server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => { const toolName = request.params.name; const args = request.params.arguments || {}; if (this.debug) { console.error(`[MCP Server] Executing tool: ${toolName}`); console.error(`[MCP Server] Arguments:`, JSON.stringify(args, null, 2)); } const tool = index_js_2.tools.find((t) => t.name === toolName); if (!tool) { throw new Error(`Tool not found: ${toolName}`); } if (!this.sdk) { throw new Error('SDK not initialized. Please ensure PRIVATE_KEY environment variable is set.'); } try { const result = await tool.handler(this.sdk, args); if (this.debug) { console.error(`[MCP Server] Tool execution successful: ${toolName}`); } return result; } catch (error) { console.error(`[MCP Server] Tool execution failed: ${toolName}`, error); throw error; } }); } /** * Setup prompt handlers */ setupPromptHandlers() { // Register prompts/list handler this.server.setRequestHandler(types_js_1.ListPromptsRequestSchema, async () => { if (this.debug) { console.error(`[MCP Server] Listing ${index_js_3.prompts.length} prompts`); } return { prompts: index_js_3.prompts.map((prompt) => ({ name: prompt.name, description: prompt.description, arguments: prompt.arguments || [], })), }; }); // Register prompts/get handler this.server.setRequestHandler(types_js_1.GetPromptRequestSchema, async (request) => { const promptName = request.params.name; const args = (request.params.arguments || {}); if (this.debug) { console.error(`[MCP Server] Getting prompt: ${promptName}`); console.error(`[MCP Server] Arguments:`, JSON.stringify(args, null, 2)); } const prompt = (0, index_js_3.getPrompt)(promptName); if (!prompt) { throw new Error(`Prompt not found: ${promptName}`); } try { const messages = prompt.handler(args); if (this.debug) { console.error(`[MCP Server] Prompt generated ${messages.length} messages`); } return { messages, }; } catch (error) { console.error(`[MCP Server] Prompt generation failed: ${promptName}`, error); throw error; } }); } /** * Setup error handlers */ setupErrorHandlers() { this.server.onerror = (error) => { console.error('[MCP Server] Protocol error:', error); }; process.on('SIGINT', () => { if (this.debug) { console.error('[MCP Server] Received SIGINT, shutting down...'); } this.cleanup(); process.exit(0); }); process.on('SIGTERM', () => { if (this.debug) { console.error('[MCP Server] Received SIGTERM, shutting down...'); } this.cleanup(); process.exit(0); }); } /** * Start the MCP server */ async start() { await this.initialize(); const transport = new stdio_js_1.StdioServerTransport(); await this.server.connect(transport); if (this.debug) { console.error('[MCP Server] Gala Launchpad MCP server running on stdio'); console.error(`[MCP Server] Registered ${index_js_2.tools.length} tools`); console.error(`[MCP Server] Registered ${index_js_3.prompts.length} prompts (slash commands)`); } } /** * Cleanup resources */ cleanup() { if (this.sdk) { this.sdk.cleanup(); if (this.debug) { console.error('[MCP Server] SDK cleaned up'); } } } } exports.LaunchpadMCPServer = LaunchpadMCPServer; //# sourceMappingURL=server.js.map