@eaperezc/mcpgen 0.1.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 +328 -0
- package/dist/cli/index.cjs +625 -0
- package/dist/cli/index.cjs.map +1 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +623 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.cjs +1516 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +297 -0
- package/dist/index.d.ts +297 -0
- package/dist/index.js +1481 -0
- package/dist/index.js.map +1 -0
- package/dist/testing/index.cjs +1056 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +183 -0
- package/dist/testing/index.d.ts +183 -0
- package/dist/testing/index.js +1049 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types-B9yzEar_.d.cts +895 -0
- package/dist/types-B9yzEar_.d.ts +895 -0
- package/package.json +81 -0
|
@@ -0,0 +1,895 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transport types supported by the framework
|
|
5
|
+
*/
|
|
6
|
+
type TransportType = 'stdio' | 'http' | 'sse';
|
|
7
|
+
/**
|
|
8
|
+
* Base transport configuration
|
|
9
|
+
*/
|
|
10
|
+
interface BaseTransportConfig {
|
|
11
|
+
type: TransportType;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Stdio transport configuration
|
|
15
|
+
*/
|
|
16
|
+
interface StdioTransportConfig extends BaseTransportConfig {
|
|
17
|
+
type: 'stdio';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* HTTP transport configuration
|
|
21
|
+
*/
|
|
22
|
+
interface HttpTransportConfig extends BaseTransportConfig {
|
|
23
|
+
type: 'http';
|
|
24
|
+
/** Port to listen on */
|
|
25
|
+
port: number;
|
|
26
|
+
/** Host to bind to */
|
|
27
|
+
host?: string;
|
|
28
|
+
/** Base path for MCP endpoints */
|
|
29
|
+
basePath?: string;
|
|
30
|
+
/** CORS configuration */
|
|
31
|
+
cors?: CorsConfig;
|
|
32
|
+
/** TLS configuration */
|
|
33
|
+
tls?: TlsConfig;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* SSE transport configuration
|
|
37
|
+
*/
|
|
38
|
+
interface SseTransportConfig extends BaseTransportConfig {
|
|
39
|
+
type: 'sse';
|
|
40
|
+
/** Port to listen on */
|
|
41
|
+
port: number;
|
|
42
|
+
/** Host to bind to */
|
|
43
|
+
host?: string;
|
|
44
|
+
/** Base path for SSE endpoints */
|
|
45
|
+
basePath?: string;
|
|
46
|
+
/** CORS configuration */
|
|
47
|
+
cors?: CorsConfig;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* CORS configuration
|
|
51
|
+
*/
|
|
52
|
+
interface CorsConfig {
|
|
53
|
+
/** Allowed origins */
|
|
54
|
+
origin?: string | string[] | boolean;
|
|
55
|
+
/** Allowed methods */
|
|
56
|
+
methods?: string[];
|
|
57
|
+
/** Allowed headers */
|
|
58
|
+
allowedHeaders?: string[];
|
|
59
|
+
/** Exposed headers */
|
|
60
|
+
exposedHeaders?: string[];
|
|
61
|
+
/** Allow credentials */
|
|
62
|
+
credentials?: boolean;
|
|
63
|
+
/** Max age for preflight cache */
|
|
64
|
+
maxAge?: number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* TLS configuration
|
|
68
|
+
*/
|
|
69
|
+
interface TlsConfig {
|
|
70
|
+
/** Path to certificate file */
|
|
71
|
+
cert: string;
|
|
72
|
+
/** Path to key file */
|
|
73
|
+
key: string;
|
|
74
|
+
/** Path to CA certificate file */
|
|
75
|
+
ca?: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Union type for all transport configurations
|
|
79
|
+
*/
|
|
80
|
+
type TransportConfig = StdioTransportConfig | HttpTransportConfig | SseTransportConfig;
|
|
81
|
+
/**
|
|
82
|
+
* Transport interface that all transports must implement
|
|
83
|
+
*/
|
|
84
|
+
interface Transport {
|
|
85
|
+
/** Start the transport (optionally with an MCP server instance) */
|
|
86
|
+
start(mcpServer?: unknown): Promise<void>;
|
|
87
|
+
/** Stop the transport */
|
|
88
|
+
stop(): Promise<void>;
|
|
89
|
+
/** Check if transport is running */
|
|
90
|
+
isRunning(): boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Log levels in order of severity
|
|
95
|
+
*/
|
|
96
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
97
|
+
/**
|
|
98
|
+
* Log entry structure
|
|
99
|
+
*/
|
|
100
|
+
interface LogEntry {
|
|
101
|
+
level: LogLevel;
|
|
102
|
+
message: string;
|
|
103
|
+
timestamp: Date;
|
|
104
|
+
correlationId?: string;
|
|
105
|
+
context?: Record<string, unknown>;
|
|
106
|
+
error?: Error;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Logger configuration options
|
|
110
|
+
*/
|
|
111
|
+
interface LoggerConfig {
|
|
112
|
+
/** Minimum log level to output */
|
|
113
|
+
level: LogLevel;
|
|
114
|
+
/** Whether to output in JSON format (for production) */
|
|
115
|
+
json?: boolean;
|
|
116
|
+
/** Whether to include timestamps */
|
|
117
|
+
timestamps?: boolean;
|
|
118
|
+
/** Custom context to include in all logs */
|
|
119
|
+
defaultContext?: Record<string, unknown>;
|
|
120
|
+
/** Pretty print output (for development) */
|
|
121
|
+
pretty?: boolean;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Logger transport interface for custom outputs
|
|
125
|
+
*/
|
|
126
|
+
interface LogTransport {
|
|
127
|
+
log(entry: LogEntry): void;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Server information
|
|
132
|
+
*/
|
|
133
|
+
interface ServerInfo {
|
|
134
|
+
/** Server name */
|
|
135
|
+
name: string;
|
|
136
|
+
/** Server version */
|
|
137
|
+
version: string;
|
|
138
|
+
/** Server description */
|
|
139
|
+
description?: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* OAuth configuration
|
|
143
|
+
*/
|
|
144
|
+
interface OAuthConfig {
|
|
145
|
+
/** OAuth issuer URL */
|
|
146
|
+
issuer: string;
|
|
147
|
+
/** Expected audience */
|
|
148
|
+
audience: string;
|
|
149
|
+
/** JWKS URI (defaults to issuer/.well-known/jwks.json) */
|
|
150
|
+
jwksUri?: string;
|
|
151
|
+
/** Required scopes for all requests */
|
|
152
|
+
requiredScopes?: string[];
|
|
153
|
+
/** Token cache TTL in seconds */
|
|
154
|
+
cacheTtl?: number;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* API Key configuration
|
|
158
|
+
*/
|
|
159
|
+
interface ApiKeyConfig {
|
|
160
|
+
/** Header name for API key */
|
|
161
|
+
header?: string;
|
|
162
|
+
/** Custom validation function */
|
|
163
|
+
validate: (key: string) => Promise<ApiKeyValidationResult> | ApiKeyValidationResult;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* API Key validation result
|
|
167
|
+
*/
|
|
168
|
+
interface ApiKeyValidationResult {
|
|
169
|
+
/** Whether the key is valid */
|
|
170
|
+
valid: boolean;
|
|
171
|
+
/** Subject/identity for the key */
|
|
172
|
+
subject?: string;
|
|
173
|
+
/** Scopes granted by this key */
|
|
174
|
+
scopes?: string[];
|
|
175
|
+
/** Additional metadata */
|
|
176
|
+
metadata?: Record<string, unknown>;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Authentication configuration
|
|
180
|
+
*/
|
|
181
|
+
interface AuthConfig {
|
|
182
|
+
/** OAuth 2.1 configuration */
|
|
183
|
+
oauth?: OAuthConfig;
|
|
184
|
+
/** API key configuration */
|
|
185
|
+
apiKey?: ApiKeyConfig;
|
|
186
|
+
/** Skip auth for these tool/resource names */
|
|
187
|
+
skipAuth?: string[];
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Server configuration options
|
|
191
|
+
*/
|
|
192
|
+
interface ServerConfig {
|
|
193
|
+
/** Server name */
|
|
194
|
+
name: string;
|
|
195
|
+
/** Server version */
|
|
196
|
+
version: string;
|
|
197
|
+
/** Server description */
|
|
198
|
+
description?: string;
|
|
199
|
+
/** Transport configuration (default: stdio) */
|
|
200
|
+
transport?: TransportType | TransportConfig;
|
|
201
|
+
/** Authentication configuration */
|
|
202
|
+
auth?: AuthConfig;
|
|
203
|
+
/** Logging configuration */
|
|
204
|
+
logging?: {
|
|
205
|
+
level?: LogLevel;
|
|
206
|
+
pretty?: boolean;
|
|
207
|
+
};
|
|
208
|
+
/** Auto-discovery configuration */
|
|
209
|
+
discovery?: {
|
|
210
|
+
/** Enable auto-discovery */
|
|
211
|
+
enabled?: boolean;
|
|
212
|
+
/** Directories to scan */
|
|
213
|
+
directories?: {
|
|
214
|
+
tools?: string;
|
|
215
|
+
resources?: string;
|
|
216
|
+
prompts?: string;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Server capabilities reported to clients
|
|
222
|
+
*/
|
|
223
|
+
interface ServerCapabilities {
|
|
224
|
+
tools?: boolean;
|
|
225
|
+
resources?: boolean;
|
|
226
|
+
prompts?: boolean;
|
|
227
|
+
logging?: boolean;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Server lifecycle hooks
|
|
231
|
+
*/
|
|
232
|
+
interface ServerHooks {
|
|
233
|
+
/** Called before server starts */
|
|
234
|
+
onBeforeStart?: () => Promise<void> | void;
|
|
235
|
+
/** Called after server starts */
|
|
236
|
+
onAfterStart?: () => Promise<void> | void;
|
|
237
|
+
/** Called before server stops */
|
|
238
|
+
onBeforeStop?: () => Promise<void> | void;
|
|
239
|
+
/** Called after server stops */
|
|
240
|
+
onAfterStop?: () => Promise<void> | void;
|
|
241
|
+
/** Called on server error */
|
|
242
|
+
onError?: (error: Error) => Promise<void> | void;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Tool definition interface
|
|
247
|
+
*/
|
|
248
|
+
interface ToolDefinition<TSchema extends z.ZodType = z.ZodType> {
|
|
249
|
+
/** Unique tool name */
|
|
250
|
+
name: string;
|
|
251
|
+
/** Human-readable description */
|
|
252
|
+
description: string;
|
|
253
|
+
/** Zod schema for input validation */
|
|
254
|
+
schema: TSchema;
|
|
255
|
+
/** Required scopes for this tool (for auth) */
|
|
256
|
+
scopes?: string[];
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Tool execution result
|
|
260
|
+
*/
|
|
261
|
+
interface ToolResult {
|
|
262
|
+
/** Result content (can be any JSON-serializable value) */
|
|
263
|
+
content: unknown;
|
|
264
|
+
/** Whether the execution was successful */
|
|
265
|
+
isError?: boolean;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Tool handler function type
|
|
269
|
+
*/
|
|
270
|
+
type ToolHandler<TInput = unknown> = (input: TInput) => Promise<ToolResult> | ToolResult;
|
|
271
|
+
/**
|
|
272
|
+
* Inline tool definition for fluent API
|
|
273
|
+
*/
|
|
274
|
+
interface InlineToolDefinition<TSchema extends z.ZodType = z.ZodType> {
|
|
275
|
+
description: string;
|
|
276
|
+
schema: TSchema;
|
|
277
|
+
handler: ToolHandler<z.infer<TSchema>>;
|
|
278
|
+
scopes?: string[];
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Tool metadata for registration
|
|
282
|
+
*/
|
|
283
|
+
interface ToolMetadata {
|
|
284
|
+
name: string;
|
|
285
|
+
description: string;
|
|
286
|
+
inputSchema: z.ZodType;
|
|
287
|
+
scopes: string[];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Base class for defining MCP tools
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* class GreetTool extends BaseTool {
|
|
296
|
+
* name = 'greet';
|
|
297
|
+
* description = 'Greets a user';
|
|
298
|
+
* schema = z.object({
|
|
299
|
+
* name: z.string().describe('Name to greet'),
|
|
300
|
+
* });
|
|
301
|
+
*
|
|
302
|
+
* async execute(input: z.infer<typeof this.schema>): Promise<ToolResult> {
|
|
303
|
+
* return { content: { greeting: `Hello, ${input.name}!` } };
|
|
304
|
+
* }
|
|
305
|
+
* }
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
declare abstract class BaseTool<TSchema extends z.ZodType = z.ZodType> {
|
|
309
|
+
/** Unique tool name */
|
|
310
|
+
abstract readonly name: string;
|
|
311
|
+
/** Human-readable description of what the tool does */
|
|
312
|
+
abstract readonly description: string;
|
|
313
|
+
/** Zod schema for input validation */
|
|
314
|
+
abstract readonly schema: TSchema;
|
|
315
|
+
/** Required scopes for this tool (optional) */
|
|
316
|
+
readonly scopes: string[];
|
|
317
|
+
/**
|
|
318
|
+
* Execute the tool with validated input
|
|
319
|
+
*/
|
|
320
|
+
abstract execute(input: z.infer<TSchema>): Promise<ToolResult> | ToolResult;
|
|
321
|
+
/**
|
|
322
|
+
* Validate and execute the tool
|
|
323
|
+
*/
|
|
324
|
+
run(input: unknown): Promise<ToolResult>;
|
|
325
|
+
/**
|
|
326
|
+
* Get tool metadata for registration
|
|
327
|
+
*/
|
|
328
|
+
getMetadata(): ToolMetadata;
|
|
329
|
+
/**
|
|
330
|
+
* Convert schema to JSON Schema format for MCP protocol
|
|
331
|
+
*/
|
|
332
|
+
getJsonSchema(): Record<string, unknown>;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Registry for managing tools
|
|
337
|
+
*/
|
|
338
|
+
declare class ToolRegistry {
|
|
339
|
+
private tools;
|
|
340
|
+
/**
|
|
341
|
+
* Register a tool instance
|
|
342
|
+
*/
|
|
343
|
+
register(tool: BaseTool): void;
|
|
344
|
+
/**
|
|
345
|
+
* Register an inline tool definition
|
|
346
|
+
*/
|
|
347
|
+
registerInline<TSchema extends z.ZodType>(name: string, definition: InlineToolDefinition<TSchema>): void;
|
|
348
|
+
/**
|
|
349
|
+
* Get a tool by name
|
|
350
|
+
*/
|
|
351
|
+
get(name: string): BaseTool | undefined;
|
|
352
|
+
/**
|
|
353
|
+
* Get a tool by name or throw if not found
|
|
354
|
+
*/
|
|
355
|
+
getOrThrow(name: string): BaseTool;
|
|
356
|
+
/**
|
|
357
|
+
* Check if a tool exists
|
|
358
|
+
*/
|
|
359
|
+
has(name: string): boolean;
|
|
360
|
+
/**
|
|
361
|
+
* Remove a tool
|
|
362
|
+
*/
|
|
363
|
+
remove(name: string): boolean;
|
|
364
|
+
/**
|
|
365
|
+
* Get all registered tools
|
|
366
|
+
*/
|
|
367
|
+
getAll(): BaseTool[];
|
|
368
|
+
/**
|
|
369
|
+
* Get all tool names
|
|
370
|
+
*/
|
|
371
|
+
getNames(): string[];
|
|
372
|
+
/**
|
|
373
|
+
* Get the number of registered tools
|
|
374
|
+
*/
|
|
375
|
+
get size(): number;
|
|
376
|
+
/**
|
|
377
|
+
* Clear all tools
|
|
378
|
+
*/
|
|
379
|
+
clear(): void;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Resource content types
|
|
384
|
+
*/
|
|
385
|
+
type ResourceContent = {
|
|
386
|
+
type: 'text';
|
|
387
|
+
text: string;
|
|
388
|
+
} | {
|
|
389
|
+
type: 'blob';
|
|
390
|
+
data: string;
|
|
391
|
+
mimeType: string;
|
|
392
|
+
};
|
|
393
|
+
/**
|
|
394
|
+
* Resource definition interface
|
|
395
|
+
*/
|
|
396
|
+
interface ResourceDefinition {
|
|
397
|
+
/** Unique resource URI */
|
|
398
|
+
uri: string;
|
|
399
|
+
/** Human-readable name */
|
|
400
|
+
name: string;
|
|
401
|
+
/** Description of the resource */
|
|
402
|
+
description?: string;
|
|
403
|
+
/** MIME type of the resource content */
|
|
404
|
+
mimeType?: string;
|
|
405
|
+
/** Required scopes for this resource (for auth) */
|
|
406
|
+
scopes?: string[];
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Resource read result
|
|
410
|
+
*/
|
|
411
|
+
interface ResourceResult {
|
|
412
|
+
contents: ResourceContent[];
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Resource handler function type
|
|
416
|
+
*/
|
|
417
|
+
type ResourceHandler = () => Promise<ResourceResult> | ResourceResult;
|
|
418
|
+
/**
|
|
419
|
+
* Inline resource definition for fluent API
|
|
420
|
+
*/
|
|
421
|
+
interface InlineResourceDefinition {
|
|
422
|
+
name: string;
|
|
423
|
+
description?: string;
|
|
424
|
+
mimeType?: string;
|
|
425
|
+
read: ResourceHandler;
|
|
426
|
+
scopes?: string[];
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Resource metadata for registration
|
|
430
|
+
*/
|
|
431
|
+
interface ResourceMetadata {
|
|
432
|
+
uri: string;
|
|
433
|
+
name: string;
|
|
434
|
+
description?: string;
|
|
435
|
+
mimeType?: string;
|
|
436
|
+
scopes: string[];
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Resource template for dynamic resources
|
|
440
|
+
*/
|
|
441
|
+
interface ResourceTemplate {
|
|
442
|
+
/** URI template with placeholders (e.g., "file://{path}") */
|
|
443
|
+
uriTemplate: string;
|
|
444
|
+
/** Human-readable name */
|
|
445
|
+
name: string;
|
|
446
|
+
/** Description of the resource template */
|
|
447
|
+
description?: string;
|
|
448
|
+
/** MIME type of resources from this template */
|
|
449
|
+
mimeType?: string;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Base class for defining MCP resources
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* ```typescript
|
|
457
|
+
* class ConfigResource extends BaseResource {
|
|
458
|
+
* uri = 'config://app';
|
|
459
|
+
* name = 'App Configuration';
|
|
460
|
+
* description = 'Application configuration settings';
|
|
461
|
+
*
|
|
462
|
+
* async read(): Promise<ResourceResult> {
|
|
463
|
+
* const config = await loadConfig();
|
|
464
|
+
* return {
|
|
465
|
+
* contents: [{ type: 'text', text: JSON.stringify(config, null, 2) }]
|
|
466
|
+
* };
|
|
467
|
+
* }
|
|
468
|
+
* }
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
declare abstract class BaseResource {
|
|
472
|
+
/** Unique resource URI */
|
|
473
|
+
abstract readonly uri: string;
|
|
474
|
+
/** Human-readable name */
|
|
475
|
+
abstract readonly name: string;
|
|
476
|
+
/** Description of the resource */
|
|
477
|
+
readonly description?: string;
|
|
478
|
+
/** MIME type of the resource content */
|
|
479
|
+
readonly mimeType?: string;
|
|
480
|
+
/** Required scopes for this resource (optional) */
|
|
481
|
+
readonly scopes: string[];
|
|
482
|
+
/**
|
|
483
|
+
* Read the resource content
|
|
484
|
+
*/
|
|
485
|
+
abstract read(): Promise<ResourceResult> | ResourceResult;
|
|
486
|
+
/**
|
|
487
|
+
* Safely read the resource with error handling
|
|
488
|
+
*/
|
|
489
|
+
safeRead(): Promise<ResourceResult>;
|
|
490
|
+
/**
|
|
491
|
+
* Get resource metadata for registration
|
|
492
|
+
*/
|
|
493
|
+
getMetadata(): ResourceMetadata;
|
|
494
|
+
/**
|
|
495
|
+
* Helper to create text content
|
|
496
|
+
*/
|
|
497
|
+
protected text(content: string): ResourceContent;
|
|
498
|
+
/**
|
|
499
|
+
* Helper to create JSON content
|
|
500
|
+
*/
|
|
501
|
+
protected json(data: unknown): ResourceContent;
|
|
502
|
+
/**
|
|
503
|
+
* Helper to create blob content
|
|
504
|
+
*/
|
|
505
|
+
protected blob(data: string, mimeType: string): ResourceContent;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Registry for managing resources
|
|
510
|
+
*/
|
|
511
|
+
declare class ResourceRegistry {
|
|
512
|
+
private resources;
|
|
513
|
+
/**
|
|
514
|
+
* Register a resource instance
|
|
515
|
+
*/
|
|
516
|
+
register(resource: BaseResource): void;
|
|
517
|
+
/**
|
|
518
|
+
* Register an inline resource definition
|
|
519
|
+
*/
|
|
520
|
+
registerInline(uri: string, definition: InlineResourceDefinition): void;
|
|
521
|
+
/**
|
|
522
|
+
* Get a resource by URI
|
|
523
|
+
*/
|
|
524
|
+
get(uri: string): BaseResource | undefined;
|
|
525
|
+
/**
|
|
526
|
+
* Get a resource by URI or throw if not found
|
|
527
|
+
*/
|
|
528
|
+
getOrThrow(uri: string): BaseResource;
|
|
529
|
+
/**
|
|
530
|
+
* Check if a resource exists
|
|
531
|
+
*/
|
|
532
|
+
has(uri: string): boolean;
|
|
533
|
+
/**
|
|
534
|
+
* Remove a resource
|
|
535
|
+
*/
|
|
536
|
+
remove(uri: string): boolean;
|
|
537
|
+
/**
|
|
538
|
+
* Get all registered resources
|
|
539
|
+
*/
|
|
540
|
+
getAll(): BaseResource[];
|
|
541
|
+
/**
|
|
542
|
+
* Get all resource URIs
|
|
543
|
+
*/
|
|
544
|
+
getUris(): string[];
|
|
545
|
+
/**
|
|
546
|
+
* Get the number of registered resources
|
|
547
|
+
*/
|
|
548
|
+
get size(): number;
|
|
549
|
+
/**
|
|
550
|
+
* Clear all resources
|
|
551
|
+
*/
|
|
552
|
+
clear(): void;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Prompt message role
|
|
557
|
+
*/
|
|
558
|
+
type PromptRole = 'user' | 'assistant';
|
|
559
|
+
/**
|
|
560
|
+
* Prompt message content
|
|
561
|
+
*/
|
|
562
|
+
interface PromptMessage {
|
|
563
|
+
role: PromptRole;
|
|
564
|
+
content: string;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Prompt definition interface
|
|
568
|
+
*/
|
|
569
|
+
interface PromptDefinition<TArgs extends z.ZodType = z.ZodType> {
|
|
570
|
+
/** Unique prompt name */
|
|
571
|
+
name: string;
|
|
572
|
+
/** Human-readable description */
|
|
573
|
+
description?: string;
|
|
574
|
+
/** Zod schema for arguments validation */
|
|
575
|
+
arguments?: TArgs;
|
|
576
|
+
/** Required scopes for this prompt (for auth) */
|
|
577
|
+
scopes?: string[];
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Prompt render result
|
|
581
|
+
*/
|
|
582
|
+
interface PromptResult {
|
|
583
|
+
messages: PromptMessage[];
|
|
584
|
+
description?: string;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Prompt handler function type
|
|
588
|
+
*/
|
|
589
|
+
type PromptHandler<TArgs = unknown> = (args: TArgs) => Promise<PromptResult> | PromptResult;
|
|
590
|
+
/**
|
|
591
|
+
* Inline prompt definition for fluent API
|
|
592
|
+
*/
|
|
593
|
+
interface InlinePromptDefinition<TArgs extends z.ZodType = z.ZodType> {
|
|
594
|
+
description?: string;
|
|
595
|
+
arguments?: TArgs;
|
|
596
|
+
render: PromptHandler<z.infer<TArgs>>;
|
|
597
|
+
scopes?: string[];
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Prompt argument metadata
|
|
601
|
+
*/
|
|
602
|
+
interface PromptArgumentMetadata {
|
|
603
|
+
name: string;
|
|
604
|
+
description?: string;
|
|
605
|
+
required: boolean;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Prompt metadata for registration
|
|
609
|
+
*/
|
|
610
|
+
interface PromptMetadata {
|
|
611
|
+
name: string;
|
|
612
|
+
description?: string;
|
|
613
|
+
arguments: PromptArgumentMetadata[];
|
|
614
|
+
scopes: string[];
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Base class for defining MCP prompts
|
|
619
|
+
*
|
|
620
|
+
* @example
|
|
621
|
+
* ```typescript
|
|
622
|
+
* class SummarizePrompt extends BasePrompt {
|
|
623
|
+
* name = 'summarize';
|
|
624
|
+
* description = 'Summarize a piece of text';
|
|
625
|
+
*
|
|
626
|
+
* arguments = z.object({
|
|
627
|
+
* text: z.string().describe('Text to summarize'),
|
|
628
|
+
* style: z.enum(['brief', 'detailed']).optional().describe('Summary style'),
|
|
629
|
+
* });
|
|
630
|
+
*
|
|
631
|
+
* async render(args: z.infer<typeof this.arguments>): Promise<PromptResult> {
|
|
632
|
+
* const style = args.style ?? 'brief';
|
|
633
|
+
* return {
|
|
634
|
+
* messages: [{
|
|
635
|
+
* role: 'user',
|
|
636
|
+
* content: `Please provide a ${style} summary of the following text:\n\n${args.text}`
|
|
637
|
+
* }]
|
|
638
|
+
* };
|
|
639
|
+
* }
|
|
640
|
+
* }
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
declare abstract class BasePrompt<TArgs extends z.ZodType = z.ZodType> {
|
|
644
|
+
/** Unique prompt name */
|
|
645
|
+
abstract readonly name: string;
|
|
646
|
+
/** Human-readable description */
|
|
647
|
+
readonly description?: string;
|
|
648
|
+
/** Zod schema for arguments validation */
|
|
649
|
+
readonly arguments?: TArgs;
|
|
650
|
+
/** Required scopes for this prompt (optional) */
|
|
651
|
+
readonly scopes: string[];
|
|
652
|
+
/**
|
|
653
|
+
* Render the prompt with given arguments
|
|
654
|
+
*/
|
|
655
|
+
abstract render(args: z.infer<TArgs>): Promise<PromptResult> | PromptResult;
|
|
656
|
+
/**
|
|
657
|
+
* Validate arguments and render the prompt
|
|
658
|
+
*/
|
|
659
|
+
safeRender(args: unknown): Promise<PromptResult>;
|
|
660
|
+
/**
|
|
661
|
+
* Get prompt metadata for registration
|
|
662
|
+
*/
|
|
663
|
+
getMetadata(): PromptMetadata;
|
|
664
|
+
/**
|
|
665
|
+
* Get arguments metadata from schema
|
|
666
|
+
*/
|
|
667
|
+
private getArgumentsMetadata;
|
|
668
|
+
/**
|
|
669
|
+
* Helper to create a user message
|
|
670
|
+
*/
|
|
671
|
+
protected user(content: string): PromptMessage;
|
|
672
|
+
/**
|
|
673
|
+
* Helper to create an assistant message
|
|
674
|
+
*/
|
|
675
|
+
protected assistant(content: string): PromptMessage;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Registry for managing prompts
|
|
680
|
+
*/
|
|
681
|
+
declare class PromptRegistry {
|
|
682
|
+
private prompts;
|
|
683
|
+
/**
|
|
684
|
+
* Register a prompt instance
|
|
685
|
+
*/
|
|
686
|
+
register(prompt: BasePrompt): void;
|
|
687
|
+
/**
|
|
688
|
+
* Register an inline prompt definition
|
|
689
|
+
*/
|
|
690
|
+
registerInline<TArgs extends z.ZodType>(name: string, definition: InlinePromptDefinition<TArgs>): void;
|
|
691
|
+
/**
|
|
692
|
+
* Get a prompt by name
|
|
693
|
+
*/
|
|
694
|
+
get(name: string): BasePrompt | undefined;
|
|
695
|
+
/**
|
|
696
|
+
* Get a prompt by name or throw if not found
|
|
697
|
+
*/
|
|
698
|
+
getOrThrow(name: string): BasePrompt;
|
|
699
|
+
/**
|
|
700
|
+
* Check if a prompt exists
|
|
701
|
+
*/
|
|
702
|
+
has(name: string): boolean;
|
|
703
|
+
/**
|
|
704
|
+
* Remove a prompt
|
|
705
|
+
*/
|
|
706
|
+
remove(name: string): boolean;
|
|
707
|
+
/**
|
|
708
|
+
* Get all registered prompts
|
|
709
|
+
*/
|
|
710
|
+
getAll(): BasePrompt[];
|
|
711
|
+
/**
|
|
712
|
+
* Get all prompt names
|
|
713
|
+
*/
|
|
714
|
+
getNames(): string[];
|
|
715
|
+
/**
|
|
716
|
+
* Get the number of registered prompts
|
|
717
|
+
*/
|
|
718
|
+
get size(): number;
|
|
719
|
+
/**
|
|
720
|
+
* Clear all prompts
|
|
721
|
+
*/
|
|
722
|
+
clear(): void;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Structured logger with support for correlation IDs and contextual logging
|
|
727
|
+
*/
|
|
728
|
+
declare class Logger {
|
|
729
|
+
private pino;
|
|
730
|
+
private config;
|
|
731
|
+
private defaultContext;
|
|
732
|
+
constructor(config?: Partial<LoggerConfig>);
|
|
733
|
+
/**
|
|
734
|
+
* Check if a log level should be output
|
|
735
|
+
*/
|
|
736
|
+
private shouldLog;
|
|
737
|
+
/**
|
|
738
|
+
* Create a child logger with additional context
|
|
739
|
+
*/
|
|
740
|
+
child(context: Record<string, unknown>): Logger;
|
|
741
|
+
/**
|
|
742
|
+
* Log a debug message
|
|
743
|
+
*/
|
|
744
|
+
debug(message: string, context?: Record<string, unknown>): void;
|
|
745
|
+
/**
|
|
746
|
+
* Log an info message
|
|
747
|
+
*/
|
|
748
|
+
info(message: string, context?: Record<string, unknown>): void;
|
|
749
|
+
/**
|
|
750
|
+
* Log a warning message
|
|
751
|
+
*/
|
|
752
|
+
warn(message: string, context?: Record<string, unknown>): void;
|
|
753
|
+
/**
|
|
754
|
+
* Log an error message
|
|
755
|
+
*/
|
|
756
|
+
error(message: string, error?: Error | Record<string, unknown>): void;
|
|
757
|
+
/**
|
|
758
|
+
* Log with correlation ID for request tracing
|
|
759
|
+
*/
|
|
760
|
+
withCorrelationId(correlationId: string): Logger;
|
|
761
|
+
/**
|
|
762
|
+
* Set the log level
|
|
763
|
+
*/
|
|
764
|
+
setLevel(level: LogLevel): void;
|
|
765
|
+
/**
|
|
766
|
+
* Get the current log level
|
|
767
|
+
*/
|
|
768
|
+
getLevel(): LogLevel;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Default logger instance
|
|
772
|
+
*/
|
|
773
|
+
declare const logger: Logger;
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Main MCP Server class
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* ```typescript
|
|
780
|
+
* const server = new McpServer({
|
|
781
|
+
* name: 'my-server',
|
|
782
|
+
* version: '1.0.0',
|
|
783
|
+
* });
|
|
784
|
+
*
|
|
785
|
+
* server.tool(new GreetTool());
|
|
786
|
+
* server.resource(new ConfigResource());
|
|
787
|
+
* await server.start();
|
|
788
|
+
* ```
|
|
789
|
+
*/
|
|
790
|
+
declare class McpServer {
|
|
791
|
+
private server;
|
|
792
|
+
private config;
|
|
793
|
+
private logger;
|
|
794
|
+
private running;
|
|
795
|
+
private httpTransport;
|
|
796
|
+
private toolRegistry;
|
|
797
|
+
private resourceRegistry;
|
|
798
|
+
private promptRegistry;
|
|
799
|
+
private hooks;
|
|
800
|
+
constructor(config: ServerConfig);
|
|
801
|
+
/**
|
|
802
|
+
* Get server info
|
|
803
|
+
*/
|
|
804
|
+
getInfo(): ServerInfo;
|
|
805
|
+
/**
|
|
806
|
+
* Get server capabilities
|
|
807
|
+
*/
|
|
808
|
+
private getCapabilities;
|
|
809
|
+
/**
|
|
810
|
+
* Set up MCP request handlers
|
|
811
|
+
*/
|
|
812
|
+
private setupHandlers;
|
|
813
|
+
/**
|
|
814
|
+
* Register a tool
|
|
815
|
+
*/
|
|
816
|
+
tool(tool: BaseTool): this;
|
|
817
|
+
tool<TSchema extends z.ZodType>(name: string, definition: InlineToolDefinition<TSchema>): this;
|
|
818
|
+
/**
|
|
819
|
+
* Register a resource
|
|
820
|
+
*/
|
|
821
|
+
resource(resource: BaseResource): this;
|
|
822
|
+
resource(uri: string, definition: InlineResourceDefinition): this;
|
|
823
|
+
/**
|
|
824
|
+
* Register a prompt
|
|
825
|
+
*/
|
|
826
|
+
prompt(prompt: BasePrompt): this;
|
|
827
|
+
prompt<TArgs extends z.ZodType>(name: string, definition: InlinePromptDefinition<TArgs>): this;
|
|
828
|
+
/**
|
|
829
|
+
* Set lifecycle hooks
|
|
830
|
+
*/
|
|
831
|
+
setHooks(hooks: ServerHooks): this;
|
|
832
|
+
/**
|
|
833
|
+
* Start the server
|
|
834
|
+
*/
|
|
835
|
+
start(): Promise<void>;
|
|
836
|
+
/**
|
|
837
|
+
* Stop the server
|
|
838
|
+
*/
|
|
839
|
+
stop(): Promise<void>;
|
|
840
|
+
/**
|
|
841
|
+
* Check if server is running
|
|
842
|
+
*/
|
|
843
|
+
isRunning(): boolean;
|
|
844
|
+
/**
|
|
845
|
+
* Get the logger instance
|
|
846
|
+
*/
|
|
847
|
+
getLogger(): Logger;
|
|
848
|
+
/**
|
|
849
|
+
* Get tool registry (for testing)
|
|
850
|
+
*/
|
|
851
|
+
getToolRegistry(): ToolRegistry;
|
|
852
|
+
/**
|
|
853
|
+
* Get resource registry (for testing)
|
|
854
|
+
*/
|
|
855
|
+
getResourceRegistry(): ResourceRegistry;
|
|
856
|
+
/**
|
|
857
|
+
* Get prompt registry (for testing)
|
|
858
|
+
*/
|
|
859
|
+
getPromptRegistry(): PromptRegistry;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Request context data available throughout request lifecycle
|
|
864
|
+
*/
|
|
865
|
+
interface RequestContextData {
|
|
866
|
+
/** Unique ID for request tracing */
|
|
867
|
+
correlationId: string;
|
|
868
|
+
/** Request start time */
|
|
869
|
+
startTime: Date;
|
|
870
|
+
/** Logger instance with correlation ID */
|
|
871
|
+
logger: Logger;
|
|
872
|
+
/** Authenticated user info (if available) */
|
|
873
|
+
auth?: AuthContext;
|
|
874
|
+
/** Custom context data */
|
|
875
|
+
custom: Map<string, unknown>;
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Authentication context
|
|
879
|
+
*/
|
|
880
|
+
interface AuthContext {
|
|
881
|
+
/** User/client identifier */
|
|
882
|
+
subject: string;
|
|
883
|
+
/** Token scopes/permissions */
|
|
884
|
+
scopes: string[];
|
|
885
|
+
/** Token expiration */
|
|
886
|
+
expiresAt?: Date;
|
|
887
|
+
/** Additional claims */
|
|
888
|
+
claims?: Record<string, unknown>;
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Type for context store key
|
|
892
|
+
*/
|
|
893
|
+
type ContextKey = string | symbol;
|
|
894
|
+
|
|
895
|
+
export { type AuthContext as A, BaseTool as B, type PromptDefinition as C, type PromptResult as D, type PromptHandler as E, type PromptArgumentMetadata as F, type PromptMetadata as G, type HttpTransportConfig as H, type InlineToolDefinition as I, logger as J, type LogEntry as K, type LogLevel as L, McpServer as M, type LoggerConfig as N, type OAuthConfig as O, PromptRegistry as P, type LogTransport as Q, type RequestContextData as R, type ServerHooks as S, type TransportConfig as T, type ContextKey as U, type TransportType as V, type StdioTransportConfig as W, type SseTransportConfig as X, type CorsConfig as Y, type TlsConfig as Z, BaseResource as a, BasePrompt as b, type InlineResourceDefinition as c, type InlinePromptDefinition as d, type AuthConfig as e, Logger as f, type Transport as g, type ServerConfig as h, type ServerInfo as i, type ServerCapabilities as j, type ApiKeyConfig as k, type ApiKeyValidationResult as l, ToolRegistry as m, type ToolDefinition as n, type ToolResult as o, type ToolHandler as p, type ToolMetadata as q, ResourceRegistry as r, type ResourceContent as s, type ResourceDefinition as t, type ResourceResult as u, type ResourceHandler as v, type ResourceMetadata as w, type ResourceTemplate as x, type PromptRole as y, type PromptMessage as z };
|