@opentiny/next-sdk 0.1.15 → 0.2.1

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.
Files changed (42) hide show
  1. package/WebMcpClient.ts +17 -19
  2. package/WebMcpServer.ts +11 -8
  3. package/agent/AgentModelProvider.ts +495 -2
  4. package/agent/type.ts +9 -3
  5. package/agent/utils/generateReActPrompt.ts +55 -0
  6. package/agent/utils/parseReActAction.ts +34 -0
  7. package/dist/WebMcpClient.d.ts +176 -35
  8. package/dist/WebMcpServer.d.ts +43 -154
  9. package/dist/agent/AgentModelProvider.d.ts +35 -1
  10. package/dist/agent/type.d.ts +9 -2
  11. package/dist/agent/utils/generateReActPrompt.d.ts +9 -0
  12. package/dist/agent/utils/parseReActAction.d.ts +14 -0
  13. package/dist/index.es.dev.js +16154 -12116
  14. package/dist/index.es.js +22208 -19236
  15. package/dist/index.js +2411 -320
  16. package/dist/index.umd.dev.js +16147 -12109
  17. package/dist/index.umd.js +109 -67
  18. package/dist/{mcpsdk@1.23.0.dev.js → mcpsdk@1.25.2.dev.js} +8592 -6902
  19. package/dist/{mcpsdk@1.23.0.es.dev.js → mcpsdk@1.25.2.es.dev.js} +8601 -6911
  20. package/dist/mcpsdk@1.25.2.es.js +16796 -0
  21. package/dist/mcpsdk@1.25.2.js +43 -0
  22. package/dist/transport/ExtensionPageServerTransport.d.ts +1 -2
  23. package/dist/webagent.dev.js +15216 -11451
  24. package/dist/webagent.es.dev.js +15260 -11495
  25. package/dist/webagent.es.js +17923 -15160
  26. package/dist/webagent.js +96 -54
  27. package/dist/webmcp-full.dev.js +9872 -8168
  28. package/dist/webmcp-full.es.dev.js +9870 -8166
  29. package/dist/webmcp-full.es.js +10712 -9513
  30. package/dist/webmcp-full.js +31 -31
  31. package/dist/webmcp.dev.js +666 -640
  32. package/dist/webmcp.es.dev.js +664 -638
  33. package/dist/webmcp.es.js +651 -619
  34. package/dist/webmcp.js +1 -1
  35. package/dist/zod@3.25.76.dev.js +30 -32
  36. package/dist/zod@3.25.76.es.dev.js +28 -30
  37. package/dist/zod@3.25.76.es.js +143 -145
  38. package/dist/zod@3.25.76.js +1 -1
  39. package/package.json +10 -9
  40. package/transport/ExtensionPageServerTransport.ts +2 -4
  41. package/dist/mcpsdk@1.23.0.es.js +0 -15584
  42. package/dist/mcpsdk@1.23.0.js +0 -43
@@ -1,7 +1,7 @@
1
1
  import { MessageChannelServerTransport } from '@opentiny/next';
2
2
  import { McpServer, ToolCallback, RegisteredTool, PromptCallback, RegisteredPrompt, ResourceMetadata, ResourceTemplate, RegisteredResource, ReadResourceCallback, RegisteredResourceTemplate, ReadResourceTemplateCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
3
3
  import { z, ZodObject, ZodLiteral, ZodType, ZodOptional, ZodRawShape, ZodTypeDef } from 'zod';
4
- import { SetLevelRequestSchema, SubscribeRequestSchema, UnsubscribeRequestSchema, ListResourcesRequestSchema, RootsListChangedNotificationSchema, Result, Request, Notification, Implementation, ToolAnnotations, ClientCapabilities, ElicitResult, ElicitRequest, ListRootsRequest, CreateMessageRequest, LoggingMessageNotification, ResourceUpdatedNotification } from '@modelcontextprotocol/sdk/types.js';
4
+ import { Result, Request, Notification, Implementation, ToolAnnotations, ClientCapabilities, ElicitResult, ElicitRequest, ListRootsRequest, CreateMessageRequest, LoggingMessageNotification, ResourceUpdatedNotification, SubscribeRequest, UnsubscribeRequest, SetLevelRequest, ListResourcesRequest, RootsListChangedNotification } from '@modelcontextprotocol/sdk/types.js';
5
5
  import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
6
6
  import { ServerOptions } from '@modelcontextprotocol/sdk/server/index.js';
7
7
  import { RequestOptions, NotificationOptions, RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
@@ -98,7 +98,13 @@ export declare class WebMcpServer {
98
98
  * Sends a ping to the client to check if it is still connected.
99
99
  */
100
100
  ping(): Promise<{
101
- _meta?: Record<string, unknown> | undefined;
101
+ _meta?: {
102
+ [x: string]: unknown;
103
+ progressToken?: string | number | undefined;
104
+ "io.modelcontextprotocol/related-task"?: {
105
+ taskId: string;
106
+ } | undefined;
107
+ } | undefined;
102
108
  }>;
103
109
  /**
104
110
  * Creates a LLM message to be sent to the client.
@@ -110,169 +116,46 @@ export declare class WebMcpServer {
110
116
  content: {
111
117
  type: "text";
112
118
  text: string;
113
- _meta?: Record<string, unknown> | undefined;
114
- } | {
115
- type: "image";
116
- data: string;
117
- mimeType: string;
118
- _meta?: Record<string, unknown> | undefined;
119
- } | {
120
- type: "audio";
121
- data: string;
122
- mimeType: string;
123
- _meta?: Record<string, unknown> | undefined;
124
- } | {
125
- [x: string]: unknown;
126
- type: "tool_use";
127
- name: string;
128
- id: string;
129
- input: {
130
- [x: string]: unknown;
131
- };
132
- _meta?: {
133
- [x: string]: unknown;
134
- } | undefined;
135
- } | {
136
- [x: string]: unknown;
137
- type: "tool_result";
138
- toolUseId: string;
139
- content: ({
140
- type: "text";
141
- text: string;
142
- _meta?: Record<string, unknown> | undefined;
143
- } | {
144
- type: "image";
145
- data: string;
146
- mimeType: string;
147
- _meta?: Record<string, unknown> | undefined;
148
- } | {
149
- type: "audio";
150
- data: string;
151
- mimeType: string;
152
- _meta?: Record<string, unknown> | undefined;
153
- } | {
154
- type: "resource";
155
- resource: {
156
- uri: string;
157
- text: string;
158
- mimeType?: string | undefined;
159
- _meta?: Record<string, unknown> | undefined;
160
- } | {
161
- uri: string;
162
- blob: string;
163
- mimeType?: string | undefined;
164
- _meta?: Record<string, unknown> | undefined;
165
- };
166
- _meta?: Record<string, unknown> | undefined;
167
- } | {
168
- uri: string;
169
- name: string;
170
- type: "resource_link";
171
- description?: string | undefined;
172
- mimeType?: string | undefined;
173
- _meta?: {
174
- [x: string]: unknown;
175
- } | undefined;
176
- icons?: {
177
- src: string;
178
- mimeType?: string | undefined;
179
- sizes?: string[] | undefined;
180
- }[] | undefined;
181
- title?: string | undefined;
182
- })[];
183
- structuredContent?: {
184
- [x: string]: unknown;
119
+ annotations?: {
120
+ audience?: ("user" | "assistant")[] | undefined;
121
+ priority?: number | undefined;
122
+ lastModified?: string | undefined;
185
123
  } | undefined;
186
- isError?: boolean | undefined;
187
124
  _meta?: {
188
125
  [x: string]: unknown;
189
126
  } | undefined;
190
- } | ({
191
- type: "text";
192
- text: string;
193
- _meta?: Record<string, unknown> | undefined;
194
127
  } | {
195
128
  type: "image";
196
129
  data: string;
197
130
  mimeType: string;
198
- _meta?: Record<string, unknown> | undefined;
131
+ annotations?: {
132
+ audience?: ("user" | "assistant")[] | undefined;
133
+ priority?: number | undefined;
134
+ lastModified?: string | undefined;
135
+ } | undefined;
136
+ _meta?: {
137
+ [x: string]: unknown;
138
+ } | undefined;
199
139
  } | {
200
140
  type: "audio";
201
141
  data: string;
202
142
  mimeType: string;
203
- _meta?: Record<string, unknown> | undefined;
204
- } | {
205
- [x: string]: unknown;
206
- type: "tool_use";
207
- name: string;
208
- id: string;
209
- input: {
210
- [x: string]: unknown;
211
- };
143
+ annotations?: {
144
+ audience?: ("user" | "assistant")[] | undefined;
145
+ priority?: number | undefined;
146
+ lastModified?: string | undefined;
147
+ } | undefined;
212
148
  _meta?: {
213
149
  [x: string]: unknown;
214
150
  } | undefined;
215
- } | {
151
+ };
152
+ _meta?: {
216
153
  [x: string]: unknown;
217
- type: "tool_result";
218
- toolUseId: string;
219
- content: ({
220
- type: "text";
221
- text: string;
222
- _meta?: Record<string, unknown> | undefined;
223
- } | {
224
- type: "image";
225
- data: string;
226
- mimeType: string;
227
- _meta?: Record<string, unknown> | undefined;
228
- } | {
229
- type: "audio";
230
- data: string;
231
- mimeType: string;
232
- _meta?: Record<string, unknown> | undefined;
233
- } | {
234
- type: "resource";
235
- resource: {
236
- uri: string;
237
- text: string;
238
- mimeType?: string | undefined;
239
- _meta?: Record<string, unknown> | undefined;
240
- } | {
241
- uri: string;
242
- blob: string;
243
- mimeType?: string | undefined;
244
- _meta?: Record<string, unknown> | undefined;
245
- };
246
- _meta?: Record<string, unknown> | undefined;
247
- } | {
248
- uri: string;
249
- name: string;
250
- type: "resource_link";
251
- description?: string | undefined;
252
- mimeType?: string | undefined;
253
- _meta? /**
254
- * Registers a handler to invoke when this protocol object receives a notification with the given method.
255
- *
256
- * Note that this will replace any previous notification handler for the same method.
257
- */: {
258
- [x: string]: unknown;
259
- } | undefined;
260
- icons?: {
261
- src: string;
262
- mimeType?: string | undefined;
263
- sizes?: string[] | undefined;
264
- }[] | undefined;
265
- title?: string | undefined;
266
- })[];
267
- structuredContent?: {
268
- [x: string]: unknown;
154
+ progressToken?: string | number | undefined;
155
+ "io.modelcontextprotocol/related-task"?: {
156
+ taskId: string;
269
157
  } | undefined;
270
- isError?: boolean | undefined;
271
- _meta?: {
272
- [x: string]: unknown;
273
- } | undefined;
274
- })[];
275
- _meta?: Record<string, unknown> | undefined;
158
+ } | undefined;
276
159
  stopReason?: string | undefined;
277
160
  }>;
278
161
  /**
@@ -289,7 +172,13 @@ export declare class WebMcpServer {
289
172
  name?: string | undefined;
290
173
  _meta?: Record<string, unknown> | undefined;
291
174
  }[];
292
- _meta?: Record<string, unknown> | undefined;
175
+ _meta?: {
176
+ [x: string]: unknown;
177
+ progressToken?: string | number | undefined;
178
+ "io.modelcontextprotocol/related-task"?: {
179
+ taskId: string;
180
+ } | undefined;
181
+ } | undefined;
293
182
  }>;
294
183
  /**
295
184
  * Sends a logging message to the client.
@@ -336,23 +225,23 @@ export declare class WebMcpServer {
336
225
  /**
337
226
  * Registers a handler for the subscribe request.
338
227
  */
339
- onSubscribe(handler: (request: z.infer<typeof SubscribeRequestSchema>, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
228
+ onSubscribe(handler: (request: SubscribeRequest, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
340
229
  /**
341
230
  * Registers a handler for the unsubscribe request.
342
231
  */
343
- onUnsubscribe(handler: (request: z.infer<typeof UnsubscribeRequestSchema>, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
232
+ onUnsubscribe(handler: (request: UnsubscribeRequest, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
344
233
  /**
345
234
  * Registers a handler for the set log level request.
346
235
  */
347
- onSetLogLevel(handler: (request: z.infer<typeof SetLevelRequestSchema>, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
236
+ onSetLogLevel(handler: (request: SetLevelRequest, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
348
237
  /**
349
238
  * Registers a handler for the list tools request.
350
239
  */
351
- onListResources(handler: (request: z.infer<typeof ListResourcesRequestSchema>, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
240
+ onListResources(handler: (request: ListResourcesRequest, extra: RequestHandlerExtra<SendRequestT, SendNotificationT>) => SendResultT | Promise<SendResultT>): void;
352
241
  /**
353
242
  * Registers a handler for the roots list changed notification.
354
243
  */
355
- onRootsListChanged(handler: (notification: z.infer<typeof RootsListChangedNotificationSchema>) => void | Promise<void>): void;
244
+ onRootsListChanged(handler: (notification: RootsListChangedNotification) => void | Promise<void>): void;
356
245
  /**
357
246
  * Close the transport for window.addEventListener('pagehide')
358
247
  */
@@ -32,6 +32,8 @@ export declare class AgentModelProvider {
32
32
  onClientDisconnected?: (serverName: string, reason?: string) => void;
33
33
  /** 缓存 ai-sdk response 中的 多轮会话的上下文 */
34
34
  messages: any[];
35
+ /** 是否使用 ReAct 模式(通过提示词而非 function calling 进行工具调用) */
36
+ useReActMode: boolean;
35
37
  constructor({ llmConfig, mcpServers }: IAgentModelProviderOption);
36
38
  /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
37
39
  private _createOneClient;
@@ -50,11 +52,43 @@ export declare class AgentModelProvider {
50
52
  /** 全量更新所有的 mcpServers */
51
53
  updateMcpServers(mcpServers?: Record<string, McpServerConfig>): Promise<void>;
52
54
  /** 插入一个新的mcpServer,如果已经存在则返回false */
53
- insertMcpServer(serverName: string, mcpServer: McpServerConfig): Promise<false | WebMcpClient | import('ai').experimental_MCPClient | null>;
55
+ insertMcpServer(serverName: string, mcpServer: McpServerConfig): Promise<false | WebMcpClient | import('@ai-sdk/mcp').experimental_MCPClient | null>;
54
56
  /** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
55
57
  removeMcpServer(serverName: string): Promise<void>;
56
58
  /** 创建临时允许调用的tools集合 */
57
59
  private _tempMergeTools;
60
+ /** 生成 ReAct 模式的系统提示词(包含工具描述) */
61
+ private _generateReActSystemPrompt;
62
+ /** 执行 ReAct 模式下的工具调用 */
63
+ private _executeReActToolCall;
64
+ /** ReAct 模式的对话实现 */
65
+ private _chatReAct;
66
+ /**
67
+ * 检查消息内容是否包含图片
68
+ * @param content 消息内容
69
+ * @returns 是否包含图片
70
+ */
71
+ private _messageHasImage;
72
+ /**
73
+ * 从消息中移除图片,但保留文本内容
74
+ * @param message 原始消息
75
+ * @returns 移除图片后的消息(如果只有图片没有文本,返回 null)
76
+ */
77
+ private _removeImageFromMessage;
78
+ /**
79
+ * 构建用于模型调用的消息列表(magentic-ui 风格)
80
+ * 策略:保留所有文本消息,仅限制图片数量(类似 magentic-ui 的 maybe_remove_old_screenshots)
81
+ *
82
+ * @param systemMessage 系统提示词
83
+ * @param allMessages 所有消息历史(包括初始消息和后续对话)
84
+ * @param maxImages 最多保留的图片数量(默认3张)
85
+ * @returns 构建好的消息列表
86
+ */
87
+ private _buildMessagesForModel;
88
+ /** ReAct 模式非流式对话 */
89
+ private _chatReActNonStream;
90
+ /** ReAct 模式流式对话 */
91
+ private _chatReActStream;
58
92
  private _chat;
59
93
  chat(options: Parameters<typeof generateText>[0] & {
60
94
  maxSteps?: number;
@@ -1,6 +1,7 @@
1
1
  import { ProviderV2 } from '@ai-sdk/provider';
2
- import { MCPTransport } from 'ai';
3
- export type { experimental_MCPClient as MCPClient } from 'ai';
2
+ import { experimental_MCPClientConfig as MCPClientConfig } from '@ai-sdk/mcp';
3
+
4
+ export type MCPTransport = MCPClientConfig['transport'];
4
5
  type ProviderFactory = 'openai' | 'deepseek' | ((options: any) => ProviderV2);
5
6
  type LlmFactoryConfig = {
6
7
  /** API密钥 */
@@ -11,6 +12,8 @@ type LlmFactoryConfig = {
11
12
  providerType: ProviderFactory;
12
13
  /** 互斥:当使用 providerType 分支时不允许传入 llm */
13
14
  llm?: never;
15
+ /** 是否使用 ReAct 模式(通过提示词而非 function calling 进行工具调用),默认为 false */
16
+ useReActMode?: boolean;
14
17
  };
15
18
  type LlmInstanceConfig = {
16
19
  /** 自定义 Provider 实例,优先级最高 */
@@ -19,6 +22,8 @@ type LlmInstanceConfig = {
19
22
  apiKey?: never;
20
23
  baseURL?: never;
21
24
  providerType?: never;
25
+ /** 是否使用 ReAct 模式(通过提示词而非 function calling 进行工具调用),默认为 false */
26
+ useReActMode?: boolean;
22
27
  };
23
28
  /** 代理模型提供器的大语言配置对象, 通过 XOR 表达二选一 */
24
29
  export type IAgentModelProviderLlmConfig = LlmFactoryConfig | LlmInstanceConfig;
@@ -37,6 +42,7 @@ export type McpServerConfig = {
37
42
  sessionId: string;
38
43
  useAISdkClient?: boolean;
39
44
  } | {
45
+ type: 'local';
40
46
  transport: MCPTransport;
41
47
  useAISdkClient?: boolean;
42
48
  };
@@ -47,3 +53,4 @@ export interface IAgentModelProviderOption {
47
53
  /** Mcp Server的配置对象的集合,键为服务器名称,值为配置对象 */
48
54
  mcpServers?: Record<string, McpServerConfig>;
49
55
  }
56
+ export {};
@@ -0,0 +1,9 @@
1
+ import { ToolSet } from 'ai';
2
+
3
+ /**
4
+ * 生成 ReAct 模式的工具描述提示词(统一使用 XML 格式)
5
+ * 将工具集合转换为 ReAct 格式的文本描述,用于添加到系统提示词中
6
+ * @param tools - 工具集合对象
7
+ * @returns 格式化的工具描述字符串
8
+ */
9
+ export declare function generateReActToolsPrompt(tools: ToolSet): string;
@@ -0,0 +1,14 @@
1
+ import { ToolSet } from 'ai';
2
+
3
+ /**
4
+ * 解析 ReAct 格式的工具调用
5
+ * 从模型输出文本中提取工具名称和参数
6
+ * 现在统一使用 XML 格式(<call> 标签),同时保留对其他格式的兼容性支持
7
+ * @param text - 模型输出的文本
8
+ * @param availableTools - 可用的工具集合,用于验证工具名称
9
+ * @returns 解析出的工具调用信息,如果未找到则返回 null
10
+ */
11
+ export declare function parseReActAction(text: string, availableTools: ToolSet): {
12
+ toolName: string;
13
+ arguments: any;
14
+ } | null;