Repository navigation
Expand file tree
/
Copy pathwebhook-example.ts
More file actions
124 lines (112 loc) · 3.61 KB
/
Copy pathwebhook-example.ts
File metadata and controls
124 lines (112 loc) · 3.61 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
// Example implementation of Webhook Management for Strapi MCP
interface WebhookData {
name: string;
url: string;
headers?: Record<string, string>;
events: string[];
}
/**
* List all webhooks in Strapi
*/
async function listWebhooks(): Promise<any[]> {
try {
console.error("[API] Fetching webhooks from Strapi");
const response = await makeAdminApiRequest('/webhooks');
return response || [];
} catch (error: any) {
console.error("[Error] Failed to fetch webhooks:", error);
throw new ExtendedMcpError(ExtendedErrorCode.InternalError, `Failed to fetch webhooks: ${error.message}`);
}
}
/**
* Create a new webhook
*/
async function createWebhook(data: WebhookData): Promise<any> {
try {
console.error(`[API] Creating webhook: ${data.name}`);
const webhookPayload = {
name: data.name,
url: data.url,
headers: data.headers || {},
events: data.events
};
const response = await makeAdminApiRequest('/webhooks', 'post', webhookPayload);
console.error(`[API] Successfully created webhook: ${data.name}`);
return response;
} catch (error: any) {
console.error(`[Error] Failed to create webhook ${data.name}:`, error);
throw new ExtendedMcpError(ExtendedErrorCode.InternalError, `Failed to create webhook: ${error.message}`);
}
}
/**
* Update an existing webhook
*/
async function updateWebhook(id: string, data: WebhookData): Promise<any> {
try {
console.error(`[API] Updating webhook: ${id}`);
const webhookPayload = {
name: data.name,
url: data.url,
headers: data.headers || {},
events: data.events
};
const response = await makeAdminApiRequest(`/webhooks/${id}`, 'put', webhookPayload);
console.error(`[API] Successfully updated webhook: ${id}`);
return response;
} catch (error: any) {
console.error(`[Error] Failed to update webhook ${id}:`, error);
throw new ExtendedMcpError(ExtendedErrorCode.InternalError, `Failed to update webhook: ${error.message}`);
}
}
/**
* Delete a webhook
*/
async function deleteWebhook(id: string): Promise<void> {
try {
console.error(`[API] Deleting webhook: ${id}`);
await makeAdminApiRequest(`/webhooks/${id}`, 'delete');
console.error(`[API] Successfully deleted webhook: ${id}`);
} catch (error: any) {
console.error(`[Error] Failed to delete webhook ${id}:`, error);
throw new ExtendedMcpError(ExtendedErrorCode.InternalError, `Failed to delete webhook: ${error.message}`);
}
}
/**
* Trigger a webhook manually
*/
async function triggerWebhook(id: string): Promise<any> {
try {
console.error(`[API] Triggering webhook: ${id}`);
const response = await makeAdminApiRequest(`/webhooks/${id}/trigger`, 'post');
console.error(`[API] Successfully triggered webhook: ${id}`);
return response;
} catch (error: any) {
console.error(`[Error] Failed to trigger webhook ${id}:`, error);
throw new ExtendedMcpError(ExtendedErrorCode.InternalError, `Failed to trigger webhook: ${error.message}`);
}
}
// Add these to your tool handlers:
/*
case "list_webhooks": {
const webhooks = await listWebhooks();
return {
content: [{
type: "text",
text: JSON.stringify(webhooks, null, 2)
}]
};
}
case "create_webhook": {
const webhookData = request.params.arguments as WebhookData;
if (!webhookData.name || !webhookData.url || !webhookData.events) {
throw new McpError(ErrorCode.InvalidParams, "name, url, and events are required");
}
const webhook = await createWebhook(webhookData);
return {
content: [{
type: "text",
text: JSON.stringify(webhook, null, 2)
}]
};
}
*/