-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtoolHandlers.ts
More file actions
217 lines (201 loc) · 6.15 KB
/
Copy pathtoolHandlers.ts
File metadata and controls
217 lines (201 loc) · 6.15 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
import { formatErrorResponse } from '../utils/formatUtils.js';
// Import all tool implementations
import { readQuery, writeQuery, exportQuery } from '../tools/queryTools.js';
import { createTable, alterTable, dropTable, listTables, describeTable } from '../tools/schemaTools.js';
import { createType, createIndex, createFunctionAndTrigger } from '../tools/ddlTools.js';
import { appendInsight, listInsights } from '../tools/insightTools.js';
/**
* Handle listing available tools
* @returns List of available tools
*/
export function handleListTools() {
return {
tools: [
{
name: "read_query",
description: "Execute SELECT queries to read data from the database",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "write_query",
description: "Execute INSERT, UPDATE, or DELETE queries",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "create_table",
description: "Create new tables in the database",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "alter_table",
description: "Modify existing table schema (add columns, rename tables, etc.)",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "drop_table",
description: "Remove a table from the database with safety confirmation",
inputSchema: {
type: "object",
properties: {
table_name: { type: "string" },
confirm: { type: "boolean" },
},
required: ["table_name", "confirm"],
},
},
{
name: "create_type",
description:
"Create a new PostgreSQL enum type. Only CREATE TYPE ... AS ENUM (...) statements are allowed. Safe-additive only; no CREATE OR REPLACE, no ALTER TYPE, no DROP TYPE.",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "create_index",
description:
"Create a PostgreSQL index. Allows CREATE [UNIQUE] INDEX [IF NOT EXISTS] ... ON ... (...). Safe-additive only.",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
},
required: ["query"],
},
},
{
name: "create_function_and_trigger",
description:
"Create paired PostgreSQL function + trigger for audit-immutability and similar append-only enforcement. Accepts two separate statements. Function runs first, then trigger; CREATE OR REPLACE is allowed for the function (trigger helpers must be idempotent) but not for the trigger (pick a unique trigger name).",
inputSchema: {
type: "object",
properties: {
functionSql: { type: "string" },
triggerSql: { type: "string" },
},
required: ["functionSql", "triggerSql"],
},
},
{
name: "export_query",
description: "Export query results to various formats (CSV, JSON)",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
format: { type: "string", enum: ["csv", "json"] },
},
required: ["query", "format"],
},
},
{
name: "list_tables",
description: "Get a list of all tables in the database",
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "describe_table",
description: "View schema information for a specific table",
inputSchema: {
type: "object",
properties: {
table_name: { type: "string" },
},
required: ["table_name"],
},
},
{
name: "append_insight",
description: "Add a business insight to the memo",
inputSchema: {
type: "object",
properties: {
insight: { type: "string" },
},
required: ["insight"],
},
},
{
name: "list_insights",
description: "List all business insights in the memo",
inputSchema: {
type: "object",
properties: {},
},
},
],
};
}
/**
* Handle tool call requests
* @param name Name of the tool to call
* @param args Arguments for the tool
* @returns Tool execution result
*/
export async function handleToolCall(name: string, args: any) {
try {
switch (name) {
case "read_query":
return await readQuery(args.query);
case "write_query":
return await writeQuery(args.query);
case "create_table":
return await createTable(args.query);
case "alter_table":
return await alterTable(args.query);
case "drop_table":
return await dropTable(args.table_name, args.confirm);
case "create_type":
return await createType(args.query);
case "create_index":
return await createIndex(args.query);
case "create_function_and_trigger":
return await createFunctionAndTrigger(args.functionSql, args.triggerSql);
case "export_query":
return await exportQuery(args.query, args.format);
case "list_tables":
return await listTables();
case "describe_table":
return await describeTable(args.table_name);
case "append_insight":
return await appendInsight(args.insight);
case "list_insights":
return await listInsights();
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error: any) {
return formatErrorResponse(error);
}
}