-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoolRegistry.ts
More file actions
469 lines (453 loc) · 15.6 KB
/
toolRegistry.ts
File metadata and controls
469 lines (453 loc) · 15.6 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { queryData, execData } from '../tools/query';
import {
getCatalogs,
getColumns,
getExportedKeys,
getImportedKeys,
getIndexes,
getPrimaryKeys,
getProcedures,
getProcedureParameters,
getSchemas,
getTables,
} from '../tools/metadata';
import { getPrompts, generatePromptMessages } from '../tools/prompts/prompts';
/**
* Register all tools and prompts with the MCP server
* @param server The MCP server instance
*/
export function registerTools(server: McpServer) {
// Register prompts
registerPrompts(server);
// Query Data tool
server.tool(
'queryData',
'Execute SQL queries against connected data sources and retrieve results',
{
query: z
.string()
.describe('The SQL statement(s) to execute. Separate multiple statements with semi-colons'),
defaultSchema: z
.string()
.optional()
.describe('Schema to use if tables are not prefixed with a schema name'),
schemaOnly: z
.boolean()
.optional()
.describe('If true, the result only includes column metadata'),
parameters: z
.record(z.any())
.optional()
.describe(
'A JSON object containing a list of query parameters. All parameter names must begin with @',
),
},
async ({ query, defaultSchema, schemaOnly, parameters }) => {
try {
const response = await queryData(query, defaultSchema, schemaOnly, parameters);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Execute Data tool
server.tool(
'execData',
'Execute stored procedures against connected data sources',
{
procedure: z.string().describe('The name of the stored procedure to execute'),
defaultSchema: z
.string()
.optional()
.describe('Schema to use if the procedure is not prefixed with a schema name'),
parameters: z
.record(z.any())
.optional()
.describe(
'A JSON object containing procedure parameters. All parameter names must begin with @',
),
},
async ({ procedure, defaultSchema, parameters }) => {
try {
const response = await execData(procedure, defaultSchema, parameters);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Catalogs tool
server.tool(
'getCatalogs',
'Retrieve a list of available connections from CData Connect Cloud. The connection names should be used as catalog names in other tools and in any queries to CData Connect Cloud. Use the `getSchemas` tool to get a list of available schemas for a specific catalog.',
{},
async () => {
try {
const response = await getCatalogs();
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Columns tool
server.tool(
'getColumns',
'Retrieve a list of available database columns from CData Connect Cloud for a specific catalog, schema, and table',
{
catalogName: z.string().optional().describe('Optional catalog name to filter columns by'),
schemaName: z.string().optional().describe('Optional schema name to filter columns by'),
tableName: z.string().optional().describe('Optional table name to filter columns by'),
columnName: z.string().optional().describe('Optional column name to filter by'),
},
async ({ catalogName, schemaName, tableName, columnName }) => {
try {
const response = await getColumns(catalogName, schemaName, tableName, columnName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Exported Keys tool
server.tool(
'getExportedKeys',
'Retrieve a list of foreign key relationships from CData Connect Cloud for a specific catalog, schema, and table',
{
catalogName: z.string().optional().describe('Optional catalog name to filter keys by'),
schemaName: z.string().optional().describe('Optional schema name to filter keys by'),
tableName: z.string().optional().describe('Optional table name to filter by'),
},
async ({ catalogName, schemaName, tableName }) => {
try {
const response = await getExportedKeys(catalogName, schemaName, tableName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Imported Keys tool
server.tool(
'getImportedKeys',
'Retrieve a list of foreign key relationships from CData Connect Cloud for a specific catalog, schema, and table',
{
catalogName: z.string().optional().describe('Optional catalog name to filter keys by'),
schemaName: z.string().optional().describe('Optional schema name to filter keys by'),
tableName: z.string().optional().describe('Optional table name to filter by'),
},
async ({ catalogName, schemaName, tableName }) => {
try {
const response = await getImportedKeys(catalogName, schemaName, tableName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Indexes tool
server.tool(
'getIndexes',
'Retrieve a list of indexes from CData Connect Cloud for a specific catalog, schema, and table',
{
catalogName: z.string().optional().describe('Optional catalog name to filter indexes by'),
schemaName: z.string().optional().describe('Optional schema name to filter indexes by'),
tableName: z.string().optional().describe('Optional table name to filter by'),
},
async ({ catalogName, schemaName, tableName }) => {
try {
const response = await getIndexes(catalogName, schemaName, tableName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Primary Keys tool
server.tool(
'getPrimaryKeys',
'Retrieve a list of primary keys from CData Connect Cloud for a specific catalog, schema, and table',
{
catalogName: z.string().optional().describe('Optional catalog name to filter keys by'),
schemaName: z.string().optional().describe('Optional schema name to filter keys by'),
tableName: z.string().optional().describe('Optional table name to filter by'),
},
async ({ catalogName, schemaName, tableName }) => {
try {
const response = await getPrimaryKeys(catalogName, schemaName, tableName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Procedure Parameters tool
server.tool(
'getProcedureParameters',
'Retrieve a list of stored procedure parameters from CData Connect Cloud for a specific catalog, schema, and procedure',
{
catalogName: z.string().optional().describe('Optional catalog name to filter parameters by'),
schemaName: z.string().optional().describe('Optional schema name to filter parameters by'),
procedureName: z
.string()
.optional()
.describe('Optional procedure name to filter parameters by'),
parameterName: z.string().optional().describe('Optional parameter name to filter by'),
},
async ({ catalogName, schemaName, procedureName, parameterName }) => {
try {
const response = await getProcedureParameters(
catalogName,
schemaName,
procedureName,
parameterName,
);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Procdures tool
server.tool(
'getProcedures',
'Retrieve a list of stored procedures from CData Connect Cloud for a specific catalog and schema',
{
catalogName: z.string().optional().describe('Optional catalog name to filter procedures by'),
schemaName: z.string().optional().describe('Optional schema name to filter procedures by'),
procedureName: z.string().optional().describe('Optional procedure name to filter by'),
},
async ({ catalogName, schemaName, procedureName }) => {
try {
const response = await getProcedures(catalogName, schemaName, procedureName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Schemas tool
server.tool(
'getSchemas',
'Retrieve a list of available database schemas from CData Connect Cloud for a specific catalog. Use the `getTables` tool to get a list of available tables for a specific catalog and schema.',
{
catalogName: z.string().optional().describe('Optional catalog name to filter schemas by'),
},
async ({ catalogName }) => {
try {
const response = await getSchemas(catalogName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
// Get Tables tool
server.tool(
'getTables',
'Retrieve a list of available database tables from CData Connect Cloud for a specific catalog and schema. Use the `getColumns` tool to get a list of available columns for a specific table.',
{
catalogName: z.string().optional().describe('Optional catalog name to filter tables by'),
schemaName: z.string().optional().describe('Optional schema name to filter tables by'),
tableName: z.string().optional().describe('Optional table name to filter by'),
},
async ({ catalogName, schemaName, tableName }) => {
try {
const response = await getTables(catalogName, schemaName, tableName);
if (response.error) {
return {
content: [{ type: 'text', text: `Error: ${response.error.message}` }],
isError: true,
};
}
return {
content: [{ type: 'text', text: JSON.stringify(response.result, null, 2) }],
};
} catch (error: any) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
},
);
}
/**
* Register prompts with the MCP server
* @param server The MCP server instance
*/
function registerPrompts(server: McpServer) {
const prompts = getPrompts();
// Register each prompt individually
prompts.forEach(prompt => {
// Convert prompt arguments to Zod schema
const argsSchema: Record<string, z.ZodType> = {};
if (prompt.arguments) {
prompt.arguments.forEach(arg => {
// Convert to Zod string schema, make optional if not required
argsSchema[arg.name] = arg.required
? z.string().describe(arg.description || '')
: z
.string()
.optional()
.describe(arg.description || '');
});
}
// Register the prompt using the correct API
if (Object.keys(argsSchema).length > 0) {
// Prompt with arguments
server.prompt(prompt.name, prompt.description || '', argsSchema, args => {
const messages = generatePromptMessages(prompt.name, args || {});
return {
description: prompt.description || '',
messages: messages.map(msg => ({
role: msg.role as 'user' | 'assistant',
content: {
type: 'text' as const,
text: msg.content.text,
},
})),
};
});
} else {
// Prompt without arguments
server.prompt(prompt.name, prompt.description || '', () => {
const messages = generatePromptMessages(prompt.name, {});
return {
description: prompt.description || '',
messages: messages.map(msg => ({
role: msg.role as 'user' | 'assistant',
content: {
type: 'text' as const,
text: msg.content.text,
},
})),
};
});
}
});
}