@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
package/WebMcpClient.ts
CHANGED
|
@@ -18,11 +18,9 @@ import {
|
|
|
18
18
|
MessageChannelClientTransport,
|
|
19
19
|
sseOptions,
|
|
20
20
|
streamOptions,
|
|
21
|
-
attemptConnection,
|
|
22
21
|
createSseProxy,
|
|
23
22
|
createStreamProxy,
|
|
24
|
-
createSocketProxy
|
|
25
|
-
AuthClientProvider
|
|
23
|
+
createSocketProxy
|
|
26
24
|
} from '@opentiny/next'
|
|
27
25
|
import type {
|
|
28
26
|
Result,
|
|
@@ -61,12 +59,9 @@ export interface ClientConnectOptions {
|
|
|
61
59
|
url: string
|
|
62
60
|
token?: string
|
|
63
61
|
sessionId?: string
|
|
64
|
-
authProvider?: AuthClientProvider
|
|
65
62
|
type?: 'channel' | 'sse' | 'stream' | 'socket'
|
|
66
63
|
agent?: boolean
|
|
67
64
|
onError?: (error: Error) => void
|
|
68
|
-
onUnauthorized?: (connect: () => Promise<void>) => Promise<void>
|
|
69
|
-
onReconnect?: () => Promise<void>
|
|
70
65
|
}
|
|
71
66
|
|
|
72
67
|
type SendRequestT = Request
|
|
@@ -117,13 +112,11 @@ export class WebMcpClient {
|
|
|
117
112
|
return { transport: this.transport, sessionId: this.transport.sessionId as string }
|
|
118
113
|
}
|
|
119
114
|
|
|
120
|
-
const { url, token, sessionId,
|
|
121
|
-
options as ClientConnectOptions
|
|
115
|
+
const { url, token, sessionId, type, agent, onError } = options as ClientConnectOptions
|
|
122
116
|
|
|
123
117
|
if (agent === true) {
|
|
124
|
-
const proxyOptions: ProxyOptions = { client: this.client, url, token, sessionId
|
|
118
|
+
const proxyOptions: ProxyOptions = { client: this.client, url, token, sessionId }
|
|
125
119
|
|
|
126
|
-
let reconnect = false
|
|
127
120
|
let response
|
|
128
121
|
|
|
129
122
|
const connectProxy = async () => {
|
|
@@ -136,17 +129,6 @@ export class WebMcpClient {
|
|
|
136
129
|
|
|
137
130
|
transport.onerror = async (error: Error) => {
|
|
138
131
|
onError?.(error)
|
|
139
|
-
|
|
140
|
-
if (error.message === 'Unauthorized' && !reconnect) {
|
|
141
|
-
if (typeof onUnauthorized === 'function') {
|
|
142
|
-
await onUnauthorized(connectProxy)
|
|
143
|
-
} else {
|
|
144
|
-
reconnect = true
|
|
145
|
-
await connectProxy()
|
|
146
|
-
reconnect = false
|
|
147
|
-
await onReconnect?.()
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
132
|
}
|
|
151
133
|
|
|
152
134
|
response = { transport, sessionId }
|
|
@@ -165,14 +147,9 @@ export class WebMcpClient {
|
|
|
165
147
|
}
|
|
166
148
|
|
|
167
149
|
if (type === 'sse') {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
} else {
|
|
172
|
-
const opts = sseOptions(token, sessionId) as SSEClientTransportOptions
|
|
173
|
-
transport = new SSEClientTransport(endpoint, opts)
|
|
174
|
-
await this.client.connect(transport)
|
|
175
|
-
}
|
|
150
|
+
const opts = sseOptions(token, sessionId) as SSEClientTransportOptions
|
|
151
|
+
transport = new SSEClientTransport(endpoint, opts)
|
|
152
|
+
await this.client.connect(transport)
|
|
176
153
|
}
|
|
177
154
|
|
|
178
155
|
if (type === 'socket') {
|
|
@@ -182,14 +159,9 @@ export class WebMcpClient {
|
|
|
182
159
|
}
|
|
183
160
|
|
|
184
161
|
if (typeof transport === 'undefined') {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
} else {
|
|
189
|
-
const opts = streamOptions(token, sessionId) as StreamableHTTPClientTransportOptions
|
|
190
|
-
transport = new StreamableHTTPClientTransport(endpoint, opts)
|
|
191
|
-
await this.client.connect(transport)
|
|
192
|
-
}
|
|
162
|
+
const opts = streamOptions(token, sessionId) as StreamableHTTPClientTransportOptions
|
|
163
|
+
transport = new StreamableHTTPClientTransport(endpoint, opts)
|
|
164
|
+
await this.client.connect(transport)
|
|
193
165
|
}
|
|
194
166
|
|
|
195
167
|
this.transport = transport
|
|
@@ -15,35 +15,31 @@ export const AIProviderFactories = {
|
|
|
15
15
|
|
|
16
16
|
type ChatMethodFn = typeof streamText | typeof generateText
|
|
17
17
|
|
|
18
|
-
/** 一个通用的ai-sdk的
|
|
18
|
+
/** 一个通用的ai-sdk的Agent封装
|
|
19
19
|
* @summary 内部自动管理了 llm, mcpServer, ai-sdk的clients 和 tools
|
|
20
20
|
* @returns 暴露了 chat, chatStream方法
|
|
21
21
|
*/
|
|
22
22
|
export class AgentModelProvider {
|
|
23
23
|
llm: ProviderV2 | OpenAIProvider
|
|
24
|
-
/** mcpServers
|
|
24
|
+
/** 当前mcpServers对象集合。键为服务器名称,值为 McpServerConfig 或任意的 MCPTransport
|
|
25
25
|
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client */
|
|
26
|
-
mcpServers: McpServerConfig
|
|
27
|
-
/**
|
|
26
|
+
mcpServers: Record<string, McpServerConfig> = {}
|
|
27
|
+
/** 当前ai-sdk的 mcpClient 数组 */
|
|
28
28
|
mcpClients: any[] = []
|
|
29
|
-
/**
|
|
29
|
+
/** 当前 mcpClients 所对应的tools */
|
|
30
30
|
mcpTools: Array<Record<string, any>> = []
|
|
31
|
-
/**
|
|
31
|
+
/** 需要实时过滤掉的tools name*/
|
|
32
32
|
ignoreToolnames: string[] = []
|
|
33
|
-
|
|
34
|
-
/** chat 时,自动更新 所有的tools 后的事件 */
|
|
33
|
+
/** Agent 自动更新所有的tools 后的事件 */
|
|
35
34
|
onUpdatedTools: (() => void) | undefined
|
|
36
|
-
/**
|
|
35
|
+
/** Agent 内部报错时,抛出的错误事件 */
|
|
37
36
|
onError: ((msg: string, err?: any) => void) | undefined
|
|
38
|
-
|
|
39
|
-
/** 缓存 ai-sdk response 中的 多轮会话 */
|
|
37
|
+
/** 缓存 ai-sdk response 中的 多轮会话的上下文 */
|
|
40
38
|
messages: any[] = []
|
|
41
39
|
|
|
42
40
|
constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption) {
|
|
43
|
-
|
|
44
|
-
this.mcpServers = mcpServers || []
|
|
41
|
+
this.mcpServers = mcpServers || {}
|
|
45
42
|
|
|
46
|
-
// 2、保存 llm
|
|
47
43
|
if (llm) {
|
|
48
44
|
this.llm = llm
|
|
49
45
|
} else if (llmConfig) {
|
|
@@ -63,7 +59,7 @@ export class AgentModelProvider {
|
|
|
63
59
|
}
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回
|
|
62
|
+
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
|
|
67
63
|
private async _createOneClient(serverConfig: McpServerConfig) {
|
|
68
64
|
try {
|
|
69
65
|
let transport: MCPClientConfig['transport']
|
|
@@ -74,7 +70,10 @@ export class AgentModelProvider {
|
|
|
74
70
|
transport = serverConfig as MCPClientConfig['transport']
|
|
75
71
|
}
|
|
76
72
|
|
|
77
|
-
|
|
73
|
+
const client = await createMCPClient({ transport: transport as MCPClientConfig['transport'] })
|
|
74
|
+
//@ts-ignore
|
|
75
|
+
client['__transport__'] = transport
|
|
76
|
+
return client
|
|
78
77
|
} catch (error: unknown) {
|
|
79
78
|
if (this.onError) {
|
|
80
79
|
this.onError((error as Error)?.message || `Failed to create MCP client`, error)
|
|
@@ -83,16 +82,24 @@ export class AgentModelProvider {
|
|
|
83
82
|
return null
|
|
84
83
|
}
|
|
85
84
|
}
|
|
86
|
-
/**
|
|
85
|
+
/** 关闭一个 mcpClient */
|
|
86
|
+
private async _closeOneClient(client: any) {
|
|
87
|
+
try {
|
|
88
|
+
await client['__transport__']?.terminateSession?.()
|
|
89
|
+
await client['__transport__']?.close?.()
|
|
90
|
+
await client?.close?.()
|
|
91
|
+
} catch (error) {}
|
|
92
|
+
}
|
|
93
|
+
/** 创建所有 mcpClients */
|
|
87
94
|
private async _createMpcClients() {
|
|
88
95
|
// 使用 Promise.all 并行处理所有 mcpServer 项
|
|
89
96
|
this.mcpClients = await Promise.all(
|
|
90
|
-
this.mcpServers.map(async (server) => {
|
|
97
|
+
Object.values(this.mcpServers).map(async (server) => {
|
|
91
98
|
return this._createOneClient(server)
|
|
92
99
|
})
|
|
93
100
|
)
|
|
94
101
|
}
|
|
95
|
-
/**
|
|
102
|
+
/** 查询所有 mcpClients 的 tools, 失败则保存为null */
|
|
96
103
|
private async _createMpcTools() {
|
|
97
104
|
this.mcpTools = await Promise.all(
|
|
98
105
|
this.mcpClients.map(async (client) => {
|
|
@@ -113,7 +120,7 @@ export class AgentModelProvider {
|
|
|
113
120
|
await Promise.all(
|
|
114
121
|
this.mcpClients.map(async (client) => {
|
|
115
122
|
try {
|
|
116
|
-
await client
|
|
123
|
+
await this._closeOneClient(client)
|
|
117
124
|
} catch (error: unknown) {
|
|
118
125
|
if (this.onError) {
|
|
119
126
|
this.onError((error as Error)?.message || `Failed to close client`, error)
|
|
@@ -124,48 +131,56 @@ export class AgentModelProvider {
|
|
|
124
131
|
)
|
|
125
132
|
}
|
|
126
133
|
|
|
134
|
+
/** 创建所有的 mcpClients,并更新它们的tools */
|
|
127
135
|
async initClientsAndTools() {
|
|
128
136
|
await this._createMpcClients()
|
|
129
137
|
await this._createMpcTools()
|
|
138
|
+
this.onUpdatedTools?.()
|
|
130
139
|
}
|
|
131
140
|
|
|
132
|
-
|
|
141
|
+
/** 全量更新所有的 mcpServers */
|
|
142
|
+
async updateMcpServers(mcpServers?: Record<string, McpServerConfig>) {
|
|
133
143
|
await this.closeAll()
|
|
134
|
-
this.mcpServers = mcpServers
|
|
144
|
+
this.mcpServers = mcpServers || this.mcpServers
|
|
135
145
|
await this.initClientsAndTools()
|
|
136
146
|
}
|
|
137
147
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
const client = await this._createOneClient(mcpServer)
|
|
144
|
-
this.mcpClients.push(client)
|
|
145
|
-
this.mcpTools.push((await client?.tools?.()) as Record<string, any>)
|
|
146
|
-
return true
|
|
148
|
+
/** 插入一个新的mcpServer,如果已经存在则返回false */
|
|
149
|
+
async insertMcpServer(serverName: string, mcpServer: McpServerConfig) {
|
|
150
|
+
// 检查是否已存在相同名称的服务器
|
|
151
|
+
if (this.mcpServers[serverName]) {
|
|
152
|
+
return false
|
|
147
153
|
}
|
|
148
|
-
|
|
154
|
+
|
|
155
|
+
this.mcpServers[serverName] = mcpServer
|
|
156
|
+
const client = await this._createOneClient(mcpServer)
|
|
157
|
+
this.mcpClients.push(client)
|
|
158
|
+
this.mcpTools.push((await client?.tools?.()) as Record<string, any>)
|
|
159
|
+
this.onUpdatedTools?.()
|
|
160
|
+
|
|
161
|
+
return true
|
|
149
162
|
}
|
|
150
|
-
/**
|
|
151
|
-
removeMcpServer(
|
|
152
|
-
|
|
163
|
+
/** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
|
|
164
|
+
async removeMcpServer(serverName: string) {
|
|
165
|
+
if (!this.mcpServers[serverName]) {
|
|
166
|
+
return
|
|
167
|
+
}
|
|
153
168
|
|
|
154
|
-
//
|
|
155
|
-
this.mcpServers
|
|
169
|
+
// 找到对应的索引
|
|
170
|
+
const serverNames = Object.keys(this.mcpServers)
|
|
171
|
+
const index = serverNames.indexOf(serverName)
|
|
172
|
+
|
|
173
|
+
delete this.mcpServers[serverName]
|
|
156
174
|
|
|
157
|
-
// 移除
|
|
158
175
|
const delClient = this.mcpClients[index]
|
|
159
176
|
this.mcpClients.splice(index, 1)
|
|
160
177
|
try {
|
|
161
|
-
delClient
|
|
178
|
+
await this._closeOneClient(delClient)
|
|
162
179
|
} catch (error) {}
|
|
163
180
|
|
|
164
|
-
// 移除 tools
|
|
165
181
|
const delTool = this.mcpTools[index]
|
|
166
182
|
this.mcpTools.splice(index, 1)
|
|
167
183
|
|
|
168
|
-
// 移除 ignoreToolnames
|
|
169
184
|
if (delTool) {
|
|
170
185
|
Object.keys(delTool).forEach((toolName) => {
|
|
171
186
|
this.ignoreToolnames = this.ignoreToolnames.filter((name) => name !== toolName)
|
|
@@ -194,8 +209,6 @@ export class AgentModelProvider {
|
|
|
194
209
|
|
|
195
210
|
await this.initClientsAndTools()
|
|
196
211
|
|
|
197
|
-
this.onUpdatedTools?.()
|
|
198
|
-
|
|
199
212
|
const chatOptions = {
|
|
200
213
|
// @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
|
|
201
214
|
model: this.llm(model),
|
package/agent/type.ts
CHANGED
|
@@ -25,6 +25,6 @@ export interface IAgentModelProviderOption {
|
|
|
25
25
|
llm?: ProviderV2
|
|
26
26
|
/** 代理模型提供器的大语言配置对象, 不能与 llm 同时传入 */
|
|
27
27
|
llmConfig?: IAgentModelProviderLlmConfig
|
|
28
|
-
/** Mcp Server
|
|
29
|
-
mcpServers?: McpServerConfig
|
|
28
|
+
/** Mcp Server的配置对象的集合,键为服务器名称,值为配置对象 */
|
|
29
|
+
mcpServers?: Record<string, McpServerConfig>
|
|
30
30
|
}
|
|
@@ -2,9 +2,9 @@ import { dynamicTool, jsonSchema, Tool, ToolCallOptions, ToolSet } from 'ai'
|
|
|
2
2
|
import { WebMcpClient } from '../../WebMcpClient'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 快速从官方 mcp 或 WebMcpClient 这2种client中读取 tools 数组,并转换成 ai-sdk 的tool的对象格式。
|
|
6
6
|
* @params client 一个已连接好的 WebMcpClient
|
|
7
|
-
* @returns
|
|
7
|
+
* @returns ai-sdk的dynamicTool对象。
|
|
8
8
|
*/
|
|
9
9
|
export const getAISDKTools = async (client: WebMcpClient): Promise<ToolSet> => {
|
|
10
10
|
const tools: Record<string, Tool> = {}
|
package/dist/WebMcpClient.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
|
3
3
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
4
4
|
import { z, ZodObject, ZodLiteral, ZodType } from 'zod';
|
|
5
5
|
import { ElicitRequestSchema, ListRootsRequestSchema, CreateMessageRequestSchema, LoggingMessageNotificationSchema, ToolListChangedNotificationSchema, ResourceUpdatedNotificationSchema, PromptListChangedNotificationSchema, ResourceListChangedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
6
|
-
import { MessageChannelClientTransport
|
|
6
|
+
import { MessageChannelClientTransport } from '@opentiny/next';
|
|
7
7
|
import type { Result, Request, Notification, Implementation, ServerCapabilities, LoggingLevel, CompleteRequest, CallToolRequest, ListToolsRequest, GetPromptRequest, SubscribeRequest, UnsubscribeRequest, ListPromptsRequest, ReadResourceRequest, ListResourcesRequest, ListResourceTemplatesRequest } from '@modelcontextprotocol/sdk/types.js';
|
|
8
8
|
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
9
9
|
import type { ClientOptions } from '@modelcontextprotocol/sdk/client/index.js';
|
|
@@ -17,12 +17,9 @@ export interface ClientConnectOptions {
|
|
|
17
17
|
url: string;
|
|
18
18
|
token?: string;
|
|
19
19
|
sessionId?: string;
|
|
20
|
-
authProvider?: AuthClientProvider;
|
|
21
20
|
type?: 'channel' | 'sse' | 'stream' | 'socket';
|
|
22
21
|
agent?: boolean;
|
|
23
22
|
onError?: (error: Error) => void;
|
|
24
|
-
onUnauthorized?: (connect: () => Promise<void>) => Promise<void>;
|
|
25
|
-
onReconnect?: () => Promise<void>;
|
|
26
23
|
}
|
|
27
24
|
type SendRequestT = Request;
|
|
28
25
|
type SendNotificationT = Notification;
|
package/dist/WebMcpClient.js
CHANGED
|
@@ -12,7 +12,7 @@ import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
|
12
12
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
13
13
|
import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
|
|
14
14
|
import { ElicitRequestSchema, CallToolResultSchema, ListRootsRequestSchema, CreateMessageRequestSchema, LoggingMessageNotificationSchema, ToolListChangedNotificationSchema, ResourceUpdatedNotificationSchema, PromptListChangedNotificationSchema, ResourceListChangedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
15
|
-
import { MessageChannelClientTransport, sseOptions, streamOptions,
|
|
15
|
+
import { MessageChannelClientTransport, sseOptions, streamOptions, createSseProxy, createStreamProxy, createSocketProxy } from '@opentiny/next';
|
|
16
16
|
/**
|
|
17
17
|
* An MCP client on top of a pluggable transport.
|
|
18
18
|
* The client will automatically begin the initialization flow with the server when connect() is called.
|
|
@@ -51,10 +51,9 @@ export class WebMcpClient {
|
|
|
51
51
|
yield this.client.connect(this.transport);
|
|
52
52
|
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
53
53
|
}
|
|
54
|
-
const { url, token, sessionId,
|
|
54
|
+
const { url, token, sessionId, type, agent, onError } = options;
|
|
55
55
|
if (agent === true) {
|
|
56
|
-
const proxyOptions = { client: this.client, url, token, sessionId
|
|
57
|
-
let reconnect = false;
|
|
56
|
+
const proxyOptions = { client: this.client, url, token, sessionId };
|
|
58
57
|
let response;
|
|
59
58
|
const connectProxy = () => __awaiter(this, void 0, void 0, function* () {
|
|
60
59
|
const { transport, sessionId } = type === 'sse'
|
|
@@ -64,17 +63,6 @@ export class WebMcpClient {
|
|
|
64
63
|
: yield createStreamProxy(proxyOptions);
|
|
65
64
|
transport.onerror = (error) => __awaiter(this, void 0, void 0, function* () {
|
|
66
65
|
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
67
|
-
if (error.message === 'Unauthorized' && !reconnect) {
|
|
68
|
-
if (typeof onUnauthorized === 'function') {
|
|
69
|
-
yield onUnauthorized(connectProxy);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
reconnect = true;
|
|
73
|
-
yield connectProxy();
|
|
74
|
-
reconnect = false;
|
|
75
|
-
yield (onReconnect === null || onReconnect === void 0 ? void 0 : onReconnect());
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
66
|
});
|
|
79
67
|
response = { transport, sessionId };
|
|
80
68
|
});
|
|
@@ -88,15 +76,9 @@ export class WebMcpClient {
|
|
|
88
76
|
yield this.client.connect(transport);
|
|
89
77
|
}
|
|
90
78
|
if (type === 'sse') {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
const opts = sseOptions(token, sessionId);
|
|
97
|
-
transport = new SSEClientTransport(endpoint, opts);
|
|
98
|
-
yield this.client.connect(transport);
|
|
99
|
-
}
|
|
79
|
+
const opts = sseOptions(token, sessionId);
|
|
80
|
+
transport = new SSEClientTransport(endpoint, opts);
|
|
81
|
+
yield this.client.connect(transport);
|
|
100
82
|
}
|
|
101
83
|
if (type === 'socket') {
|
|
102
84
|
transport = new WebSocketClientTransport(new URL(`${url}?sessionId=${sessionId}&token=${token}`));
|
|
@@ -104,15 +86,9 @@ export class WebMcpClient {
|
|
|
104
86
|
yield this.client.connect(transport);
|
|
105
87
|
}
|
|
106
88
|
if (typeof transport === 'undefined') {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
const opts = streamOptions(token, sessionId);
|
|
113
|
-
transport = new StreamableHTTPClientTransport(endpoint, opts);
|
|
114
|
-
yield this.client.connect(transport);
|
|
115
|
-
}
|
|
89
|
+
const opts = streamOptions(token, sessionId);
|
|
90
|
+
transport = new StreamableHTTPClientTransport(endpoint, opts);
|
|
91
|
+
yield this.client.connect(transport);
|
|
116
92
|
}
|
|
117
93
|
this.transport = transport;
|
|
118
94
|
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
@@ -8,41 +8,46 @@ export declare const AIProviderFactories: {
|
|
|
8
8
|
openai: typeof createOpenAI;
|
|
9
9
|
deepseek: typeof createDeepSeek;
|
|
10
10
|
};
|
|
11
|
-
/** 一个通用的ai-sdk的
|
|
11
|
+
/** 一个通用的ai-sdk的Agent封装
|
|
12
12
|
* @summary 内部自动管理了 llm, mcpServer, ai-sdk的clients 和 tools
|
|
13
13
|
* @returns 暴露了 chat, chatStream方法
|
|
14
14
|
*/
|
|
15
15
|
export declare class AgentModelProvider {
|
|
16
16
|
llm: ProviderV2 | OpenAIProvider;
|
|
17
|
-
/** mcpServers
|
|
17
|
+
/** 当前mcpServers对象集合。键为服务器名称,值为 McpServerConfig 或任意的 MCPTransport
|
|
18
18
|
* 参考: https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#initializing-an-mcp-client */
|
|
19
|
-
mcpServers: McpServerConfig
|
|
20
|
-
/**
|
|
19
|
+
mcpServers: Record<string, McpServerConfig>;
|
|
20
|
+
/** 当前ai-sdk的 mcpClient 数组 */
|
|
21
21
|
mcpClients: any[];
|
|
22
|
-
/**
|
|
22
|
+
/** 当前 mcpClients 所对应的tools */
|
|
23
23
|
mcpTools: Array<Record<string, any>>;
|
|
24
|
-
/**
|
|
24
|
+
/** 需要实时过滤掉的tools name*/
|
|
25
25
|
ignoreToolnames: string[];
|
|
26
|
-
/**
|
|
26
|
+
/** Agent 自动更新所有的tools 后的事件 */
|
|
27
27
|
onUpdatedTools: (() => void) | undefined;
|
|
28
|
-
/**
|
|
28
|
+
/** Agent 内部报错时,抛出的错误事件 */
|
|
29
29
|
onError: ((msg: string, err?: any) => void) | undefined;
|
|
30
|
-
/** 缓存 ai-sdk response 中的
|
|
30
|
+
/** 缓存 ai-sdk response 中的 多轮会话的上下文 */
|
|
31
31
|
messages: any[];
|
|
32
32
|
constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption);
|
|
33
|
-
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回
|
|
33
|
+
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
|
|
34
34
|
private _createOneClient;
|
|
35
|
-
/**
|
|
35
|
+
/** 关闭一个 mcpClient */
|
|
36
|
+
private _closeOneClient;
|
|
37
|
+
/** 创建所有 mcpClients */
|
|
36
38
|
private _createMpcClients;
|
|
37
|
-
/**
|
|
39
|
+
/** 查询所有 mcpClients 的 tools, 失败则保存为null */
|
|
38
40
|
private _createMpcTools;
|
|
39
41
|
/** 关闭所有的 clients */
|
|
40
42
|
closeAll(): Promise<void>;
|
|
43
|
+
/** 创建所有的 mcpClients,并更新它们的tools */
|
|
41
44
|
initClientsAndTools(): Promise<void>;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
|
|
45
|
+
/** 全量更新所有的 mcpServers */
|
|
46
|
+
updateMcpServers(mcpServers?: Record<string, McpServerConfig>): Promise<void>;
|
|
47
|
+
/** 插入一个新的mcpServer,如果已经存在则返回false */
|
|
48
|
+
insertMcpServer(serverName: string, mcpServer: McpServerConfig): Promise<boolean>;
|
|
49
|
+
/** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
|
|
50
|
+
removeMcpServer(serverName: string): Promise<void>;
|
|
46
51
|
/** 创建临时允许调用的tools集合 */
|
|
47
52
|
private _tempMergeTools;
|
|
48
53
|
private _chat;
|