@opentiny/next-sdk 0.0.1-alpha.5 → 0.1.0-alpha.2
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 +21 -0
- package/README.md +13 -322
- package/WebMcpClient.ts +497 -0
- package/WebMcpServer.ts +422 -0
- package/agent/AgentModelProvider.ts +69 -0
- package/agent/type.ts +23 -0
- package/agent/utils/aiProviderFactories.ts +8 -0
- package/agent/utils/index.ts +37 -0
- package/dist/index.cjs.js +40227 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.es.js +40227 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +40621 -0
- package/dist/index.umd.js.map +1 -0
- package/index.ts +20 -0
- package/package.json +21 -25
- package/tsconfig.json +14 -0
- package/vite.config.ts +19 -0
- package/dist/error.d.ts +0 -9
- package/dist/index.d.ts +0 -7
- package/dist/index.mjs +0 -20
- package/dist/mcp-host/core/index.d.ts +0 -59
- package/dist/mcp-host/core/index.mjs +0 -259
- package/dist/mcp-host/deepseek/index.d.ts +0 -7
- package/dist/mcp-host/deepseek/index.mjs +0 -9
- package/dist/mcp-host/index.d.ts +0 -4
- package/dist/mcp-host/index.mjs +0 -8
- package/dist/next-client/index.d.ts +0 -1708
- package/dist/next-client/index.mjs +0 -61
- package/dist/next-server/index.d.ts +0 -1098
- package/dist/next-server/index.mjs +0 -45
- package/dist/plugins/connectMcpServer.d.ts +0 -3
- package/dist/plugins/connectMcpServer.mjs +0 -11
- package/dist/plugins/createInMemoryTransport.d.ts +0 -3
- package/dist/plugins/createInMemoryTransport.mjs +0 -8
- package/dist/plugins/createMessageChannelTransport.d.ts +0 -3
- package/dist/plugins/createMessageChannelTransport.mjs +0 -7
- package/dist/plugins/createProxy.d.ts +0 -10
- package/dist/plugins/createProxy.mjs +0 -27
- package/dist/type.d.ts +0 -266
- package/dist/type.mjs +0 -4
- package/dist/utils/dom.d.ts +0 -6
- package/dist/utils/dom.mjs +0 -12
- package/dist/utils/index.d.ts +0 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-present, OpenTiny contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,322 +1,13 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- `createInMemoryTransport.ts`:内存传输实现,便于本地调试和测试
|
|
15
|
-
- `createMessageChannelTransport.ts`:基于 MessageChannel 的多窗口 iframe 通信的 transport 实现
|
|
16
|
-
- `createProxy.ts`:创建代理对象和孪生 Transport,支持 stream/sse 两种传输方式
|
|
17
|
-
- `index.ts`:导出所有客户端模块
|
|
18
|
-
|
|
19
|
-
- `server/`:服务端相关能力
|
|
20
|
-
- `createServer.ts`:创建服务端实例,提供事件监听和处理能力
|
|
21
|
-
- `index.ts`:导出所有服务端模块
|
|
22
|
-
|
|
23
|
-
- `mcp-host/`:多模型能力平台(MCP)相关实现
|
|
24
|
-
- `core/`:核心能力实现
|
|
25
|
-
- `deepseek/`:DeepSeek LLM 接入实现
|
|
26
|
-
- `index.ts`:统一导出 MCP 相关能力
|
|
27
|
-
|
|
28
|
-
- `utils/`:工具方法
|
|
29
|
-
- `dom.ts`:DOM 相关工具方法
|
|
30
|
-
- `index.ts`:导出所有工具方法
|
|
31
|
-
|
|
32
|
-
- `type.ts`:类型定义文件
|
|
33
|
-
- 包含客户端、服务端、传输方式等相关类型定义
|
|
34
|
-
- 提供完整的 TypeScript 类型支持
|
|
35
|
-
|
|
36
|
-
- `index.ts`:根入口文件
|
|
37
|
-
- 统一导出客户端和服务端模块
|
|
38
|
-
- 方便用户引入使用
|
|
39
|
-
|
|
40
|
-
---
|
|
41
|
-
|
|
42
|
-
## 1. 客户端能力
|
|
43
|
-
|
|
44
|
-
### 1.1 NextClient 类
|
|
45
|
-
|
|
46
|
-
`NextClient` 封装了底层 Client 实例,提供事件注册、插件机制、连接等能力。
|
|
47
|
-
|
|
48
|
-
#### 创建 NextClient 实例
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
import { createClient } from '@opentiny/next-sdk'
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// 通过 createClient 创建 NextClient 实例,便于后续扩展和事件注册。
|
|
55
|
-
const client = createClient({name: 'next-sdk',version: '1.0.0'})
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### 1.2 注册插件
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
// 通过插件机制扩展 client 能力,plugin函数执行返回的是一个接收 client 实例的函数。
|
|
62
|
-
await client.use(plugin({options}))
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
#### 1.2.1 createInMemoryTransport
|
|
66
|
-
|
|
67
|
-
此插件用于本地或跨 iframe 的消息通道通信。
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
import { createInMemoryTransport } from '@opentiny/next-sdk'
|
|
71
|
-
|
|
72
|
-
// 设置为 InMemory 通信,适合前端多窗口/iframe 场景。
|
|
73
|
-
await client.use(createInMemoryTransport())
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
#### 1.2.2 createMessageChannelTransport
|
|
77
|
-
|
|
78
|
-
此插件用于本地或跨 iframe 的消息通道通信。
|
|
79
|
-
|
|
80
|
-
```ts
|
|
81
|
-
import { createMessageChannelTransport } from '@opentiny/next-sdk'
|
|
82
|
-
|
|
83
|
-
// 设置为 MessageChannel 通信,适合前端多窗口/iframe 场景。
|
|
84
|
-
await client.use(createMessageChannelTransport({ endpoint, globalObject }))
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
#### 1.2.3 createClientProxy
|
|
88
|
-
|
|
89
|
-
此插件用于通过 stream 或 sse 代理方式与服务端通信。
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
import { createClientProxy } from '@opentiny/next-sdk'
|
|
93
|
-
|
|
94
|
-
// 根据 type 选择 stream 或 sse 方式,自动管理 session 和连接关闭。
|
|
95
|
-
await client.use(createClientProxy({
|
|
96
|
-
type: 'stream', // 或 'sse'
|
|
97
|
-
url: 'https://agent-server.com/mcp',
|
|
98
|
-
token: 'your-token',
|
|
99
|
-
sessionId: 'xxx'
|
|
100
|
-
}))
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
#### 1.2.4 connectMcpServer
|
|
104
|
-
|
|
105
|
-
此插件用于遥控器端或者业界通用的 `MCP HOST` 直接连接 MCP 服务端,支持 stream/sse。
|
|
106
|
-
|
|
107
|
-
```ts
|
|
108
|
-
import { connectMcpServer } from 'next-sdk'
|
|
109
|
-
|
|
110
|
-
// 直接配置服务端地址和鉴权信息,快速建立连接。
|
|
111
|
-
await client.use(connectMcpServer({
|
|
112
|
-
type: 'stream', // 或 'sse'
|
|
113
|
-
url: 'https://agent-server.com/mcp?sessionId=xxx',
|
|
114
|
-
token: 'your-token',
|
|
115
|
-
}))
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### 1.3 事件注册
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
// 支持注册通知事件(如 toolListChanged、resourceUpdated 等)和请求事件(如 createMessage、listRoots 等)。
|
|
122
|
-
|
|
123
|
-
// 监听工具变化事件
|
|
124
|
-
client.on('toolListChanged', callback)
|
|
125
|
-
// 监听资源变化事件
|
|
126
|
-
client.on('resourceUpdated', callback)
|
|
127
|
-
// 监听日志发送事件
|
|
128
|
-
client.on('loggingMessage', callback)
|
|
129
|
-
// 监听listRoots
|
|
130
|
-
client.on('listRoots', callback)
|
|
131
|
-
// 监听创建消息,并返回大模型响应
|
|
132
|
-
client.on('createMessage', callback)
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
#### 1.4 连接
|
|
136
|
-
|
|
137
|
-
```ts
|
|
138
|
-
// 建立与服务端的连接,具体传输方式由 插件的 transport 决定。
|
|
139
|
-
await client.connectTransport()
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
## 2. 服务端能力
|
|
145
|
-
|
|
146
|
-
### 2.1 NextServer 类
|
|
147
|
-
|
|
148
|
-
`NextServer` 封装了底层 McpServer 实例,提供事件注册、插件机制、连接等能力。
|
|
149
|
-
|
|
150
|
-
#### 创建 NextServer 实例
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
import { createServer } from '@opentiny/next-sdk'
|
|
154
|
-
|
|
155
|
-
// 通过 createServer 创建 NextServer 实例。
|
|
156
|
-
const server = createServer({name: 'next-sdk', version: '1.0.0'})
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### 2.2 注册插件
|
|
160
|
-
|
|
161
|
-
```ts
|
|
162
|
-
// 通过插件机制扩展 server 能力,plugin函数执行返回的是一个接收 server 实例的函数。
|
|
163
|
-
await server.use(plugin({options}))
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
#### 2.2.1 createInMemoryTransport
|
|
167
|
-
|
|
168
|
-
此插件用于本地或跨 iframe 的消息通道通信。
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
import { createInMemoryTransport } from '@opentiny/next-sdk'
|
|
172
|
-
|
|
173
|
-
// 设置为 InMemory 通信,适合前端多窗口/iframe 场景。
|
|
174
|
-
await server.use(createInMemoryTransport())
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
#### 2.2.2 createMessageChannelTransport
|
|
178
|
-
|
|
179
|
-
此插件用于本地或跨 iframe 的消息通道通信。
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
import { createMessageChannelTransport } from '@opentiny/next-sdk'
|
|
183
|
-
|
|
184
|
-
// 设置为 MessageChannel 通信,适合前端多窗口/iframe 场景。
|
|
185
|
-
await server.use(createMessageChannelTransport({ endpoint, globalObject }))
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
#### 2.2.3 createServerProxy
|
|
189
|
-
|
|
190
|
-
此插件用于通过 stream 或 sse 代理方式与客户端通信。
|
|
191
|
-
|
|
192
|
-
```ts
|
|
193
|
-
import { createServerProxy } from '@opentiny/next-sdk'
|
|
194
|
-
|
|
195
|
-
// 根据 type 选择 stream 或 sse 方式,自动管理 session 和连接关闭。
|
|
196
|
-
await server.use(createServerProxy())
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
### 2.3 事件注册
|
|
200
|
-
|
|
201
|
-
```ts
|
|
202
|
-
// 注册服务端请求事件(如 subscribe、unsubscribe、listResources 等)。
|
|
203
|
-
|
|
204
|
-
// 监听订阅资源
|
|
205
|
-
server.on('subscribe', callback)
|
|
206
|
-
// 监听取消订阅资源
|
|
207
|
-
server.on('unsubscribe', callback)
|
|
208
|
-
// 监听设置日志级别
|
|
209
|
-
server.on('setLogLevel', callback)
|
|
210
|
-
// 监听ping
|
|
211
|
-
server.on('ping', callback)
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### 2.4 连接
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
// 建立服务端监听,支持 messageChannel 等多种传输方式。
|
|
218
|
-
await server.connectTransport()
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
---
|
|
222
|
-
|
|
223
|
-
## 3. 典型用法示例 -- (前端工程MCP 服务端、agent-server代理服务器、遥控端)
|
|
224
|
-
|
|
225
|
-
### 3.1 前端工程(App.vue)通过 createProxy 代理 stream 方式连接agent-server代理服务器
|
|
226
|
-
|
|
227
|
-
```ts
|
|
228
|
-
import { createClient, createClientProxy } from '@opentiny/next-sdk'
|
|
229
|
-
|
|
230
|
-
const nextClient = createClient({name: 'next-sdk',version: '1.0.0'})
|
|
231
|
-
|
|
232
|
-
const { sessionId, transport } = await nextClient.use(createClientProxy({
|
|
233
|
-
type: 'stream',
|
|
234
|
-
url: 'https://agent-server.com/mcp',
|
|
235
|
-
sessionId: 'xxx',
|
|
236
|
-
token: 'your-token'
|
|
237
|
-
}))
|
|
238
|
-
|
|
239
|
-
await client.connectTransport()
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
### 3.2 MCP 服务端(子路由页面)定义工具或者监听请求
|
|
243
|
-
|
|
244
|
-
```ts
|
|
245
|
-
import { createServer,createServerProxy } from '@opentiny/next-sdk'
|
|
246
|
-
|
|
247
|
-
const server = createServer({name: 'next-sdk',version: '1.0.0'})
|
|
248
|
-
|
|
249
|
-
server.use(createServerProxy())
|
|
250
|
-
|
|
251
|
-
server.tool('get-weather', 'Get the weather of a city', { value: z.string() }, async ({ value }) => {
|
|
252
|
-
return { content: [{ type: 'text', text: `The weather of ${value} is sunny` }] }
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
server.on('subscribe', callback)
|
|
256
|
-
|
|
257
|
-
await server.connectTransport()
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
### 3.3 远程遥控端(可以是手机、平板、电脑等)通过 connectMcpServer 连接agent-server代理服务器
|
|
261
|
-
|
|
262
|
-
```ts
|
|
263
|
-
import { createClient, connectMcpServer } from '@opentiny/next-sdk'
|
|
264
|
-
|
|
265
|
-
const client = createClient({name: 'next-sdk',version: '1.0.0'})
|
|
266
|
-
|
|
267
|
-
await client.use(connectMcpServer({
|
|
268
|
-
type: 'stream',
|
|
269
|
-
url: 'https://agent-server.com/mcp?sessionId=xxx',
|
|
270
|
-
token: 'your-token'
|
|
271
|
-
}))
|
|
272
|
-
|
|
273
|
-
await client.connectTransport()
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
## 4. 典型用法示例 -- (前端工程MCP 服务端、iframe 帮助文档遥控端)
|
|
277
|
-
|
|
278
|
-
### 4.1 iframe 帮助文档遥控端通过 messageChannel 连接前端工程
|
|
279
|
-
|
|
280
|
-
```ts
|
|
281
|
-
import { createClient, createMessageChannelTransport } from '@opentiny/next-sdk'
|
|
282
|
-
|
|
283
|
-
const client = createClient({name: 'next-sdk',version: '1.0.0'})
|
|
284
|
-
|
|
285
|
-
client.use(createMessageChannelTransport({
|
|
286
|
-
endpoint: 'endpoint',
|
|
287
|
-
globalObject: window.parent
|
|
288
|
-
}))
|
|
289
|
-
|
|
290
|
-
await client.connectTransport()
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### 4.2 前端工程通过 messageChannel 连接 iframe 帮助文档遥控端,并定义工具或者监听请求
|
|
294
|
-
|
|
295
|
-
```ts
|
|
296
|
-
import { createServer, createMessageChannelTransport } from '@opentiny/next-sdk'
|
|
297
|
-
|
|
298
|
-
const server = createServer({name: 'next-sdk',version: '1.0.0'})
|
|
299
|
-
|
|
300
|
-
server.use(createMessageChannelTransport({
|
|
301
|
-
endpoint: 'endpoint',
|
|
302
|
-
globalObject: window
|
|
303
|
-
}))
|
|
304
|
-
|
|
305
|
-
server.tool('get-weather', 'Get the weather of a city', { value: z.string() }, async ({ value }) => {
|
|
306
|
-
return { content: [{ type: 'text', text: `The weather of ${value} is sunny` }] }
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
server.on('subscribe', callback)
|
|
310
|
-
|
|
311
|
-
await server.connectTransport()
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
---
|
|
315
|
-
|
|
316
|
-
## 5. 其他说明
|
|
317
|
-
|
|
318
|
-
- 所有通信方式均支持插件机制,便于灵活扩展。
|
|
319
|
-
- 支持多种事件注册,覆盖通知和请求两大类。
|
|
320
|
-
- 传输方式可根据实际场景选择(如 messageChannel 适合前端多窗口,stream/sse 适合前后端通信)。
|
|
321
|
-
|
|
322
|
-
---
|
|
1
|
+
# OpenTiny NEXT SDK
|
|
2
|
+
|
|
3
|
+
OpenTiny NEXT SDK
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @opentiny/next-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 许可证
|
|
12
|
+
|
|
13
|
+
MIT
|