@muzikanto/nestjs-mcp 1.1.0 → 1.2.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/CHANGELOG.md +12 -0
- package/README.md +100 -31
- package/dist/decorators/mcp-tool.decorator.d.ts +4 -1
- package/dist/dto/McpMessage.dto.js +2 -2
- package/dist/dto/McpPrompt.dto.js +6 -2
- package/dist/dto/McpPromptMessage.js +12 -3
- package/dist/dto/McpPromptMessages.dto.js +5 -1
- package/dist/dto/McpPrompts.dto.js +5 -1
- package/dist/dto/McpTool.dto.js +12 -3
- package/dist/dto/McpTools.dto.js +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +6 -3
- package/dist/mcp-client/mcp-client.module.d.ts +15 -0
- package/dist/mcp-client/mcp-client.module.js +42 -0
- package/dist/mcp-client/mcp-client.service.d.ts +12 -0
- package/dist/mcp-client/mcp-client.service.js +49 -0
- package/dist/mcp-server/decorators/mcp-prompt.decorator.d.ts +25 -0
- package/dist/mcp-server/decorators/mcp-prompt.decorator.js +16 -0
- package/dist/mcp-server/decorators/mcp-resource.decorator.d.ts +26 -0
- package/dist/mcp-server/decorators/mcp-resource.decorator.js +16 -0
- package/dist/mcp-server/decorators/mcp-tool.decorator.d.ts +20 -0
- package/dist/mcp-server/decorators/mcp-tool.decorator.js +16 -0
- package/dist/mcp-server/dto/McpMessage.dto.d.ts +5 -0
- package/dist/mcp-server/dto/McpMessage.dto.js +26 -0
- package/dist/mcp-server/dto/McpPrompt.dto.d.ts +6 -0
- package/dist/mcp-server/dto/McpPrompt.dto.js +49 -0
- package/dist/mcp-server/dto/McpPromptMessage.d.ts +5 -0
- package/dist/mcp-server/dto/McpPromptMessage.js +40 -0
- package/dist/mcp-server/dto/McpPromptMessages.dto.d.ts +4 -0
- package/dist/mcp-server/dto/McpPromptMessages.dto.js +26 -0
- package/dist/mcp-server/dto/McpPrompts.dto.d.ts +4 -0
- package/dist/mcp-server/dto/McpPrompts.dto.js +26 -0
- package/dist/mcp-server/dto/McpTool.dto.d.ts +6 -0
- package/dist/mcp-server/dto/McpTool.dto.js +49 -0
- package/dist/mcp-server/dto/McpTools.dto.d.ts +4 -0
- package/dist/mcp-server/dto/McpTools.dto.js +22 -0
- package/dist/mcp-server/mcp.controller.d.ts +27 -0
- package/dist/mcp-server/mcp.controller.js +181 -0
- package/dist/mcp-server/mcp.explorer.d.ts +9 -0
- package/dist/mcp-server/mcp.explorer.js +71 -0
- package/dist/mcp-server/mcp.module.d.ts +8 -0
- package/dist/mcp-server/mcp.module.js +45 -0
- package/dist/mcp-server/mcp.service.d.ts +59 -0
- package/dist/mcp-server/mcp.service.js +193 -0
- package/dist/mcp-server/sse.service.d.ts +6 -0
- package/dist/mcp-server/sse.service.js +45 -0
- package/dist/mcp-server/utils/context.decorator.d.ts +1 -0
- package/dist/mcp-server/utils/context.decorator.js +5 -0
- package/dist/mcp-server/utils/http.d.ts +10 -0
- package/dist/mcp-server/utils/http.js +2 -0
- package/dist/mcp-server/utils/inject-tokens.d.ts +2 -0
- package/dist/mcp-server/utils/inject-tokens.js +5 -0
- package/dist/mcp-server/utils/zod.d.ts +1 -0
- package/dist/mcp-server/utils/zod.js +29 -0
- package/dist/mcp.controller.d.ts +8 -5
- package/dist/mcp.controller.js +55 -24
- package/dist/mcp.module.d.ts +4 -2
- package/dist/mcp.module.js +16 -2
- package/dist/mcp.service.d.ts +2 -2
- package/dist/mcp.service.js +4 -4
- package/dist/utils/context.decorator.d.ts +1 -0
- package/dist/utils/context.decorator.js +5 -0
- package/dist/utils/inject-tokens.d.ts +1 -0
- package/dist/utils/inject-tokens.js +4 -0
- package/package.json +6 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/Muzikanto/nestjs-mcp)
|
|
6
6
|
[](https://github.com/Muzikanto/nestjs-mcp/blob/main/LICENSE)
|
|
7
7
|
|
|
8
|
-
NestJS MCP (Model Context Protocol) module — allows you to create “tools”
|
|
8
|
+
NestJS MCP (Model Context Protocol) module — allows you to create “tools”, “prompts”, “resources” for LLM or HTTP, with automatic detection via decorators, validation, and integration with OpenAI Function Calls.
|
|
9
9
|
|
|
10
10
|
---
|
|
11
11
|
|
|
@@ -16,8 +16,12 @@ NestJS MCP (Model Context Protocol) module — allows you to create “tools”
|
|
|
16
16
|
- [Connecting the MCP module](#connecting-the-mcp-module)
|
|
17
17
|
- [Create MCP tool](#create-mcp-tool)
|
|
18
18
|
- [Create MCP prompt](#create-mcp-prompt)
|
|
19
|
+
- [Create MCP resource](#create-mcp-resource)
|
|
19
20
|
- [Calling MCP tools via HTTP](#calling-mcp-tools-via-http)
|
|
21
|
+
- [Calling MCP prompt via HTTP](#obtain-prompt)
|
|
20
22
|
- [Obtaining all tools](#obtaining-all-tools)
|
|
23
|
+
- [Obtaining all prompts](#obtaining-all-prompts)
|
|
24
|
+
- [Auth guard](#auth-guard)
|
|
21
25
|
- [Integration with OpenAI Function Calls](#integration-with-openai-function-calls)
|
|
22
26
|
|
|
23
27
|
## Features
|
|
@@ -26,10 +30,11 @@ NestJS MCP (Model Context Protocol) module — allows you to create “tools”
|
|
|
26
30
|
- Register MCP prompts using the `@McpPrompt()` decorator
|
|
27
31
|
- Automatic detection of all providers (tools) in the module
|
|
28
32
|
- Input data validation
|
|
29
|
-
-
|
|
33
|
+
- SSE endpoints for MCP (`GET /mcp/sse, POST /mcp/messages`)
|
|
34
|
+
- Endpoint for calling tool (`POST /mcp/toos`)
|
|
30
35
|
- Endpoint for a list of all tools (`GET /mcp/tools`)
|
|
36
|
+
- Endpoint for calling prompt (`POST /mcp/prompts/:name`)
|
|
31
37
|
- Endpoint for a list of all prompts (`GET /mcp/prompts`)
|
|
32
|
-
- Endpoint for prompt (`GET /mcp/prompts/:name`)
|
|
33
38
|
- Easy integration with LLM (OpenAI Function Calls)
|
|
34
39
|
- Full TypeScript typing
|
|
35
40
|
|
|
@@ -68,6 +73,12 @@ export class AppModule {}
|
|
|
68
73
|
```ts
|
|
69
74
|
import { IMcpTool, McpTool } from '@muzikanto/nestjs-mcp';
|
|
70
75
|
import { Telegraf } from 'telegraf';
|
|
76
|
+
import z from 'zod';
|
|
77
|
+
|
|
78
|
+
const schema = {
|
|
79
|
+
chatId: z.string().describe('Telegram chat id'), // строка с описанием
|
|
80
|
+
text: z.string().describe('Message text'), // строка с описанием
|
|
81
|
+
};
|
|
71
82
|
|
|
72
83
|
@McpTool()
|
|
73
84
|
export class TelegramSendMessageTool implements IMcpTool<
|
|
@@ -76,20 +87,7 @@ export class TelegramSendMessageTool implements IMcpTool<
|
|
|
76
87
|
> {
|
|
77
88
|
name = 'telegram.sendMessage';
|
|
78
89
|
|
|
79
|
-
inputSchema =
|
|
80
|
-
type: "object",
|
|
81
|
-
properties: {
|
|
82
|
-
chatId: {
|
|
83
|
-
type: "string",
|
|
84
|
-
description: "Telegram chat id"
|
|
85
|
-
},
|
|
86
|
-
text: {
|
|
87
|
-
type: "string",
|
|
88
|
-
description: "Message text"
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
required: ["chatId", "text"]
|
|
92
|
-
};
|
|
90
|
+
inputSchema = schema;
|
|
93
91
|
|
|
94
92
|
constructor(private readonly bot: Telegraf) {}
|
|
95
93
|
|
|
@@ -133,14 +131,21 @@ GET /mcp/tool
|
|
|
133
131
|
"name": "telegram.sendMessage",
|
|
134
132
|
"description": "Sent message via Telegram",
|
|
135
133
|
"inputSchema": {
|
|
136
|
-
"
|
|
137
|
-
|
|
138
|
-
|
|
134
|
+
"type": "object",
|
|
135
|
+
"properties": {
|
|
136
|
+
"chatId": {
|
|
137
|
+
"type": "number",
|
|
138
|
+
"description": "Telegram chat ID"
|
|
139
|
+
},
|
|
140
|
+
"text": {
|
|
141
|
+
"type": "string",
|
|
142
|
+
"description": "Message text"
|
|
143
|
+
}
|
|
139
144
|
},
|
|
140
|
-
"
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
"required": [
|
|
146
|
+
"chatId",
|
|
147
|
+
"text"
|
|
148
|
+
]
|
|
144
149
|
}
|
|
145
150
|
}
|
|
146
151
|
]
|
|
@@ -150,11 +155,18 @@ GET /mcp/tool
|
|
|
150
155
|
|
|
151
156
|
```ts
|
|
152
157
|
import { IMcpPrompt, McpPrompt } from '@muzikanto/nestjs-mcp';
|
|
158
|
+
import z from 'zod';
|
|
159
|
+
|
|
160
|
+
const schema = {
|
|
161
|
+
chatId: z.string().describe('Telegram chat id'), // строка с описанием
|
|
162
|
+
text: z.string().describe('Message text'), // строка с описанием
|
|
163
|
+
};
|
|
153
164
|
|
|
154
165
|
@McpPrompt()
|
|
155
166
|
export class TelegramAutoReplyPrompt implements IMcpPrompt<{ text: string; chatId: number; }> {
|
|
156
167
|
name = 'telegram_auto_reply';
|
|
157
168
|
description = 'Generate a short, fiendly reply to an incoming Telegram message and send it back to the same chat using teegram.sendMessage tool';
|
|
169
|
+
schema = schema;
|
|
158
170
|
|
|
159
171
|
async execute({ text, chatId }: { text: string; chatId: number }) {
|
|
160
172
|
return [
|
|
@@ -189,12 +201,29 @@ GET /mcp/prompts
|
|
|
189
201
|
[
|
|
190
202
|
{
|
|
191
203
|
"name": "telegram_auto_reply",
|
|
192
|
-
"description": "Generate a short, fiendly reply to an incoming Telegram message and send it back to the same chat using teegram.sendMessage tool"
|
|
204
|
+
"description": "Generate a short, fiendly reply to an incoming Telegram message and send it back to the same chat using teegram.sendMessage tool",
|
|
205
|
+
"inputSchema": {
|
|
206
|
+
"type": "object",
|
|
207
|
+
"properties": {
|
|
208
|
+
"chatId": {
|
|
209
|
+
"type": "number",
|
|
210
|
+
"description": "Telegram chat ID"
|
|
211
|
+
},
|
|
212
|
+
"text": {
|
|
213
|
+
"type": "string",
|
|
214
|
+
"description": "Message text"
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
"required": [
|
|
218
|
+
"chatId",
|
|
219
|
+
"text"
|
|
220
|
+
]
|
|
221
|
+
}
|
|
193
222
|
}
|
|
194
223
|
]
|
|
195
224
|
```
|
|
196
225
|
|
|
197
|
-
###
|
|
226
|
+
### Calling MCP prompt via HTTP
|
|
198
227
|
|
|
199
228
|
POST /mcp/prompts/telegram_auto_reply
|
|
200
229
|
|
|
@@ -231,13 +260,55 @@ Response
|
|
|
231
260
|
}
|
|
232
261
|
```
|
|
233
262
|
|
|
263
|
+
### Create MCP resource
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { IMcpResource, McpResource } from '@muzikanto/nestjs-mcp';
|
|
267
|
+
|
|
268
|
+
@McpResource()
|
|
269
|
+
export class TestResource implements IMcpResource<{ userId: string }> {
|
|
270
|
+
name = 'users.get';
|
|
271
|
+
uri = 'users://{userId}';
|
|
272
|
+
title = 'Get test user';
|
|
273
|
+
description = 'Get user by id';
|
|
274
|
+
|
|
275
|
+
async execute(url: URL, vars: { userId: string }) {
|
|
276
|
+
return [{ uri: url.href, text: `Hello ${vars.userId}` }];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Auth guard
|
|
282
|
+
```ts
|
|
283
|
+
import { McpModule } from '@muzikanto/nestjs-mcp';
|
|
284
|
+
import { Module, CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
|
|
285
|
+
|
|
286
|
+
@Injectable()
|
|
287
|
+
export class TestGuard implements CanActivate {
|
|
288
|
+
canActivate(context: ExecutionContext): Promise<boolean> {
|
|
289
|
+
const request = context.switchToHttp().getRequest();
|
|
290
|
+
const authHeader = request.headers['authorization'];
|
|
291
|
+
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
@Module({
|
|
297
|
+
imports: [
|
|
298
|
+
McpModule.forRoot({
|
|
299
|
+
guard: TestGuard
|
|
300
|
+
}),
|
|
301
|
+
],
|
|
302
|
+
})
|
|
303
|
+
export class AppModule {}
|
|
304
|
+
```
|
|
305
|
+
|
|
234
306
|
### Integration with OpenAI Function Calls
|
|
235
307
|
|
|
236
308
|
```ts
|
|
237
309
|
import axios from 'axios';
|
|
238
310
|
import OpenAI from 'openai';
|
|
239
311
|
|
|
240
|
-
const MCP_URL = 'http://localhost:3000/mcp';
|
|
241
312
|
const MCP_TOOLS_URL = 'http://localhost:3000/mcp/tools';
|
|
242
313
|
const MCP_TELEGRAM_PROMPT_URL = 'http://localhost:3000/mcp/prompts/telegram_auto_reply';
|
|
243
314
|
|
|
@@ -249,7 +320,7 @@ const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
|
249
320
|
*/
|
|
250
321
|
async function getMcpTools() {
|
|
251
322
|
const response = await axios.get(MCP_TOOLS_URL);
|
|
252
|
-
return response.data;
|
|
323
|
+
return response.data.tools.map(el => ({ name: el.name, description: el.description, parameters: el.inputSchema }));
|
|
253
324
|
}
|
|
254
325
|
|
|
255
326
|
/**
|
|
@@ -258,16 +329,14 @@ async function getMcpTools() {
|
|
|
258
329
|
async function getMcpPrompt() {
|
|
259
330
|
const response = await axios.post(MCP_TELEGRAM_PROMPT_URL, {/* propmpt generation arguments */});
|
|
260
331
|
return response.data;
|
|
261
|
-
}
|
|
262
332
|
|
|
263
333
|
/**
|
|
264
334
|
* Request mcp tool
|
|
265
335
|
*/
|
|
266
336
|
async function callMcpTool(toolName: string, payload: Record<string, any>) {
|
|
267
337
|
const response = await axios.post(
|
|
268
|
-
|
|
338
|
+
MCP_TOOLS_URL,
|
|
269
339
|
{ type: toolName, payload },
|
|
270
|
-
{ headers: { 'Content-Type': 'application/json' } }
|
|
271
340
|
);
|
|
272
341
|
return response.data;
|
|
273
342
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
+
export type IMcpToolContext = {
|
|
3
|
+
request: any;
|
|
4
|
+
};
|
|
2
5
|
export interface IMcpTool<Payload = any, Result = any> {
|
|
3
6
|
name: string;
|
|
4
7
|
description?: string;
|
|
5
8
|
inputSchema?: object | object[];
|
|
6
|
-
execute(input: Payload): Promise<Result>;
|
|
9
|
+
execute(input: Payload, context: IMcpToolContext): Promise<Result>;
|
|
7
10
|
}
|
|
8
11
|
export declare const MCP_TOOL_METADATA = "mcp:tool-class";
|
|
9
12
|
/**
|
|
@@ -17,10 +17,10 @@ class McpMessageDto {
|
|
|
17
17
|
}
|
|
18
18
|
exports.McpMessageDto = McpMessageDto;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, swagger_1.ApiProperty)({ type:
|
|
20
|
+
(0, swagger_1.ApiProperty)({ type: "string", description: "Tool name" }),
|
|
21
21
|
__metadata("design:type", String)
|
|
22
22
|
], McpMessageDto.prototype, "type", void 0);
|
|
23
23
|
__decorate([
|
|
24
|
-
(0, swagger_1.ApiProperty)({ description:
|
|
24
|
+
(0, swagger_1.ApiProperty)({ description: "Tool handler payload" }),
|
|
25
25
|
__metadata("design:type", Object)
|
|
26
26
|
], McpMessageDto.prototype, "payload", void 0);
|
|
@@ -17,10 +17,14 @@ class McpPromptDto {
|
|
|
17
17
|
}
|
|
18
18
|
exports.McpPromptDto = McpPromptDto;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, swagger_1.ApiProperty)({ type:
|
|
20
|
+
(0, swagger_1.ApiProperty)({ type: "string", description: "Propmt name" }),
|
|
21
21
|
__metadata("design:type", String)
|
|
22
22
|
], McpPromptDto.prototype, "name", void 0);
|
|
23
23
|
__decorate([
|
|
24
|
-
(0, swagger_1.ApiProperty)({
|
|
24
|
+
(0, swagger_1.ApiProperty)({
|
|
25
|
+
type: "string",
|
|
26
|
+
description: "Propmt description",
|
|
27
|
+
nullable: true,
|
|
28
|
+
}),
|
|
25
29
|
__metadata("design:type", String)
|
|
26
30
|
], McpPromptDto.prototype, "description", void 0);
|
|
@@ -18,14 +18,23 @@ class McpPromptMessageDto {
|
|
|
18
18
|
}
|
|
19
19
|
exports.McpPromptMessageDto = McpPromptMessageDto;
|
|
20
20
|
__decorate([
|
|
21
|
-
(0, swagger_1.ApiProperty)({ type:
|
|
21
|
+
(0, swagger_1.ApiProperty)({ type: "string", description: "AI role" }),
|
|
22
22
|
__metadata("design:type", String)
|
|
23
23
|
], McpPromptMessageDto.prototype, "role", void 0);
|
|
24
24
|
__decorate([
|
|
25
|
-
(0, swagger_1.ApiProperty)({
|
|
25
|
+
(0, swagger_1.ApiProperty)({
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "Request ai message",
|
|
28
|
+
nullable: true,
|
|
29
|
+
}),
|
|
26
30
|
__metadata("design:type", String)
|
|
27
31
|
], McpPromptMessageDto.prototype, "content", void 0);
|
|
28
32
|
__decorate([
|
|
29
|
-
(0, swagger_1.ApiProperty)({
|
|
33
|
+
(0, swagger_1.ApiProperty)({
|
|
34
|
+
type: "object",
|
|
35
|
+
description: "Tool call context",
|
|
36
|
+
nullable: true,
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
}),
|
|
30
39
|
__metadata("design:type", Object)
|
|
31
40
|
], McpPromptMessageDto.prototype, "tool_call", void 0);
|
|
@@ -17,6 +17,10 @@ class McpPromptMessagesDto {
|
|
|
17
17
|
}
|
|
18
18
|
exports.McpPromptMessagesDto = McpPromptMessagesDto;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, swagger_1.ApiProperty)({
|
|
20
|
+
(0, swagger_1.ApiProperty)({
|
|
21
|
+
type: McpPromptMessage_1.McpPromptMessageDto,
|
|
22
|
+
isArray: true,
|
|
23
|
+
description: "Messages list",
|
|
24
|
+
}),
|
|
21
25
|
__metadata("design:type", Array)
|
|
22
26
|
], McpPromptMessagesDto.prototype, "messages", void 0);
|
|
@@ -17,6 +17,10 @@ class McpPromptsDto {
|
|
|
17
17
|
}
|
|
18
18
|
exports.McpPromptsDto = McpPromptsDto;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, swagger_1.ApiProperty)({
|
|
20
|
+
(0, swagger_1.ApiProperty)({
|
|
21
|
+
type: McpPrompt_dto_1.McpPromptDto,
|
|
22
|
+
isArray: true,
|
|
23
|
+
description: "Prompts list",
|
|
24
|
+
}),
|
|
21
25
|
__metadata("design:type", Array)
|
|
22
26
|
], McpPromptsDto.prototype, "prompts", void 0);
|
package/dist/dto/McpTool.dto.js
CHANGED
|
@@ -18,14 +18,23 @@ class McpToolDto {
|
|
|
18
18
|
}
|
|
19
19
|
exports.McpToolDto = McpToolDto;
|
|
20
20
|
__decorate([
|
|
21
|
-
(0, swagger_1.ApiProperty)({ type:
|
|
21
|
+
(0, swagger_1.ApiProperty)({ type: "string", description: "Tool name" }),
|
|
22
22
|
__metadata("design:type", String)
|
|
23
23
|
], McpToolDto.prototype, "name", void 0);
|
|
24
24
|
__decorate([
|
|
25
|
-
(0, swagger_1.ApiProperty)({
|
|
25
|
+
(0, swagger_1.ApiProperty)({
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "Tool description",
|
|
28
|
+
nullable: true,
|
|
29
|
+
}),
|
|
26
30
|
__metadata("design:type", String)
|
|
27
31
|
], McpToolDto.prototype, "description", void 0);
|
|
28
32
|
__decorate([
|
|
29
|
-
(0, swagger_1.ApiProperty)({
|
|
33
|
+
(0, swagger_1.ApiProperty)({
|
|
34
|
+
type: "object",
|
|
35
|
+
description: "Tool validation schema",
|
|
36
|
+
nullable: true,
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
}),
|
|
30
39
|
__metadata("design:type", Object)
|
|
31
40
|
], McpToolDto.prototype, "parameters", void 0);
|
package/dist/dto/McpTools.dto.js
CHANGED
|
@@ -17,6 +17,6 @@ class McpToolsDto {
|
|
|
17
17
|
}
|
|
18
18
|
exports.McpToolsDto = McpToolsDto;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, swagger_1.ApiProperty)({ type: McpTool_dto_1.McpToolDto, isArray: true, description:
|
|
20
|
+
(0, swagger_1.ApiProperty)({ type: McpTool_dto_1.McpToolDto, isArray: true, description: "Tools list" }),
|
|
21
21
|
__metadata("design:type", Array)
|
|
22
22
|
], McpToolsDto.prototype, "tools", void 0);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export * from "./mcp.module";
|
|
2
|
-
export * from "./decorators/mcp-tool.decorator";
|
|
3
|
-
export * from "./decorators/mcp-prompt.decorator";
|
|
1
|
+
export * from "./mcp-server/mcp.module";
|
|
2
|
+
export * from "./mcp-server/decorators/mcp-tool.decorator";
|
|
3
|
+
export * from "./mcp-server/decorators/mcp-prompt.decorator";
|
|
4
|
+
export * from "./mcp-server/decorators/mcp-resource.decorator";
|
|
5
|
+
export * from "./mcp-client/mcp-client.module";
|
|
6
|
+
export * from "./mcp-client/mcp-client.service";
|
package/dist/index.js
CHANGED
|
@@ -14,6 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./mcp.module"), exports);
|
|
18
|
-
__exportStar(require("./decorators/mcp-tool.decorator"), exports);
|
|
19
|
-
__exportStar(require("./decorators/mcp-prompt.decorator"), exports);
|
|
17
|
+
__exportStar(require("./mcp-server/mcp.module"), exports);
|
|
18
|
+
__exportStar(require("./mcp-server/decorators/mcp-tool.decorator"), exports);
|
|
19
|
+
__exportStar(require("./mcp-server/decorators/mcp-prompt.decorator"), exports);
|
|
20
|
+
__exportStar(require("./mcp-server/decorators/mcp-resource.decorator"), exports);
|
|
21
|
+
__exportStar(require("./mcp-client/mcp-client.module"), exports);
|
|
22
|
+
__exportStar(require("./mcp-client/mcp-client.service"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Provider, Type } from "@nestjs/common";
|
|
2
|
+
export interface IMcpClientConfig {
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class McpClientModule {
|
|
6
|
+
static forRoot(metadata: {
|
|
7
|
+
useValue?: IMcpClientConfig;
|
|
8
|
+
useExisting?: Type<IMcpClientConfig>;
|
|
9
|
+
useFactory?: (...args: any) => IMcpClientConfig | Promise<IMcpClientConfig>;
|
|
10
|
+
}): {
|
|
11
|
+
module: typeof McpClientModule;
|
|
12
|
+
implements: import("@nestjs/common").DynamicModule[];
|
|
13
|
+
providers: Provider<IMcpClientConfig>[];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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 McpClientModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.McpClientModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const mcp_client_service_1 = require("./mcp-client.service");
|
|
13
|
+
const inject_tokens_1 = require("../mcp-server/utils/inject-tokens");
|
|
14
|
+
const axios_1 = require("@nestjs/axios");
|
|
15
|
+
let McpClientModule = McpClientModule_1 = class McpClientModule {
|
|
16
|
+
static forRoot(metadata) {
|
|
17
|
+
const configProvider = {
|
|
18
|
+
provide: inject_tokens_1.MCP_CLIENT_CONFIG,
|
|
19
|
+
...(metadata.useValue
|
|
20
|
+
? { useValue: metadata.useValue }
|
|
21
|
+
: metadata.useExisting
|
|
22
|
+
? { useExisting: metadata.useExisting }
|
|
23
|
+
: { useFactory: metadata.useFactory }),
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
module: McpClientModule_1,
|
|
27
|
+
implements: [
|
|
28
|
+
axios_1.HttpModule.registerAsync({
|
|
29
|
+
inject: [inject_tokens_1.MCP_CLIENT_CONFIG],
|
|
30
|
+
useFactory: (config) => {
|
|
31
|
+
return { baseUrl: config.url };
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
],
|
|
35
|
+
providers: [mcp_client_service_1.McpClientService, configProvider],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.McpClientModule = McpClientModule;
|
|
40
|
+
exports.McpClientModule = McpClientModule = McpClientModule_1 = __decorate([
|
|
41
|
+
(0, common_1.Module)({})
|
|
42
|
+
], McpClientModule);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HttpService } from "@nestjs/axios";
|
|
2
|
+
import { McpToolsDto } from "../mcp-server/dto/McpTools.dto";
|
|
3
|
+
import { McpPromptsDto } from "../mcp-server/dto/McpPrompts.dto";
|
|
4
|
+
import { McpPromptMessagesDto } from "../mcp-server/dto/McpPromptMessages.dto";
|
|
5
|
+
export declare class McpClientService {
|
|
6
|
+
private readonly httpService;
|
|
7
|
+
constructor(httpService: HttpService);
|
|
8
|
+
getAllTools(): Promise<McpToolsDto>;
|
|
9
|
+
getAllPrompts(): Promise<McpPromptsDto>;
|
|
10
|
+
getPromptByName<Payload = any>(promptName: string, params: Payload): Promise<McpPromptMessagesDto>;
|
|
11
|
+
callMcpTool<Payload = any, Result = any>(toolName: string, payload: Payload): Promise<Result>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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.McpClientService = void 0;
|
|
13
|
+
const axios_1 = require("@nestjs/axios");
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
let McpClientService = class McpClientService {
|
|
16
|
+
httpService;
|
|
17
|
+
constructor(httpService) {
|
|
18
|
+
this.httpService = httpService;
|
|
19
|
+
}
|
|
20
|
+
// Получить все инструменты
|
|
21
|
+
async getAllTools() {
|
|
22
|
+
const response = await this.httpService.get("/mcp/tools").toPromise();
|
|
23
|
+
return response.data;
|
|
24
|
+
}
|
|
25
|
+
// Получить все подсказки
|
|
26
|
+
async getAllPrompts() {
|
|
27
|
+
const response = await this.httpService.get("/mcp/prompts").toPromise();
|
|
28
|
+
return response.data;
|
|
29
|
+
}
|
|
30
|
+
// Получить подсказку по имени
|
|
31
|
+
async getPromptByName(promptName, params) {
|
|
32
|
+
const response = await this.httpService
|
|
33
|
+
.post(`/mcp/prompts/${promptName}`, params)
|
|
34
|
+
.toPromise();
|
|
35
|
+
return response.data;
|
|
36
|
+
}
|
|
37
|
+
// Вызвать инструмент MCP
|
|
38
|
+
async callMcpTool(toolName, payload) {
|
|
39
|
+
const response = await this.httpService
|
|
40
|
+
.post("/mcp/tools", { type: toolName, payload }, { headers: { "Content-Type": "application/json" } })
|
|
41
|
+
.toPromise();
|
|
42
|
+
return response.data;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.McpClientService = McpClientService;
|
|
46
|
+
exports.McpClientService = McpClientService = __decorate([
|
|
47
|
+
(0, common_1.Injectable)(),
|
|
48
|
+
__metadata("design:paramtypes", [axios_1.HttpService])
|
|
49
|
+
], McpClientService);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import type * as z3 from "zod/v3";
|
|
3
|
+
type AnySchema = z3.ZodTypeAny;
|
|
4
|
+
type ZodRawShapeCompat = Record<string, AnySchema>;
|
|
5
|
+
export type IMcpPromptMessage = {
|
|
6
|
+
role: "user" | "assistant" | "system";
|
|
7
|
+
content?: string;
|
|
8
|
+
tool_call?: {
|
|
9
|
+
name: string;
|
|
10
|
+
arguments: object;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export interface IMcpPrompt<Payload = any, Result extends IMcpPromptMessage[] = IMcpPromptMessage[]> {
|
|
14
|
+
name: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
inputSchema?: ZodRawShapeCompat;
|
|
18
|
+
execute(input: Payload): Promise<Result>;
|
|
19
|
+
}
|
|
20
|
+
export declare const MCP_PROMPT_METADATA = "mcp:prompt-class";
|
|
21
|
+
/**
|
|
22
|
+
* Декоратор класса для MCP prompt
|
|
23
|
+
*/
|
|
24
|
+
export declare const McpPrompt: () => (target: any) => void;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpPrompt = exports.MCP_PROMPT_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
exports.MCP_PROMPT_METADATA = "mcp:prompt-class";
|
|
7
|
+
/**
|
|
8
|
+
* Декоратор класса для MCP prompt
|
|
9
|
+
*/
|
|
10
|
+
const McpPrompt = () => {
|
|
11
|
+
return (target) => {
|
|
12
|
+
(0, common_1.Injectable)()(target);
|
|
13
|
+
Reflect.defineMetadata(exports.MCP_PROMPT_METADATA, {}, target);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.McpPrompt = McpPrompt;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol";
|
|
2
|
+
import { ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types";
|
|
3
|
+
import "reflect-metadata";
|
|
4
|
+
export type IMcpResourceResult = {
|
|
5
|
+
uri: string;
|
|
6
|
+
text: string;
|
|
7
|
+
};
|
|
8
|
+
export type IMcpResourceListItem = {
|
|
9
|
+
name: string;
|
|
10
|
+
uri: string;
|
|
11
|
+
title?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
};
|
|
14
|
+
export interface IMcpResource<Payload = any> {
|
|
15
|
+
name: string;
|
|
16
|
+
uri: string;
|
|
17
|
+
title?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
execute(url: URL, input: Payload): Promise<IMcpResourceResult[]>;
|
|
20
|
+
list?(extra: RequestHandlerExtra<ServerRequest, ServerNotification>): Promise<IMcpResourceListItem[]>;
|
|
21
|
+
}
|
|
22
|
+
export declare const MCP_RESOURCE_METADATA = "mcp:resource-class";
|
|
23
|
+
/**
|
|
24
|
+
* Декоратор класса для MCP resource
|
|
25
|
+
*/
|
|
26
|
+
export declare const McpResource: () => (target: any) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpResource = exports.MCP_RESOURCE_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
exports.MCP_RESOURCE_METADATA = "mcp:resource-class";
|
|
7
|
+
/**
|
|
8
|
+
* Декоратор класса для MCP resource
|
|
9
|
+
*/
|
|
10
|
+
const McpResource = () => {
|
|
11
|
+
return (target) => {
|
|
12
|
+
(0, common_1.Injectable)()(target);
|
|
13
|
+
Reflect.defineMetadata(exports.MCP_RESOURCE_METADATA, {}, target);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.McpResource = McpResource;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import type * as z3 from "zod/v3";
|
|
3
|
+
type AnySchema = z3.ZodTypeAny;
|
|
4
|
+
type ZodRawShapeCompat = Record<string, AnySchema>;
|
|
5
|
+
export type IMcpToolContext = {
|
|
6
|
+
request: any;
|
|
7
|
+
};
|
|
8
|
+
export interface IMcpTool<Payload = any, Result = any> {
|
|
9
|
+
name: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
inputSchema?: ZodRawShapeCompat;
|
|
13
|
+
execute(input: Payload, context: IMcpToolContext): Promise<Result>;
|
|
14
|
+
}
|
|
15
|
+
export declare const MCP_TOOL_METADATA = "mcp:tool-class";
|
|
16
|
+
/**
|
|
17
|
+
* Декоратор класса для MCP тулзы
|
|
18
|
+
*/
|
|
19
|
+
export declare const McpTool: () => (target: any) => void;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpTool = exports.MCP_TOOL_METADATA = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
exports.MCP_TOOL_METADATA = "mcp:tool-class";
|
|
7
|
+
/**
|
|
8
|
+
* Декоратор класса для MCP тулзы
|
|
9
|
+
*/
|
|
10
|
+
const McpTool = () => {
|
|
11
|
+
return (target) => {
|
|
12
|
+
(0, common_1.Injectable)()(target);
|
|
13
|
+
Reflect.defineMetadata(exports.MCP_TOOL_METADATA, {}, target);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.McpTool = McpTool;
|