@nitrostack/core 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.
- package/LICENSE +201 -0
- package/README.md +80 -0
- package/dist/auth/api-key.d.ts +118 -0
- package/dist/auth/api-key.d.ts.map +1 -0
- package/dist/auth/api-key.js +168 -0
- package/dist/auth/api-key.js.map +1 -0
- package/dist/auth/client.d.ts +151 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +330 -0
- package/dist/auth/client.js.map +1 -0
- package/dist/auth/index.d.ts +31 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +46 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/middleware.d.ts +95 -0
- package/dist/auth/middleware.d.ts.map +1 -0
- package/dist/auth/middleware.js +260 -0
- package/dist/auth/middleware.js.map +1 -0
- package/dist/auth/pkce.d.ts +53 -0
- package/dist/auth/pkce.d.ts.map +1 -0
- package/dist/auth/pkce.js +105 -0
- package/dist/auth/pkce.js.map +1 -0
- package/dist/auth/quick-setup.d.ts +94 -0
- package/dist/auth/quick-setup.d.ts.map +1 -0
- package/dist/auth/quick-setup.js +210 -0
- package/dist/auth/quick-setup.js.map +1 -0
- package/dist/auth/secure-secret.d.ts +136 -0
- package/dist/auth/secure-secret.d.ts.map +1 -0
- package/dist/auth/secure-secret.js +182 -0
- package/dist/auth/secure-secret.js.map +1 -0
- package/dist/auth/server-integration.d.ts +97 -0
- package/dist/auth/server-integration.d.ts.map +1 -0
- package/dist/auth/server-integration.js +182 -0
- package/dist/auth/server-integration.js.map +1 -0
- package/dist/auth/server-metadata.d.ts +51 -0
- package/dist/auth/server-metadata.d.ts.map +1 -0
- package/dist/auth/server-metadata.js +106 -0
- package/dist/auth/server-metadata.js.map +1 -0
- package/dist/auth/simple-jwt.d.ts +174 -0
- package/dist/auth/simple-jwt.d.ts.map +1 -0
- package/dist/auth/simple-jwt.js +162 -0
- package/dist/auth/simple-jwt.js.map +1 -0
- package/dist/auth/token-store.d.ts +104 -0
- package/dist/auth/token-store.d.ts.map +1 -0
- package/dist/auth/token-store.js +205 -0
- package/dist/auth/token-store.js.map +1 -0
- package/dist/auth/token-validation.d.ts +59 -0
- package/dist/auth/token-validation.d.ts.map +1 -0
- package/dist/auth/token-validation.js +241 -0
- package/dist/auth/token-validation.js.map +1 -0
- package/dist/auth/types.d.ts +215 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +6 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/core/apikey-module.d.ts +69 -0
- package/dist/core/apikey-module.d.ts.map +1 -0
- package/dist/core/apikey-module.js +114 -0
- package/dist/core/apikey-module.js.map +1 -0
- package/dist/core/app-decorator.d.ts +59 -0
- package/dist/core/app-decorator.d.ts.map +1 -0
- package/dist/core/app-decorator.js +322 -0
- package/dist/core/app-decorator.js.map +1 -0
- package/dist/core/builders.d.ts +50 -0
- package/dist/core/builders.d.ts.map +1 -0
- package/dist/core/builders.js +139 -0
- package/dist/core/builders.js.map +1 -0
- package/dist/core/component.d.ts +111 -0
- package/dist/core/component.d.ts.map +1 -0
- package/dist/core/component.js +228 -0
- package/dist/core/component.js.map +1 -0
- package/dist/core/config-module.d.ts +62 -0
- package/dist/core/config-module.d.ts.map +1 -0
- package/dist/core/config-module.js +94 -0
- package/dist/core/config-module.js.map +1 -0
- package/dist/core/decorators/cache.decorator.d.ts +61 -0
- package/dist/core/decorators/cache.decorator.d.ts.map +1 -0
- package/dist/core/decorators/cache.decorator.js +115 -0
- package/dist/core/decorators/cache.decorator.js.map +1 -0
- package/dist/core/decorators/health-check.decorator.d.ts +80 -0
- package/dist/core/decorators/health-check.decorator.d.ts.map +1 -0
- package/dist/core/decorators/health-check.decorator.js +153 -0
- package/dist/core/decorators/health-check.decorator.js.map +1 -0
- package/dist/core/decorators/rate-limit.decorator.d.ts +63 -0
- package/dist/core/decorators/rate-limit.decorator.d.ts.map +1 -0
- package/dist/core/decorators/rate-limit.decorator.js +129 -0
- package/dist/core/decorators/rate-limit.decorator.js.map +1 -0
- package/dist/core/decorators.d.ts +190 -0
- package/dist/core/decorators.d.ts.map +1 -0
- package/dist/core/decorators.js +170 -0
- package/dist/core/decorators.js.map +1 -0
- package/dist/core/di/container.d.ts +64 -0
- package/dist/core/di/container.d.ts.map +1 -0
- package/dist/core/di/container.js +105 -0
- package/dist/core/di/container.js.map +1 -0
- package/dist/core/di/injectable.decorator.d.ts +62 -0
- package/dist/core/di/injectable.decorator.d.ts.map +1 -0
- package/dist/core/di/injectable.decorator.js +66 -0
- package/dist/core/di/injectable.decorator.js.map +1 -0
- package/dist/core/errors.d.ts +54 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +87 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/events/event-emitter.d.ts +50 -0
- package/dist/core/events/event-emitter.d.ts.map +1 -0
- package/dist/core/events/event-emitter.js +94 -0
- package/dist/core/events/event-emitter.js.map +1 -0
- package/dist/core/events/event.decorator.d.ts +48 -0
- package/dist/core/events/event.decorator.d.ts.map +1 -0
- package/dist/core/events/event.decorator.js +72 -0
- package/dist/core/events/event.decorator.js.map +1 -0
- package/dist/core/events/log-emitter.d.ts +14 -0
- package/dist/core/events/log-emitter.d.ts.map +1 -0
- package/dist/core/events/log-emitter.js +20 -0
- package/dist/core/events/log-emitter.js.map +1 -0
- package/dist/core/filters/exception-filter.decorator.d.ts +40 -0
- package/dist/core/filters/exception-filter.decorator.d.ts.map +1 -0
- package/dist/core/filters/exception-filter.decorator.js +54 -0
- package/dist/core/filters/exception-filter.decorator.js.map +1 -0
- package/dist/core/filters/exception-filter.interface.d.ts +39 -0
- package/dist/core/filters/exception-filter.interface.d.ts.map +1 -0
- package/dist/core/filters/exception-filter.interface.js +2 -0
- package/dist/core/filters/exception-filter.interface.js.map +1 -0
- package/dist/core/guards/apikey.guard.d.ts +22 -0
- package/dist/core/guards/apikey.guard.d.ts.map +1 -0
- package/dist/core/guards/apikey.guard.js +11 -0
- package/dist/core/guards/apikey.guard.js.map +1 -0
- package/dist/core/guards/guard.interface.d.ts +18 -0
- package/dist/core/guards/guard.interface.d.ts.map +1 -0
- package/dist/core/guards/guard.interface.js +2 -0
- package/dist/core/guards/guard.interface.js.map +1 -0
- package/dist/core/guards/jwt.guard.d.ts +18 -0
- package/dist/core/guards/jwt.guard.d.ts.map +1 -0
- package/dist/core/guards/jwt.guard.js +2 -0
- package/dist/core/guards/jwt.guard.js.map +1 -0
- package/dist/core/guards/oauth.guard.d.ts +35 -0
- package/dist/core/guards/oauth.guard.d.ts.map +1 -0
- package/dist/core/guards/oauth.guard.js +2 -0
- package/dist/core/guards/oauth.guard.js.map +1 -0
- package/dist/core/guards/use-guards.decorator.d.ts +25 -0
- package/dist/core/guards/use-guards.decorator.d.ts.map +1 -0
- package/dist/core/guards/use-guards.decorator.js +32 -0
- package/dist/core/guards/use-guards.decorator.js.map +1 -0
- package/dist/core/health/health-checks.resource.d.ts +14 -0
- package/dist/core/health/health-checks.resource.d.ts.map +1 -0
- package/dist/core/health/health-checks.resource.js +29 -0
- package/dist/core/health/health-checks.resource.js.map +1 -0
- package/dist/core/index.d.ts +57 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +59 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/interceptors/interceptor.decorator.d.ts +37 -0
- package/dist/core/interceptors/interceptor.decorator.d.ts.map +1 -0
- package/dist/core/interceptors/interceptor.decorator.js +51 -0
- package/dist/core/interceptors/interceptor.decorator.js.map +1 -0
- package/dist/core/interceptors/interceptor.interface.d.ts +31 -0
- package/dist/core/interceptors/interceptor.interface.d.ts.map +1 -0
- package/dist/core/interceptors/interceptor.interface.js +2 -0
- package/dist/core/interceptors/interceptor.interface.js.map +1 -0
- package/dist/core/jwt-module.d.ts +51 -0
- package/dist/core/jwt-module.d.ts.map +1 -0
- package/dist/core/jwt-module.js +52 -0
- package/dist/core/jwt-module.js.map +1 -0
- package/dist/core/logger.d.ts +18 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +53 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/middleware/middleware.decorator.d.ts +39 -0
- package/dist/core/middleware/middleware.decorator.d.ts.map +1 -0
- package/dist/core/middleware/middleware.decorator.js +53 -0
- package/dist/core/middleware/middleware.decorator.js.map +1 -0
- package/dist/core/middleware/middleware.interface.d.ts +29 -0
- package/dist/core/middleware/middleware.interface.d.ts.map +1 -0
- package/dist/core/middleware/middleware.interface.js +2 -0
- package/dist/core/middleware/middleware.interface.js.map +1 -0
- package/dist/core/module.d.ts +93 -0
- package/dist/core/module.d.ts.map +1 -0
- package/dist/core/module.js +87 -0
- package/dist/core/module.js.map +1 -0
- package/dist/core/oauth-module.d.ts +123 -0
- package/dist/core/oauth-module.d.ts.map +1 -0
- package/dist/core/oauth-module.js +324 -0
- package/dist/core/oauth-module.js.map +1 -0
- package/dist/core/pipes/pipe.decorator.d.ts +64 -0
- package/dist/core/pipes/pipe.decorator.d.ts.map +1 -0
- package/dist/core/pipes/pipe.decorator.js +85 -0
- package/dist/core/pipes/pipe.decorator.js.map +1 -0
- package/dist/core/pipes/pipe.interface.d.ts +41 -0
- package/dist/core/pipes/pipe.interface.d.ts.map +1 -0
- package/dist/core/pipes/pipe.interface.js +2 -0
- package/dist/core/pipes/pipe.interface.js.map +1 -0
- package/dist/core/prompt.d.ts +46 -0
- package/dist/core/prompt.d.ts.map +1 -0
- package/dist/core/prompt.js +76 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/resource.d.ts +47 -0
- package/dist/core/resource.d.ts.map +1 -0
- package/dist/core/resource.js +90 -0
- package/dist/core/resource.js.map +1 -0
- package/dist/core/server.d.ts +129 -0
- package/dist/core/server.d.ts.map +1 -0
- package/dist/core/server.js +617 -0
- package/dist/core/server.js.map +1 -0
- package/dist/core/tool.d.ts +108 -0
- package/dist/core/tool.d.ts.map +1 -0
- package/dist/core/tool.js +241 -0
- package/dist/core/tool.js.map +1 -0
- package/dist/core/transports/discovery-http-server.d.ts +19 -0
- package/dist/core/transports/discovery-http-server.d.ts.map +1 -0
- package/dist/core/transports/discovery-http-server.js +54 -0
- package/dist/core/transports/discovery-http-server.js.map +1 -0
- package/dist/core/transports/http-server.d.ts +108 -0
- package/dist/core/transports/http-server.d.ts.map +1 -0
- package/dist/core/transports/http-server.js +293 -0
- package/dist/core/transports/http-server.js.map +1 -0
- package/dist/core/transports/streamable-http.d.ts +177 -0
- package/dist/core/transports/streamable-http.d.ts.map +1 -0
- package/dist/core/transports/streamable-http.js +1287 -0
- package/dist/core/transports/streamable-http.js.map +1 -0
- package/dist/core/types.d.ts +195 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/widgets/widget-examples.resource.d.ts +17 -0
- package/dist/core/widgets/widget-examples.resource.d.ts.map +1 -0
- package/dist/core/widgets/widget-examples.resource.js +28 -0
- package/dist/core/widgets/widget-examples.resource.js.map +1 -0
- package/dist/core/widgets/widget-registry.d.ts +56 -0
- package/dist/core/widgets/widget-registry.d.ts.map +1 -0
- package/dist/core/widgets/widget-registry.js +75 -0
- package/dist/core/widgets/widget-registry.js.map +1 -0
- package/dist/testing/index.d.ts +103 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +161 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/ui-next/index.d.ts +31 -0
- package/dist/ui-next/index.d.ts.map +1 -0
- package/dist/ui-next/index.js +687 -0
- package/dist/ui-next/index.js.map +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Component } from './component.js';
|
|
4
|
+
import { ExecutionContext, JsonValue } from './types.js';
|
|
5
|
+
import { GuardConstructor } from './guards/guard.interface.js';
|
|
6
|
+
import { MiddlewareConstructor } from './middleware/middleware.interface.js';
|
|
7
|
+
import { InterceptorConstructor } from './interceptors/interceptor.interface.js';
|
|
8
|
+
import { PipeConstructor } from './pipes/pipe.interface.js';
|
|
9
|
+
import { ExceptionFilterConstructor } from './filters/exception-filter.interface.js';
|
|
10
|
+
/**
|
|
11
|
+
* JSON Schema representation for tool input
|
|
12
|
+
*/
|
|
13
|
+
export interface JsonSchema {
|
|
14
|
+
type?: string;
|
|
15
|
+
properties?: Record<string, JsonSchema>;
|
|
16
|
+
required?: string[];
|
|
17
|
+
items?: JsonSchema;
|
|
18
|
+
additionalProperties?: boolean | JsonSchema;
|
|
19
|
+
description?: string;
|
|
20
|
+
default?: JsonValue;
|
|
21
|
+
enum?: JsonValue[];
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Tool input schema - can be Zod schema or JSON Schema
|
|
26
|
+
*/
|
|
27
|
+
export type ToolInputSchema = z.ZodSchema | JsonSchema;
|
|
28
|
+
/**
|
|
29
|
+
* Tool handler function type
|
|
30
|
+
*/
|
|
31
|
+
export type ToolHandler<TInput = unknown, TOutput = unknown> = (input: TInput, context: ExecutionContext) => Promise<TOutput>;
|
|
32
|
+
/**
|
|
33
|
+
* Tool examples for documentation
|
|
34
|
+
*/
|
|
35
|
+
export interface ToolExamples {
|
|
36
|
+
request?: JsonValue;
|
|
37
|
+
response?: JsonValue;
|
|
38
|
+
}
|
|
39
|
+
export interface ToolOptions<TInput = unknown, TOutput = unknown> {
|
|
40
|
+
name: string;
|
|
41
|
+
description: string;
|
|
42
|
+
inputSchema: ToolInputSchema;
|
|
43
|
+
handler: ToolHandler<TInput, TOutput>;
|
|
44
|
+
guards?: GuardConstructor[];
|
|
45
|
+
middlewares?: MiddlewareConstructor[];
|
|
46
|
+
interceptors?: InterceptorConstructor[];
|
|
47
|
+
pipes?: PipeConstructor[];
|
|
48
|
+
filters?: ExceptionFilterConstructor[];
|
|
49
|
+
examples?: ToolExamples;
|
|
50
|
+
widget?: {
|
|
51
|
+
route: string;
|
|
52
|
+
};
|
|
53
|
+
outputTemplate?: string;
|
|
54
|
+
isInitial?: boolean;
|
|
55
|
+
}
|
|
56
|
+
export declare class Tool<TInput = unknown, TOutput = unknown> {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
inputSchema: ToolInputSchema;
|
|
60
|
+
examples?: ToolExamples;
|
|
61
|
+
widget?: {
|
|
62
|
+
route: string;
|
|
63
|
+
};
|
|
64
|
+
outputTemplate?: string;
|
|
65
|
+
isInitial?: boolean;
|
|
66
|
+
private handler;
|
|
67
|
+
private guards;
|
|
68
|
+
private middlewares;
|
|
69
|
+
private interceptors;
|
|
70
|
+
private pipes;
|
|
71
|
+
private filters;
|
|
72
|
+
private component?;
|
|
73
|
+
constructor(options: ToolOptions<TInput, TOutput>);
|
|
74
|
+
/**
|
|
75
|
+
* Execute the tool with full pipeline:
|
|
76
|
+
* Exception Filters -> Guards -> Middleware -> Interceptors -> Pipes -> Handler
|
|
77
|
+
*/
|
|
78
|
+
execute(input: TInput, context: ExecutionContext): Promise<TOutput>;
|
|
79
|
+
/**
|
|
80
|
+
* Execute with interceptor chain
|
|
81
|
+
*/
|
|
82
|
+
private executeWithInterceptors;
|
|
83
|
+
/**
|
|
84
|
+
* Execute pipes and then the handler
|
|
85
|
+
*/
|
|
86
|
+
private executeWithPipes;
|
|
87
|
+
/**
|
|
88
|
+
* Attach a UI component to this tool
|
|
89
|
+
*/
|
|
90
|
+
setComponent(component: Component): void;
|
|
91
|
+
/**
|
|
92
|
+
* Check if tool has a component
|
|
93
|
+
*/
|
|
94
|
+
hasComponent(): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Get the attached component
|
|
97
|
+
*/
|
|
98
|
+
getComponent(): Component | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Check if schema is a Zod schema
|
|
101
|
+
*/
|
|
102
|
+
private isZodSchema;
|
|
103
|
+
/**
|
|
104
|
+
* Convert to MCP tool format
|
|
105
|
+
*/
|
|
106
|
+
toMcpTool(): Promise<McpTool>;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/core/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAS,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAuB,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAClG,OAAO,EAAwB,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AACvG,OAAO,EAAiB,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,EAA4B,0BAA0B,EAAE,MAAM,yCAAyC,CAAC;AAG/G;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,oBAAoB,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IAC5C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,SAAS,CAAC;IACpB,IAAI,CAAC,EAAE,SAAS,EAAE,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC;AAEvD;;GAEG;AACH,MA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OAAO,CAAC,OAAO,CAA+B;IAC9C,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,WAAW,CAA0B;IAC7C,OAAO,CAAC,YAAY,CAA2B;IAC/C,OAAO,CAAC,KAAK,CAAoB;IACjC,OAAO,CAAC,OAAO,CAA+B;IAC9C,OAAO,CAAC,SAAS,CAAC,CAAY;gBAElB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC;IAgBjD;;;OAGG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAuDzE;;OAEG;YACW,uBAAuB;IAoBrC;;OAEG;YACW,gBAAgB;IAqB9B;;OAEG;IACH,YAAY,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;IAIxC;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,YAAY,IAAI,SAAS,GAAG,SAAS;IAIrC;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;CAuHpC"}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { DIContainer } from './di/container.js';
|
|
2
|
+
export class Tool {
|
|
3
|
+
name;
|
|
4
|
+
description;
|
|
5
|
+
inputSchema;
|
|
6
|
+
examples;
|
|
7
|
+
widget;
|
|
8
|
+
outputTemplate;
|
|
9
|
+
isInitial;
|
|
10
|
+
handler;
|
|
11
|
+
guards;
|
|
12
|
+
middlewares;
|
|
13
|
+
interceptors;
|
|
14
|
+
pipes;
|
|
15
|
+
filters;
|
|
16
|
+
component;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.name = options.name;
|
|
19
|
+
this.description = options.description;
|
|
20
|
+
this.inputSchema = options.inputSchema;
|
|
21
|
+
this.handler = options.handler;
|
|
22
|
+
this.guards = options.guards || [];
|
|
23
|
+
this.middlewares = options.middlewares || [];
|
|
24
|
+
this.interceptors = options.interceptors || [];
|
|
25
|
+
this.pipes = options.pipes || [];
|
|
26
|
+
this.filters = options.filters || [];
|
|
27
|
+
this.examples = options.examples;
|
|
28
|
+
this.widget = options.widget;
|
|
29
|
+
this.outputTemplate = options.outputTemplate;
|
|
30
|
+
this.isInitial = options.isInitial;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Execute the tool with full pipeline:
|
|
34
|
+
* Exception Filters -> Guards -> Middleware -> Interceptors -> Pipes -> Handler
|
|
35
|
+
*/
|
|
36
|
+
async execute(input, context) {
|
|
37
|
+
const container = DIContainer.getInstance();
|
|
38
|
+
try {
|
|
39
|
+
// 1. Execute Guards
|
|
40
|
+
for (const GuardClass of this.guards) {
|
|
41
|
+
const guard = container.has(GuardClass)
|
|
42
|
+
? container.resolve(GuardClass)
|
|
43
|
+
: new GuardClass();
|
|
44
|
+
const canActivate = await guard.canActivate(context);
|
|
45
|
+
if (!canActivate) {
|
|
46
|
+
throw new Error('Access denied by guard');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// 2. Build Middleware Chain
|
|
50
|
+
const middlewareChain = async (chainInput) => {
|
|
51
|
+
let index = 0;
|
|
52
|
+
const middlewareInstances = this.middlewares.map(M => container.has(M) ? container.resolve(M) : new M());
|
|
53
|
+
const next = async () => {
|
|
54
|
+
if (index >= middlewareInstances.length) {
|
|
55
|
+
// 3. Build Interceptor Chain
|
|
56
|
+
return await this.executeWithInterceptors(chainInput, context);
|
|
57
|
+
}
|
|
58
|
+
const middleware = middlewareInstances[index++];
|
|
59
|
+
return await middleware.use(context, next);
|
|
60
|
+
};
|
|
61
|
+
return await next();
|
|
62
|
+
};
|
|
63
|
+
return await middlewareChain(input);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
// Execute Exception Filters
|
|
67
|
+
if (this.filters.length > 0) {
|
|
68
|
+
for (const FilterClass of this.filters) {
|
|
69
|
+
const filter = container.has(FilterClass)
|
|
70
|
+
? container.resolve(FilterClass)
|
|
71
|
+
: new FilterClass();
|
|
72
|
+
return await filter.catch(error, context);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Re-throw if no filters handled it
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Execute with interceptor chain
|
|
81
|
+
*/
|
|
82
|
+
async executeWithInterceptors(input, context) {
|
|
83
|
+
const container = DIContainer.getInstance();
|
|
84
|
+
let index = 0;
|
|
85
|
+
const interceptorInstances = this.interceptors.map(I => container.has(I) ? container.resolve(I) : new I());
|
|
86
|
+
const next = async () => {
|
|
87
|
+
if (index >= interceptorInstances.length) {
|
|
88
|
+
// 4. Execute Pipes, then Handler
|
|
89
|
+
return await this.executeWithPipes(input, context);
|
|
90
|
+
}
|
|
91
|
+
const interceptor = interceptorInstances[index++];
|
|
92
|
+
return await interceptor.intercept(context, next);
|
|
93
|
+
};
|
|
94
|
+
return await next();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Execute pipes and then the handler
|
|
98
|
+
*/
|
|
99
|
+
async executeWithPipes(input, context) {
|
|
100
|
+
const container = DIContainer.getInstance();
|
|
101
|
+
let transformedInput = input;
|
|
102
|
+
// Execute Pipes
|
|
103
|
+
for (const PipeClass of this.pipes) {
|
|
104
|
+
const pipe = container.has(PipeClass)
|
|
105
|
+
? container.resolve(PipeClass)
|
|
106
|
+
: new PipeClass();
|
|
107
|
+
transformedInput = await pipe.transform(transformedInput, {
|
|
108
|
+
type: 'body',
|
|
109
|
+
metatype: undefined,
|
|
110
|
+
data: undefined,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// Finally, execute the actual handler
|
|
114
|
+
return await this.handler(transformedInput, context);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Attach a UI component to this tool
|
|
118
|
+
*/
|
|
119
|
+
setComponent(component) {
|
|
120
|
+
this.component = component;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Check if tool has a component
|
|
124
|
+
*/
|
|
125
|
+
hasComponent() {
|
|
126
|
+
return this.component !== undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get the attached component
|
|
130
|
+
*/
|
|
131
|
+
getComponent() {
|
|
132
|
+
return this.component;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if schema is a Zod schema
|
|
136
|
+
*/
|
|
137
|
+
isZodSchema(schema) {
|
|
138
|
+
return schema && typeof schema === 'object' && '_def' in schema;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Convert to MCP tool format
|
|
142
|
+
*/
|
|
143
|
+
async toMcpTool() {
|
|
144
|
+
// Convert Zod schema to JSON Schema if needed
|
|
145
|
+
let jsonSchema = this.inputSchema;
|
|
146
|
+
// If it's a Zod schema, convert it
|
|
147
|
+
if (this.isZodSchema(this.inputSchema)) {
|
|
148
|
+
// It's a Zod schema, need to convert to JSON Schema
|
|
149
|
+
try {
|
|
150
|
+
// Dynamic import for ES modules
|
|
151
|
+
const zodToJsonSchemaModule = await import('zod-to-json-schema');
|
|
152
|
+
const zodToJsonSchema = zodToJsonSchemaModule.zodToJsonSchema || zodToJsonSchemaModule.default;
|
|
153
|
+
// Convert with $refStrategy: 'none' to inline all properties
|
|
154
|
+
const converted = zodToJsonSchema(this.inputSchema, {
|
|
155
|
+
$refStrategy: 'none', // Don't use $refs, inline everything
|
|
156
|
+
target: 'jsonSchema7',
|
|
157
|
+
});
|
|
158
|
+
jsonSchema = converted;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
// Error converting - use stderr to avoid corrupting MCP protocol
|
|
162
|
+
console.error('Error converting Zod schema:', error);
|
|
163
|
+
// Fallback: create a basic object schema
|
|
164
|
+
jsonSchema = {
|
|
165
|
+
type: 'object',
|
|
166
|
+
properties: {},
|
|
167
|
+
additionalProperties: true,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Ensure type is set to "object" if not present
|
|
172
|
+
if (!jsonSchema.type) {
|
|
173
|
+
jsonSchema = {
|
|
174
|
+
...jsonSchema,
|
|
175
|
+
type: 'object',
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
// Construct the final schema, ensuring type is "object"
|
|
179
|
+
const { type: schemaType, ...restSchema } = jsonSchema;
|
|
180
|
+
const finalSchema = {
|
|
181
|
+
...restSchema,
|
|
182
|
+
type: 'object',
|
|
183
|
+
};
|
|
184
|
+
const mcpTool = {
|
|
185
|
+
name: this.name,
|
|
186
|
+
description: this.description,
|
|
187
|
+
inputSchema: finalSchema,
|
|
188
|
+
};
|
|
189
|
+
// Initialize _meta if needed
|
|
190
|
+
mcpTool._meta = mcpTool._meta || {};
|
|
191
|
+
// Initial tool flag
|
|
192
|
+
if (this.isInitial) {
|
|
193
|
+
mcpTool._meta['tool/initial'] = true;
|
|
194
|
+
}
|
|
195
|
+
// Include examples if available
|
|
196
|
+
if (this.examples) {
|
|
197
|
+
mcpTool.examples = this.examples;
|
|
198
|
+
// CRITICAL: Also store examples in _meta to ensure they're preserved
|
|
199
|
+
// through MCP serialization/deserialization. The studio checks _meta
|
|
200
|
+
// fields, and custom top-level fields may be stripped by the MCP SDK.
|
|
201
|
+
mcpTool._meta['tool/examples'] = this.examples;
|
|
202
|
+
}
|
|
203
|
+
// Include widget if available (for backward compatibility)
|
|
204
|
+
if (this.widget) {
|
|
205
|
+
mcpTool.widget = this.widget;
|
|
206
|
+
}
|
|
207
|
+
// Include outputTemplate if available (for backward compatibility)
|
|
208
|
+
if (this.outputTemplate) {
|
|
209
|
+
mcpTool.outputTemplate = this.outputTemplate;
|
|
210
|
+
}
|
|
211
|
+
// CRITICAL: Also store widget info in _meta to ensure it's preserved
|
|
212
|
+
// through MCP serialization/deserialization. The studio checks _meta
|
|
213
|
+
// fields, and custom top-level fields may be stripped by the MCP SDK.
|
|
214
|
+
// 1. Check if we have an attached component (preferred)
|
|
215
|
+
if (this.component) {
|
|
216
|
+
const resourceUri = this.component.getResourceUri();
|
|
217
|
+
mcpTool._meta['ui/template'] = resourceUri;
|
|
218
|
+
mcpTool._meta['openai/outputTemplate'] = resourceUri;
|
|
219
|
+
// Add other component metadata
|
|
220
|
+
const componentMeta = this.component.getResourceMetadata();
|
|
221
|
+
if (componentMeta) {
|
|
222
|
+
// Merge relevant metadata
|
|
223
|
+
const widgetDesc = componentMeta['openai/widgetDescription'];
|
|
224
|
+
if (widgetDesc !== undefined) {
|
|
225
|
+
mcpTool._meta['openai/widgetDescription'] = widgetDesc;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// 2. Fallback to legacy widget/outputTemplate options
|
|
230
|
+
else if (this.widget || this.outputTemplate) {
|
|
231
|
+
// Prioritize widget.route over outputTemplate
|
|
232
|
+
const widgetRoute = this.widget?.route || this.outputTemplate;
|
|
233
|
+
if (widgetRoute) {
|
|
234
|
+
mcpTool._meta['ui/template'] = widgetRoute;
|
|
235
|
+
mcpTool._meta['openai/outputTemplate'] = widgetRoute;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return mcpTool;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../../src/core/tool.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAwDhD,MAAM,OAAO,IAAI;IACf,IAAI,CAAS;IACb,WAAW,CAAS;IACpB,WAAW,CAAkB;IAC7B,QAAQ,CAAgB;IACxB,MAAM,CAEJ;IACF,cAAc,CAAU;IACxB,SAAS,CAAW;IACZ,OAAO,CAA+B;IACtC,MAAM,CAAqB;IAC3B,WAAW,CAA0B;IACrC,YAAY,CAA2B;IACvC,KAAK,CAAoB;IACzB,OAAO,CAA+B;IACtC,SAAS,CAAa;IAE9B,YAAY,OAAqC;QAC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AAK,CAAC,uBAAuB,CAAC,GAAG,WAAW,CAAC;YACvD,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
interface Logger {
|
|
3
|
+
info(message: string, ...args: unknown[]): void;
|
|
4
|
+
warn(message: string, ...args: unknown[]): void;
|
|
5
|
+
error(message: string, ...args: unknown[]): void;
|
|
6
|
+
}
|
|
7
|
+
export declare class DiscoveryHttpServer {
|
|
8
|
+
private port;
|
|
9
|
+
private logger;
|
|
10
|
+
private server;
|
|
11
|
+
private handlers;
|
|
12
|
+
constructor(port: number, logger: Logger);
|
|
13
|
+
private handleRequest;
|
|
14
|
+
on(path: string, handler: http.RequestListener): void;
|
|
15
|
+
start(): Promise<void>;
|
|
16
|
+
stop(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=discovery-http-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery-http-server.d.ts","sourceRoot":"","sources":["../../../src/core/transports/discovery-http-server.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,UAAU,MAAM;IACd,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAChD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAChD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;CAClD;AAED,qBAAa,mBAAmB;IAIlB,OAAO,CAAC,IAAI;IAAU,OAAO,CAAC,MAAM;IAHhD,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,QAAQ,CAAgD;gBAE5C,IAAI,EAAE,MAAM,EAAU,MAAM,EAAE,MAAM;IAIxD,OAAO,CAAC,aAAa;IAYd,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,eAAe;IAK9C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IActB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAY7B"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
export class DiscoveryHttpServer {
|
|
3
|
+
port;
|
|
4
|
+
logger;
|
|
5
|
+
server;
|
|
6
|
+
handlers = new Map();
|
|
7
|
+
constructor(port, logger) {
|
|
8
|
+
this.port = port;
|
|
9
|
+
this.logger = logger;
|
|
10
|
+
this.server = http.createServer(this.handleRequest.bind(this));
|
|
11
|
+
}
|
|
12
|
+
handleRequest(req, res) {
|
|
13
|
+
this.logger.info(`DiscoveryHttpServer: Received request for ${req.url}`);
|
|
14
|
+
const handler = this.handlers.get(req.url || '');
|
|
15
|
+
if (handler) {
|
|
16
|
+
handler(req, res);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
this.logger.warn(`DiscoveryHttpServer: No handler found for ${req.url}`);
|
|
20
|
+
res.writeHead(404);
|
|
21
|
+
res.end();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
on(path, handler) {
|
|
25
|
+
this.logger.info(`DiscoveryHttpServer: Registering handler for ${path}`);
|
|
26
|
+
this.handlers.set(path, handler);
|
|
27
|
+
}
|
|
28
|
+
start() {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
this.server.listen(this.port, () => {
|
|
31
|
+
this.logger.info(`🚀 OAuth discovery server running at http://localhost:${this.port}`);
|
|
32
|
+
resolve();
|
|
33
|
+
});
|
|
34
|
+
this.server.on('error', (err) => {
|
|
35
|
+
this.logger.error('OAuth discovery server error', err);
|
|
36
|
+
reject(err);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
stop() {
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
if (this.server.listening) {
|
|
43
|
+
this.server.close(() => {
|
|
44
|
+
this.logger.info('OAuth discovery server stopped');
|
|
45
|
+
resolve();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
resolve();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=discovery-http-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery-http-server.js","sourceRoot":"","sources":["../../../src/core/transports/discovery-http-server.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AAQxB,MAAM,OAAO,mBAAmB;IAIV;IAAsB;IAHlC,MAAM,CAAc;IACpB,QAAQ,GAAsC,IAAI,GAAG,EAAE,CAAC;IAEhE,YAAoB,IAAY,EAAU,MAAc;QAApC,SAAI,GAAJ,IAAI,CAAQ;QAAU,WAAM,GAAN,MAAM,CAAQ;QACtD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACjE,CAAC;IAEO,aAAa,CAAC,GAAyB,EAAE,GAAwB;QACvE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,6CAA6C,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;QACzE,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QACjD,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACpB,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,6CAA6C,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;YACzE,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;YACnB,GAA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+
* HTTP Server Transport for MCP
|
|
3
|
+
*
|
|
4
|
+
* Implements HTTP transport with Server-Sent Events (SSE) for bidirectional communication.
|
|
5
|
+
* Required for OAuth 2.1 authentication as per MCP specification.
|
|
6
|
+
*
|
|
7
|
+
* This transport:
|
|
8
|
+
* - Exposes MCP over HTTP endpoints
|
|
9
|
+
* - Provides OAuth 2.1 metadata endpoints (RFC 9728)
|
|
10
|
+
* - Uses SSE for server-to-client messages
|
|
11
|
+
* - Uses POST for client-to-server messages
|
|
12
|
+
*/
|
|
13
|
+
import { Express, Request, Response } from 'express';
|
|
14
|
+
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
15
|
+
import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js';
|
|
16
|
+
export interface HttpServerTransportOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Port to listen on (default: 3000)
|
|
19
|
+
*/
|
|
20
|
+
port?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Host to bind to (default: '0.0.0.0')
|
|
23
|
+
*/
|
|
24
|
+
host?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Base path for MCP endpoints (default: '/mcp')
|
|
27
|
+
*/
|
|
28
|
+
basePath?: string;
|
|
29
|
+
/**
|
|
30
|
+
* OAuth configuration (if enabled)
|
|
31
|
+
*/
|
|
32
|
+
oauth?: {
|
|
33
|
+
resourceUri: string;
|
|
34
|
+
authorizationServers: string[];
|
|
35
|
+
scopesSupported?: string[];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Custom Express app (optional - for integration with existing apps)
|
|
39
|
+
*/
|
|
40
|
+
app?: Express;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* HTTP Server Transport
|
|
44
|
+
*
|
|
45
|
+
* Implements MCP protocol over HTTP with SSE for real-time communication.
|
|
46
|
+
* Compatible with OAuth 2.1 authentication.
|
|
47
|
+
*/
|
|
48
|
+
export declare class HttpServerTransport implements Transport {
|
|
49
|
+
private app;
|
|
50
|
+
private server;
|
|
51
|
+
private sseClients;
|
|
52
|
+
private messageHandler?;
|
|
53
|
+
private closeHandler?;
|
|
54
|
+
private errorHandler?;
|
|
55
|
+
private options;
|
|
56
|
+
constructor(options?: HttpServerTransportOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Setup HTTP routes for MCP protocol
|
|
59
|
+
*/
|
|
60
|
+
private setupRoutes;
|
|
61
|
+
/**
|
|
62
|
+
* Handle SSE connection for server-to-client messages
|
|
63
|
+
*/
|
|
64
|
+
private handleSSE;
|
|
65
|
+
/**
|
|
66
|
+
* Handle incoming message from client
|
|
67
|
+
*/
|
|
68
|
+
private handleMessage;
|
|
69
|
+
/**
|
|
70
|
+
* Handle OAuth Protected Resource Metadata request (RFC 9728)
|
|
71
|
+
*/
|
|
72
|
+
private handleProtectedResourceMetadata;
|
|
73
|
+
/**
|
|
74
|
+
* Start the HTTP server
|
|
75
|
+
*/
|
|
76
|
+
start(): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Send message to client(s)
|
|
79
|
+
*/
|
|
80
|
+
send(message: JSONRPCMessage): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Close the transport
|
|
83
|
+
*/
|
|
84
|
+
close(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Set message handler
|
|
87
|
+
*/
|
|
88
|
+
set onmessage(handler: (message: JSONRPCMessage) => Promise<void>);
|
|
89
|
+
/**
|
|
90
|
+
* Set close handler
|
|
91
|
+
*/
|
|
92
|
+
set onclose(handler: () => void);
|
|
93
|
+
/**
|
|
94
|
+
* Set error handler
|
|
95
|
+
*/
|
|
96
|
+
set onerror(handler: (error: Error) => void);
|
|
97
|
+
/**
|
|
98
|
+
* Get the Express app (for adding custom routes)
|
|
99
|
+
*/
|
|
100
|
+
getApp(): Express;
|
|
101
|
+
/**
|
|
102
|
+
* Register a custom route handler (compatible with DiscoveryHttpServer interface)
|
|
103
|
+
* @param path - The route path
|
|
104
|
+
* @param handler - The request handler (Node.js http.RequestListener format)
|
|
105
|
+
*/
|
|
106
|
+
on(path: string, handler: (req: Request, res: Response) => void): void;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=http-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-server.d.ts","sourceRoot":"","sources":["../../../src/core/transports/http-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAgB,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE9D,OAAO,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,KAAK,CAAC,EAAE;QACN,WAAW,EAAE,MAAM,CAAC;QACpB,oBAAoB,EAAE,MAAM,EAAE,CAAC;QAC/B,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;KAC5B,CAAC;IAEF;;OAEG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;;;GAKG;AACH,qBAAa,mBAAoB,YAAW,SAAS;IACnD,OAAO,CAAC,GAAG,CAAU;IACrB,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,UAAU,CAAoC;IACtD,OAAO,CAAC,cAAc,CAAC,CAA6C;IACpE,OAAO,CAAC,YAAY,CAAC,CAAa;IAClC,OAAO,CAAC,YAAY,CAAC,CAAyB;IAC9C,OAAO,CAAC,OAAO,CAAgH;gBAEnH,OAAO,GAAE,0BAA+B;IAYpD;;OAEG;IACH,OAAO,CAAC,WAAW;IA8DnB;;OAEG;IACH,OAAO,CAAC,SAAS;IAsBjB;;OAEG;Y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