-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
305 lines (275 loc) · 9.96 KB
/
Copy pathserver.js
File metadata and controls
305 lines (275 loc) · 9.96 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
#!/usr/bin/env node
/**
* AgentGate — MCP Tool Approval Gateway
* https://github.com/RLASAF12/agent-gate
*
* Adds human-in-the-loop approval to any agent tool call.
* Framework-agnostic: works with Claude Desktop, Cursor, Windsurf, and any
* MCP client. Zero changes to your existing agent prompts or tool wiring.
*
* Usage in claude_desktop_config.json:
* {
* "mcpServers": {
* "agent-gate": {
* "command": "node",
* "args": ["/path/to/agent-gate/server.js"],
* "env": {
* "AGENTGATE_SUPABASE_URL": "https://yourproject.supabase.co",
* "AGENTGATE_SUPABASE_KEY": "your-anon-key",
* "AGENTGATE_DASHBOARD_URL": "https://rlasaf12.github.io/agent-gate/"
* }
* }
* }
* }
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
// ─── Config ──────────────────────────────────────────────────────────────────
const SUPABASE_URL =
process.env.AGENTGATE_SUPABASE_URL ||
'https://beseparjuerxjygszlta.supabase.co';
const SUPABASE_KEY =
process.env.AGENTGATE_SUPABASE_KEY ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJlc2VwYXJqdWVyeGp5Z3N6bHRhIiwicm9sZSI6ImFub24iLCJpYXQiOjE3ODEzODY2OTYsImV4cCI6MjA5Njk2MjY5Nn0.olesTppkswAP5360f2A5Qn9w5pRHTPt0POGfqKtfI3Y';
const DASHBOARD_URL =
process.env.AGENTGATE_DASHBOARD_URL ||
'https://rlasaf12.github.io/agent-gate/';
/** How long (ms) before a pending request is auto-expired. Default: 10 min. */
const TIMEOUT_MS = Number(process.env.AGENTGATE_TIMEOUT_MS) || 10 * 60 * 1000;
// ─── Supabase helper ─────────────────────────────────────────────────────────
async function sb(path, method = 'GET', body = null) {
const url = `${SUPABASE_URL}/rest/v1/${path}`;
const headers = {
apikey: SUPABASE_KEY,
Authorization: `Bearer ${SUPABASE_KEY}`,
'Content-Type': 'application/json',
Prefer: 'return=representation',
};
const opts = { method, headers };
if (body !== null) opts.body = JSON.stringify(body);
const res = await fetch(url, opts);
if (res.status === 204) return null;
const text = await res.text();
if (!res.ok) throw new Error(`Supabase ${res.status}: ${text}`);
return JSON.parse(text);
}
// ─── Server ──────────────────────────────────────────────────────────────────
const server = new Server(
{ name: 'agent-gate', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'gate_request',
description:
'Submit a tool action for human approval BEFORE executing it. ' +
'Returns a request_id. Poll gate_check() until status is approved or rejected. ' +
'Do NOT execute the action until gate_check returns "approved".',
inputSchema: {
type: 'object',
properties: {
tool_name: {
type: 'string',
description:
'Name of the tool or action being gated (e.g. "delete_rows", "send_bulk_email")',
},
action_description: {
type: 'string',
description:
'Plain-English description of exactly what will happen if approved',
},
context: {
type: 'object',
description:
'Supporting details: affected row counts, recipient lists, amounts, etc.',
default: {},
},
risk_level: {
type: 'string',
enum: ['low', 'medium', 'high', 'critical'],
default: 'medium',
description: 'Self-assessed risk level of this action',
},
},
required: ['tool_name', 'action_description'],
},
},
{
name: 'gate_check',
description:
'Check approval status of a gate_request. ' +
'Poll every 10–30 seconds. Status: pending | approved | rejected | expired. ' +
'Only execute the action when status is "approved".',
inputSchema: {
type: 'object',
properties: {
request_id: {
type: 'string',
description: 'UUID returned by gate_request',
},
},
required: ['request_id'],
},
},
{
name: 'gate_list',
description:
'List all currently pending approval requests. ' +
'Useful for status checks and debugging.',
inputSchema: {
type: 'object',
properties: {},
},
},
],
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
// ── gate_request ──────────────────────────────────────────────────────────
if (name === 'gate_request') {
const {
tool_name,
action_description,
context = {},
risk_level = 'medium',
} = args;
const rows = await sb('gate_requests', 'POST', {
tool_name,
action_description,
context,
risk_level,
status: 'pending',
});
const row = Array.isArray(rows) ? rows[0] : rows;
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
request_id: row.id,
status: 'pending',
risk_level: row.risk_level,
message:
`Approval request submitted. ` +
`Visit the dashboard to approve or reject: ${DASHBOARD_URL}\n` +
`Poll gate_check("${row.id}") every 15 seconds until status changes.`,
dashboard_url: DASHBOARD_URL,
next_step: `Call gate_check with request_id: "${row.id}"`,
},
null,
2
),
},
],
};
}
// ── gate_check ────────────────────────────────────────────────────────────
if (name === 'gate_check') {
const { request_id } = args;
const rows = await sb(
`gate_requests?id=eq.${encodeURIComponent(request_id)}&select=*`
);
if (!rows || rows.length === 0) {
return {
content: [
{
type: 'text',
text: JSON.stringify({
error: 'Request not found',
request_id,
}),
},
],
};
}
let row = rows[0];
// Auto-expire stale pending requests
if (row.status === 'pending') {
const ageMs = Date.now() - new Date(row.requested_at).getTime();
if (ageMs > TIMEOUT_MS) {
await sb(`gate_requests?id=eq.${encodeURIComponent(request_id)}`, 'PATCH', {
status: 'expired',
decided_at: new Date().toISOString(),
decision_note: `Auto-expired after ${Math.round(TIMEOUT_MS / 60000)} minutes`,
});
row.status = 'expired';
}
}
const instructions = {
pending: `Still awaiting human decision. Visit ${DASHBOARD_URL}`,
approved: 'APPROVED ✓ — you may now execute the action.',
rejected: 'REJECTED ✗ — do NOT execute this action. Abort the workflow.',
expired: 'EXPIRED — request timed out. Submit a new gate_request if still needed.',
cancelled: 'CANCELLED — request was cancelled. Do not proceed.',
};
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
request_id: row.id,
status: row.status,
tool_name: row.tool_name,
action_description: row.action_description,
risk_level: row.risk_level,
requested_at: row.requested_at,
decided_at: row.decided_at ?? null,
decision_note: row.decision_note ?? null,
instruction: instructions[row.status] ?? 'Unknown status',
},
null,
2
),
},
],
};
}
// ── gate_list ─────────────────────────────────────────────────────────────
if (name === 'gate_list') {
const rows = await sb(
'gate_requests?status=eq.pending&order=requested_at.desc&select=*'
);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
pending_count: rows?.length ?? 0,
requests: (rows || []).map((r) => ({
request_id: r.id,
tool_name: r.tool_name,
action_description: r.action_description,
risk_level: r.risk_level,
waiting_since: r.requested_at,
})),
dashboard_url: DASHBOARD_URL,
},
null,
2
),
},
],
};
}
throw new Error(`Unknown tool: ${name}`);
});
// ─── Boot ────────────────────────────────────────────────────────────────────
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
process.stderr.write(
`[agent-gate] MCP server running on stdio — dashboard: ${DASHBOARD_URL}\n`
);
}
main().catch((err) => {
process.stderr.write(`[agent-gate] Fatal: ${err.message}\n`);
process.exit(1);
});