@iludolf/mcp-adapter 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/README.md +168 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +9 -0
- package/dist/constants.js.map +1 -0
- package/dist/decorators/base.decorator.d.ts +1 -0
- package/dist/decorators/base.decorator.js +15 -0
- package/dist/decorators/base.decorator.js.map +1 -0
- package/dist/decorators/constants.d.ts +3 -0
- package/dist/decorators/constants.js +7 -0
- package/dist/decorators/constants.js.map +1 -0
- package/dist/decorators/index.d.ts +5 -0
- package/dist/decorators/index.js +22 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/mcp-prompt.decorator.d.ts +2 -0
- package/dist/decorators/mcp-prompt.decorator.js +8 -0
- package/dist/decorators/mcp-prompt.decorator.js.map +1 -0
- package/dist/decorators/mcp-resource.decorator.d.ts +2 -0
- package/dist/decorators/mcp-resource.decorator.js +8 -0
- package/dist/decorators/mcp-resource.decorator.js.map +1 -0
- package/dist/decorators/mcp-tool.decorator.d.ts +2 -0
- package/dist/decorators/mcp-tool.decorator.js +8 -0
- package/dist/decorators/mcp-tool.decorator.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/index.d.ts +2 -0
- package/dist/interfaces/index.js +19 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/mcp-logger.interface.d.ts +12 -0
- package/dist/interfaces/mcp-logger.interface.js +22 -0
- package/dist/interfaces/mcp-logger.interface.js.map +1 -0
- package/dist/interfaces/mcp-module-options.interface.d.ts +49 -0
- package/dist/interfaces/mcp-module-options.interface.js +11 -0
- package/dist/interfaces/mcp-module-options.interface.js.map +1 -0
- package/dist/mcp.controller.d.ts +13 -0
- package/dist/mcp.controller.js +85 -0
- package/dist/mcp.controller.js.map +1 -0
- package/dist/mcp.module.d.ts +16 -0
- package/dist/mcp.module.js +168 -0
- package/dist/mcp.module.js.map +1 -0
- package/dist/services/explorer.service.d.ts +22 -0
- package/dist/services/explorer.service.js +71 -0
- package/dist/services/explorer.service.js.map +1 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.js +20 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/mcp-http.service.d.ts +12 -0
- package/dist/services/mcp-http.service.js +93 -0
- package/dist/services/mcp-http.service.js.map +1 -0
- package/dist/services/mcp.service.d.ts +21 -0
- package/dist/services/mcp.service.js +134 -0
- package/dist/services/mcp.service.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var McpModule_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.McpModule = void 0;
|
|
17
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
18
|
+
const common_1 = require("@nestjs/common");
|
|
19
|
+
const core_1 = require("@nestjs/core");
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
|
+
const interfaces_1 = require("./interfaces");
|
|
22
|
+
const mcp_controller_1 = require("./mcp.controller");
|
|
23
|
+
const services_1 = require("./services");
|
|
24
|
+
let McpModule = McpModule_1 = class McpModule {
|
|
25
|
+
constructor(explorerService, mcpService, logger) {
|
|
26
|
+
this.explorerService = explorerService;
|
|
27
|
+
this.mcpService = mcpService;
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
}
|
|
30
|
+
static forRoot(options) {
|
|
31
|
+
const logger = options.logger || new interfaces_1.DefaultMcpLogger();
|
|
32
|
+
const providers = [
|
|
33
|
+
{ provide: constants_1.MCP_MODULE_OPTIONS, useValue: options },
|
|
34
|
+
{ provide: constants_1.MCP_LOGGER, useValue: logger },
|
|
35
|
+
services_1.ExplorerService,
|
|
36
|
+
services_1.McpService,
|
|
37
|
+
services_1.McpHttpService,
|
|
38
|
+
core_1.MetadataScanner,
|
|
39
|
+
core_1.Reflector,
|
|
40
|
+
];
|
|
41
|
+
const controllers = options.transport === interfaces_1.TransportType.SSE ||
|
|
42
|
+
options.transport === interfaces_1.TransportType.HTTP
|
|
43
|
+
? [mcp_controller_1.McpHttpController]
|
|
44
|
+
: [];
|
|
45
|
+
return {
|
|
46
|
+
module: McpModule_1,
|
|
47
|
+
imports: [core_1.DiscoveryModule],
|
|
48
|
+
providers,
|
|
49
|
+
controllers,
|
|
50
|
+
exports: [services_1.McpService, constants_1.MCP_MODULE_OPTIONS, constants_1.MCP_LOGGER],
|
|
51
|
+
global: options.global ?? false,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
static forRootAsync(options) {
|
|
55
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
56
|
+
return {
|
|
57
|
+
module: McpModule_1,
|
|
58
|
+
imports: [...(options.imports || []), core_1.DiscoveryModule],
|
|
59
|
+
providers: [
|
|
60
|
+
...asyncProviders,
|
|
61
|
+
services_1.ExplorerService,
|
|
62
|
+
services_1.McpService,
|
|
63
|
+
services_1.McpHttpService,
|
|
64
|
+
core_1.MetadataScanner,
|
|
65
|
+
core_1.Reflector,
|
|
66
|
+
],
|
|
67
|
+
controllers: [mcp_controller_1.McpHttpController],
|
|
68
|
+
exports: [services_1.McpService, constants_1.MCP_MODULE_OPTIONS, constants_1.MCP_LOGGER],
|
|
69
|
+
global: options.global ?? false,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
static createAsyncProviders(options) {
|
|
73
|
+
if (options.useExisting || options.useFactory) {
|
|
74
|
+
return [
|
|
75
|
+
this.createAsyncOptionsProvider(options),
|
|
76
|
+
this.createAsyncLoggerProvider(),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
const useClass = options.useClass;
|
|
80
|
+
return [
|
|
81
|
+
this.createAsyncOptionsProvider(options),
|
|
82
|
+
this.createAsyncLoggerProvider(),
|
|
83
|
+
{ provide: useClass, useClass },
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
static createAsyncOptionsProvider(options) {
|
|
87
|
+
if (options.useFactory) {
|
|
88
|
+
return {
|
|
89
|
+
provide: constants_1.MCP_MODULE_OPTIONS,
|
|
90
|
+
useFactory: options.useFactory,
|
|
91
|
+
inject: options.inject || [],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const inject = [
|
|
95
|
+
(options.useClass || options.useExisting),
|
|
96
|
+
];
|
|
97
|
+
return {
|
|
98
|
+
provide: constants_1.MCP_MODULE_OPTIONS,
|
|
99
|
+
useFactory: async (optionsFactory) => {
|
|
100
|
+
return optionsFactory.createMcpOptions();
|
|
101
|
+
},
|
|
102
|
+
inject,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
static createAsyncLoggerProvider() {
|
|
106
|
+
return {
|
|
107
|
+
provide: constants_1.MCP_LOGGER,
|
|
108
|
+
useFactory: (options) => {
|
|
109
|
+
return options.logger || new interfaces_1.DefaultMcpLogger();
|
|
110
|
+
},
|
|
111
|
+
inject: [constants_1.MCP_MODULE_OPTIONS],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async onApplicationBootstrap() {
|
|
115
|
+
this.logger.info("Bootstrapping MCP Module...", constants_1.MCP_LOGGER_CONTEXT);
|
|
116
|
+
this.registerMcpHandlers();
|
|
117
|
+
this.logger.info("MCP Handlers registered.", constants_1.MCP_LOGGER_CONTEXT);
|
|
118
|
+
const opts = this.mcpService.getOptions();
|
|
119
|
+
if (opts?.transport === interfaces_1.TransportType.STDIO) {
|
|
120
|
+
try {
|
|
121
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
122
|
+
const server = this.mcpService.getServer();
|
|
123
|
+
await server.connect(transport);
|
|
124
|
+
this.logger.info("McpServer connected to stdio transport.", constants_1.MCP_LOGGER_CONTEXT);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
this.logger.error("Failed to connect McpServer to stdio transport:", constants_1.MCP_LOGGER_CONTEXT, error.stack);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
this.logger.info("MCP Module configured for non-stdio transport. No automatic connection needed.", constants_1.MCP_LOGGER_CONTEXT);
|
|
132
|
+
}
|
|
133
|
+
this.logger.info("MCP Module bootstrapped successfully.", constants_1.MCP_LOGGER_CONTEXT);
|
|
134
|
+
}
|
|
135
|
+
registerMcpHandlers() {
|
|
136
|
+
const resources = this.explorerService.exploreResources();
|
|
137
|
+
resources.forEach(({ handler, metadata }) => {
|
|
138
|
+
this.logger.debug(`Registering resource: ${metadata.options?.name || metadata.methodName}`, constants_1.MCP_LOGGER_CONTEXT);
|
|
139
|
+
this.mcpService.registerResource(metadata.options, handler);
|
|
140
|
+
});
|
|
141
|
+
const tools = this.explorerService.exploreTools();
|
|
142
|
+
tools.forEach(({ handler, metadata }) => {
|
|
143
|
+
if (!metadata.options?.name) {
|
|
144
|
+
this.logger.warn(`Tool handler ${metadata.methodName} missing required name. Skipping.`, constants_1.MCP_LOGGER_CONTEXT);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.logger.debug(`Registering tool: ${metadata.options.name}`, constants_1.MCP_LOGGER_CONTEXT);
|
|
148
|
+
this.mcpService.registerTool(metadata.options, handler);
|
|
149
|
+
});
|
|
150
|
+
const prompts = this.explorerService.explorePrompts();
|
|
151
|
+
prompts.forEach(({ handler, metadata }) => {
|
|
152
|
+
if (!metadata.options?.name) {
|
|
153
|
+
this.logger.warn(`Prompt handler ${metadata.methodName} missing required name. Skipping.`, constants_1.MCP_LOGGER_CONTEXT);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
this.logger.debug(`Registering prompt: ${metadata.options.name}`, constants_1.MCP_LOGGER_CONTEXT);
|
|
157
|
+
this.mcpService.registerPrompt(metadata.options, handler);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
exports.McpModule = McpModule;
|
|
162
|
+
exports.McpModule = McpModule = McpModule_1 = __decorate([
|
|
163
|
+
(0, common_1.Module)({}),
|
|
164
|
+
__param(2, (0, common_1.Inject)(constants_1.MCP_LOGGER)),
|
|
165
|
+
__metadata("design:paramtypes", [services_1.ExplorerService,
|
|
166
|
+
services_1.McpService, Object])
|
|
167
|
+
], McpModule);
|
|
168
|
+
//# sourceMappingURL=mcp.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.module.js","sourceRoot":"","sources":["../src/mcp.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wEAAgF;AAChF,2CAOuB;AACvB,uCAA0E;AAE1E,2CAAgF;AAChF,6CAOqB;AACrB,qDAAoD;AACpD,yCAAwE;AAgDjE,IAAM,SAAS,iBAAf,MAAM,SAAS;IACpB,YACmB,eAAgC,EAChC,UAAsB,EACF,MAAiB;QAFrC,oBAAe,GAAf,eAAe,CAAiB;QAChC,eAAU,GAAV,UAAU,CAAY;QACF,WAAM,GAAN,MAAM,CAAW;IACrD,CAAC;IAKJ,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AC,CAAA;QAC3D,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AA1LY,8BAAS;oBAAT,SAAS;IADrB,IAAA,eAAM,EAAC,EAAE,CAAC;IAKN,WAAA,IAAA,eAAM,EAAC,sBAAU,CAAC,CAAA;qCAFe,0BAAe;QACpB,qBAAU;GAH9B,SAAS,CA0LrB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Prompt as PromptType } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
import { ModulesContainer } from "@nestjs/core/injector/modules-container";
|
|
4
|
+
import { MetadataScanner } from "@nestjs/core/metadata-scanner";
|
|
5
|
+
import { IMetadataBase, ResourceOptions, ToolOptions } from "../interfaces";
|
|
6
|
+
export interface DiscoveredItem<T = any> {
|
|
7
|
+
instance: any;
|
|
8
|
+
handler: (...args: any[]) => any;
|
|
9
|
+
metadata: IMetadataBase & {
|
|
10
|
+
options: T;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export declare class ExplorerService {
|
|
14
|
+
private readonly modulesContainer;
|
|
15
|
+
private readonly metadataScanner;
|
|
16
|
+
private readonly reflector;
|
|
17
|
+
constructor(modulesContainer: ModulesContainer, metadataScanner: MetadataScanner, reflector: Reflector);
|
|
18
|
+
explore<T = any>(metadataKey: string): DiscoveredItem<T>[];
|
|
19
|
+
exploreResources(): DiscoveredItem<ResourceOptions>[];
|
|
20
|
+
exploreTools(): DiscoveredItem<ToolOptions>[];
|
|
21
|
+
explorePrompts(): DiscoveredItem<PromptType>[];
|
|
22
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ExplorerService = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const modules_container_1 = require("@nestjs/core/injector/modules-container");
|
|
16
|
+
const metadata_scanner_1 = require("@nestjs/core/metadata-scanner");
|
|
17
|
+
const constants_1 = require("../decorators/constants");
|
|
18
|
+
let ExplorerService = class ExplorerService {
|
|
19
|
+
constructor(modulesContainer, metadataScanner, reflector) {
|
|
20
|
+
this.modulesContainer = modulesContainer;
|
|
21
|
+
this.metadataScanner = metadataScanner;
|
|
22
|
+
this.reflector = reflector;
|
|
23
|
+
}
|
|
24
|
+
explore(metadataKey) {
|
|
25
|
+
const modules = [...this.modulesContainer.values()];
|
|
26
|
+
const components = modules
|
|
27
|
+
.flatMap((module) => [...module.providers.values()])
|
|
28
|
+
.filter((wrapper) => wrapper.instance);
|
|
29
|
+
const discovered = [];
|
|
30
|
+
components.forEach((wrapper) => {
|
|
31
|
+
const { instance } = wrapper;
|
|
32
|
+
if (!instance || typeof instance !== "object" || !instance.constructor) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
36
|
+
const methodNames = this.metadataScanner.getAllMethodNames(prototype);
|
|
37
|
+
methodNames.forEach((methodName) => {
|
|
38
|
+
const handler = instance[methodName];
|
|
39
|
+
if (!handler) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const metadata = this.reflector.get(metadataKey, handler);
|
|
43
|
+
if (metadata) {
|
|
44
|
+
discovered.push({
|
|
45
|
+
instance,
|
|
46
|
+
handler: handler.bind(instance),
|
|
47
|
+
metadata: metadata,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
return discovered;
|
|
53
|
+
}
|
|
54
|
+
exploreResources() {
|
|
55
|
+
return this.explore(constants_1.MCP_RESOURCE_METADATA);
|
|
56
|
+
}
|
|
57
|
+
exploreTools() {
|
|
58
|
+
return this.explore(constants_1.MCP_TOOL_METADATA);
|
|
59
|
+
}
|
|
60
|
+
explorePrompts() {
|
|
61
|
+
return this.explore(constants_1.MCP_PROMPT_METADATA);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.ExplorerService = ExplorerService;
|
|
65
|
+
exports.ExplorerService = ExplorerService = __decorate([
|
|
66
|
+
(0, common_1.Injectable)(),
|
|
67
|
+
__metadata("design:paramtypes", [modules_container_1.ModulesContainer,
|
|
68
|
+
metadata_scanner_1.MetadataScanner,
|
|
69
|
+
core_1.Reflector])
|
|
70
|
+
], ExplorerService);
|
|
71
|
+
//# sourceMappingURL=explorer.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"explorer.service.js","sourceRoot":"","sources":["../../src/services/explorer.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,2CAA2C;AAC3C,uCAAwC;AAExC,+EAA0E;AAC1E,oEAA+D;AAE/D,uDAIgC;AA0BzB,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,gBAAkC,EAClC,eAAgC,EAChC,SAAoB;QAFpB,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,oBAAe,GAAf,eAAe,CAAiB;QAChC,cAAS,GAAT,SAAS,CAAW;IACpC,CAAC;IAKJ,OAAO,CAAU,WAAmB;QAClC,MAAM,OAAO,GAAG,CAAC,G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AKD,gBAAgB;QACd,OAAO,IAAI,CAAC,OAAO,CAAkB,iCAAqB,CAAC,CAAA;IAC7D,CAAC;IAKD,YAAY;QACV,OAAO,IAAI,CAAC,OAAO,CAAc,6BAAiB,CAAC,CAAA;IACrD,CAAC;IAKD,cAAc;QACZ,OAAO,IAAI,CAAC,OAAO,CAAa,+BAAmB,CAAC,CAAA;IACtD,CAAC;CACF,CAAA;AApEY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;qCAG0B,oCAAgB;QACjB,kCAAe;QACrB,gBAAS;GAJ5B,eAAe,CAoE3B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./explorer.service"), exports);
|
|
18
|
+
__exportStar(require("./mcp.service"), exports);
|
|
19
|
+
__exportStar(require("./mcp-http.service"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,gDAA6B;AAC7B,qDAAkC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Request, Response } from "express";
|
|
2
|
+
import { McpLogger } from "../interfaces";
|
|
3
|
+
import { McpService } from "./mcp.service";
|
|
4
|
+
export declare class McpHttpService {
|
|
5
|
+
private readonly mcpService;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
private readonly loggerCtx;
|
|
8
|
+
private transports;
|
|
9
|
+
constructor(mcpService: McpService, logger: McpLogger);
|
|
10
|
+
handleSSE(res: Response, messagesPath?: string): Promise<void>;
|
|
11
|
+
handleMessage(req: Request, res: Response): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.McpHttpService = void 0;
|
|
16
|
+
const sse_js_1 = require("@modelcontextprotocol/sdk/server/sse.js");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const constants_1 = require("../constants");
|
|
19
|
+
const mcp_service_1 = require("./mcp.service");
|
|
20
|
+
let McpHttpService = class McpHttpService {
|
|
21
|
+
constructor(mcpService, logger) {
|
|
22
|
+
this.mcpService = mcpService;
|
|
23
|
+
this.logger = logger;
|
|
24
|
+
this.loggerCtx = "McpHttpService";
|
|
25
|
+
this.transports = {};
|
|
26
|
+
}
|
|
27
|
+
async handleSSE(res, messagesPath) {
|
|
28
|
+
const basePath = this.mcpService.getOptions().basePath || "mcp";
|
|
29
|
+
const path = messagesPath || `/${basePath}/messages`;
|
|
30
|
+
const transport = new sse_js_1.SSEServerTransport(path, res);
|
|
31
|
+
const sessionId = transport.sessionId;
|
|
32
|
+
this.transports[sessionId] = transport;
|
|
33
|
+
this.logger.info(`New SSE connection established: ${sessionId}`, this.loggerCtx);
|
|
34
|
+
res.on("close", () => {
|
|
35
|
+
this.logger.info(`SSE connection closed: ${sessionId}`, this.loggerCtx);
|
|
36
|
+
delete this.transports[sessionId];
|
|
37
|
+
transport
|
|
38
|
+
.close()
|
|
39
|
+
.catch((err) => this.logger.error("Error closing transport on disconnect: " + err.message, this.loggerCtx));
|
|
40
|
+
});
|
|
41
|
+
try {
|
|
42
|
+
const server = this.mcpService.getServer();
|
|
43
|
+
if (!server) {
|
|
44
|
+
this.logger.error("MCP Server instance is not available.", this.loggerCtx);
|
|
45
|
+
if (!res.headersSent) {
|
|
46
|
+
res.status(500).send("MCP Server not initialized");
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await server.connect(transport);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
this.logger.error(`Error in SSE connection or during connect: ${error.message}`, this.loggerCtx);
|
|
54
|
+
if (!res.headersSent) {
|
|
55
|
+
res.status(500).send("Error establishing SSE connection");
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
res.end();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async handleMessage(req, res) {
|
|
63
|
+
const sessionId = req.query.sessionId;
|
|
64
|
+
if (!sessionId) {
|
|
65
|
+
this.logger.warn("Message received without sessionId", this.loggerCtx);
|
|
66
|
+
res.status(400).send("Missing sessionId parameter");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const transport = this.transports[sessionId];
|
|
70
|
+
if (transport) {
|
|
71
|
+
try {
|
|
72
|
+
await transport.handlePostMessage(req, res, { ...req.body });
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.logger.error(`Error handling message for session ${sessionId}: ${error.message}`, this.loggerCtx);
|
|
76
|
+
if (!res.headersSent) {
|
|
77
|
+
res.status(500).send("Error processing message");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.logger.warn(`No active transport found for sessionId: ${sessionId}`, this.loggerCtx);
|
|
83
|
+
res.status(404).send("No connection found for this sessionId");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
exports.McpHttpService = McpHttpService;
|
|
88
|
+
exports.McpHttpService = McpHttpService = __decorate([
|
|
89
|
+
(0, common_1.Injectable)(),
|
|
90
|
+
__param(1, (0, common_1.Inject)(constants_1.MCP_LOGGER)),
|
|
91
|
+
__metadata("design:paramtypes", [mcp_service_1.McpService, Object])
|
|
92
|
+
], McpHttpService);
|
|
93
|
+
//# sourceMappingURL=mcp-http.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-http.service.js","sourceRoot":"","sources":["../../src/services/mcp-http.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oEAA4E;AAC5E,2CAAmD;AAGnD,4CAAyC;AAEzC,+CAA0C;AAMnC,IAAM,cAAc,GAApB,MAAM,cAAc;IAIzB,YACmB,UAAsB,EACnB,MAAkC;QADrC,eAAU,GAAV,UAAU,CAAY;QACF,WAAM,GAAN,MAAM,CAAW;Q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,OAAO,EAAE,EACnE,IAAI,CAAC,SAAS,CACf,CAAA;gBACD,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;oBACrB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;gBAClD,CAAC;YACH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,4CAA4C,SAAS,EAAE,EACvD,IAAI,CAAC,SAAS,CACf,CAAA;YACD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,wCAAwC,CAAC,CAAA;QAChE,CAAC;IACH,CAAC;CACF,CAAA;AA7FY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAOR,WAAA,IAAA,eAAM,EAAC,sBAAU,CAAC,CAAA;qCADU,wBAAU;GAL9B,cAAc,CA6F1B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { McpServer, PromptCallback, ReadResourceCallback, ReadResourceTemplateCallback, ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { Prompt as PromptType } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import { ZodOptional, ZodRawShape, ZodType, ZodTypeDef } from "zod";
|
|
4
|
+
import { McpLogger, McpModuleOptions, ResourceOptions, ToolOptions } from "../interfaces";
|
|
5
|
+
type PromptArgsRawShape = {
|
|
6
|
+
[k: string]: ZodType<string, ZodTypeDef, string> | ZodOptional<ZodType<string, ZodTypeDef, string>>;
|
|
7
|
+
};
|
|
8
|
+
export declare class McpService {
|
|
9
|
+
private readonly options;
|
|
10
|
+
private readonly logger;
|
|
11
|
+
private readonly loggerCtx;
|
|
12
|
+
private readonly server;
|
|
13
|
+
constructor(options: McpModuleOptions, logger: McpLogger);
|
|
14
|
+
getServer(): McpServer;
|
|
15
|
+
getOptions(): McpModuleOptions;
|
|
16
|
+
registerResource(definition: ResourceOptions, handler: ReadResourceCallback | ReadResourceTemplateCallback): import("@modelcontextprotocol/sdk/server/mcp").RegisteredResourceTemplate | import("@modelcontextprotocol/sdk/server/mcp").RegisteredResource | undefined;
|
|
17
|
+
registerPrompt(definition: PromptType, handler: PromptCallback<undefined> | PromptCallback<PromptArgsRawShape>): import("@modelcontextprotocol/sdk/server/mcp").RegisteredPrompt | undefined;
|
|
18
|
+
registerTool(definition: ToolOptions, handler: ToolCallback | ToolCallback<ZodRawShape>): import("@modelcontextprotocol/sdk/server/mcp").RegisteredTool | undefined;
|
|
19
|
+
private isTemplateResource;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.McpService = void 0;
|
|
16
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const zod_1 = require("zod");
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const interfaces_1 = require("../interfaces");
|
|
21
|
+
let McpService = class McpService {
|
|
22
|
+
constructor(options, logger) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.logger = logger;
|
|
25
|
+
this.loggerCtx = "McpService";
|
|
26
|
+
const { serverInfo, serverOptions, transport } = options;
|
|
27
|
+
this.server = new mcp_js_1.McpServer(serverInfo, serverOptions);
|
|
28
|
+
this.logger.info("McpServer instance created.", this.loggerCtx);
|
|
29
|
+
this.logger.info(`MCP Server Transport: ${transport || interfaces_1.TransportType.STDIO}`, this.loggerCtx);
|
|
30
|
+
}
|
|
31
|
+
getServer() {
|
|
32
|
+
return this.server;
|
|
33
|
+
}
|
|
34
|
+
getOptions() {
|
|
35
|
+
return this.options;
|
|
36
|
+
}
|
|
37
|
+
registerResource(definition, handler) {
|
|
38
|
+
if (!definition.name) {
|
|
39
|
+
this.logger.warn("Resource missing required name. Skipping registration.", this.loggerCtx);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.logger.info(`Registering resource: ${definition.name}`, this.loggerCtx);
|
|
43
|
+
const metadata = definition.metadata || {};
|
|
44
|
+
if (this.isTemplateResource(definition)) {
|
|
45
|
+
if (typeof definition.uriTemplate === "string") {
|
|
46
|
+
const template = new mcp_js_1.ResourceTemplate(definition.uriTemplate, {
|
|
47
|
+
list: undefined,
|
|
48
|
+
});
|
|
49
|
+
return this.server.resource(definition.name, template, metadata, handler);
|
|
50
|
+
}
|
|
51
|
+
else if (definition.uriTemplate instanceof mcp_js_1.ResourceTemplate) {
|
|
52
|
+
return this.server.resource(definition.name, definition.uriTemplate, metadata, handler);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.logger.error("Invalid uriTemplate type for template resource.", this.loggerCtx);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return this.server.resource(definition.name, definition.uri, metadata, handler);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
registerPrompt(definition, handler) {
|
|
64
|
+
if (!definition.name) {
|
|
65
|
+
this.logger.warn("Prompt missing required name. Skipping registration.", this.loggerCtx);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.logger.info(`Registering prompt: ${definition.name}`, this.loggerCtx);
|
|
69
|
+
const description = definition.description || "";
|
|
70
|
+
if (!definition.arguments || definition.arguments.length === 0) {
|
|
71
|
+
return this.server.prompt(definition.name, description, handler);
|
|
72
|
+
}
|
|
73
|
+
const argsSchema = {};
|
|
74
|
+
for (const arg of definition.arguments) {
|
|
75
|
+
if (!arg.name) {
|
|
76
|
+
this.logger.warn(`Prompt "${definition.name}" has an argument without a name. Skipping argument.`, this.loggerCtx);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
let schema = zod_1.z.string();
|
|
80
|
+
if (arg.description) {
|
|
81
|
+
schema = schema.describe(arg.description);
|
|
82
|
+
}
|
|
83
|
+
if (arg.required) {
|
|
84
|
+
argsSchema[arg.name] = schema;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
argsSchema[arg.name] = schema.optional();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (Object.keys(argsSchema).length === 0) {
|
|
91
|
+
this.logger.warn(`Prompt "${definition.name}" arguments processing resulted in empty schema. Registering as no-argument prompt.`, this.loggerCtx);
|
|
92
|
+
return this.server.prompt(definition.name, description, handler);
|
|
93
|
+
}
|
|
94
|
+
return this.server.prompt(definition.name, description, argsSchema, handler);
|
|
95
|
+
}
|
|
96
|
+
registerTool(definition, handler) {
|
|
97
|
+
if (!definition || !definition.name) {
|
|
98
|
+
this.logger.warn("Tool definition missing required name. Skipping registration.", this.loggerCtx);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.logger.info(`Registering tool: ${definition.name}`, this.loggerCtx);
|
|
102
|
+
const description = definition.description;
|
|
103
|
+
const paramsSchema = definition.paramsSchema;
|
|
104
|
+
if (paramsSchema && Object.keys(paramsSchema).length > 0) {
|
|
105
|
+
const specificHandler = handler;
|
|
106
|
+
if (description) {
|
|
107
|
+
return this.server.tool(definition.name, description, paramsSchema, specificHandler);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
this.server.tool(definition.name, paramsSchema, specificHandler);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const specificHandler = handler;
|
|
115
|
+
if (description) {
|
|
116
|
+
this.server.tool(definition.name, description, specificHandler);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this.server.tool(definition.name, specificHandler);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
isTemplateResource(options) {
|
|
124
|
+
return "uriTemplate" in options;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
exports.McpService = McpService;
|
|
128
|
+
exports.McpService = McpService = __decorate([
|
|
129
|
+
(0, common_1.Injectable)(),
|
|
130
|
+
__param(0, (0, common_1.Inject)(constants_1.MCP_MODULE_OPTIONS)),
|
|
131
|
+
__param(1, (0, common_1.Inject)(constants_1.MCP_LOGGER)),
|
|
132
|
+
__metadata("design:paramtypes", [Object, Object])
|
|
133
|
+
], McpService);
|
|
134
|
+
//# sourceMappingURL=mcp.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.service.js","sourceRoot":"","sources":["../../src/services/mcp.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oEAQgD;AAEhD,2CAAmD;AACnD,6BAOY;AAEZ,4CAA6D;AAC7D,8CAOsB;AAaf,IAAM,UAAU,GAAhB,MAAM,UAAU;IAIrB,YAC8B,OAA0C,EAClD,MAAkC;QADT,YAAO,GAAP,OAAO,CAAkB;QACjC,WAAM,GAAN,MAAM,CAAW;QALvC,cAAS,GAAG,YAAY,CAAA;QAOvC,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,GAAG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