@opentiny/next-sdk 0.1.8 → 0.1.10
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/WebMcpClient.ts +9 -37
- package/agent/AgentModelProvider.ts +56 -43
- package/agent/type.ts +2 -2
- package/agent/utils/getAISDKTools.ts +2 -2
- package/dist/WebMcpClient.d.ts +1 -4
- package/dist/WebMcpClient.js +9 -33
- package/dist/agent/AgentModelProvider.d.ts +21 -16
- package/dist/agent/AgentModelProvider.js +72 -51
- package/dist/agent/type.d.ts +2 -2
- package/dist/agent/utils/getAISDKTools.d.ts +2 -2
- package/dist/agent/utils/getAISDKTools.js +2 -2
- package/dist/index.es.dev.js +56 -57
- package/dist/index.es.js +4076 -4070
- package/dist/index.umd.dev.js +56 -57
- package/dist/index.umd.js +27 -27
- package/dist/remoter/createRemoter.js +1 -1
- package/dist/webagent.dev.js +48 -28
- package/dist/webagent.es.dev.js +48 -28
- package/dist/webagent.es.js +1617 -1602
- package/dist/webagent.js +21 -21
- package/dist/webmcp-full.dev.js +930 -987
- package/dist/webmcp-full.es.dev.js +930 -987
- package/dist/webmcp-full.es.js +1388 -1397
- package/dist/webmcp-full.js +7 -7
- package/dist/webmcp.dev.js +8 -29
- package/dist/webmcp.es.dev.js +8 -29
- package/dist/webmcp.es.js +432 -441
- package/dist/webmcp.js +1 -1
- package/package.json +1 -1
- package/remoter/createRemoter.ts +1 -1
- package/runtime.html +21 -80
|
@@ -27,26 +27,24 @@ export const AIProviderFactories = {
|
|
|
27
27
|
['openai']: createOpenAI,
|
|
28
28
|
['deepseek']: createDeepSeek
|
|
29
29
|
};
|
|
30
|
-
/** 一个通用的ai-sdk的
|
|
30
|
+
/** 一个通用的ai-sdk的Agent封装
|
|
31
31
|
* @summary 内部自动管理了 llm, mcpServer, ai-sdk的clients 和 tools
|
|
32
32
|
* @returns 暴露了 chat, chatStream方法
|
|
33
33
|
*/
|
|
34
34
|
export class AgentModelProvider {
|
|
35
35
|
constructor({ llmConfig, mcpServers, llm }) {
|
|
36
|
-
/** mcpServers
|
|
36
|
+
/** 当前mcpServers对象集合。键为服务器名称,值为 McpServerConfig 或任意的 MCPTransport
|
|
37
37
|
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client */
|
|
38
|
-
this.mcpServers =
|
|
39
|
-
/**
|
|
38
|
+
this.mcpServers = {};
|
|
39
|
+
/** 当前ai-sdk的 mcpClient 数组 */
|
|
40
40
|
this.mcpClients = [];
|
|
41
|
-
/**
|
|
41
|
+
/** 当前 mcpClients 所对应的tools */
|
|
42
42
|
this.mcpTools = [];
|
|
43
|
-
/**
|
|
43
|
+
/** 需要实时过滤掉的tools name*/
|
|
44
44
|
this.ignoreToolnames = [];
|
|
45
|
-
/** 缓存 ai-sdk response 中的
|
|
45
|
+
/** 缓存 ai-sdk response 中的 多轮会话的上下文 */
|
|
46
46
|
this.messages = [];
|
|
47
|
-
|
|
48
|
-
this.mcpServers = mcpServers || [];
|
|
49
|
-
// 2、保存 llm
|
|
47
|
+
this.mcpServers = mcpServers || {};
|
|
50
48
|
if (llm) {
|
|
51
49
|
this.llm = llm;
|
|
52
50
|
}
|
|
@@ -67,7 +65,7 @@ export class AgentModelProvider {
|
|
|
67
65
|
throw new Error('Either llmConfig or llm must be provided');
|
|
68
66
|
}
|
|
69
67
|
}
|
|
70
|
-
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回
|
|
68
|
+
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
|
|
71
69
|
_createOneClient(serverConfig) {
|
|
72
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
71
|
try {
|
|
@@ -79,7 +77,10 @@ export class AgentModelProvider {
|
|
|
79
77
|
else {
|
|
80
78
|
transport = serverConfig;
|
|
81
79
|
}
|
|
82
|
-
|
|
80
|
+
const client = yield createMCPClient({ transport: transport });
|
|
81
|
+
//@ts-ignore
|
|
82
|
+
client['__transport__'] = transport;
|
|
83
|
+
return client;
|
|
83
84
|
}
|
|
84
85
|
catch (error) {
|
|
85
86
|
if (this.onError) {
|
|
@@ -90,16 +91,28 @@ export class AgentModelProvider {
|
|
|
90
91
|
}
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
|
-
/**
|
|
94
|
+
/** 关闭一个 mcpClient */
|
|
95
|
+
_closeOneClient(client) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
var _a, _b, _c, _d, _e;
|
|
98
|
+
try {
|
|
99
|
+
yield ((_b = (_a = client['__transport__']) === null || _a === void 0 ? void 0 : _a.terminateSession) === null || _b === void 0 ? void 0 : _b.call(_a));
|
|
100
|
+
yield ((_d = (_c = client['__transport__']) === null || _c === void 0 ? void 0 : _c.close) === null || _d === void 0 ? void 0 : _d.call(_c));
|
|
101
|
+
yield ((_e = client === null || client === void 0 ? void 0 : client.close) === null || _e === void 0 ? void 0 : _e.call(client));
|
|
102
|
+
}
|
|
103
|
+
catch (error) { }
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** 创建所有 mcpClients */
|
|
94
107
|
_createMpcClients() {
|
|
95
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
109
|
// 使用 Promise.all 并行处理所有 mcpServer 项
|
|
97
|
-
this.mcpClients = yield Promise.all(this.mcpServers.map((server) => __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
this.mcpClients = yield Promise.all(Object.values(this.mcpServers).map((server) => __awaiter(this, void 0, void 0, function* () {
|
|
98
111
|
return this._createOneClient(server);
|
|
99
112
|
})));
|
|
100
113
|
});
|
|
101
114
|
}
|
|
102
|
-
/**
|
|
115
|
+
/** 查询所有 mcpClients 的 tools, 失败则保存为null */
|
|
103
116
|
_createMpcTools() {
|
|
104
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
118
|
this.mcpTools = yield Promise.all(this.mcpClients.map((client) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -122,7 +135,7 @@ export class AgentModelProvider {
|
|
|
122
135
|
return __awaiter(this, void 0, void 0, function* () {
|
|
123
136
|
yield Promise.all(this.mcpClients.map((client) => __awaiter(this, void 0, void 0, function* () {
|
|
124
137
|
try {
|
|
125
|
-
yield (client
|
|
138
|
+
yield this._closeOneClient(client);
|
|
126
139
|
}
|
|
127
140
|
catch (error) {
|
|
128
141
|
if (this.onError) {
|
|
@@ -133,54 +146,63 @@ export class AgentModelProvider {
|
|
|
133
146
|
})));
|
|
134
147
|
});
|
|
135
148
|
}
|
|
149
|
+
/** 创建所有的 mcpClients,并更新它们的tools */
|
|
136
150
|
initClientsAndTools() {
|
|
137
151
|
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
var _a;
|
|
138
153
|
yield this._createMpcClients();
|
|
139
154
|
yield this._createMpcTools();
|
|
155
|
+
(_a = this.onUpdatedTools) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
140
156
|
});
|
|
141
157
|
}
|
|
158
|
+
/** 全量更新所有的 mcpServers */
|
|
142
159
|
updateMcpServers(mcpServers) {
|
|
143
160
|
return __awaiter(this, void 0, void 0, function* () {
|
|
144
161
|
yield this.closeAll();
|
|
145
|
-
this.mcpServers = mcpServers;
|
|
162
|
+
this.mcpServers = mcpServers || this.mcpServers;
|
|
146
163
|
yield this.initClientsAndTools();
|
|
147
164
|
});
|
|
148
165
|
}
|
|
149
|
-
|
|
166
|
+
/** 插入一个新的mcpServer,如果已经存在则返回false */
|
|
167
|
+
insertMcpServer(serverName, mcpServer) {
|
|
150
168
|
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
-
var _a;
|
|
152
|
-
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
const client = yield this._createOneClient(mcpServer);
|
|
156
|
-
this.mcpClients.push(client);
|
|
157
|
-
this.mcpTools.push((yield ((_a = client === null || client === void 0 ? void 0 : client.tools) === null || _a === void 0 ? void 0 : _a.call(client))));
|
|
158
|
-
return true;
|
|
169
|
+
var _a, _b;
|
|
170
|
+
// 检查是否已存在相同名称的服务器
|
|
171
|
+
if (this.mcpServers[serverName]) {
|
|
172
|
+
return false;
|
|
159
173
|
}
|
|
160
|
-
|
|
174
|
+
this.mcpServers[serverName] = mcpServer;
|
|
175
|
+
const client = yield this._createOneClient(mcpServer);
|
|
176
|
+
this.mcpClients.push(client);
|
|
177
|
+
this.mcpTools.push((yield ((_a = client === null || client === void 0 ? void 0 : client.tools) === null || _a === void 0 ? void 0 : _a.call(client))));
|
|
178
|
+
(_b = this.onUpdatedTools) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
179
|
+
return true;
|
|
161
180
|
});
|
|
162
181
|
}
|
|
163
|
-
/**
|
|
164
|
-
removeMcpServer(
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
182
|
+
/** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
|
|
183
|
+
removeMcpServer(serverName) {
|
|
184
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
if (!this.mcpServers[serverName]) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// 找到对应的索引
|
|
189
|
+
const serverNames = Object.keys(this.mcpServers);
|
|
190
|
+
const index = serverNames.indexOf(serverName);
|
|
191
|
+
delete this.mcpServers[serverName];
|
|
192
|
+
const delClient = this.mcpClients[index];
|
|
193
|
+
this.mcpClients.splice(index, 1);
|
|
194
|
+
try {
|
|
195
|
+
yield this._closeOneClient(delClient);
|
|
196
|
+
}
|
|
197
|
+
catch (error) { }
|
|
198
|
+
const delTool = this.mcpTools[index];
|
|
199
|
+
this.mcpTools.splice(index, 1);
|
|
200
|
+
if (delTool) {
|
|
201
|
+
Object.keys(delTool).forEach((toolName) => {
|
|
202
|
+
this.ignoreToolnames = this.ignoreToolnames.filter((name) => name !== toolName);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
});
|
|
184
206
|
}
|
|
185
207
|
/** 创建临时允许调用的tools集合 */
|
|
186
208
|
_tempMergeTools(extraTool = {}) {
|
|
@@ -193,13 +215,12 @@ export class AgentModelProvider {
|
|
|
193
215
|
}
|
|
194
216
|
_chat(chatMethod, _a) {
|
|
195
217
|
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
-
var _b
|
|
218
|
+
var _b;
|
|
197
219
|
var { model, maxSteps = 5 } = _a, options = __rest(_a, ["model", "maxSteps"]);
|
|
198
220
|
if (!this.llm) {
|
|
199
221
|
throw new Error('LLM is not initialized');
|
|
200
222
|
}
|
|
201
223
|
yield this.initClientsAndTools();
|
|
202
|
-
(_b = this.onUpdatedTools) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
203
224
|
const chatOptions = Object.assign(Object.assign({
|
|
204
225
|
// @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
|
|
205
226
|
model: this.llm(model), stopWhen: stepCountIs(maxSteps) }, options), { tools: this._tempMergeTools(options.tools) });
|
|
@@ -208,7 +229,7 @@ export class AgentModelProvider {
|
|
|
208
229
|
chatOptions.messages = [...this.messages];
|
|
209
230
|
}
|
|
210
231
|
const result = chatMethod(chatOptions);
|
|
211
|
-
(
|
|
232
|
+
(_b = result === null || result === void 0 ? void 0 : result.response) === null || _b === void 0 ? void 0 : _b.then((res) => {
|
|
212
233
|
this.messages.push(...res.messages);
|
|
213
234
|
});
|
|
214
235
|
return result;
|
package/dist/agent/type.d.ts
CHANGED
|
@@ -27,6 +27,6 @@ export interface IAgentModelProviderOption {
|
|
|
27
27
|
llm?: ProviderV2;
|
|
28
28
|
/** 代理模型提供器的大语言配置对象, 不能与 llm 同时传入 */
|
|
29
29
|
llmConfig?: IAgentModelProviderLlmConfig;
|
|
30
|
-
/** Mcp Server
|
|
31
|
-
mcpServers?: McpServerConfig
|
|
30
|
+
/** Mcp Server的配置对象的集合,键为服务器名称,值为配置对象 */
|
|
31
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
32
32
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ToolSet } from 'ai';
|
|
2
2
|
import { WebMcpClient } from '../../WebMcpClient';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 快速从官方 mcp 或 WebMcpClient 这2种client中读取 tools 数组,并转换成 ai-sdk 的tool的对象格式。
|
|
5
5
|
* @params client 一个已连接好的 WebMcpClient
|
|
6
|
-
* @returns
|
|
6
|
+
* @returns ai-sdk的dynamicTool对象。
|
|
7
7
|
*/
|
|
8
8
|
export declare const getAISDKTools: (client: WebMcpClient) => Promise<ToolSet>;
|
|
@@ -9,9 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { dynamicTool, jsonSchema } from 'ai';
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* 快速从官方 mcp 或 WebMcpClient 这2种client中读取 tools 数组,并转换成 ai-sdk 的tool的对象格式。
|
|
13
13
|
* @params client 一个已连接好的 WebMcpClient
|
|
14
|
-
* @returns
|
|
14
|
+
* @returns ai-sdk的dynamicTool对象。
|
|
15
15
|
*/
|
|
16
16
|
export const getAISDKTools = (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
17
|
var _a;
|
package/dist/index.es.dev.js
CHANGED
|
@@ -22631,25 +22631,14 @@ class WebMcpClient {
|
|
|
22631
22631
|
await this.client.connect(this.transport);
|
|
22632
22632
|
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
22633
22633
|
}
|
|
22634
|
-
const { url, token, sessionId,
|
|
22634
|
+
const { url, token, sessionId, type: type2, agent, onError } = options;
|
|
22635
22635
|
if (agent === true) {
|
|
22636
|
-
const proxyOptions = { client: this.client, url, token, sessionId
|
|
22637
|
-
let reconnect = false;
|
|
22636
|
+
const proxyOptions = { client: this.client, url, token, sessionId };
|
|
22638
22637
|
let response;
|
|
22639
22638
|
const connectProxy = async () => {
|
|
22640
22639
|
const { transport: transport2, sessionId: sessionId2 } = type2 === "sse" ? await createSseProxy(proxyOptions) : type2 === "socket" ? await createSocketProxy(proxyOptions) : await createStreamProxy(proxyOptions);
|
|
22641
22640
|
transport2.onerror = async (error2) => {
|
|
22642
22641
|
onError == null ? void 0 : onError(error2);
|
|
22643
|
-
if (error2.message === "Unauthorized" && !reconnect) {
|
|
22644
|
-
if (typeof onUnauthorized === "function") {
|
|
22645
|
-
await onUnauthorized(connectProxy);
|
|
22646
|
-
} else {
|
|
22647
|
-
reconnect = true;
|
|
22648
|
-
await connectProxy();
|
|
22649
|
-
reconnect = false;
|
|
22650
|
-
await (onReconnect == null ? void 0 : onReconnect());
|
|
22651
|
-
}
|
|
22652
|
-
}
|
|
22653
22642
|
};
|
|
22654
22643
|
response = { transport: transport2, sessionId: sessionId2 };
|
|
22655
22644
|
};
|
|
@@ -22663,14 +22652,9 @@ class WebMcpClient {
|
|
|
22663
22652
|
await this.client.connect(transport);
|
|
22664
22653
|
}
|
|
22665
22654
|
if (type2 === "sse") {
|
|
22666
|
-
|
|
22667
|
-
|
|
22668
|
-
|
|
22669
|
-
} else {
|
|
22670
|
-
const opts = sseOptions(token, sessionId);
|
|
22671
|
-
transport = new SSEClientTransport(endpoint, opts);
|
|
22672
|
-
await this.client.connect(transport);
|
|
22673
|
-
}
|
|
22655
|
+
const opts = sseOptions(token, sessionId);
|
|
22656
|
+
transport = new SSEClientTransport(endpoint, opts);
|
|
22657
|
+
await this.client.connect(transport);
|
|
22674
22658
|
}
|
|
22675
22659
|
if (type2 === "socket") {
|
|
22676
22660
|
transport = new WebSocketClientTransport(new URL(`${url}?sessionId=${sessionId}&token=${token}`));
|
|
@@ -22678,14 +22662,9 @@ class WebMcpClient {
|
|
|
22678
22662
|
await this.client.connect(transport);
|
|
22679
22663
|
}
|
|
22680
22664
|
if (typeof transport === "undefined") {
|
|
22681
|
-
|
|
22682
|
-
|
|
22683
|
-
|
|
22684
|
-
} else {
|
|
22685
|
-
const opts = streamOptions(token, sessionId);
|
|
22686
|
-
transport = new StreamableHTTPClientTransport(endpoint, opts);
|
|
22687
|
-
await this.client.connect(transport);
|
|
22688
|
-
}
|
|
22665
|
+
const opts = streamOptions(token, sessionId);
|
|
22666
|
+
transport = new StreamableHTTPClientTransport(endpoint, opts);
|
|
22667
|
+
await this.client.connect(transport);
|
|
22689
22668
|
}
|
|
22690
22669
|
this.transport = transport;
|
|
22691
22670
|
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
@@ -25098,7 +25077,7 @@ class FloatingBlock {
|
|
|
25098
25077
|
this.copyToClipboard(this.options.sessionId.slice(-6));
|
|
25099
25078
|
}
|
|
25100
25079
|
copyRemoteURL() {
|
|
25101
|
-
this.copyToClipboard(
|
|
25080
|
+
this.copyToClipboard(this.options.remoteUrl + this.sessionPrefix + this.options.sessionId);
|
|
25102
25081
|
}
|
|
25103
25082
|
// 实现复制到剪贴板功能
|
|
25104
25083
|
async copyToClipboard(text2) {
|
|
@@ -42809,12 +42788,12 @@ const AIProviderFactories = {
|
|
|
42809
42788
|
};
|
|
42810
42789
|
class AgentModelProvider {
|
|
42811
42790
|
constructor({ llmConfig, mcpServers, llm }) {
|
|
42812
|
-
this.mcpServers =
|
|
42791
|
+
this.mcpServers = {};
|
|
42813
42792
|
this.mcpClients = [];
|
|
42814
42793
|
this.mcpTools = [];
|
|
42815
42794
|
this.ignoreToolnames = [];
|
|
42816
42795
|
this.messages = [];
|
|
42817
|
-
this.mcpServers = mcpServers ||
|
|
42796
|
+
this.mcpServers = mcpServers || {};
|
|
42818
42797
|
if (llm) {
|
|
42819
42798
|
this.llm = llm;
|
|
42820
42799
|
} else if (llmConfig) {
|
|
@@ -42832,7 +42811,7 @@ class AgentModelProvider {
|
|
|
42832
42811
|
throw new Error("Either llmConfig or llm must be provided");
|
|
42833
42812
|
}
|
|
42834
42813
|
}
|
|
42835
|
-
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回
|
|
42814
|
+
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
|
|
42836
42815
|
async _createOneClient(serverConfig) {
|
|
42837
42816
|
try {
|
|
42838
42817
|
let transport;
|
|
@@ -42841,7 +42820,9 @@ class AgentModelProvider {
|
|
|
42841
42820
|
} else {
|
|
42842
42821
|
transport = serverConfig;
|
|
42843
42822
|
}
|
|
42844
|
-
|
|
42823
|
+
const client = await createMCPClient({ transport });
|
|
42824
|
+
client["__transport__"] = transport;
|
|
42825
|
+
return client;
|
|
42845
42826
|
} catch (error2) {
|
|
42846
42827
|
if (this.onError) {
|
|
42847
42828
|
this.onError((error2 == null ? void 0 : error2.message) || `Failed to create MCP client`, error2);
|
|
@@ -42850,15 +42831,25 @@ class AgentModelProvider {
|
|
|
42850
42831
|
return null;
|
|
42851
42832
|
}
|
|
42852
42833
|
}
|
|
42853
|
-
/**
|
|
42834
|
+
/** 关闭一个 mcpClient */
|
|
42835
|
+
async _closeOneClient(client) {
|
|
42836
|
+
var _a16, _b8, _c, _d, _e;
|
|
42837
|
+
try {
|
|
42838
|
+
await ((_b8 = (_a16 = client["__transport__"]) == null ? void 0 : _a16.terminateSession) == null ? void 0 : _b8.call(_a16));
|
|
42839
|
+
await ((_d = (_c = client["__transport__"]) == null ? void 0 : _c.close) == null ? void 0 : _d.call(_c));
|
|
42840
|
+
await ((_e = client == null ? void 0 : client.close) == null ? void 0 : _e.call(client));
|
|
42841
|
+
} catch (error2) {
|
|
42842
|
+
}
|
|
42843
|
+
}
|
|
42844
|
+
/** 创建所有 mcpClients */
|
|
42854
42845
|
async _createMpcClients() {
|
|
42855
42846
|
this.mcpClients = await Promise.all(
|
|
42856
|
-
this.mcpServers.map(async (server) => {
|
|
42847
|
+
Object.values(this.mcpServers).map(async (server) => {
|
|
42857
42848
|
return this._createOneClient(server);
|
|
42858
42849
|
})
|
|
42859
42850
|
);
|
|
42860
42851
|
}
|
|
42861
|
-
/**
|
|
42852
|
+
/** 查询所有 mcpClients 的 tools, 失败则保存为null */
|
|
42862
42853
|
async _createMpcTools() {
|
|
42863
42854
|
this.mcpTools = await Promise.all(
|
|
42864
42855
|
this.mcpClients.map(async (client) => {
|
|
@@ -42880,7 +42871,7 @@ class AgentModelProvider {
|
|
|
42880
42871
|
await Promise.all(
|
|
42881
42872
|
this.mcpClients.map(async (client) => {
|
|
42882
42873
|
try {
|
|
42883
|
-
await (client
|
|
42874
|
+
await this._closeOneClient(client);
|
|
42884
42875
|
} catch (error2) {
|
|
42885
42876
|
if (this.onError) {
|
|
42886
42877
|
this.onError((error2 == null ? void 0 : error2.message) || `Failed to close client`, error2);
|
|
@@ -42890,35 +42881,44 @@ class AgentModelProvider {
|
|
|
42890
42881
|
})
|
|
42891
42882
|
);
|
|
42892
42883
|
}
|
|
42884
|
+
/** 创建所有的 mcpClients,并更新它们的tools */
|
|
42893
42885
|
async initClientsAndTools() {
|
|
42886
|
+
var _a16;
|
|
42894
42887
|
await this._createMpcClients();
|
|
42895
42888
|
await this._createMpcTools();
|
|
42889
|
+
(_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
|
|
42896
42890
|
}
|
|
42891
|
+
/** 全量更新所有的 mcpServers */
|
|
42897
42892
|
async updateMcpServers(mcpServers) {
|
|
42898
42893
|
await this.closeAll();
|
|
42899
|
-
this.mcpServers = mcpServers;
|
|
42894
|
+
this.mcpServers = mcpServers || this.mcpServers;
|
|
42900
42895
|
await this.initClientsAndTools();
|
|
42901
42896
|
}
|
|
42902
|
-
|
|
42903
|
-
|
|
42904
|
-
|
|
42905
|
-
if (
|
|
42906
|
-
|
|
42907
|
-
const client = await this._createOneClient(mcpServer);
|
|
42908
|
-
this.mcpClients.push(client);
|
|
42909
|
-
this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
|
|
42910
|
-
return true;
|
|
42897
|
+
/** 插入一个新的mcpServer,如果已经存在则返回false */
|
|
42898
|
+
async insertMcpServer(serverName, mcpServer) {
|
|
42899
|
+
var _a16, _b8;
|
|
42900
|
+
if (this.mcpServers[serverName]) {
|
|
42901
|
+
return false;
|
|
42911
42902
|
}
|
|
42912
|
-
|
|
42903
|
+
this.mcpServers[serverName] = mcpServer;
|
|
42904
|
+
const client = await this._createOneClient(mcpServer);
|
|
42905
|
+
this.mcpClients.push(client);
|
|
42906
|
+
this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
|
|
42907
|
+
(_b8 = this.onUpdatedTools) == null ? void 0 : _b8.call(this);
|
|
42908
|
+
return true;
|
|
42913
42909
|
}
|
|
42914
|
-
/**
|
|
42915
|
-
removeMcpServer(
|
|
42916
|
-
|
|
42917
|
-
|
|
42910
|
+
/** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
|
|
42911
|
+
async removeMcpServer(serverName) {
|
|
42912
|
+
if (!this.mcpServers[serverName]) {
|
|
42913
|
+
return;
|
|
42914
|
+
}
|
|
42915
|
+
const serverNames = Object.keys(this.mcpServers);
|
|
42916
|
+
const index = serverNames.indexOf(serverName);
|
|
42917
|
+
delete this.mcpServers[serverName];
|
|
42918
42918
|
const delClient = this.mcpClients[index];
|
|
42919
42919
|
this.mcpClients.splice(index, 1);
|
|
42920
42920
|
try {
|
|
42921
|
-
|
|
42921
|
+
await this._closeOneClient(delClient);
|
|
42922
42922
|
} catch (error2) {
|
|
42923
42923
|
}
|
|
42924
42924
|
const delTool = this.mcpTools[index];
|
|
@@ -42939,12 +42939,11 @@ class AgentModelProvider {
|
|
|
42939
42939
|
return toolsResult;
|
|
42940
42940
|
}
|
|
42941
42941
|
async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
|
|
42942
|
-
var _a16
|
|
42942
|
+
var _a16;
|
|
42943
42943
|
if (!this.llm) {
|
|
42944
42944
|
throw new Error("LLM is not initialized");
|
|
42945
42945
|
}
|
|
42946
42946
|
await this.initClientsAndTools();
|
|
42947
|
-
(_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
|
|
42948
42947
|
const chatOptions = {
|
|
42949
42948
|
// @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
|
|
42950
42949
|
model: this.llm(model),
|
|
@@ -42957,7 +42956,7 @@ class AgentModelProvider {
|
|
|
42957
42956
|
chatOptions.messages = [...this.messages];
|
|
42958
42957
|
}
|
|
42959
42958
|
const result = chatMethod(chatOptions);
|
|
42960
|
-
(
|
|
42959
|
+
(_a16 = result == null ? void 0 : result.response) == null ? void 0 : _a16.then((res) => {
|
|
42961
42960
|
this.messages.push(...res.messages);
|
|
42962
42961
|
});
|
|
42963
42962
|
return result;
|