-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapplication.ts
More file actions
340 lines (326 loc) · 10.8 KB
/
Copy pathapplication.ts
File metadata and controls
340 lines (326 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
import { getIndexerClient, extractNetwork } from '../../../algorand-client.js';
import type { NetworkId } from '../../../algorand-client.js';
import { ResponseProcessor } from '../../../utils/responseProcessor.js';
import { withCommonParams } from '../../commonParams.js';
import type {
ApplicationResponse,
ApplicationsResponse,
ApplicationLogsResponse,
Box
} from 'algosdk/dist/types/client/v2/indexer/models/types';
import algosdk from 'algosdk';
export const applicationTools = [
// Disabled: redundant with api_algod_get_application_by_id (live algod variant covers the same query).
// See .notes/redundant-tools-report.md §1.
// {
// name: 'api_indexer_lookup_applications',
// description: 'Get application information from indexer',
// inputSchema: withCommonParams({
// type: 'object',
// properties: {
// appId: {
// type: 'integer',
// description: 'Application ID'
// }
// },
// required: ['appId']
// })
// },
{
name: 'api_indexer_lookup_application_logs',
description: 'Get application log messages',
inputSchema: withCommonParams({
type: 'object',
properties: {
appId: {
type: 'integer',
description: 'Application ID'
},
limit: {
type: 'integer',
description: 'Maximum number of logs to return'
},
minRound: {
type: 'integer',
description: 'Only return logs after this round'
},
maxRound: {
type: 'integer',
description: 'Only return logs before this round'
},
txid: {
type: 'string',
description: 'Filter by transaction ID'
},
sender: {
type: 'string',
description: 'Filter by sender address'
},
nextToken: {
type: 'string',
description: 'Token for retrieving the next page of results'
}
},
required: ['appId']
})
},
{
name: 'api_indexer_search_for_applications',
description: 'Search for applications with various criteria',
inputSchema: withCommonParams({
type: 'object',
properties: {
limit: {
type: 'integer',
description: 'Maximum number of applications to return'
},
creator: {
type: 'string',
description: 'Filter by creator address'
},
nextToken: {
type: 'string',
description: 'Token for retrieving the next page of results'
}
}
})
},
// Disabled: redundant with api_algod_get_application_box.
// {
// name: 'api_indexer_lookup_application_box',
// description: 'Get application box by name',
// inputSchema: withCommonParams({
// type: 'object',
// properties: {
// appId: {
// type: 'integer',
// description: 'Application ID'
// },
// boxName: {
// type: 'string',
// description: 'Box name Buffer'
// }
// },
// required: ['appId', 'boxName']
// })
// },
// Disabled: redundant with api_algod_get_application_boxes.
// {
// name: 'api_indexer_lookup_application_boxes',
// description: 'Get all application boxes',
// inputSchema: withCommonParams({
// type: 'object',
// properties: {
// appId: {
// type: 'integer',
// description: 'Application ID'
// },
// maxBoxes: {
// type: 'integer',
// description: 'Maximum number of boxes to return'
// }
// },
// required: ['appId']
// })
// }
];
export async function lookupApplications(appId: number, network: NetworkId = 'testnet'): Promise<ApplicationResponse> {
try {
const indexerClient = getIndexerClient(network);
console.error(`Looking up application info for ID ${appId}`);
const response = await indexerClient.lookupApplications(appId).do() as ApplicationResponse;
console.error('Application response:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Application lookup error:', error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(
ErrorCode.InternalError,
`Failed to get application info: ${error instanceof Error ? error.message : String(error)}`
);
}
}
export async function lookupApplicationLogs(appId: number, params?: {
limit?: number;
minRound?: number;
maxRound?: number;
txid?: string;
sender?: string;
nextToken?: string;
}, network: NetworkId = 'testnet'): Promise<ApplicationLogsResponse> {
try {
const indexerClient = getIndexerClient(network);
console.error(`Looking up logs for application ${appId}`);
let search = indexerClient.lookupApplicationLogs(appId);
if (params?.limit) {
search = search.limit(params.limit);
}
if (params?.minRound) {
search = search.minRound(params.minRound);
}
if (params?.maxRound) {
search = search.maxRound(params.maxRound);
}
if (params?.txid) {
search = search.txid(params.txid);
}
if (params?.sender) {
search = search.sender(params.sender);
}
if (params?.nextToken) {
search = search.nextToken(params.nextToken);
}
const response = await search.do() as ApplicationLogsResponse;
console.error('Application logs response:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Application logs lookup error:', error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(
ErrorCode.InternalError,
`Failed to get application logs: ${error instanceof Error ? error.message : String(error)}`
);
}
}
export async function searchForApplications(params?: {
limit?: number;
creator?: string;
nextToken?: string;
}, network: NetworkId = 'testnet'): Promise<ApplicationsResponse> {
try {
const indexerClient = getIndexerClient(network);
console.error('Searching applications with params:', params);
let search = indexerClient.searchForApplications();
if (params?.limit) {
search = search.limit(params.limit);
}
if (params?.creator) {
search = search.creator(params.creator);
}
if (params?.nextToken) {
search = search.nextToken(params.nextToken);
}
const response = await search.do() as ApplicationsResponse;
console.error('Search applications response:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Search applications error:', error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(
ErrorCode.InternalError,
`Failed to search applications: ${error instanceof Error ? error.message : String(error)}`
);
}
}
export async function lookupApplicationBoxByIDandName(appId: number, boxName: string, network: NetworkId = 'testnet'): Promise<Box> {
try {
const indexerClient = getIndexerClient(network);
const encoder = new TextEncoder();
let boxNameBytes: Uint8Array;
// Check if string is a valid number
if (!isNaN(Number(boxName))) {
boxNameBytes = encoder.encode(boxName);
}
// Check if string is a valid Algorand address
else if (algosdk.isValidAddress(boxName)) {
boxNameBytes = encoder.encode(boxName);
}
// Try to decode as base64, if it fails then treat as regular string
else {
try {
// Test if the string is valid base64 by decoding and re-encoding
const decoded = algosdk.base64ToBytes(boxName);
const reencoded = algosdk.bytesToBase64(decoded);
if (reencoded === boxName) {
// Valid base64
boxNameBytes = decoded;
} else {
boxNameBytes = encoder.encode(boxName);
}
} catch {
// If base64 decoding fails, treat as regular string
boxNameBytes = encoder.encode(boxName);
}
}
console.error('Box name bytes:', boxNameBytes);
const response = await indexerClient.lookupApplicationBoxByIDandName(appId, boxNameBytes).do() as Box;
console.error('Box response:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Box fetch error:', error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(
ErrorCode.InternalError,
`Failed to get application box: ${error instanceof Error ? error.message : String(error)}`
);
}
}
export async function searchForApplicationBoxes(appId: number, maxBoxes?: number, network: NetworkId = 'testnet'): Promise<any> {
try {
const indexerClient = getIndexerClient(network);
console.error(`Searching boxes for application ${appId}`);
let search = indexerClient.searchForApplicationBoxes(appId);
if (maxBoxes !== undefined) {
search = search.limit(maxBoxes);
}
const response = await search.do();
console.error('Application boxes response:', JSON.stringify(response, null, 2));
return response;
} catch (error) {
console.error('Application boxes fetch error:', error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(
ErrorCode.InternalError,
`Failed to get application boxes: ${error instanceof Error ? error.message : String(error)}`
);
}
}
export const handleApplicationTools = ResponseProcessor.wrapResourceHandler(async function handleApplicationTools(args: any): Promise<any> {
const name = args.name;
const network = extractNetwork(args);
switch (name) {
// Disabled: redundant with api_algod_get_application_by_id. See .notes/redundant-tools-report.md §1.
// case 'api_indexer_lookup_applications': {
// const { appId } = args;
// const info = await lookupApplications(appId, network);
// return info.application;
// }
case 'api_indexer_lookup_application_logs': {
const { appId, ...params } = args;
const logs = await lookupApplicationLogs(appId, params, network);
return logs;
}
case 'api_indexer_search_for_applications': {
const info = await searchForApplications(args, network);
return info.applications;
}
// Disabled: redundant with api_algod_get_application_box.
// case 'api_indexer_lookup_application_box': {
// const { appId, boxName } = args;
// const box = await lookupApplicationBoxByIDandName(appId, boxName, network);
// return box;
// }
// Disabled: redundant with api_algod_get_application_boxes.
// case 'api_indexer_lookup_application_boxes': {
// const { appId, maxBoxes } = args;
// const boxes = await searchForApplicationBoxes(appId, maxBoxes, network);
// return boxes.boxes;
// }
default:
throw new McpError(
ErrorCode.MethodNotFound,
`Unknown tool: ${name}`
);
}
});