-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
387 lines (333 loc) · 12.4 KB
/
index.ts
File metadata and controls
387 lines (333 loc) · 12.4 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
#!/usr/bin/env bun
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
ListToolsRequestSchema,
CallToolRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { HookdeckClient } from "@hookdeck/sdk";
// ---------------------------------------------------------------------------
// Configuration (environment variables)
// ---------------------------------------------------------------------------
const PORT = parseInt(process.env.HOOKDECK_PORT || "8788", 10);
const API_KEY = process.env.HOOKDECK_API_KEY || "";
const SOURCES = process.env.HOOKDECK_SOURCES
? process.env.HOOKDECK_SOURCES.split(",").map((s) => s.trim()).filter(Boolean)
: [];
const ALLOWED_IPS = process.env.HOOKDECK_ALLOWED_IPS
? process.env.HOOKDECK_ALLOWED_IPS.split(",").map((s) => s.trim()).filter(Boolean)
: [];
const EVENT_FILTER = process.env.HOOKDECK_EVENT_FILTER
? process.env.HOOKDECK_EVENT_FILTER.split(",").map((s) => s.trim()).filter(Boolean)
: [];
// ---------------------------------------------------------------------------
// Logging — stdout is reserved for MCP stdio, so all logging goes to stderr
// ---------------------------------------------------------------------------
function log(msg: string) {
console.error(`[hookdeck-channel] ${msg}`);
}
// ---------------------------------------------------------------------------
// MCP Server — uses low-level Server class per channels reference docs
// ---------------------------------------------------------------------------
const mcp = new Server(
{ name: "hookdeck-channel", version: "1.0.0" },
{
capabilities: {
experimental: { "claude/channel": {} },
tools: {},
},
instructions: [
"Events from the hookdeck-channel arrive as <channel source=\"hookdeck-channel\" ...>.",
"Each event is a webhook forwarded from Hookdeck Event Gateway.",
"Attributes on the tag include: hookdeck_source (the Hookdeck source name),",
"event_type (e.g. push, invoice.paid, build.failed), and hookdeck_event_id.",
"Read the event payload and act on it. For example, investigate CI failures,",
"process payment webhooks, or respond to monitoring alerts.",
"To send an outbound HTTP request in response, use the hookdeck_reply tool.",
].join(" "),
}
);
// ---------------------------------------------------------------------------
// Reply Tool — lets Claude send outbound HTTP requests in response to events
// ---------------------------------------------------------------------------
mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "hookdeck_reply",
description:
"Send an outbound webhook or HTTP request. Useful for replying to events — posting CI comments, acknowledging alerts, triggering downstream services, etc.",
inputSchema: {
type: "object" as const,
properties: {
destination_url: {
type: "string",
description: "URL to POST the response to",
},
body: {
type: "string",
description: "JSON payload to send",
},
headers: {
type: "string",
description:
'Optional HTTP headers as JSON object, e.g. {"Authorization": "Bearer ..."}. Pass "{}" for no extra headers.',
},
},
required: ["destination_url", "body"],
},
},
],
}));
mcp.setRequestHandler(CallToolRequestSchema, async (req) => {
if (req.params.name === "hookdeck_reply") {
const {
destination_url,
body,
headers: headersStr,
} = req.params.arguments as {
destination_url: string;
body: string;
headers?: string;
};
let headers: Record<string, string> = {};
try {
headers = headersStr ? JSON.parse(headersStr) : {};
} catch {
// ignore parse errors, use empty headers
}
try {
const response = await fetch(destination_url, {
method: "POST",
headers: { "Content-Type": "application/json", ...headers },
body,
});
const responseText = await response.text();
return {
content: [
{
type: "text" as const,
text: `Sent to ${destination_url}: ${response.status} ${response.statusText}\n${responseText}`,
},
],
};
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return {
content: [{ type: "text" as const, text: `Error: ${message}` }],
isError: true,
};
}
}
throw new Error(`Unknown tool: ${req.params.name}`);
});
// ---------------------------------------------------------------------------
// Channel notification helper — uses the official notification format
// ---------------------------------------------------------------------------
async function emitChannelEvent(opts: {
sourceName: string;
eventType: string;
eventId: string;
payload: string;
}) {
await mcp.notification({
method: "notifications/claude/channel",
params: {
content: opts.payload,
meta: {
hookdeck_source: opts.sourceName,
event_type: opts.eventType,
hookdeck_event_id: opts.eventId,
},
},
});
log(
`Emitted channel event: ${opts.sourceName} / ${opts.eventType} / ${opts.eventId}`
);
}
// ---------------------------------------------------------------------------
// Sender gating — basic IP allowlist
// ---------------------------------------------------------------------------
function isAllowed(req: Request, server: any): boolean {
// If no allowlist configured, allow all (trust the Hookdeck CLI or local network)
if (ALLOWED_IPS.length === 0) return true;
// Bun's server provides the remote address via server.requestIP(req)
const addr = server?.requestIP?.(req);
const ip = addr?.address || "";
// Always allow localhost
if (ip === "127.0.0.1" || ip === "::1" || ip === "::ffff:127.0.0.1") {
return true;
}
if (ALLOWED_IPS.includes(ip)) return true;
log(`Blocked request from ${ip} (not in HOOKDECK_ALLOWED_IPS)`);
return false;
}
// ---------------------------------------------------------------------------
// Extract metadata from incoming webhook request
// ---------------------------------------------------------------------------
function extractMetadata(req: Request): {
sourceName: string;
eventType: string;
eventId: string;
} {
const headers = req.headers;
const hookdeckSource =
headers.get("x-hookdeck-source-name") ||
headers.get("x-hookdeck-source-id") ||
"unknown";
const hookdeckEventId =
headers.get("x-hookdeck-event-id") || crypto.randomUUID();
// Try to determine event type from well-known webhook headers
const eventType =
headers.get("x-github-event") ||
headers.get("x-gitlab-event") ||
headers.get("x-hookdeck-event-type") ||
"webhook";
return {
sourceName: hookdeckSource,
eventType,
eventId: hookdeckEventId,
};
}
// ---------------------------------------------------------------------------
// HTTP Server — receives forwarded webhooks from Hookdeck CLI
// ---------------------------------------------------------------------------
function startHttpServer() {
const server = Bun.serve({
port: PORT,
hostname: "127.0.0.1",
async fetch(req, server) {
const url = new URL(req.url);
// Health check
if (
req.method === "GET" &&
(url.pathname === "/" || url.pathname === "/health")
) {
return new Response(
JSON.stringify({ status: "ok", channel: "hookdeck" }),
{ headers: { "Content-Type": "application/json" } }
);
}
// Only accept POST for webhooks
if (req.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
// Accept on / and /webhook
if (url.pathname !== "/" && url.pathname !== "/webhook") {
return new Response("Not found", { status: 404 });
}
// Sender gating
if (!isAllowed(req, server)) {
return new Response("Forbidden", { status: 403 });
}
try {
const body = await req.text();
const meta = extractMetadata(req);
// Event type filtering
if (
EVENT_FILTER.length > 0 &&
!EVENT_FILTER.includes(meta.eventType)
) {
log(`Filtered out event type: ${meta.eventType}`);
return new Response(JSON.stringify({ status: "filtered" }), {
headers: { "Content-Type": "application/json" },
});
}
// Extract event type from JSON payload if not in headers
let payload = body;
try {
const parsed = JSON.parse(body);
if (parsed.type && meta.eventType === "webhook") {
meta.eventType = parsed.type;
}
payload = JSON.stringify(parsed);
} catch {
// Not JSON — use raw body
}
await emitChannelEvent({
sourceName: meta.sourceName,
eventType: meta.eventType,
eventId: meta.eventId,
payload,
});
return new Response(
JSON.stringify({ status: "ok", eventId: meta.eventId }),
{ headers: { "Content-Type": "application/json" } }
);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
log(`Error processing webhook: ${message}`);
return new Response(
JSON.stringify({ status: "error", error: message }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
},
});
log(`HTTP server listening on port ${server.port}`);
log(`Webhook endpoint: http://localhost:${server.port}/webhook`);
return server;
}
// ---------------------------------------------------------------------------
// Hookdeck SDK Integration (Approach B) — auto-provision sources/connections
// ---------------------------------------------------------------------------
async function provisionHookdeckConnections() {
if (!API_KEY) {
log(
"No HOOKDECK_API_KEY set — skipping SDK provisioning (Approach A: manual CLI mode)"
);
log(`Run: hookdeck listen ${PORT} <source-name> in another terminal`);
return;
}
if (SOURCES.length === 0) {
log("HOOKDECK_API_KEY is set but no HOOKDECK_SOURCES configured");
log("Set HOOKDECK_SOURCES=source1,source2 to auto-provision connections");
return;
}
log("Provisioning Hookdeck connections via SDK...");
const hookdeck = new HookdeckClient({ token: API_KEY });
for (const sourceName of SOURCES) {
try {
const connection = await hookdeck.connection.upsert({
name: `claude-channel-${sourceName}`,
source: { name: sourceName },
destination: {
name: `claude-code-local-${sourceName}`,
cliPath: "/webhook",
},
});
log(`Source "${sourceName}" → ${connection.source.url}`);
log(` Connection ID: ${connection.id}`);
log(` Point your webhook provider at: ${connection.source.url}`);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
log(`Failed to provision source "${sourceName}": ${message}`);
}
}
log(`\nRun: hookdeck listen ${PORT} --cli-path /webhook`);
log("The CLI will forward matching events to this plugin's HTTP server.");
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
log("Starting Hookdeck Channel Plugin for Claude Code");
// 1. Start the local HTTP server to receive webhooks
try {
startHttpServer();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
log(`Warning: Could not start HTTP server on port ${PORT}: ${message}`);
log("The MCP server will still connect, but webhooks won't be received.");
log("Check if another process is using the port: lsof -ti:" + PORT);
}
// 2. Provision Hookdeck connections if API key is available (Approach B)
await provisionHookdeckConnections();
// 3. Connect MCP server over stdio (Claude Code spawns this process)
const transport = new StdioServerTransport();
await mcp.connect(transport);
log("MCP server connected — channel is live");
}
main().catch((err) => {
log(`Fatal error: ${err}`);
process.exit(1);
});