mcp-http-webhook 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/.eslintrc.json +16 -0
  2. package/.prettierrc.json +8 -0
  3. package/ARCHITECTURE.md +269 -0
  4. package/CONTRIBUTING.md +136 -0
  5. package/GETTING_STARTED.md +310 -0
  6. package/IMPLEMENTATION.md +294 -0
  7. package/LICENSE +21 -0
  8. package/MIGRATION_TO_SDK.md +263 -0
  9. package/README.md +496 -0
  10. package/SDK_INTEGRATION_COMPLETE.md +300 -0
  11. package/STANDARD_SUBSCRIPTIONS.md +268 -0
  12. package/STANDARD_SUBSCRIPTIONS_COMPLETE.md +309 -0
  13. package/SUMMARY.md +272 -0
  14. package/Spec.md +2778 -0
  15. package/dist/errors/index.d.ts +52 -0
  16. package/dist/errors/index.d.ts.map +1 -0
  17. package/dist/errors/index.js +81 -0
  18. package/dist/errors/index.js.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +37 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/protocol/ProtocolHandler.d.ts +37 -0
  24. package/dist/protocol/ProtocolHandler.d.ts.map +1 -0
  25. package/dist/protocol/ProtocolHandler.js +172 -0
  26. package/dist/protocol/ProtocolHandler.js.map +1 -0
  27. package/dist/server.d.ts +6 -0
  28. package/dist/server.d.ts.map +1 -0
  29. package/dist/server.js +502 -0
  30. package/dist/server.js.map +1 -0
  31. package/dist/stores/InMemoryStore.d.ts +27 -0
  32. package/dist/stores/InMemoryStore.d.ts.map +1 -0
  33. package/dist/stores/InMemoryStore.js +73 -0
  34. package/dist/stores/InMemoryStore.js.map +1 -0
  35. package/dist/stores/RedisStore.d.ts +18 -0
  36. package/dist/stores/RedisStore.d.ts.map +1 -0
  37. package/dist/stores/RedisStore.js +45 -0
  38. package/dist/stores/RedisStore.js.map +1 -0
  39. package/dist/stores/index.d.ts +3 -0
  40. package/dist/stores/index.d.ts.map +1 -0
  41. package/dist/stores/index.js +9 -0
  42. package/dist/stores/index.js.map +1 -0
  43. package/dist/subscriptions/SubscriptionManager.d.ts +49 -0
  44. package/dist/subscriptions/SubscriptionManager.d.ts.map +1 -0
  45. package/dist/subscriptions/SubscriptionManager.js +181 -0
  46. package/dist/subscriptions/SubscriptionManager.js.map +1 -0
  47. package/dist/types/index.d.ts +271 -0
  48. package/dist/types/index.d.ts.map +1 -0
  49. package/dist/types/index.js +16 -0
  50. package/dist/types/index.js.map +1 -0
  51. package/dist/utils/index.d.ts +51 -0
  52. package/dist/utils/index.d.ts.map +1 -0
  53. package/dist/utils/index.js +154 -0
  54. package/dist/utils/index.js.map +1 -0
  55. package/dist/webhooks/WebhookManager.d.ts +27 -0
  56. package/dist/webhooks/WebhookManager.d.ts.map +1 -0
  57. package/dist/webhooks/WebhookManager.js +174 -0
  58. package/dist/webhooks/WebhookManager.js.map +1 -0
  59. package/examples/GITHUB_LIVE_EXAMPLE.md +308 -0
  60. package/examples/GITHUB_LIVE_SETUP.md +253 -0
  61. package/examples/QUICKSTART.md +130 -0
  62. package/examples/basic-setup.ts +142 -0
  63. package/examples/github-server-live.ts +690 -0
  64. package/examples/github-server.ts +223 -0
  65. package/examples/google-drive-server-live.ts +773 -0
  66. package/examples/start-github-live.sh +53 -0
  67. package/jest.config.js +20 -0
  68. package/package.json +58 -0
  69. package/src/errors/index.ts +81 -0
  70. package/src/index.ts +19 -0
  71. package/src/server.ts +595 -0
  72. package/src/stores/InMemoryStore.ts +87 -0
  73. package/src/stores/RedisStore.ts +51 -0
  74. package/src/stores/index.ts +2 -0
  75. package/src/subscriptions/SubscriptionManager.ts +240 -0
  76. package/src/types/index.ts +341 -0
  77. package/src/utils/index.ts +156 -0
  78. package/src/webhooks/WebhookManager.ts +230 -0
  79. package/test-sdk-integration.sh +157 -0
  80. package/tsconfig.json +21 -0
package/README.md ADDED
@@ -0,0 +1,496 @@
1
+ # MCP HTTP Webhook Server Library
2
+
3
+ ![Version](https://img.shields.io/badge/version-1.0.0-blue.svg)
4
+ ![License](https://img.shields.io/badge/license-MIT-green.svg)
5
+
6
+ Production-ready TypeScript library for building Model Context Protocol (MCP) servers using the **official `@modelcontextprotocol/sdk`** with HTTP transport and webhook-based resource subscriptions.
7
+
8
+ ## โœจ Key Highlights
9
+
10
+ - **๐ŸŽฏ Standard MCP Compliant**: Built on official `@modelcontextprotocol/sdk` - works with all MCP clients
11
+ - **๐Ÿ”Œ Universal Client Support**: Compatible with MCP Inspector, Claude Desktop, VS Code, Cursor, and more
12
+ - **๐Ÿ“ก Webhook Subscriptions**: Native support for third-party webhooks (GitHub, Google Drive, Slack, etc.)
13
+ - **๐Ÿš€ Horizontally Scalable**: Stateless HTTP design for trivial multi-instance deployment
14
+ - **๐Ÿ”’ Production Ready**: Built-in retry logic, signature verification, and persistent storage
15
+ - **๐Ÿ“˜ Type Safe**: Full TypeScript support with comprehensive type definitions
16
+ - **๐Ÿ’พ Flexible Storage**: Pluggable key-value store (Redis, in-memory, or custom)
17
+
18
+ ## ๐Ÿ†• v1.0 Update
19
+
20
+ This library now uses the **standard MCP SDK** (`@modelcontextprotocol/sdk`) while preserving all webhook subscription, authentication, and persistence features.
21
+
22
+ **[๐Ÿ“– Migration Guide](./MIGRATION_TO_SDK.md)** - Upgrading from custom protocol? Read this first!
23
+
24
+ ## ๐Ÿ“ฆ Installation
25
+
26
+ ```bash
27
+ npm install mcp-http-webhook zod
28
+ # or
29
+ pnpm add mcp-http-webhook zod
30
+ # or
31
+ yarn add mcp-http-webhook zod
32
+ ```
33
+
34
+ ### Peer Dependencies
35
+
36
+ ```bash
37
+ npm install @modelcontextprotocol/sdk express
38
+ ```
39
+
40
+ ### Optional Dependencies
41
+
42
+ ```bash
43
+ npm install ioredis # For Redis store
44
+ npm install prom-client # For Prometheus metrics
45
+ ```
46
+
47
+ ## ๐ŸŽฏ Quick Start
48
+
49
+ ```typescript
50
+ import { createMCPServer, InMemoryStore } from 'mcp-http-webhook';
51
+
52
+ const server = createMCPServer({
53
+ name: 'my-mcp-server',
54
+ version: '1.0.0',
55
+ publicUrl: 'https://mcp.example.com',
56
+ store: new InMemoryStore(),
57
+
58
+ tools: [
59
+ {
60
+ name: 'echo',
61
+ description: 'Echo back the input',
62
+ inputSchema: {
63
+ type: 'object',
64
+ properties: {
65
+ message: { type: 'string' }
66
+ },
67
+ required: ['message']
68
+ },
69
+ handler: async (input) => {
70
+ return { echo: input.message };
71
+ }
72
+ }
73
+ ],
74
+
75
+ resources: [
76
+ {
77
+ uri: 'example://resource',
78
+ name: 'Example Resource',
79
+ description: 'An example resource',
80
+ read: async (uri) => {
81
+ return { contents: [{ uri, text: 'Hello, World!' }] };
82
+ }
83
+ }
84
+ ]
85
+ });
86
+
87
+ await server.start();
88
+ console.log('Server running on http://localhost:3000');
89
+ ```
90
+
91
+ ## ๐Ÿ”Œ Client Integration
92
+
93
+ ### With MCP Inspector
94
+
95
+ ```bash
96
+ npx @modelcontextprotocol/inspector http://localhost:3000/mcp
97
+ ```
98
+
99
+ ### With Claude Desktop / VS Code
100
+
101
+ Add to your MCP configuration:
102
+
103
+ ```json
104
+ {
105
+ "mcpServers": {
106
+ "my-server": {
107
+ "url": "http://localhost:3000/mcp"
108
+ }
109
+ }
110
+ }
111
+ ```
112
+
113
+ ## ๐Ÿ“š Documentation
114
+
115
+ ### Configuration
116
+
117
+ ```typescript
118
+ interface MCPServerConfig {
119
+ // Server Identity
120
+ name: string;
121
+ version: string;
122
+
123
+ // HTTP Configuration
124
+ port?: number; // Default: 3000
125
+ host?: string; // Default: '0.0.0.0'
126
+ basePath?: string; // Default: '/mcp'
127
+ publicUrl: string; // Required
128
+
129
+ // Core Components
130
+ tools: ToolDefinition[];
131
+ resources: ResourceDefinition[];
132
+ prompts?: PromptDefinition[];
133
+
134
+ // Storage (Required)
135
+ store: KeyValueStore;
136
+
137
+ // Authentication
138
+ authenticate?: (req: Request) => Promise<AuthContext>;
139
+
140
+ // Webhook Configuration
141
+ webhooks?: WebhookConfig;
142
+
143
+ // Logging
144
+ logger?: Logger;
145
+ logLevel?: 'debug' | 'info' | 'warn' | 'error';
146
+ }
147
+ ```
148
+
149
+ ### Tools
150
+
151
+ Tools are functions that can be called by clients:
152
+
153
+ ```typescript
154
+ {
155
+ name: 'create_issue',
156
+ description: 'Create a GitHub issue',
157
+ inputSchema: {
158
+ type: 'object',
159
+ properties: {
160
+ title: { type: 'string' },
161
+ body: { type: 'string' }
162
+ },
163
+ required: ['title']
164
+ },
165
+ handler: async (input, context) => {
166
+ // Your logic here
167
+ return { issue_id: 123 };
168
+ }
169
+ }
170
+ ```
171
+
172
+ ### Resources
173
+
174
+ Resources represent data that can be read and subscribed to:
175
+
176
+ ```typescript
177
+ {
178
+ uri: 'github://repo/{owner}/{repo}/issues',
179
+ name: 'GitHub Issues',
180
+ description: 'Repository issues',
181
+
182
+ read: async (uri, context) => {
183
+ return { contents: [...] };
184
+ },
185
+
186
+ list: async (context) => {
187
+ return [
188
+ { uri: '...', name: '...' }
189
+ ];
190
+ },
191
+
192
+ subscription: {
193
+ onSubscribe: async (uri, subscriptionId, webhookUrl, context) => {
194
+ // Register webhook with third-party service
195
+ return { thirdPartyWebhookId: 'webhook_123' };
196
+ },
197
+
198
+ onUnsubscribe: async (uri, subscriptionId, storedData, context) => {
199
+ // Remove webhook from third-party service
200
+ },
201
+
202
+ onWebhook: async (subscriptionId, payload, headers) => {
203
+ // Process incoming webhook
204
+ return {
205
+ resourceUri: uri,
206
+ changeType: 'created',
207
+ data: { ... }
208
+ };
209
+ }
210
+ }
211
+ }
212
+ ```
213
+
214
+ ### Storage
215
+
216
+ The library requires a key-value store for subscription persistence:
217
+
218
+ **In-Memory (Development Only):**
219
+ ```typescript
220
+ import { InMemoryStore } from 'mcp-http-webhook/stores';
221
+ const store = new InMemoryStore();
222
+ ```
223
+
224
+ **Redis:**
225
+ ```typescript
226
+ import { createRedisStore } from 'mcp-http-webhook/stores';
227
+ import Redis from 'ioredis';
228
+
229
+ const redis = new Redis(process.env.REDIS_URL);
230
+ const store = createRedisStore(redis);
231
+ ```
232
+
233
+ **Custom Implementation:**
234
+ ```typescript
235
+ import { KeyValueStore } from 'mcp-http-webhook';
236
+
237
+ class MyStore implements KeyValueStore {
238
+ async get(key: string): Promise<string | null> { ... }
239
+ async set(key: string, value: string, ttl?: number): Promise<void> { ... }
240
+ async delete(key: string): Promise<void> { ... }
241
+ async scan?(pattern: string): Promise<string[]> { ... }
242
+ }
243
+ ```
244
+
245
+ ### Authentication
246
+
247
+ ```typescript
248
+ authenticate: async (req) => {
249
+ const token = req.headers.authorization?.replace('Bearer ', '');
250
+
251
+ // Verify token
252
+ const payload = await verifyJWT(token);
253
+
254
+ return {
255
+ userId: payload.sub,
256
+ // Add any custom context
257
+ permissions: payload.permissions
258
+ };
259
+ }
260
+ ```
261
+
262
+ ### Webhooks
263
+
264
+ Configure webhook behavior:
265
+
266
+ ```typescript
267
+ webhooks: {
268
+ incomingPath: '/webhooks/incoming',
269
+ incomingSecret: process.env.WEBHOOK_SECRET,
270
+
271
+ verifyIncomingSignature: (payload, signature, secret) => {
272
+ // Verify third-party webhook signature
273
+ return verifyHmacSignature(payload, signature, secret);
274
+ },
275
+
276
+ outgoing: {
277
+ timeout: 5000,
278
+ retries: 3,
279
+ retryDelay: 1000,
280
+
281
+ signPayload: (payload, secret) => {
282
+ // Sign outgoing notifications
283
+ return createHmacSignature(payload, secret);
284
+ }
285
+ }
286
+ }
287
+ ```
288
+
289
+ ## ๐Ÿ”ง API Endpoints
290
+
291
+ The server automatically exposes these endpoints:
292
+
293
+ ### Health Checks
294
+ - `GET /health` - Basic health check
295
+ - `GET /ready` - Readiness check (includes store connectivity)
296
+
297
+ ### MCP Protocol
298
+ - `POST /mcp/tools/list` - List available tools
299
+ - `POST /mcp/tools/call` - Call a tool
300
+ - `POST /mcp/resources/list` - List available resources
301
+ - `POST /mcp/resources/read` - Read a resource
302
+ - `POST /mcp/resources/subscribe` - Subscribe to resource changes
303
+ - `POST /mcp/resources/unsubscribe` - Unsubscribe from resource
304
+ - `POST /mcp/prompts/list` - List available prompts
305
+ - `POST /mcp/prompts/get` - Get a prompt
306
+
307
+ ### Webhooks
308
+ - `POST /webhooks/incoming/{subscriptionId}` - Receive third-party webhooks
309
+
310
+ ## ๐Ÿ“– Examples
311
+
312
+ See the [`examples/`](./examples) directory for complete examples:
313
+
314
+ - **[basic-setup.ts](./examples/basic-setup.ts)** - Simple server with tools and resources
315
+ - **[github-server.ts](./examples/github-server.ts)** - GitHub integration with webhooks
316
+
317
+ ## ๐Ÿ”’ Security
318
+
319
+ ### HTTPS Only in Production
320
+
321
+ ```typescript
322
+ // Automatically enforced in production
323
+ publicUrl: 'https://mcp.example.com' // Must use HTTPS
324
+ ```
325
+
326
+ ### Signature Verification
327
+
328
+ Always verify webhook signatures:
329
+
330
+ ```typescript
331
+ import crypto from 'crypto';
332
+
333
+ verifyIncomingSignature: (payload, signature, secret) => {
334
+ const hmac = crypto.createHmac('sha256', secret);
335
+ hmac.update(JSON.stringify(payload));
336
+ const expected = `sha256=${hmac.digest('hex')}`;
337
+ return crypto.timingSafeEqual(
338
+ Buffer.from(signature),
339
+ Buffer.from(expected)
340
+ );
341
+ }
342
+ ```
343
+
344
+ ### Rate Limiting
345
+
346
+ Add rate limiting middleware:
347
+
348
+ ```typescript
349
+ import rateLimit from 'express-rate-limit';
350
+
351
+ const server = createMCPServer({
352
+ // ...
353
+ middleware: [
354
+ rateLimit({
355
+ windowMs: 15 * 60 * 1000,
356
+ max: 100
357
+ })
358
+ ]
359
+ });
360
+ ```
361
+
362
+ ## ๐Ÿš€ Deployment
363
+
364
+ ### Docker
365
+
366
+ ```dockerfile
367
+ FROM node:20-alpine
368
+ WORKDIR /app
369
+ COPY package*.json ./
370
+ RUN npm ci --production
371
+ COPY . .
372
+ RUN npm run build
373
+ EXPOSE 3000
374
+ CMD ["node", "dist/index.js"]
375
+ ```
376
+
377
+ ### Docker Compose
378
+
379
+ ```yaml
380
+ version: '3.8'
381
+ services:
382
+ mcp-server:
383
+ build: .
384
+ ports:
385
+ - "3000:3000"
386
+ environment:
387
+ - PUBLIC_URL=https://mcp.example.com
388
+ - REDIS_URL=redis://redis:6379
389
+ depends_on:
390
+ - redis
391
+
392
+ redis:
393
+ image: redis:7-alpine
394
+ volumes:
395
+ - redis-data:/data
396
+
397
+ volumes:
398
+ redis-data:
399
+ ```
400
+
401
+ ### Environment Variables
402
+
403
+ ```bash
404
+ # Server
405
+ PORT=3000
406
+ PUBLIC_URL=https://mcp.example.com
407
+ NODE_ENV=production
408
+
409
+ # Storage
410
+ REDIS_URL=redis://localhost:6379
411
+
412
+ # Webhooks
413
+ WEBHOOK_SECRET=your-secret-here
414
+
415
+ # Auth
416
+ JWT_SECRET=your-jwt-secret
417
+ ```
418
+
419
+ ## ๐Ÿงช Testing
420
+
421
+ ```bash
422
+ # Run tests
423
+ npm test
424
+
425
+ # Run with coverage
426
+ npm test -- --coverage
427
+
428
+ # Run specific test
429
+ npm test -- SubscriptionManager
430
+ ```
431
+
432
+ ## ๐Ÿ“Š Monitoring
433
+
434
+ ### Logging
435
+
436
+ ```typescript
437
+ import winston from 'winston';
438
+
439
+ const logger = winston.createLogger({
440
+ level: 'info',
441
+ format: winston.format.json(),
442
+ transports: [
443
+ new winston.transports.Console()
444
+ ]
445
+ });
446
+
447
+ const server = createMCPServer({
448
+ logger,
449
+ logLevel: 'info'
450
+ });
451
+ ```
452
+
453
+ ### Metrics
454
+
455
+ Enable Prometheus metrics:
456
+
457
+ ```typescript
458
+ import promClient from 'prom-client';
459
+
460
+ const register = new promClient.Registry();
461
+ promClient.collectDefaultMetrics({ register });
462
+
463
+ const server = createMCPServer({
464
+ metrics: {
465
+ enabled: true,
466
+ registry: register
467
+ }
468
+ });
469
+
470
+ // Metrics available at GET /metrics
471
+ ```
472
+
473
+ ## ๐Ÿค Contributing
474
+
475
+ Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for details.
476
+
477
+ ## ๐Ÿ“„ License
478
+
479
+ MIT License - see [LICENSE](./LICENSE) file for details.
480
+
481
+ ## ๐Ÿ”— Links
482
+
483
+ - [MCP Specification](https://spec.modelcontextprotocol.io/)
484
+ - [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk)
485
+ - [Documentation](https://docs.mcp-http-webhook.dev)
486
+ - [Examples](./examples)
487
+
488
+ ## ๐Ÿ’ฌ Support
489
+
490
+ - [GitHub Issues](https://github.com/your-org/mcp-http-webhook/issues)
491
+ - [Discord Community](https://discord.gg/mcp-webhook)
492
+ - [Stack Overflow](https://stackoverflow.com/questions/tagged/mcp-http-webhook)
493
+
494
+ ---
495
+
496
+ Made with โค๏ธ by the MCP Community