|
| 1 | +/* eslint-disable no-async-promise-executor */ |
| 2 | +/** |
| 3 | + * Amp Query Tool Handlers for Remix MCP Server |
| 4 | + * |
| 5 | + * Provides functionality to query data using the Amp hosted server |
| 6 | + */ |
| 7 | + |
| 8 | +import { IMCPToolResult } from '../../types/mcp'; |
| 9 | +import { BaseToolHandler } from '../registry/RemixToolRegistry'; |
| 10 | +import { |
| 11 | + ToolCategory, |
| 12 | + RemixToolDefinition |
| 13 | +} from '../types/mcpTools'; |
| 14 | +import { Plugin } from '@remixproject/engine'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Amp Query argument types |
| 18 | + */ |
| 19 | +export interface AmpQueryArgs { |
| 20 | + query: string |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Amp Query result types |
| 25 | + */ |
| 26 | +export interface AmpQueryResult<T = any> { |
| 27 | + success: boolean; |
| 28 | + data: Array<T>; |
| 29 | + rowCount: number; |
| 30 | + query: string; |
| 31 | + error?: string; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Create an Amp client with the given configuration |
| 36 | + */ |
| 37 | +async function createAmpClient(baseUrl?: string, authToken?: string) { |
| 38 | + // Dynamic import for ES module packages |
| 39 | + // @ts-ignore - ES module dynamic import |
| 40 | + const { createConnectTransport } = await import("@connectrpc/connect-web"); |
| 41 | + // @ts-ignore - ES module dynamic import |
| 42 | + const { createAuthInterceptor, createClient } = await import("@edgeandnode/amp"); |
| 43 | + |
| 44 | + const ampBaseUrl = baseUrl || "/amp"; |
| 45 | + |
| 46 | + const transport = createConnectTransport({ |
| 47 | + baseUrl: ampBaseUrl, |
| 48 | + /** |
| 49 | + * If present, adds the auth token to the interceptor path. |
| 50 | + * This adds it to the connect-rpc transport layer and is passed to requests. |
| 51 | + * This is REQUIRED for querying published datasets through the gateway |
| 52 | + */ |
| 53 | + interceptors: authToken |
| 54 | + ? [createAuthInterceptor(authToken)] |
| 55 | + : undefined, |
| 56 | + }); |
| 57 | + |
| 58 | + return createClient(transport); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Performs the given query with the AmpClient instance. |
| 63 | + * Waits for all batches to complete/resolve before returning. |
| 64 | + * @param query the query to run |
| 65 | + * @param baseUrl optional base URL for the Amp server |
| 66 | + * @param authToken optional authentication token |
| 67 | + * @returns an array of the results from all resolved batches |
| 68 | + */ |
| 69 | +async function performAmpQuery<T = any>( |
| 70 | + query: string, |
| 71 | + baseUrl?: string, |
| 72 | + authToken?: string |
| 73 | +): Promise<Array<T>> { |
| 74 | + return await new Promise<Array<T>>(async (resolve, reject) => { |
| 75 | + try { |
| 76 | + const ampClient = await createAmpClient(baseUrl, authToken); |
| 77 | + const data: Array<T> = []; |
| 78 | + |
| 79 | + for await (const batch of ampClient.query(query)) { |
| 80 | + data.push(...batch); |
| 81 | + } |
| 82 | + |
| 83 | + resolve(data); |
| 84 | + } catch (error) { |
| 85 | + reject(error); |
| 86 | + } |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Amp Query Tool Handler |
| 92 | + */ |
| 93 | +export class AmpQueryHandler extends BaseToolHandler { |
| 94 | + name = 'amp_query'; |
| 95 | + description = 'Execute SQL queries against the Amp hosted server to retrieve blockchain data'; |
| 96 | + inputSchema = { |
| 97 | + type: 'object', |
| 98 | + properties: { |
| 99 | + query: { |
| 100 | + type: 'string', |
| 101 | + description: 'SQL query to execute against the Amp server' |
| 102 | + } |
| 103 | + }, |
| 104 | + required: ['query'] |
| 105 | + }; |
| 106 | + |
| 107 | + getPermissions(): string[] { |
| 108 | + return ['amp:query']; |
| 109 | + } |
| 110 | + |
| 111 | + validate(args: AmpQueryArgs): boolean | string { |
| 112 | + const required = this.validateRequired(args, ['query']); |
| 113 | + if (required !== true) return required; |
| 114 | + |
| 115 | + const types = this.validateTypes(args, { |
| 116 | + query: 'string' |
| 117 | + }); |
| 118 | + if (types !== true) return types; |
| 119 | + |
| 120 | + if (args.query.trim().length === 0) { |
| 121 | + return 'Query cannot be empty'; |
| 122 | + } |
| 123 | + |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + async execute(args: AmpQueryArgs, plugin: Plugin): Promise<IMCPToolResult> { |
| 128 | + try { |
| 129 | + // Show a notification that the query is being executed |
| 130 | + plugin.call('notification', 'toast', `Executing Amp query...`); |
| 131 | + |
| 132 | + const authToken: string | undefined = await plugin.call('config', 'getEnv', 'AMP_QUERY_TOKEN'); |
| 133 | + const baseUrl: string | undefined = await plugin.call('config', 'getEnv', 'AMP_QUERY_URL'); |
| 134 | + // Perform the Amp query |
| 135 | + const data = await performAmpQuery( |
| 136 | + args.query, |
| 137 | + baseUrl, |
| 138 | + authToken |
| 139 | + ); |
| 140 | + |
| 141 | + const result: AmpQueryResult = { |
| 142 | + success: true, |
| 143 | + data: data, |
| 144 | + rowCount: data.length, |
| 145 | + query: args.query |
| 146 | + }; |
| 147 | + |
| 148 | + // Show success notification |
| 149 | + plugin.call('notification', 'toast', `Query completed successfully. Retrieved ${data.length} rows.`); |
| 150 | + |
| 151 | + return this.createSuccessResult(result); |
| 152 | + |
| 153 | + } catch (error) { |
| 154 | + console.error('Amp query error:', error); |
| 155 | + |
| 156 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 157 | + |
| 158 | + // Show error notification |
| 159 | + plugin.call('notification', 'toast', `Amp query failed: ${errorMessage}`); |
| 160 | + |
| 161 | + return this.createErrorResult(`Amp query failed: ${errorMessage}`); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Create Amp tool definitions |
| 168 | + */ |
| 169 | +export function createAmpTools(): RemixToolDefinition[] { |
| 170 | + return [ |
| 171 | + { |
| 172 | + name: 'amp_query', |
| 173 | + description: 'Execute SQL queries against the Amp hosted server to retrieve blockchain data', |
| 174 | + inputSchema: new AmpQueryHandler().inputSchema, |
| 175 | + category: ToolCategory.ANALYSIS, |
| 176 | + permissions: ['amp:query'], |
| 177 | + handler: new AmpQueryHandler() |
| 178 | + } |
| 179 | + ]; |
| 180 | +} |
0 commit comments