@gala-chain/launchpad-mcp-server 1.22.3 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/CHANGELOG.md +161 -0
  2. package/README.md +83 -8
  3. package/dist/constants/mcpToolNames.d.ts +69 -11
  4. package/dist/constants/mcpToolNames.d.ts.map +1 -1
  5. package/dist/constants/mcpToolNames.js +47 -9
  6. package/dist/constants/mcpToolNames.js.map +1 -1
  7. package/dist/generated/version.d.ts +1 -1
  8. package/dist/generated/version.js +1 -1
  9. package/dist/prompts/balances.d.ts +24 -0
  10. package/dist/prompts/balances.d.ts.map +1 -0
  11. package/dist/prompts/balances.js +191 -0
  12. package/dist/prompts/balances.js.map +1 -0
  13. package/dist/prompts/creation-utils.d.ts +20 -0
  14. package/dist/prompts/creation-utils.d.ts.map +1 -0
  15. package/dist/prompts/creation-utils.js +115 -0
  16. package/dist/prompts/creation-utils.js.map +1 -0
  17. package/dist/prompts/index.d.ts +9 -2
  18. package/dist/prompts/index.d.ts.map +1 -1
  19. package/dist/prompts/index.js +23 -2
  20. package/dist/prompts/index.js.map +1 -1
  21. package/dist/prompts/pools.d.ts +64 -0
  22. package/dist/prompts/pools.d.ts.map +1 -0
  23. package/dist/prompts/pools.js +548 -0
  24. package/dist/prompts/pools.js.map +1 -0
  25. package/dist/prompts/social.d.ts +16 -0
  26. package/dist/prompts/social.d.ts.map +1 -0
  27. package/dist/prompts/social.js +97 -0
  28. package/dist/prompts/social.js.map +1 -0
  29. package/dist/prompts/trading-calculations.d.ts +52 -0
  30. package/dist/prompts/trading-calculations.d.ts.map +1 -0
  31. package/dist/prompts/trading-calculations.js +479 -0
  32. package/dist/prompts/trading-calculations.js.map +1 -0
  33. package/dist/prompts/transfers.d.ts +16 -0
  34. package/dist/prompts/transfers.d.ts.map +1 -0
  35. package/dist/prompts/transfers.js +100 -0
  36. package/dist/prompts/transfers.js.map +1 -0
  37. package/dist/prompts/utility-tools.d.ts +56 -0
  38. package/dist/prompts/utility-tools.d.ts.map +1 -0
  39. package/dist/prompts/utility-tools.js +338 -0
  40. package/dist/prompts/utility-tools.js.map +1 -0
  41. package/dist/prompts/utility.d.ts +8 -0
  42. package/dist/prompts/utility.d.ts.map +1 -1
  43. package/dist/prompts/utility.js +49 -2
  44. package/dist/prompts/utility.js.map +1 -1
  45. package/dist/server.d.ts +17 -0
  46. package/dist/server.d.ts.map +1 -1
  47. package/dist/server.js +81 -3
  48. package/dist/server.js.map +1 -1
  49. package/dist/tools/index.d.ts +9 -1
  50. package/dist/tools/index.d.ts.map +1 -1
  51. package/dist/tools/index.js +10 -2
  52. package/dist/tools/index.js.map +1 -1
  53. package/dist/tools/utils/getConfig.d.ts +2 -0
  54. package/dist/tools/utils/getConfig.d.ts.map +1 -1
  55. package/dist/tools/utils/getConfig.js +12 -3
  56. package/dist/tools/utils/getConfig.js.map +1 -1
  57. package/dist/tools/utils/getEnvironment.d.ts +8 -0
  58. package/dist/tools/utils/getEnvironment.d.ts.map +1 -0
  59. package/dist/tools/utils/getEnvironment.js +19 -0
  60. package/dist/tools/utils/getEnvironment.js.map +1 -0
  61. package/dist/tools/utils/index.d.ts.map +1 -1
  62. package/dist/tools/utils/index.js +4 -0
  63. package/dist/tools/utils/index.js.map +1 -1
  64. package/dist/tools/utils/switchEnvironment.d.ts +8 -0
  65. package/dist/tools/utils/switchEnvironment.d.ts.map +1 -0
  66. package/dist/tools/utils/switchEnvironment.js +30 -0
  67. package/dist/tools/utils/switchEnvironment.js.map +1 -0
  68. package/dist/types/mcp.d.ts +1 -1
  69. package/dist/types/mcp.d.ts.map +1 -1
  70. package/dist/utils/tool-factory.d.ts +30 -0
  71. package/dist/utils/tool-factory.d.ts.map +1 -1
  72. package/dist/utils/tool-factory.js +31 -0
  73. package/dist/utils/tool-factory.js.map +1 -1
  74. package/docs/AI-AGENT-PATTERNS.md +555 -0
  75. package/docs/CONSTRAINTS-REFERENCE.md +454 -0
  76. package/docs/PROMPT-TOOL-MAPPING.md +352 -0
  77. package/docs/examples/default-values-pattern.md +240 -0
  78. package/docs/examples/tool-factory-pattern.md +217 -0
  79. package/jest.config.js +94 -0
  80. package/package.json +1 -1
  81. package/src/__tests__/integration/fetchTokenDetails.integration.test.ts +258 -0
  82. package/src/__tests__/integration/poolTools.integration.test.ts +185 -0
  83. package/src/__tests__/server.test.ts +256 -0
  84. package/src/constants/mcpToolNames.ts +181 -0
  85. package/src/index.ts +19 -0
  86. package/src/prompts/__tests__/promptStructure.test.ts +137 -0
  87. package/src/prompts/__tests__/registry.test.ts +359 -0
  88. package/src/prompts/analysis.ts +429 -0
  89. package/src/prompts/balances.ts +198 -0
  90. package/src/prompts/create-token.ts +123 -0
  91. package/src/prompts/creation-utils.ts +118 -0
  92. package/src/prompts/dex-trading.ts +86 -0
  93. package/src/prompts/discover-tokens.ts +86 -0
  94. package/src/prompts/index.ts +175 -0
  95. package/src/prompts/liquidity-positions.ts +270 -0
  96. package/src/prompts/pools.ts +571 -0
  97. package/src/prompts/portfolio.ts +242 -0
  98. package/src/prompts/social.ts +100 -0
  99. package/src/prompts/trading-calculations.ts +499 -0
  100. package/src/prompts/trading.ts +191 -0
  101. package/src/prompts/transfers.ts +103 -0
  102. package/src/prompts/utility-tools.ts +349 -0
  103. package/src/prompts/utility.ts +92 -0
  104. package/src/prompts/utils/workflowTemplates.ts +511 -0
  105. package/src/schemas/common-schemas.ts +393 -0
  106. package/src/scripts/test-all-prompts.ts +184 -0
  107. package/src/server.ts +367 -0
  108. package/src/tools/__tests__/dex-tools.test.ts +562 -0
  109. package/src/tools/__tests__/liquidity-positions.test.ts +673 -0
  110. package/src/tools/balance/index.ts +174 -0
  111. package/src/tools/creation/index.ts +182 -0
  112. package/src/tools/dex/index.ts +226 -0
  113. package/src/tools/dex/liquidity-positions.ts +547 -0
  114. package/src/tools/index.ts +94 -0
  115. package/src/tools/pools/fetchAllPools.ts +47 -0
  116. package/src/tools/pools/fetchAllPriceHistory.ts +119 -0
  117. package/src/tools/pools/fetchPoolDetails.ts +27 -0
  118. package/src/tools/pools/fetchPoolDetailsForCalculation.ts +22 -0
  119. package/src/tools/pools/fetchPools.ts +47 -0
  120. package/src/tools/pools/fetchPriceHistory.ts +124 -0
  121. package/src/tools/pools/fetchTokenDetails.ts +77 -0
  122. package/src/tools/pools/index.ts +284 -0
  123. package/src/tools/social/index.ts +64 -0
  124. package/src/tools/trading/index.ts +605 -0
  125. package/src/tools/transfers/index.ts +75 -0
  126. package/src/tools/utils/clearCache.ts +36 -0
  127. package/src/tools/utils/createWallet.ts +19 -0
  128. package/src/tools/utils/explainSdkUsage.ts +1446 -0
  129. package/src/tools/utils/getAddress.ts +12 -0
  130. package/src/tools/utils/getCacheInfo.ts +14 -0
  131. package/src/tools/utils/getConfig.ts +21 -0
  132. package/src/tools/utils/getEnvironment.ts +17 -0
  133. package/src/tools/utils/getEthereumAddress.ts +12 -0
  134. package/src/tools/utils/getUrlByTokenName.ts +12 -0
  135. package/src/tools/utils/getVersion.ts +25 -0
  136. package/src/tools/utils/getWallet.ts +25 -0
  137. package/src/tools/utils/hasWallet.ts +15 -0
  138. package/src/tools/utils/index.ts +37 -0
  139. package/src/tools/utils/isTokenGraduated.ts +16 -0
  140. package/src/tools/utils/setWallet.ts +41 -0
  141. package/src/tools/utils/switchEnvironment.ts +28 -0
  142. package/src/types/mcp.ts +72 -0
  143. package/src/utils/__tests__/validation.test.ts +147 -0
  144. package/src/utils/constraints.ts +155 -0
  145. package/src/utils/default-values.ts +208 -0
  146. package/src/utils/error-handler.ts +69 -0
  147. package/src/utils/error-templates.ts +273 -0
  148. package/src/utils/response-formatter.ts +51 -0
  149. package/src/utils/tool-factory.ts +303 -0
  150. package/src/utils/tool-registry.ts +296 -0
  151. package/src/utils/validation.ts +371 -0
  152. package/tests/wallet-management-integration.test.ts +284 -0
  153. package/tsconfig.json +23 -0
package/src/server.ts ADDED
@@ -0,0 +1,367 @@
1
+ /**
2
+ * Gala Launchpad MCP Server
3
+ *
4
+ * Provides MCP tools for AI agents to interact with Gala Launchpad
5
+ */
6
+
7
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
8
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
9
+ import {
10
+ ListToolsRequestSchema,
11
+ CallToolRequestSchema,
12
+ ListPromptsRequestSchema,
13
+ GetPromptRequestSchema,
14
+ } from '@modelcontextprotocol/sdk/types.js';
15
+ import { AgentConfig, type LaunchpadSDK } from '@gala-chain/launchpad-sdk';
16
+ import { tools } from './tools/index.js';
17
+ import { prompts, getPrompt } from './prompts/index.js';
18
+ import { MCP_SERVER_VERSION } from './generated/version.js';
19
+
20
+ export class LaunchpadMCPServer {
21
+ private server: Server;
22
+ private sdk: LaunchpadSDK | null = null;
23
+ private debug: boolean;
24
+ private currentEnvironment: 'production' | 'development' | 'testing' = 'production';
25
+ private currentPrivateKey: string | undefined;
26
+
27
+ constructor() {
28
+ this.debug = process.env.DEBUG === 'true';
29
+
30
+ this.server = new Server(
31
+ {
32
+ name: '@gala-chain/launchpad-mcp-server',
33
+ version: MCP_SERVER_VERSION,
34
+ },
35
+ {
36
+ capabilities: {
37
+ tools: {},
38
+ prompts: {
39
+ listChanged: true,
40
+ },
41
+ },
42
+ }
43
+ );
44
+
45
+ this.setupToolHandlers();
46
+ this.setupPromptHandlers();
47
+ this.setupErrorHandlers();
48
+ }
49
+
50
+ /**
51
+ * Initialize SDK with AgentConfig
52
+ *
53
+ * Supports two operational modes:
54
+ * 1. Full-access mode (PRIVATE_KEY provided): Uses quickSetup() - all operations available
55
+ * 2. Read-only mode (PRIVATE_KEY not provided): Uses readOnlySetup() - query operations only
56
+ *
57
+ * In read-only mode, signing operations (buy, sell, create tokens, etc.) will throw ValidationError
58
+ * when attempted, but all query operations (fetchPools, fetchBalance, etc.) work normally.
59
+ */
60
+ async initialize() {
61
+ try {
62
+ if (this.debug) {
63
+ console.error('[MCP Server] Initializing SDK...');
64
+ }
65
+
66
+ const hasPrivateKey = Boolean(process.env.PRIVATE_KEY && process.env.PRIVATE_KEY.trim());
67
+ const environment = ((process.env.ENVIRONMENT as any) || 'production') as 'production' | 'development' | 'testing';
68
+ const timeout = parseInt(process.env.TIMEOUT || '30000', 10);
69
+
70
+ // Store environment and private key for dynamic switching
71
+ this.currentEnvironment = environment;
72
+ if (hasPrivateKey) {
73
+ this.currentPrivateKey = process.env.PRIVATE_KEY;
74
+ }
75
+
76
+ if (hasPrivateKey) {
77
+ // Full-access mode: use quickSetup() to enable signing operations
78
+ if (this.debug) {
79
+ console.error('[MCP Server] PRIVATE_KEY detected - initializing in full-access mode');
80
+ }
81
+
82
+ const { sdk, validation } = await AgentConfig.quickSetup({
83
+ environment,
84
+ privateKey: process.env.PRIVATE_KEY,
85
+ timeout,
86
+ debug: this.debug,
87
+ autoValidate: false,
88
+ });
89
+
90
+ this.sdk = sdk;
91
+
92
+ if (this.debug) {
93
+ console.error('[MCP Server] SDK initialized successfully (full-access mode)');
94
+ if (validation) {
95
+ console.error(`[MCP Server] Validation - Ready: ${validation.ready}`);
96
+ console.error(`[MCP Server] Validation - Can Trade: ${validation.capabilities.canTrade}`);
97
+ }
98
+ }
99
+
100
+ if (validation && !validation.ready) {
101
+ console.error('[MCP Server] Warning: SDK not ready', validation.issues);
102
+ }
103
+ } else {
104
+ // Read-only mode: use readOnlySetup() for query-only operations
105
+ if (this.debug) {
106
+ console.error('[MCP Server] PRIVATE_KEY not provided - initializing in read-only mode');
107
+ }
108
+
109
+ const { sdk } = await AgentConfig.readOnlySetup({
110
+ environment,
111
+ timeout,
112
+ debug: this.debug,
113
+ });
114
+
115
+ this.sdk = sdk;
116
+
117
+ if (this.debug) {
118
+ console.error('[MCP Server] SDK initialized successfully (read-only mode)');
119
+ console.error('[MCP Server] Note: Signing operations will fail - wallet required');
120
+ }
121
+ }
122
+ } catch (error) {
123
+ console.error('[MCP Server] Failed to initialize SDK:', error);
124
+ throw error;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Setup tool handlers
130
+ */
131
+ private setupToolHandlers() {
132
+ // Register tools/list handler
133
+ this.server.setRequestHandler(ListToolsRequestSchema, async () => {
134
+ if (this.debug) {
135
+ console.error(`[MCP Server] Listing ${tools.length} tools`);
136
+ }
137
+
138
+ return {
139
+ tools: tools.map((tool) => ({
140
+ name: tool.name,
141
+ description: tool.description,
142
+ inputSchema: tool.inputSchema,
143
+ })),
144
+ };
145
+ });
146
+
147
+ // Register tools/call handler
148
+ this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
149
+ const toolName = request.params.name as string;
150
+ const args = request.params.arguments || {};
151
+
152
+ if (this.debug) {
153
+ console.error(`[MCP Server] Executing tool: ${toolName}`);
154
+ console.error(`[MCP Server] Arguments:`, JSON.stringify(args, null, 2));
155
+ }
156
+
157
+ const tool = tools.find((t) => t.name === toolName);
158
+
159
+ if (!tool) {
160
+ throw new Error(`Tool not found: ${toolName}`);
161
+ }
162
+
163
+ if (!this.sdk) {
164
+ throw new Error('SDK not initialized. Failed during startup - check logs for details.');
165
+ }
166
+
167
+ try {
168
+ const result = await tool.handler(this.sdk, args, this);
169
+
170
+ if (this.debug) {
171
+ console.error(`[MCP Server] Tool execution successful: ${toolName}`);
172
+ }
173
+
174
+ return result as any;
175
+ } catch (error) {
176
+ console.error(`[MCP Server] Tool execution failed: ${toolName}`, error);
177
+ throw error;
178
+ }
179
+ });
180
+ }
181
+
182
+ /**
183
+ * Setup prompt handlers
184
+ */
185
+ private setupPromptHandlers() {
186
+ // Register prompts/list handler
187
+ this.server.setRequestHandler(ListPromptsRequestSchema, async () => {
188
+ if (this.debug) {
189
+ console.error(`[MCP Server] Listing ${prompts.length} prompts`);
190
+ }
191
+
192
+ return {
193
+ prompts: prompts.map((prompt) => ({
194
+ name: prompt.name,
195
+ description: prompt.description,
196
+ arguments: prompt.arguments || [],
197
+ })),
198
+ };
199
+ });
200
+
201
+ // Register prompts/get handler
202
+ this.server.setRequestHandler(GetPromptRequestSchema, async (request) => {
203
+ const promptName = request.params.name as string;
204
+ const args = (request.params.arguments || {}) as Record<string, string>;
205
+
206
+ if (this.debug) {
207
+ console.error(`[MCP Server] Getting prompt: ${promptName}`);
208
+ console.error(`[MCP Server] Arguments:`, JSON.stringify(args, null, 2));
209
+ }
210
+
211
+ const prompt = getPrompt(promptName);
212
+
213
+ if (!prompt) {
214
+ throw new Error(`Prompt not found: ${promptName}`);
215
+ }
216
+
217
+ try {
218
+ const messages = prompt.handler(args);
219
+
220
+ if (this.debug) {
221
+ console.error(`[MCP Server] Prompt generated ${messages.length} messages`);
222
+ }
223
+
224
+ return {
225
+ messages,
226
+ };
227
+ } catch (error) {
228
+ console.error(`[MCP Server] Prompt generation failed: ${promptName}`, error);
229
+ throw error;
230
+ }
231
+ });
232
+ }
233
+
234
+ /**
235
+ * Setup error handlers
236
+ */
237
+ private setupErrorHandlers() {
238
+ this.server.onerror = (error) => {
239
+ console.error('[MCP Server] Protocol error:', error);
240
+ };
241
+
242
+ process.on('SIGINT', () => {
243
+ if (this.debug) {
244
+ console.error('[MCP Server] Received SIGINT, shutting down...');
245
+ }
246
+ this.cleanup();
247
+ process.exit(0);
248
+ });
249
+
250
+ process.on('SIGTERM', () => {
251
+ if (this.debug) {
252
+ console.error('[MCP Server] Received SIGTERM, shutting down...');
253
+ }
254
+ this.cleanup();
255
+ process.exit(0);
256
+ });
257
+ }
258
+
259
+ /**
260
+ * Start the MCP server
261
+ */
262
+ async start() {
263
+ await this.initialize();
264
+
265
+ const transport = new StdioServerTransport();
266
+ await this.server.connect(transport);
267
+
268
+ if (this.debug) {
269
+ console.error('[MCP Server] Gala Launchpad MCP server running on stdio');
270
+ console.error(`[MCP Server] Registered ${tools.length} tools`);
271
+ console.error(`[MCP Server] Registered ${prompts.length} prompts (slash commands)`);
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Switch to a different environment dynamically
277
+ * Creates a new SDK instance with the new environment while preserving the wallet
278
+ *
279
+ * Strategy: Create new SDK BEFORE destroying old one to avoid broken state on error
280
+ */
281
+ async switchEnvironment(newEnvironment: 'production' | 'development' | 'testing') {
282
+ try {
283
+ if (this.debug) {
284
+ console.error(`[MCP Server] Switching environment: ${this.currentEnvironment} → ${newEnvironment}`);
285
+ }
286
+
287
+ const previousEnvironment = this.currentEnvironment;
288
+ const previousSdk = this.sdk;
289
+ const timeout = parseInt(process.env.TIMEOUT || '30000', 10);
290
+
291
+ // Create new SDK BEFORE destroying old one (prevents broken state on error)
292
+ let newSdk: LaunchpadSDK;
293
+
294
+ if (this.currentPrivateKey) {
295
+ // Preserve wallet in full-access mode
296
+ if (this.debug) {
297
+ console.error('[MCP Server] Switching with wallet preservation (full-access mode)');
298
+ }
299
+
300
+ const { sdk } = await AgentConfig.quickSetup({
301
+ environment: newEnvironment,
302
+ privateKey: this.currentPrivateKey,
303
+ timeout,
304
+ debug: this.debug,
305
+ autoValidate: false,
306
+ });
307
+
308
+ newSdk = sdk;
309
+ } else {
310
+ // Switch in read-only mode
311
+ if (this.debug) {
312
+ console.error('[MCP Server] Switching in read-only mode');
313
+ }
314
+
315
+ const { sdk } = await AgentConfig.readOnlySetup({
316
+ environment: newEnvironment,
317
+ timeout,
318
+ debug: this.debug,
319
+ });
320
+
321
+ newSdk = sdk;
322
+ }
323
+
324
+ // Only cleanup old SDK after new one is successfully created and ready
325
+ if (previousSdk) {
326
+ previousSdk.cleanup();
327
+ }
328
+
329
+ // Now safe to update SDK instance and environment
330
+ this.sdk = newSdk;
331
+ this.currentEnvironment = newEnvironment;
332
+
333
+ if (this.debug) {
334
+ console.error(`[MCP Server] Environment switched successfully: ${previousEnvironment} → ${newEnvironment}`);
335
+ }
336
+
337
+ return {
338
+ success: true,
339
+ previousEnvironment,
340
+ newEnvironment,
341
+ };
342
+ } catch (error) {
343
+ console.error('[MCP Server] Failed to switch environment:', error);
344
+ // Old SDK remains intact if switch fails
345
+ throw error;
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Get current environment
351
+ */
352
+ getEnvironment(): 'production' | 'development' | 'testing' {
353
+ return this.currentEnvironment;
354
+ }
355
+
356
+ /**
357
+ * Cleanup resources
358
+ */
359
+ cleanup() {
360
+ if (this.sdk) {
361
+ this.sdk.cleanup();
362
+ if (this.debug) {
363
+ console.error('[MCP Server] SDK cleaned up');
364
+ }
365
+ }
366
+ }
367
+ }