@mcp-weave/nestjs 0.1.1 → 0.3.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.
package/README.md CHANGED
@@ -27,6 +27,7 @@ pnpm add @mcp-weave/nestjs reflect-metadata
27
27
  - **Method Decorators** - `@McpTool`, `@McpResource`, `@McpPrompt`
28
28
  - **Parameter Decorators** - `@McpInput`, `@McpParam`, `@McpPromptArg`
29
29
  - **Runtime Server** - Start MCP servers from decorated classes
30
+ - **Multiple Transports** - Stdio (default) and SSE (Server-Sent Events)
30
31
 
31
32
  ## Quick Start
32
33
 
@@ -215,6 +216,46 @@ const server = new McpRuntimeServer(MyServer, {
215
216
  await server.start();
216
217
  ```
217
218
 
219
+ ### SSE Transport (Server-Sent Events)
220
+
221
+ For web-based integrations, use the SSE transport:
222
+
223
+ ```typescript
224
+ import { McpRuntimeServer } from '@mcp-weave/nestjs';
225
+
226
+ const server = new McpRuntimeServer(MyServer, {
227
+ transport: 'sse',
228
+ port: 3000,
229
+ endpoint: '/sse',
230
+ });
231
+
232
+ // Starts HTTP server with SSE endpoint
233
+ const httpServer = await server.start();
234
+
235
+ // Server available at:
236
+ // - SSE endpoint: http://localhost:3000/sse
237
+ // - Health check: http://localhost:3000/health
238
+ ```
239
+
240
+ Or use the `startSSE` method directly:
241
+
242
+ ```typescript
243
+ const server = new McpRuntimeServer(MyServer);
244
+
245
+ // Start with SSE transport
246
+ const httpServer = await server.startSSE({
247
+ port: 8080,
248
+ endpoint: '/mcp',
249
+ });
250
+ ```
251
+
252
+ **SSE Features:**
253
+
254
+ - CORS enabled by default
255
+ - Health check endpoint at `/health`
256
+ - Session management for multiple clients
257
+ - Automatic cleanup on connection close
258
+
218
259
  ## Metadata Extraction
219
260
 
220
261
  Extract metadata from decorated classes for code generation:
package/dist/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ToolInputSchema, PromptArgument, McpParamMetadata, McpPromptMetadata, McpResourceMetadata, McpServerMetadata, McpToolMetadata } from '@mcp-weave/core';
2
2
  export { METADATA_KEYS, McpParamMetadata, McpPromptMetadata, McpResourceMetadata, McpServerMetadata, McpToolMetadata, ScannedMetadata, extractMetadata } from '@mcp-weave/core';
3
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
3
+ import { IncomingMessage, ServerResponse, Server } from 'http';
4
+ import { Server as Server$1 } from '@modelcontextprotocol/sdk/server/index.js';
4
5
 
5
6
  /**
6
7
  * Options for @McpServer decorator
@@ -204,14 +205,142 @@ declare function getPromptsMetadata(target: Function): McpPromptMetadata[];
204
205
  */
205
206
  declare function getParamsMetadata(target: Function): McpParamMetadata[];
206
207
 
208
+ /**
209
+ * @mcp-weave/nestjs - Authentication Module
210
+ * Provides API key authentication and request tracking
211
+ */
212
+
213
+ /**
214
+ * API Key configuration
215
+ */
216
+ interface ApiKeyConfig {
217
+ /** The API key value */
218
+ key: string;
219
+ /** Human-readable name for the key (for logging) */
220
+ name?: string;
221
+ /** Optional metadata associated with this key */
222
+ metadata?: Record<string, unknown>;
223
+ /** Scopes/permissions for this key */
224
+ scopes?: string[];
225
+ }
226
+ /**
227
+ * Authentication options for MCP servers
228
+ */
229
+ interface McpAuthOptions {
230
+ /**
231
+ * Enable authentication (default: false)
232
+ */
233
+ enabled?: boolean;
234
+ /**
235
+ * API keys allowed to access the server
236
+ * Can be a single key string, array of keys, or array of ApiKeyConfig objects
237
+ */
238
+ apiKeys?: string | string[] | ApiKeyConfig[];
239
+ /**
240
+ * Header name for the API key (default: 'x-api-key')
241
+ */
242
+ headerName?: string;
243
+ /**
244
+ * Query parameter name for the API key (default: 'api_key')
245
+ */
246
+ queryParamName?: string;
247
+ /**
248
+ * Custom authentication function
249
+ * Return true to allow, false to deny, or an AuthResult object
250
+ */
251
+ authenticate?: (token: string | undefined, request: IncomingMessage) => boolean | AuthResult | Promise<boolean | AuthResult>;
252
+ /**
253
+ * Called when authentication fails
254
+ */
255
+ onAuthFailure?: (request: IncomingMessage, reason: string) => void;
256
+ /**
257
+ * Called when authentication succeeds
258
+ */
259
+ onAuthSuccess?: (request: IncomingMessage, result: AuthResult) => void;
260
+ }
261
+ /**
262
+ * Result of authentication
263
+ */
264
+ interface AuthResult {
265
+ /** Whether authentication succeeded */
266
+ success: boolean;
267
+ /** Client identifier (for tracking) */
268
+ clientId?: string;
269
+ /** Client name (for logging) */
270
+ clientName?: string;
271
+ /** Scopes/permissions granted */
272
+ scopes?: string[];
273
+ /** Additional metadata */
274
+ metadata?: Record<string, unknown>;
275
+ /** Error message if authentication failed */
276
+ error?: string;
277
+ }
278
+ /**
279
+ * Request context with authentication info
280
+ */
281
+ interface AuthenticatedRequest {
282
+ /** The original request */
283
+ request: IncomingMessage;
284
+ /** Authentication result */
285
+ auth: AuthResult;
286
+ /** Request ID for tracking */
287
+ requestId: string;
288
+ /** Timestamp of the request */
289
+ timestamp: Date;
290
+ }
291
+ /**
292
+ * Normalize API keys configuration to ApiKeyConfig array
293
+ */
294
+ declare function normalizeApiKeys(keys: string | string[] | ApiKeyConfig[] | undefined): ApiKeyConfig[];
295
+ /**
296
+ * Extract API key from request
297
+ */
298
+ declare function extractApiKey(req: IncomingMessage, headerName?: string, queryParamName?: string): string | undefined;
299
+ /**
300
+ * Validate API key against configured keys
301
+ */
302
+ declare function validateApiKey(token: string | undefined, apiKeys: ApiKeyConfig[]): AuthResult;
303
+ /**
304
+ * Hash a token for safe logging (shows first 4 and last 4 chars)
305
+ */
306
+ declare function hashToken(token: string): string;
307
+ /**
308
+ * Generate a unique request ID
309
+ */
310
+ declare function generateRequestId(): string;
311
+ /**
312
+ * Create an authentication middleware for HTTP requests
313
+ */
314
+ declare function createAuthMiddleware(options?: McpAuthOptions): (req: IncomingMessage, res: ServerResponse) => Promise<AuthenticatedRequest | null>;
315
+ /**
316
+ * Send unauthorized response
317
+ */
318
+ declare function sendUnauthorized(res: ServerResponse, message?: string, requestId?: string): void;
319
+ /**
320
+ * Generate a random API key
321
+ */
322
+ declare function generateApiKey(prefix?: string): string;
323
+
207
324
  /**
208
325
  * Options for MCP runtime server
209
326
  */
210
327
  interface McpRuntimeOptions {
211
328
  /**
212
- * Transport type
329
+ * Transport type: 'stdio' (default), 'sse', or 'websocket'
330
+ */
331
+ transport?: 'stdio' | 'sse' | 'websocket';
332
+ /**
333
+ * Port for SSE/WebSocket transport (default: 3000)
213
334
  */
214
- transport?: 'stdio' | 'sse';
335
+ port?: number;
336
+ /**
337
+ * Endpoint path for SSE/WebSocket (default: '/sse' or '/ws')
338
+ */
339
+ endpoint?: string;
340
+ /**
341
+ * Authentication options
342
+ */
343
+ auth?: McpAuthOptions;
215
344
  }
216
345
  /**
217
346
  * Runtime MCP server that wraps a decorated class
@@ -220,7 +349,9 @@ declare class McpRuntimeServer {
220
349
  private server;
221
350
  private instance;
222
351
  private metadata;
223
- constructor(target: Function, _options?: McpRuntimeOptions);
352
+ private authOptions;
353
+ private authMiddleware;
354
+ constructor(target: Function, options?: McpRuntimeOptions);
224
355
  private setupHandlers;
225
356
  private setupToolHandlers;
226
357
  private setupResourceHandlers;
@@ -230,19 +361,49 @@ declare class McpRuntimeServer {
230
361
  private resolvePromptArgs;
231
362
  private extractUriParams;
232
363
  /**
233
- * Start the MCP server
364
+ * Start the MCP server with stdio transport
234
365
  */
235
366
  start(): Promise<void>;
367
+ /**
368
+ * Start the MCP server with SSE transport
369
+ */
370
+ startSSE(options?: {
371
+ port?: number;
372
+ endpoint?: string;
373
+ }): Promise<Server>;
236
374
  /**
237
375
  * Get the underlying MCP server instance
238
376
  */
239
- getServer(): Server;
377
+ getServer(): Server$1;
378
+ /**
379
+ * Start the MCP server with WebSocket transport
380
+ */
381
+ startWebSocket(options?: {
382
+ port?: number;
383
+ endpoint?: string;
384
+ }): Promise<Server>;
385
+ /**
386
+ * Handle WebSocket JSON-RPC messages
387
+ */
388
+ private handleWebSocketMessage;
240
389
  }
241
390
  /**
242
391
  * Create and start an MCP server from a decorated class
392
+ *
393
+ * @example
394
+ * // Stdio transport (default)
395
+ * await createMcpServer(MyServer);
396
+ *
397
+ * @example
398
+ * // SSE transport
399
+ * await createMcpServer(MyServer, { transport: 'sse', port: 3000 });
400
+ *
401
+ * @example
402
+ * // WebSocket transport
403
+ * await createMcpServer(MyServer, { transport: 'websocket', port: 8080 });
243
404
  */
244
405
  declare function createMcpServer(target: Function, options?: McpRuntimeOptions): Promise<McpRuntimeServer>;
245
406
 
246
407
  declare const VERSION = "0.1.0";
247
408
 
248
- export { McpInput, McpParam, McpPrompt, McpPromptArg, type McpPromptOptions, McpResource, type McpResourceOptions, type McpRuntimeOptions, McpRuntimeServer, McpServer, type McpServerOptions, McpTool, type McpToolOptions, VERSION, createMcpServer, getMcpServers, getParamsMetadata, getPromptsMetadata, getResourcesMetadata, getServerMetadata, getToolsMetadata, isMcpServer };
409
+ export { type ApiKeyConfig, type AuthResult, type AuthenticatedRequest, type McpAuthOptions, McpInput, McpParam, McpPrompt, McpPromptArg, type McpPromptOptions, McpResource, type McpResourceOptions, type McpRuntimeOptions, McpRuntimeServer, McpServer, type McpServerOptions, McpTool, type McpToolOptions, VERSION, createAuthMiddleware, createMcpServer, extractApiKey, generateApiKey, generateRequestId, getMcpServers, getParamsMetadata, getPromptsMetadata, getResourcesMetadata, getServerMetadata, getToolsMetadata, hashToken, isMcpServer, normalizeApiKeys, sendUnauthorized, validateApiKey };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ToolInputSchema, PromptArgument, McpParamMetadata, McpPromptMetadata, McpResourceMetadata, McpServerMetadata, McpToolMetadata } from '@mcp-weave/core';
2
2
  export { METADATA_KEYS, McpParamMetadata, McpPromptMetadata, McpResourceMetadata, McpServerMetadata, McpToolMetadata, ScannedMetadata, extractMetadata } from '@mcp-weave/core';
3
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
3
+ import { IncomingMessage, ServerResponse, Server } from 'http';
4
+ import { Server as Server$1 } from '@modelcontextprotocol/sdk/server/index.js';
4
5
 
5
6
  /**
6
7
  * Options for @McpServer decorator
@@ -204,14 +205,142 @@ declare function getPromptsMetadata(target: Function): McpPromptMetadata[];
204
205
  */
205
206
  declare function getParamsMetadata(target: Function): McpParamMetadata[];
206
207
 
208
+ /**
209
+ * @mcp-weave/nestjs - Authentication Module
210
+ * Provides API key authentication and request tracking
211
+ */
212
+
213
+ /**
214
+ * API Key configuration
215
+ */
216
+ interface ApiKeyConfig {
217
+ /** The API key value */
218
+ key: string;
219
+ /** Human-readable name for the key (for logging) */
220
+ name?: string;
221
+ /** Optional metadata associated with this key */
222
+ metadata?: Record<string, unknown>;
223
+ /** Scopes/permissions for this key */
224
+ scopes?: string[];
225
+ }
226
+ /**
227
+ * Authentication options for MCP servers
228
+ */
229
+ interface McpAuthOptions {
230
+ /**
231
+ * Enable authentication (default: false)
232
+ */
233
+ enabled?: boolean;
234
+ /**
235
+ * API keys allowed to access the server
236
+ * Can be a single key string, array of keys, or array of ApiKeyConfig objects
237
+ */
238
+ apiKeys?: string | string[] | ApiKeyConfig[];
239
+ /**
240
+ * Header name for the API key (default: 'x-api-key')
241
+ */
242
+ headerName?: string;
243
+ /**
244
+ * Query parameter name for the API key (default: 'api_key')
245
+ */
246
+ queryParamName?: string;
247
+ /**
248
+ * Custom authentication function
249
+ * Return true to allow, false to deny, or an AuthResult object
250
+ */
251
+ authenticate?: (token: string | undefined, request: IncomingMessage) => boolean | AuthResult | Promise<boolean | AuthResult>;
252
+ /**
253
+ * Called when authentication fails
254
+ */
255
+ onAuthFailure?: (request: IncomingMessage, reason: string) => void;
256
+ /**
257
+ * Called when authentication succeeds
258
+ */
259
+ onAuthSuccess?: (request: IncomingMessage, result: AuthResult) => void;
260
+ }
261
+ /**
262
+ * Result of authentication
263
+ */
264
+ interface AuthResult {
265
+ /** Whether authentication succeeded */
266
+ success: boolean;
267
+ /** Client identifier (for tracking) */
268
+ clientId?: string;
269
+ /** Client name (for logging) */
270
+ clientName?: string;
271
+ /** Scopes/permissions granted */
272
+ scopes?: string[];
273
+ /** Additional metadata */
274
+ metadata?: Record<string, unknown>;
275
+ /** Error message if authentication failed */
276
+ error?: string;
277
+ }
278
+ /**
279
+ * Request context with authentication info
280
+ */
281
+ interface AuthenticatedRequest {
282
+ /** The original request */
283
+ request: IncomingMessage;
284
+ /** Authentication result */
285
+ auth: AuthResult;
286
+ /** Request ID for tracking */
287
+ requestId: string;
288
+ /** Timestamp of the request */
289
+ timestamp: Date;
290
+ }
291
+ /**
292
+ * Normalize API keys configuration to ApiKeyConfig array
293
+ */
294
+ declare function normalizeApiKeys(keys: string | string[] | ApiKeyConfig[] | undefined): ApiKeyConfig[];
295
+ /**
296
+ * Extract API key from request
297
+ */
298
+ declare function extractApiKey(req: IncomingMessage, headerName?: string, queryParamName?: string): string | undefined;
299
+ /**
300
+ * Validate API key against configured keys
301
+ */
302
+ declare function validateApiKey(token: string | undefined, apiKeys: ApiKeyConfig[]): AuthResult;
303
+ /**
304
+ * Hash a token for safe logging (shows first 4 and last 4 chars)
305
+ */
306
+ declare function hashToken(token: string): string;
307
+ /**
308
+ * Generate a unique request ID
309
+ */
310
+ declare function generateRequestId(): string;
311
+ /**
312
+ * Create an authentication middleware for HTTP requests
313
+ */
314
+ declare function createAuthMiddleware(options?: McpAuthOptions): (req: IncomingMessage, res: ServerResponse) => Promise<AuthenticatedRequest | null>;
315
+ /**
316
+ * Send unauthorized response
317
+ */
318
+ declare function sendUnauthorized(res: ServerResponse, message?: string, requestId?: string): void;
319
+ /**
320
+ * Generate a random API key
321
+ */
322
+ declare function generateApiKey(prefix?: string): string;
323
+
207
324
  /**
208
325
  * Options for MCP runtime server
209
326
  */
210
327
  interface McpRuntimeOptions {
211
328
  /**
212
- * Transport type
329
+ * Transport type: 'stdio' (default), 'sse', or 'websocket'
330
+ */
331
+ transport?: 'stdio' | 'sse' | 'websocket';
332
+ /**
333
+ * Port for SSE/WebSocket transport (default: 3000)
213
334
  */
214
- transport?: 'stdio' | 'sse';
335
+ port?: number;
336
+ /**
337
+ * Endpoint path for SSE/WebSocket (default: '/sse' or '/ws')
338
+ */
339
+ endpoint?: string;
340
+ /**
341
+ * Authentication options
342
+ */
343
+ auth?: McpAuthOptions;
215
344
  }
216
345
  /**
217
346
  * Runtime MCP server that wraps a decorated class
@@ -220,7 +349,9 @@ declare class McpRuntimeServer {
220
349
  private server;
221
350
  private instance;
222
351
  private metadata;
223
- constructor(target: Function, _options?: McpRuntimeOptions);
352
+ private authOptions;
353
+ private authMiddleware;
354
+ constructor(target: Function, options?: McpRuntimeOptions);
224
355
  private setupHandlers;
225
356
  private setupToolHandlers;
226
357
  private setupResourceHandlers;
@@ -230,19 +361,49 @@ declare class McpRuntimeServer {
230
361
  private resolvePromptArgs;
231
362
  private extractUriParams;
232
363
  /**
233
- * Start the MCP server
364
+ * Start the MCP server with stdio transport
234
365
  */
235
366
  start(): Promise<void>;
367
+ /**
368
+ * Start the MCP server with SSE transport
369
+ */
370
+ startSSE(options?: {
371
+ port?: number;
372
+ endpoint?: string;
373
+ }): Promise<Server>;
236
374
  /**
237
375
  * Get the underlying MCP server instance
238
376
  */
239
- getServer(): Server;
377
+ getServer(): Server$1;
378
+ /**
379
+ * Start the MCP server with WebSocket transport
380
+ */
381
+ startWebSocket(options?: {
382
+ port?: number;
383
+ endpoint?: string;
384
+ }): Promise<Server>;
385
+ /**
386
+ * Handle WebSocket JSON-RPC messages
387
+ */
388
+ private handleWebSocketMessage;
240
389
  }
241
390
  /**
242
391
  * Create and start an MCP server from a decorated class
392
+ *
393
+ * @example
394
+ * // Stdio transport (default)
395
+ * await createMcpServer(MyServer);
396
+ *
397
+ * @example
398
+ * // SSE transport
399
+ * await createMcpServer(MyServer, { transport: 'sse', port: 3000 });
400
+ *
401
+ * @example
402
+ * // WebSocket transport
403
+ * await createMcpServer(MyServer, { transport: 'websocket', port: 8080 });
243
404
  */
244
405
  declare function createMcpServer(target: Function, options?: McpRuntimeOptions): Promise<McpRuntimeServer>;
245
406
 
246
407
  declare const VERSION = "0.1.0";
247
408
 
248
- export { McpInput, McpParam, McpPrompt, McpPromptArg, type McpPromptOptions, McpResource, type McpResourceOptions, type McpRuntimeOptions, McpRuntimeServer, McpServer, type McpServerOptions, McpTool, type McpToolOptions, VERSION, createMcpServer, getMcpServers, getParamsMetadata, getPromptsMetadata, getResourcesMetadata, getServerMetadata, getToolsMetadata, isMcpServer };
409
+ export { type ApiKeyConfig, type AuthResult, type AuthenticatedRequest, type McpAuthOptions, McpInput, McpParam, McpPrompt, McpPromptArg, type McpPromptOptions, McpResource, type McpResourceOptions, type McpRuntimeOptions, McpRuntimeServer, McpServer, type McpServerOptions, McpTool, type McpToolOptions, VERSION, createAuthMiddleware, createMcpServer, extractApiKey, generateApiKey, generateRequestId, getMcpServers, getParamsMetadata, getPromptsMetadata, getResourcesMetadata, getServerMetadata, getToolsMetadata, hashToken, isMcpServer, normalizeApiKeys, sendUnauthorized, validateApiKey };