@nocobase/ai 2.1.0-beta.2 → 2.1.0-beta.21
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 -661
- package/README.md +79 -10
- package/lib/ai-employee-manager/index.d.ts +29 -0
- package/lib/ai-employee-manager/index.js +167 -0
- package/lib/ai-employee-manager/types.d.ts +56 -0
- package/lib/ai-employee-manager/types.js +24 -0
- package/lib/ai-manager.d.ts +8 -0
- package/lib/ai-manager.js +12 -0
- package/lib/document-loader/index.d.ts +10 -0
- package/lib/document-loader/index.js +90 -0
- package/lib/document-loader/loader.worker.d.ts +9 -0
- package/lib/document-loader/loader.worker.js +83 -0
- package/lib/document-loader/vendor/langchain/document_loaders/fs/text.d.ts +20 -0
- package/lib/document-loader/vendor/langchain/document_loaders/fs/text.js +99 -0
- package/lib/document-loader/xlsx.d.ts +10 -0
- package/lib/document-loader/xlsx.js +100 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +11 -1
- package/lib/loader/employee.d.ts +37 -0
- package/lib/loader/employee.js +207 -0
- package/lib/loader/index.d.ts +3 -0
- package/lib/loader/index.js +7 -1
- package/lib/loader/mcp.d.ts +35 -0
- package/lib/loader/mcp.js +108 -0
- package/lib/loader/skills.d.ts +43 -0
- package/lib/loader/skills.js +138 -0
- package/lib/loader/tools.d.ts +1 -0
- package/lib/loader/tools.js +4 -3
- package/lib/mcp-manager/index.d.ts +43 -0
- package/lib/mcp-manager/index.js +341 -0
- package/lib/mcp-manager/types.d.ts +61 -0
- package/lib/mcp-manager/types.js +24 -0
- package/lib/mcp-tools-manager.d.ts +43 -0
- package/lib/mcp-tools-manager.js +77 -0
- package/lib/skills-manager/index.d.ts +29 -0
- package/lib/skills-manager/index.js +169 -0
- package/lib/skills-manager/types.d.ts +33 -0
- package/lib/skills-manager/types.js +24 -0
- package/lib/tools-manager/index.d.ts +2 -1
- package/lib/tools-manager/index.js +17 -7
- package/lib/tools-manager/types.d.ts +12 -4
- package/package.json +27 -7
- package/src/__tests__/ai-employees.test.ts +108 -0
- package/src/__tests__/mcp.test.ts +105 -0
- package/src/__tests__/resource/ai/ai-employees/index-employee/index.ts +16 -0
- package/src/__tests__/resource/ai/ai-employees/index-employee/prompt.md +1 -0
- package/src/__tests__/resource/ai/ai-employees/named-file-employee.ts +16 -0
- package/src/__tests__/resource/ai/ai-employees/with-skills/index.ts +16 -0
- package/src/__tests__/resource/ai/ai-employees/with-skills/skills/analysis/SKILLS.md +6 -0
- package/src/__tests__/resource/ai/ai-employees/with-skills-merge/index.ts +17 -0
- package/src/__tests__/resource/ai/ai-employees/with-skills-merge/skills/discovered-skill/SKILLS.md +6 -0
- package/src/__tests__/resource/ai/ai-employees/with-tools/index.ts +16 -0
- package/src/__tests__/resource/ai/ai-employees/with-tools/tools/discoveredTool.ts +23 -0
- package/src/__tests__/resource/ai/ai-employees/with-tools-merge/index.ts +16 -0
- package/src/__tests__/resource/ai/ai-employees/with-tools-merge/tools/discoveredTool.ts +23 -0
- package/src/__tests__/resource/ai/mcp/weather.ts +25 -0
- package/src/__tests__/resource/ai/skills/data-modeling/SKILLS.md +24 -0
- package/src/__tests__/resource/ai/skills/data-modeling/tools/read.ts +23 -0
- package/src/__tests__/resource/ai/skills/data-modeling/tools/search/description.md +1 -0
- package/src/__tests__/resource/ai/skills/data-modeling/tools/search/index.ts +23 -0
- package/src/__tests__/resource/ai/skills/document/tools/read.ts +1 -1
- package/src/__tests__/resource/ai/skills/document/tools/search/index.ts +1 -1
- package/src/__tests__/resource/ai/tools/desc/index.ts +1 -1
- package/src/__tests__/resource/ai/tools/group/group1.ts +1 -1
- package/src/__tests__/resource/ai/tools/group/group2.ts +1 -1
- package/src/__tests__/resource/ai/tools/group/group3/index.ts +1 -1
- package/src/__tests__/resource/ai/tools/hallow/index.ts +1 -1
- package/src/__tests__/resource/ai/tools/print.ts +1 -1
- package/src/__tests__/skills.test.ts +55 -0
- package/src/__tests__/tools.test.ts +5 -3
- package/src/ai-employee-manager/index.ts +148 -0
- package/src/ai-employee-manager/types.ts +63 -0
- package/src/ai-manager.ts +12 -0
- package/src/document-loader/index.ts +57 -0
- package/src/document-loader/loader.worker.ts +100 -0
- package/src/document-loader/vendor/langchain/document_loaders/fs/text.ts +72 -0
- package/src/document-loader/xlsx.ts +82 -0
- package/src/index.ts +5 -0
- package/src/loader/employee.ts +194 -0
- package/src/loader/index.ts +3 -0
- package/src/loader/mcp.ts +101 -0
- package/src/loader/skills.ts +129 -0
- package/src/loader/tools.ts +3 -2
- package/src/mcp-manager/index.ts +364 -0
- package/src/mcp-manager/types.ts +68 -0
- package/src/mcp-tools-manager.ts +90 -0
- package/src/skills-manager/index.ts +148 -0
- package/src/skills-manager/types.ts +38 -0
- package/src/tools-manager/index.ts +18 -7
- package/src/tools-manager/types.ts +13 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { DirectoryScanner, DirectoryScannerOptions, FileDescriptor } from './scanner';
|
|
10
|
+
import { AIManager } from '../ai-manager';
|
|
11
|
+
import { LoadAndRegister } from './types';
|
|
12
|
+
import { Logger } from '@nocobase/logger';
|
|
13
|
+
import { SkillsScope } from '../skills-manager';
|
|
14
|
+
export type SkillsLoaderOptions = {
|
|
15
|
+
pluginName: string;
|
|
16
|
+
scan: DirectoryScannerOptions;
|
|
17
|
+
log?: Logger;
|
|
18
|
+
};
|
|
19
|
+
export declare class SkillsLoader extends LoadAndRegister<SkillsLoaderOptions> {
|
|
20
|
+
protected readonly ai: AIManager;
|
|
21
|
+
protected readonly options: SkillsLoaderOptions;
|
|
22
|
+
protected readonly scanner: DirectoryScanner;
|
|
23
|
+
protected files: FileDescriptor[];
|
|
24
|
+
protected skillsDescriptors: SkillsDescriptor[];
|
|
25
|
+
protected log: Logger;
|
|
26
|
+
constructor(ai: AIManager, options: SkillsLoaderOptions);
|
|
27
|
+
protected scan(): Promise<void>;
|
|
28
|
+
protected import(): Promise<void>;
|
|
29
|
+
protected register(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export type SkillsDescriptor = {
|
|
32
|
+
scope: SkillsScope;
|
|
33
|
+
name: string;
|
|
34
|
+
description: string;
|
|
35
|
+
content: string;
|
|
36
|
+
skillsFile: FileDescriptor;
|
|
37
|
+
skillsDir: FileDescriptor;
|
|
38
|
+
tools?: string[];
|
|
39
|
+
introduction?: {
|
|
40
|
+
title: string;
|
|
41
|
+
about?: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var skills_exports = {};
|
|
39
|
+
__export(skills_exports, {
|
|
40
|
+
SkillsLoader: () => SkillsLoader
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(skills_exports);
|
|
43
|
+
var import_scanner = require("./scanner");
|
|
44
|
+
var import_promises = require("fs/promises");
|
|
45
|
+
var import_fs = require("fs");
|
|
46
|
+
var import_types = require("./types");
|
|
47
|
+
var import_gray_matter = __toESM(require("gray-matter"));
|
|
48
|
+
var import_path = __toESM(require("path"));
|
|
49
|
+
const _SkillsLoader = class _SkillsLoader extends import_types.LoadAndRegister {
|
|
50
|
+
constructor(ai, options) {
|
|
51
|
+
super(ai, options);
|
|
52
|
+
this.ai = ai;
|
|
53
|
+
this.options = options;
|
|
54
|
+
this.log = options.log;
|
|
55
|
+
this.scanner = new import_scanner.DirectoryScanner(this.options.scan);
|
|
56
|
+
}
|
|
57
|
+
scanner;
|
|
58
|
+
files = [];
|
|
59
|
+
skillsDescriptors = [];
|
|
60
|
+
log;
|
|
61
|
+
async scan() {
|
|
62
|
+
this.files = await this.scanner.scan();
|
|
63
|
+
}
|
|
64
|
+
async import() {
|
|
65
|
+
if (!this.files.length) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.skillsDescriptors = await Promise.all(
|
|
69
|
+
this.files.map(async (skillsFile) => {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
if (skillsFile.basename !== "SKILLS.md") {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (!(0, import_fs.existsSync)(skillsFile.path)) {
|
|
75
|
+
(_a = this.log) == null ? void 0 : _a.error(`skills [${skillsFile.directory}] ignored: can not find SKILLS.md at ${skillsFile.path}`);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const skillsDir = new import_scanner.FileDescriptor(import_path.default.dirname(skillsFile.path));
|
|
79
|
+
const name = skillsFile.directory;
|
|
80
|
+
const entry = { name, skillsFile, skillsDir };
|
|
81
|
+
try {
|
|
82
|
+
const skills = await (0, import_promises.readFile)(skillsFile.path, "utf-8");
|
|
83
|
+
const { data, content } = (0, import_gray_matter.default)(skills);
|
|
84
|
+
entry.scope = data["scope"] ?? "SPECIFIED";
|
|
85
|
+
entry.name = data["name"];
|
|
86
|
+
entry.description = data["description"];
|
|
87
|
+
entry.content = content;
|
|
88
|
+
entry.introduction = data["introduction"];
|
|
89
|
+
entry.tools = data["tools"] ?? [];
|
|
90
|
+
} catch (e) {
|
|
91
|
+
(_b = this.log) == null ? void 0 : _b.error(`skills [${name}] load fail: error occur when reading SKILLS.md at ${skillsFile.path}`, e);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const toolsScanner = new import_scanner.DirectoryScanner({
|
|
96
|
+
basePath: skillsDir.path,
|
|
97
|
+
pattern: ["tools/**/*.ts", "tools/**/*.js", "!tools/**/*.d.ts"]
|
|
98
|
+
});
|
|
99
|
+
const toolsFiles = await toolsScanner.scan();
|
|
100
|
+
entry.tools = Array.from(
|
|
101
|
+
/* @__PURE__ */ new Set([
|
|
102
|
+
...entry.tools,
|
|
103
|
+
...toolsFiles.map(
|
|
104
|
+
(it) => it.basename === "index.ts" || it.basename === "index.js" ? it.directory : it.name
|
|
105
|
+
)
|
|
106
|
+
])
|
|
107
|
+
);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
(_c = this.log) == null ? void 0 : _c.error(`skills [${name}] load fail: error occur when loading tools at ${skillsDir.path}`, e);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return entry;
|
|
113
|
+
}).filter((it) => it != null)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
async register() {
|
|
117
|
+
if (!this.skillsDescriptors.length) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const { skillsManager } = this.ai;
|
|
121
|
+
for (const descriptor of this.skillsDescriptors) {
|
|
122
|
+
await skillsManager.registerSkills({
|
|
123
|
+
scope: descriptor.scope,
|
|
124
|
+
name: descriptor.name,
|
|
125
|
+
description: descriptor.description,
|
|
126
|
+
content: descriptor.content,
|
|
127
|
+
tools: descriptor.tools,
|
|
128
|
+
introduction: descriptor.introduction
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
__name(_SkillsLoader, "SkillsLoader");
|
|
134
|
+
let SkillsLoader = _SkillsLoader;
|
|
135
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
136
|
+
0 && (module.exports = {
|
|
137
|
+
SkillsLoader
|
|
138
|
+
});
|
package/lib/loader/tools.d.ts
CHANGED
package/lib/loader/tools.js
CHANGED
|
@@ -111,7 +111,7 @@ const _ToolsLoader = class _ToolsLoader extends import_types.LoadAndRegister {
|
|
|
111
111
|
)).filter((t) => t != null);
|
|
112
112
|
}
|
|
113
113
|
async register() {
|
|
114
|
-
var _a, _b, _c;
|
|
114
|
+
var _a, _b, _c, _d;
|
|
115
115
|
if (!this.toolsDescriptors.length) {
|
|
116
116
|
return;
|
|
117
117
|
}
|
|
@@ -124,7 +124,7 @@ const _ToolsLoader = class _ToolsLoader extends import_types.LoadAndRegister {
|
|
|
124
124
|
continue;
|
|
125
125
|
}
|
|
126
126
|
const { name, toolsOptions, description } = descriptor;
|
|
127
|
-
if (
|
|
127
|
+
if (toolsManager.isToolsExisted(name)) {
|
|
128
128
|
(_b = this.log) == null ? void 0 : _b.warn(`tools [${descriptor.name}] register ignored: duplicate register for tools`);
|
|
129
129
|
continue;
|
|
130
130
|
}
|
|
@@ -136,8 +136,9 @@ const _ToolsLoader = class _ToolsLoader extends import_types.LoadAndRegister {
|
|
|
136
136
|
}
|
|
137
137
|
try {
|
|
138
138
|
toolsManager.registerTools(toolsOptions);
|
|
139
|
+
(_c = this.log) == null ? void 0 : _c.info(`tools [${toolsOptions.definition.name}] registered from plugin [${this.options.pluginName}]`);
|
|
139
140
|
} catch (e) {
|
|
140
|
-
(
|
|
141
|
+
(_d = this.log) == null ? void 0 : _d.error(`tools [${descriptor.name}] register ignored: error occur when invoke registerTools`, e);
|
|
141
142
|
continue;
|
|
142
143
|
}
|
|
143
144
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { MultiServerMCPClient } from '@langchain/mcp-adapters';
|
|
10
|
+
import { MCPEntry, MCPFilter, MCPManager, MCPOptions, MCPTestResult, MCPToolEntry } from './types';
|
|
11
|
+
import type { DynamicToolsProvider, Permission } from '../tools-manager/types';
|
|
12
|
+
export declare class DefaultMCPManager implements MCPManager {
|
|
13
|
+
private readonly app;
|
|
14
|
+
private readonly mcpRegistry;
|
|
15
|
+
private readonly provideCollectionManager;
|
|
16
|
+
private mode;
|
|
17
|
+
private client;
|
|
18
|
+
private toolsMap;
|
|
19
|
+
private toolsPermissionMap;
|
|
20
|
+
constructor(app: any);
|
|
21
|
+
init(): Promise<void>;
|
|
22
|
+
registerMCP(registration: {
|
|
23
|
+
[key: string | symbol]: MCPOptions;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
getMCP(name: string): Promise<MCPEntry>;
|
|
26
|
+
listMCP(filter?: MCPFilter): Promise<MCPEntry[]>;
|
|
27
|
+
rebuildClient(): Promise<void>;
|
|
28
|
+
getClient(): MultiServerMCPClient | null;
|
|
29
|
+
getMCPToolsProvider(): DynamicToolsProvider;
|
|
30
|
+
listMCPTools(): Promise<Record<string, MCPToolEntry[]>>;
|
|
31
|
+
updateMCPToolPermission(toolName: string, permission: Permission): Promise<void>;
|
|
32
|
+
testConnection(options: MCPOptions): Promise<MCPTestResult>;
|
|
33
|
+
private buildMCPConnection;
|
|
34
|
+
persistence(): Promise<void>;
|
|
35
|
+
private persistenceEntry;
|
|
36
|
+
private normalizeEntry;
|
|
37
|
+
private get aiMcpClientsCollection();
|
|
38
|
+
private get aiMcpClientsModel();
|
|
39
|
+
private get sequelize();
|
|
40
|
+
private get collectionManager();
|
|
41
|
+
}
|
|
42
|
+
export declare function defineMCP(options: MCPOptions): MCPOptions;
|
|
43
|
+
export * from './types';
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var mcp_manager_exports = {};
|
|
30
|
+
__export(mcp_manager_exports, {
|
|
31
|
+
DefaultMCPManager: () => DefaultMCPManager,
|
|
32
|
+
defineMCP: () => defineMCP
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(mcp_manager_exports);
|
|
35
|
+
var import_database = require("@nocobase/database");
|
|
36
|
+
var import_utils = require("@nocobase/utils");
|
|
37
|
+
var import_mcp_adapters = require("@langchain/mcp-adapters");
|
|
38
|
+
__reExport(mcp_manager_exports, require("./types"), module.exports);
|
|
39
|
+
const _DefaultMCPManager = class _DefaultMCPManager {
|
|
40
|
+
constructor(app) {
|
|
41
|
+
this.app = app;
|
|
42
|
+
this.provideCollectionManager = () => app.mainDataSource;
|
|
43
|
+
}
|
|
44
|
+
mcpRegistry = new import_utils.Registry();
|
|
45
|
+
provideCollectionManager;
|
|
46
|
+
mode = "memory";
|
|
47
|
+
client = null;
|
|
48
|
+
toolsMap = {};
|
|
49
|
+
toolsPermissionMap = {};
|
|
50
|
+
async init() {
|
|
51
|
+
if (this.mode === "memory") {
|
|
52
|
+
await this.persistence();
|
|
53
|
+
this.mode = "database";
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
await this.rebuildClient();
|
|
57
|
+
} catch (e) {
|
|
58
|
+
this.app.log.error("fail to init mcp clients", e);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async registerMCP(registration) {
|
|
62
|
+
if (this.mode === "memory") {
|
|
63
|
+
for (const [name, options] of Object.entries(registration)) {
|
|
64
|
+
this.mcpRegistry.register(name, this.normalizeEntry(name, options));
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
for (const [name, options] of Object.entries(registration)) {
|
|
68
|
+
await this.persistenceEntry({
|
|
69
|
+
name,
|
|
70
|
+
...this.normalizeEntry(name, options)
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async getMCP(name) {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
return (_b = await ((_a = this.aiMcpClientsModel) == null ? void 0 : _a.findOne({ where: { name } }))) == null ? void 0 : _b.toJSON();
|
|
78
|
+
}
|
|
79
|
+
async listMCP(filter = {}) {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
const where = {};
|
|
82
|
+
if (filter.name) {
|
|
83
|
+
where["name"] = {
|
|
84
|
+
[import_database.Op.substring]: filter.name
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (filter.enabled != null) {
|
|
88
|
+
where["enabled"] = filter.enabled;
|
|
89
|
+
}
|
|
90
|
+
if (filter.transport) {
|
|
91
|
+
where["transport"] = filter.transport;
|
|
92
|
+
}
|
|
93
|
+
return ((_b = await ((_a = this.aiMcpClientsModel) == null ? void 0 : _a.findAll({ where }))) == null ? void 0 : _b.map((item) => item.toJSON())) ?? [];
|
|
94
|
+
}
|
|
95
|
+
async rebuildClient() {
|
|
96
|
+
if (this.client) {
|
|
97
|
+
try {
|
|
98
|
+
await this.client.close();
|
|
99
|
+
} catch (e) {
|
|
100
|
+
}
|
|
101
|
+
this.client = null;
|
|
102
|
+
this.toolsMap = {};
|
|
103
|
+
}
|
|
104
|
+
const entries = await this.listMCP({ enabled: true });
|
|
105
|
+
if (entries.length === 0) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const connections = {};
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
connections[entry.name] = this.buildMCPConnection(entry);
|
|
111
|
+
}
|
|
112
|
+
this.client = new import_mcp_adapters.MultiServerMCPClient(connections);
|
|
113
|
+
const toolsMap = await this.client.initializeConnections();
|
|
114
|
+
for (const [serverName, tools] of Object.entries(toolsMap)) {
|
|
115
|
+
this.toolsMap[serverName] = tools;
|
|
116
|
+
for (const tool of tools) {
|
|
117
|
+
const toolName = `mcp-${serverName}-${tool.name}`;
|
|
118
|
+
if (!(toolName in this.toolsPermissionMap)) {
|
|
119
|
+
this.toolsPermissionMap[toolName] = tool.name.startsWith("get") ? "ALLOW" : "ASK";
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
getClient() {
|
|
125
|
+
return this.client;
|
|
126
|
+
}
|
|
127
|
+
getMCPToolsProvider() {
|
|
128
|
+
return async (register) => {
|
|
129
|
+
for (const [serverName, tools] of Object.entries(this.toolsMap)) {
|
|
130
|
+
for (const tool of tools) {
|
|
131
|
+
const toolName = `mcp-${serverName}-${tool.name}`;
|
|
132
|
+
const toolOptions = {
|
|
133
|
+
scope: "GENERAL",
|
|
134
|
+
from: "mcp",
|
|
135
|
+
defaultPermission: this.toolsPermissionMap[toolName],
|
|
136
|
+
introduction: {
|
|
137
|
+
title: tool.name,
|
|
138
|
+
about: tool.description
|
|
139
|
+
},
|
|
140
|
+
definition: {
|
|
141
|
+
name: toolName,
|
|
142
|
+
description: tool.description || `MCP tool: ${tool.name} from ${serverName}`,
|
|
143
|
+
schema: tool.schema
|
|
144
|
+
},
|
|
145
|
+
invoke: /* @__PURE__ */ __name(async (_ctx, args) => {
|
|
146
|
+
try {
|
|
147
|
+
const result = await tool.invoke(args);
|
|
148
|
+
return result;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
return {
|
|
151
|
+
status: "error",
|
|
152
|
+
content: (error == null ? void 0 : error.message) || "Tool invocation failed"
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}, "invoke")
|
|
156
|
+
};
|
|
157
|
+
register.registerTools(toolOptions);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
async listMCPTools() {
|
|
163
|
+
return Object.fromEntries(
|
|
164
|
+
Object.entries(this.toolsMap).map(([serverName, tools]) => [
|
|
165
|
+
serverName,
|
|
166
|
+
tools.map((tool) => {
|
|
167
|
+
const toolName = `mcp-${serverName}-${tool.name}`;
|
|
168
|
+
return {
|
|
169
|
+
name: toolName,
|
|
170
|
+
title: tool.name,
|
|
171
|
+
description: tool.description,
|
|
172
|
+
serverName,
|
|
173
|
+
permission: this.toolsPermissionMap[toolName] ?? "ASK"
|
|
174
|
+
};
|
|
175
|
+
})
|
|
176
|
+
])
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
async updateMCPToolPermission(toolName, permission) {
|
|
180
|
+
this.toolsPermissionMap[toolName] = permission;
|
|
181
|
+
}
|
|
182
|
+
async testConnection(options) {
|
|
183
|
+
const { transport } = options;
|
|
184
|
+
if (!transport) {
|
|
185
|
+
return {
|
|
186
|
+
success: false,
|
|
187
|
+
error: "Transport type is required"
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
if (transport === "stdio" && !options.command) {
|
|
191
|
+
return {
|
|
192
|
+
success: false,
|
|
193
|
+
error: "Command is required for stdio transport"
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if ((transport === "http" || transport === "sse") && !options.url) {
|
|
197
|
+
return {
|
|
198
|
+
success: false,
|
|
199
|
+
error: "URL is required for HTTP/SSE transport"
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
let client = null;
|
|
203
|
+
try {
|
|
204
|
+
const connection = this.buildMCPConnection(options);
|
|
205
|
+
const serverName = "test-server";
|
|
206
|
+
client = new import_mcp_adapters.MultiServerMCPClient({
|
|
207
|
+
[serverName]: connection
|
|
208
|
+
});
|
|
209
|
+
const toolsMap = await Promise.race([
|
|
210
|
+
client.initializeConnections(),
|
|
211
|
+
new Promise(
|
|
212
|
+
(_resolve, reject) => setTimeout(() => reject(new Error("Connection timeout (60s)")), 6e4)
|
|
213
|
+
)
|
|
214
|
+
]);
|
|
215
|
+
const tools = toolsMap[serverName] || [];
|
|
216
|
+
const toolNames = tools.map((tool) => tool.name);
|
|
217
|
+
return {
|
|
218
|
+
success: true,
|
|
219
|
+
message: "Connection successful",
|
|
220
|
+
toolsCount: tools.length,
|
|
221
|
+
tools: toolNames.slice(0, 20),
|
|
222
|
+
// Limit to 20 tools for display
|
|
223
|
+
toolsTruncated: toolNames.length > 20
|
|
224
|
+
};
|
|
225
|
+
} catch (error) {
|
|
226
|
+
const errorMessage = (error == null ? void 0 : error.message) || "Failed to connect to MCP server";
|
|
227
|
+
let hint;
|
|
228
|
+
if (errorMessage.includes("EACCES") || errorMessage.includes("permission denied")) {
|
|
229
|
+
hint = "Try running: npm cache clean --force";
|
|
230
|
+
} else if (errorMessage.includes("ENOENT") || errorMessage.includes("not found")) {
|
|
231
|
+
hint = "Make sure the command exists and is accessible";
|
|
232
|
+
} else if (errorMessage.includes("timeout")) {
|
|
233
|
+
hint = "The server took too long to respond. Check if the server is running correctly.";
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
success: false,
|
|
237
|
+
error: errorMessage,
|
|
238
|
+
details: hint ? `Hint: ${hint}
|
|
239
|
+
|
|
240
|
+
${(error == null ? void 0 : error.stack) || ""}` : (error == null ? void 0 : error.stack) || ""
|
|
241
|
+
};
|
|
242
|
+
} finally {
|
|
243
|
+
if (client) {
|
|
244
|
+
try {
|
|
245
|
+
await client.close();
|
|
246
|
+
} catch (e) {
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
buildMCPConnection(options) {
|
|
252
|
+
const { transport, command, args, env, url, headers, restart } = options;
|
|
253
|
+
if (transport === "stdio") {
|
|
254
|
+
const connection2 = {
|
|
255
|
+
transport: "stdio",
|
|
256
|
+
command: command || "",
|
|
257
|
+
args: args || []
|
|
258
|
+
};
|
|
259
|
+
if (env && Object.keys(env).length > 0) {
|
|
260
|
+
connection2.env = env;
|
|
261
|
+
}
|
|
262
|
+
if (restart && typeof restart === "object" && !Array.isArray(restart)) {
|
|
263
|
+
connection2.restart = restart;
|
|
264
|
+
}
|
|
265
|
+
return connection2;
|
|
266
|
+
}
|
|
267
|
+
const connection = {
|
|
268
|
+
transport: transport === "sse" ? "sse" : "http",
|
|
269
|
+
url: url || ""
|
|
270
|
+
};
|
|
271
|
+
if (headers && Object.keys(headers).length > 0) {
|
|
272
|
+
connection.headers = headers;
|
|
273
|
+
}
|
|
274
|
+
return connection;
|
|
275
|
+
}
|
|
276
|
+
async persistence() {
|
|
277
|
+
for (const entry of this.mcpRegistry.getValues()) {
|
|
278
|
+
await this.persistenceEntry(entry);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
async persistenceEntry(entry) {
|
|
282
|
+
await this.sequelize.transaction(async (transaction) => {
|
|
283
|
+
const existed = await this.aiMcpClientsModel.findOne({ where: { name: entry.name }, transaction });
|
|
284
|
+
if (existed) {
|
|
285
|
+
await existed.update(
|
|
286
|
+
{
|
|
287
|
+
transport: entry.transport,
|
|
288
|
+
command: entry.command,
|
|
289
|
+
args: entry.args,
|
|
290
|
+
env: entry.env,
|
|
291
|
+
url: entry.url,
|
|
292
|
+
headers: entry.headers,
|
|
293
|
+
restart: entry.restart
|
|
294
|
+
},
|
|
295
|
+
{ transaction }
|
|
296
|
+
);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
await this.aiMcpClientsModel.create(
|
|
300
|
+
{
|
|
301
|
+
...entry
|
|
302
|
+
},
|
|
303
|
+
{ transaction }
|
|
304
|
+
);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
normalizeEntry(name, options) {
|
|
308
|
+
return {
|
|
309
|
+
name,
|
|
310
|
+
enabled: true,
|
|
311
|
+
...options,
|
|
312
|
+
args: options.args ?? [],
|
|
313
|
+
env: options.env ?? {}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
get aiMcpClientsCollection() {
|
|
317
|
+
return this.collectionManager.getCollection("aiMcpClients");
|
|
318
|
+
}
|
|
319
|
+
get aiMcpClientsModel() {
|
|
320
|
+
var _a;
|
|
321
|
+
return (_a = this.aiMcpClientsCollection) == null ? void 0 : _a.model;
|
|
322
|
+
}
|
|
323
|
+
get sequelize() {
|
|
324
|
+
return this.collectionManager.db.sequelize;
|
|
325
|
+
}
|
|
326
|
+
get collectionManager() {
|
|
327
|
+
return this.provideCollectionManager().collectionManager;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
__name(_DefaultMCPManager, "DefaultMCPManager");
|
|
331
|
+
let DefaultMCPManager = _DefaultMCPManager;
|
|
332
|
+
function defineMCP(options) {
|
|
333
|
+
return options;
|
|
334
|
+
}
|
|
335
|
+
__name(defineMCP, "defineMCP");
|
|
336
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
337
|
+
0 && (module.exports = {
|
|
338
|
+
DefaultMCPManager,
|
|
339
|
+
defineMCP,
|
|
340
|
+
...require("./types")
|
|
341
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import type { MultiServerMCPClient } from '@langchain/mcp-adapters';
|
|
10
|
+
import type { DynamicToolsProvider, Permission } from '../tools-manager/types';
|
|
11
|
+
export interface MCPManager extends MCPRegistration {
|
|
12
|
+
init(): Promise<void>;
|
|
13
|
+
getMCP(name: string): Promise<MCPEntry>;
|
|
14
|
+
listMCP(filter: MCPFilter): Promise<MCPEntry[]>;
|
|
15
|
+
testConnection(options: MCPOptions): Promise<MCPTestResult>;
|
|
16
|
+
rebuildClient(): Promise<void>;
|
|
17
|
+
getClient(): MultiServerMCPClient | null;
|
|
18
|
+
getMCPToolsProvider(): DynamicToolsProvider;
|
|
19
|
+
listMCPTools(): Promise<Record<string, MCPToolEntry[]>>;
|
|
20
|
+
updateMCPToolPermission(toolName: string, permission: Permission): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export interface MCPRegistration {
|
|
23
|
+
registerMCP(registration: {
|
|
24
|
+
[key: string | symbol]: MCPOptions;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export type MCPOptions = {
|
|
28
|
+
transport: MCPTransport;
|
|
29
|
+
command?: string;
|
|
30
|
+
args?: string[];
|
|
31
|
+
env?: Record<string, string>;
|
|
32
|
+
url?: string;
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
restart?: Record<string, any>;
|
|
35
|
+
};
|
|
36
|
+
export type MCPEntry = MCPOptions & {
|
|
37
|
+
name: string;
|
|
38
|
+
enabled: boolean;
|
|
39
|
+
};
|
|
40
|
+
export type MCPFilter = {
|
|
41
|
+
name?: string;
|
|
42
|
+
enabled?: boolean;
|
|
43
|
+
transport?: MCPTransport;
|
|
44
|
+
};
|
|
45
|
+
export type MCPTransport = 'stdio' | 'sse' | 'http';
|
|
46
|
+
export type MCPTestResult = {
|
|
47
|
+
success: boolean;
|
|
48
|
+
message?: string;
|
|
49
|
+
error?: string;
|
|
50
|
+
details?: string;
|
|
51
|
+
toolsCount?: number;
|
|
52
|
+
tools?: string[];
|
|
53
|
+
toolsTruncated?: boolean;
|
|
54
|
+
};
|
|
55
|
+
export type MCPToolEntry = {
|
|
56
|
+
name: string;
|
|
57
|
+
title: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
serverName: string;
|
|
60
|
+
permission: Permission;
|
|
61
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var types_exports = {};
|
|
24
|
+
module.exports = __toCommonJS(types_exports);
|