@n0ts123/mcplink-core 0.0.2 → 0.0.3
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/README.md +167 -238
- package/dist/index.d.ts +100 -53
- package/dist/index.js +389 -68
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LanguageModel } from 'ai';
|
|
2
|
+
export { createOpenAI } from '@ai-sdk/openai';
|
|
3
|
+
export { createAnthropic } from '@ai-sdk/anthropic';
|
|
2
4
|
|
|
3
5
|
/** MCP 服务器配置 - stdio 模式 */
|
|
4
6
|
interface MCPServerConfigStdio {
|
|
@@ -18,8 +20,29 @@ interface MCPServerConfigSSE {
|
|
|
18
20
|
/** 请求头 */
|
|
19
21
|
headers?: Record<string, string>;
|
|
20
22
|
}
|
|
23
|
+
/** MCP 服务器配置 - Streamable HTTP 模式 */
|
|
24
|
+
interface MCPServerConfigStreamableHTTP {
|
|
25
|
+
type: 'streamable-http';
|
|
26
|
+
/** HTTP 服务地址 */
|
|
27
|
+
url: string;
|
|
28
|
+
/** 请求头 */
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
}
|
|
21
31
|
/** MCP 服务器配置 */
|
|
22
|
-
type MCPServerConfig = MCPServerConfigStdio | MCPServerConfigSSE;
|
|
32
|
+
type MCPServerConfig = MCPServerConfigStdio | MCPServerConfigSSE | MCPServerConfigStreamableHTTP;
|
|
33
|
+
/**
|
|
34
|
+
* 即时结果匹配器
|
|
35
|
+
* 用于检测 MCP 工具返回的特殊格式数据
|
|
36
|
+
* 当工具返回的 JSON 包含匹配器中所有的 key-value 时,会立即发送 IMMEDIATE_RESULT 事件
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // 匹配包含 { type: "card" } 的返回结果
|
|
40
|
+
* { type: "card" }
|
|
41
|
+
*
|
|
42
|
+
* // 匹配包含 { type: "product_list", isCard: true } 的返回结果
|
|
43
|
+
* { type: "product_list", isCard: true }
|
|
44
|
+
*/
|
|
45
|
+
type ImmediateResultMatcher = Record<string, unknown>;
|
|
23
46
|
/** MCPLink 配置 */
|
|
24
47
|
interface MCPLinkConfig {
|
|
25
48
|
/** AI 模型实例 (Vercel AI SDK) */
|
|
@@ -41,6 +64,37 @@ interface MCPLinkConfig {
|
|
|
41
64
|
* - 'auto': 自动检测(默认)
|
|
42
65
|
*/
|
|
43
66
|
usePromptBasedTools?: boolean | 'auto';
|
|
67
|
+
/**
|
|
68
|
+
* 即时结果匹配器列表
|
|
69
|
+
* 当 MCP 工具返回的结果匹配任意一个匹配器时,会立即发送 IMMEDIATE_RESULT 事件
|
|
70
|
+
* 匹配规则:结果 JSON 中包含匹配器的所有 key 且对应的 value 相等
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* immediateResultMatchers: [
|
|
74
|
+
* { type: "card" }, // 匹配 { type: "card", ... }
|
|
75
|
+
* { type: "product_list" }, // 匹配 { type: "product_list", ... }
|
|
76
|
+
* ]
|
|
77
|
+
*/
|
|
78
|
+
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
79
|
+
/**
|
|
80
|
+
* 是否启用思考阶段(两阶段调用)
|
|
81
|
+
* - true: 每次迭代先让 AI 思考分析,再执行工具调用(默认,推荐)
|
|
82
|
+
* - false: 直接调用 AI 并执行工具(更快但可能不够准确)
|
|
83
|
+
*
|
|
84
|
+
* 启用后的流程:
|
|
85
|
+
* 1. 思考阶段:AI 分析需求,输出思考过程,决定调用什么工具
|
|
86
|
+
* 2. 执行阶段:根据思考结果执行工具调用
|
|
87
|
+
*
|
|
88
|
+
* 优点:
|
|
89
|
+
* - 任何模型都能看到思考过程
|
|
90
|
+
* - Chain-of-Thought 效应,显著提高复杂任务准确性
|
|
91
|
+
*
|
|
92
|
+
* 缺点:
|
|
93
|
+
* - 每次迭代多一次 API 调用,增加延迟和成本
|
|
94
|
+
*
|
|
95
|
+
* @default true
|
|
96
|
+
*/
|
|
97
|
+
enableThinkingPhase?: boolean;
|
|
44
98
|
}
|
|
45
99
|
/** 工具调用 */
|
|
46
100
|
interface ToolCall {
|
|
@@ -85,12 +139,12 @@ declare enum MCPLinkEventType {
|
|
|
85
139
|
TOOL_CALL_START = "tool_call_start",
|
|
86
140
|
/** 工具参数 (流式) */
|
|
87
141
|
TOOL_CALL_DELTA = "tool_call_delta",
|
|
88
|
-
/** 工具调用参数完成 */
|
|
89
|
-
TOOL_CALL_END = "tool_call_end",
|
|
90
142
|
/** 工具正在执行 */
|
|
91
143
|
TOOL_EXECUTING = "tool_executing",
|
|
92
144
|
/** 工具返回结果 */
|
|
93
145
|
TOOL_RESULT = "tool_result",
|
|
146
|
+
/** 工具返回的即时结果(匹配 immediateResultMatchers 时触发) */
|
|
147
|
+
IMMEDIATE_RESULT = "immediate_result",
|
|
94
148
|
/** 开始新一轮迭代 */
|
|
95
149
|
ITERATION_START = "iteration_start",
|
|
96
150
|
/** 迭代结束 */
|
|
@@ -98,15 +152,7 @@ declare enum MCPLinkEventType {
|
|
|
98
152
|
/** 全部完成 */
|
|
99
153
|
COMPLETE = "complete",
|
|
100
154
|
/** 发生错误 */
|
|
101
|
-
ERROR = "error"
|
|
102
|
-
/** TODO 列表开始 */
|
|
103
|
-
TODO_START = "todo_start",
|
|
104
|
-
/** 添加 TODO 项 */
|
|
105
|
-
TODO_ITEM_ADD = "todo_item_add",
|
|
106
|
-
/** 更新 TODO 项状态 */
|
|
107
|
-
TODO_ITEM_UPDATE = "todo_item_update",
|
|
108
|
-
/** TODO 列表完成 */
|
|
109
|
-
TODO_END = "todo_end"
|
|
155
|
+
ERROR = "error"
|
|
110
156
|
}
|
|
111
157
|
/** 事件数据 */
|
|
112
158
|
interface MCPLinkEventData {
|
|
@@ -123,12 +169,8 @@ interface MCPLinkEventData {
|
|
|
123
169
|
argsTextDelta?: string;
|
|
124
170
|
isError?: boolean;
|
|
125
171
|
error?: Error;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
todoItemId?: string;
|
|
129
|
-
todoItemContent?: string;
|
|
130
|
-
todoItemStatus?: TodoItemStatus;
|
|
131
|
-
todoItemResult?: string;
|
|
172
|
+
/** 即时结果数据(原封不动的工具返回结果) */
|
|
173
|
+
immediateResult?: unknown;
|
|
132
174
|
}
|
|
133
175
|
/** 事件 */
|
|
134
176
|
interface MCPLinkEvent {
|
|
@@ -152,14 +194,6 @@ interface ChatCallbacks {
|
|
|
152
194
|
onIterationEnd?: (iteration: number) => void;
|
|
153
195
|
/** 发生错误 */
|
|
154
196
|
onError?: (error: Error) => void;
|
|
155
|
-
/** TODO 列表开始 */
|
|
156
|
-
onTodoStart?: (todoId: string, title: string) => void;
|
|
157
|
-
/** TODO 项添加 */
|
|
158
|
-
onTodoItemAdd?: (todoId: string, item: TodoItem) => void;
|
|
159
|
-
/** TODO 项更新 */
|
|
160
|
-
onTodoItemUpdate?: (todoId: string, itemId: string, status: TodoItemStatus, result?: string) => void;
|
|
161
|
-
/** TODO 列表完成 */
|
|
162
|
-
onTodoEnd?: (todoId: string) => void;
|
|
163
197
|
}
|
|
164
198
|
/** 对话结果 */
|
|
165
199
|
interface ChatResult {
|
|
@@ -204,30 +238,6 @@ interface MCPServerStatus {
|
|
|
204
238
|
tools: MCPTool[];
|
|
205
239
|
error?: string;
|
|
206
240
|
}
|
|
207
|
-
/** TODO 项状态 */
|
|
208
|
-
type TodoItemStatus = 'pending' | 'in_progress' | 'completed' | 'failed';
|
|
209
|
-
/** TODO 项 */
|
|
210
|
-
interface TodoItem {
|
|
211
|
-
/** 唯一标识 */
|
|
212
|
-
id: string;
|
|
213
|
-
/** 任务内容 */
|
|
214
|
-
content: string;
|
|
215
|
-
/** 当前状态 */
|
|
216
|
-
status: TodoItemStatus;
|
|
217
|
-
/** 完成后的结果摘要 */
|
|
218
|
-
result?: string;
|
|
219
|
-
}
|
|
220
|
-
/** TODO 列表 */
|
|
221
|
-
interface TodoList {
|
|
222
|
-
/** 唯一标识 */
|
|
223
|
-
id: string;
|
|
224
|
-
/** 整体任务标题 */
|
|
225
|
-
title: string;
|
|
226
|
-
/** 任务项列表 */
|
|
227
|
-
items: TodoItem[];
|
|
228
|
-
/** 创建时间 */
|
|
229
|
-
createdAt: number;
|
|
230
|
-
}
|
|
231
241
|
|
|
232
242
|
/**
|
|
233
243
|
* MCPLink 主类
|
|
@@ -358,7 +368,7 @@ declare class MCPManager {
|
|
|
358
368
|
* 默认用户提示词
|
|
359
369
|
* 这只是用户自定义的部分,核心工具调用逻辑已内置到代码中
|
|
360
370
|
*/
|
|
361
|
-
declare const DEFAULT_SYSTEM_PROMPT = "\u4F60\u662F\u4E00\u4E2A\u667A\u80FD\u52A9\u624B\uFF0C\
|
|
371
|
+
declare const DEFAULT_SYSTEM_PROMPT = "\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u3001\u53CB\u597D\u7684\u667A\u80FD\u52A9\u624B\u3002\n\n## \u56DE\u590D\u8981\u6C42\n- \u7B80\u6D01\u6E05\u6670\uFF0C\u91CD\u70B9\u7A81\u51FA\n- \u7528\u5217\u8868\u5448\u73B0\u5173\u952E\u4FE1\u606F\n- \u8BED\u6C14\u793C\u8C8C\u81EA\u7136\uFF0C\u50CF\u4E13\u4E1A\u52A9\u624B\n- \u6709\u7ED3\u8BBA\u65F6\u76F4\u63A5\u7ED9\u51FA\uFF0C\u9700\u8981\u8865\u5145\u4FE1\u606F\u65F6\u7B80\u5355\u8BE2\u95EE";
|
|
362
372
|
/**
|
|
363
373
|
* Agent 引擎
|
|
364
374
|
* 负责执行 AI 对话循环,处理工具调用
|
|
@@ -368,18 +378,43 @@ declare class Agent {
|
|
|
368
378
|
private mcpManager;
|
|
369
379
|
private systemPrompt;
|
|
370
380
|
private maxIterations;
|
|
381
|
+
private immediateResultMatchers;
|
|
382
|
+
private parallelToolCalls;
|
|
383
|
+
private enableThinkingPhase;
|
|
371
384
|
constructor(model: LanguageModel, mcpManager: MCPManager, options?: {
|
|
372
385
|
systemPrompt?: string;
|
|
373
386
|
maxIterations?: number;
|
|
387
|
+
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
388
|
+
parallelToolCalls?: boolean;
|
|
389
|
+
enableThinkingPhase?: boolean;
|
|
374
390
|
});
|
|
391
|
+
/**
|
|
392
|
+
* 生成工具描述文本(用于思考阶段)
|
|
393
|
+
*/
|
|
394
|
+
private generateToolsDescription;
|
|
395
|
+
/**
|
|
396
|
+
* 思考阶段的系统提示词
|
|
397
|
+
*/
|
|
398
|
+
private readonly THINKING_PHASE_PROMPT;
|
|
399
|
+
/**
|
|
400
|
+
* 检查工具返回结果是否匹配即时结果匹配器
|
|
401
|
+
* @param result 工具返回的结果
|
|
402
|
+
* @returns 如果匹配返回 true,否则返回 false
|
|
403
|
+
*/
|
|
404
|
+
private matchImmediateResult;
|
|
375
405
|
/**
|
|
376
406
|
* 将 MCP 工具转换为 Vercel AI SDK 格式
|
|
377
407
|
*/
|
|
378
408
|
private convertMCPToolsToAITools;
|
|
379
409
|
/**
|
|
380
|
-
*
|
|
410
|
+
* JSON Schema 到 Zod 的完整递归转换
|
|
411
|
+
* 支持嵌套对象、对象数组、枚举等所有常见类型
|
|
381
412
|
*/
|
|
382
413
|
private jsonSchemaToZod;
|
|
414
|
+
/**
|
|
415
|
+
* 递归转换 JSON Schema 节点为 Zod 类型
|
|
416
|
+
*/
|
|
417
|
+
private convertSchemaToZod;
|
|
383
418
|
/**
|
|
384
419
|
* 执行对话
|
|
385
420
|
*/
|
|
@@ -414,10 +449,22 @@ declare class PromptBasedAgent {
|
|
|
414
449
|
private mcpManager;
|
|
415
450
|
private systemPrompt;
|
|
416
451
|
private maxIterations;
|
|
452
|
+
private immediateResultMatchers;
|
|
453
|
+
private parallelToolCalls;
|
|
454
|
+
private enableThinkingPhase;
|
|
417
455
|
constructor(model: LanguageModel, mcpManager: MCPManager, options?: {
|
|
418
456
|
systemPrompt?: string;
|
|
419
457
|
maxIterations?: number;
|
|
458
|
+
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
459
|
+
parallelToolCalls?: boolean;
|
|
460
|
+
enableThinkingPhase?: boolean;
|
|
420
461
|
});
|
|
462
|
+
/**
|
|
463
|
+
* 检查工具返回结果是否匹配即时结果匹配器
|
|
464
|
+
* @param result 工具返回的结果
|
|
465
|
+
* @returns 如果匹配返回 true,否则返回 false
|
|
466
|
+
*/
|
|
467
|
+
private matchImmediateResult;
|
|
421
468
|
/**
|
|
422
469
|
* 生成工具列表描述
|
|
423
470
|
*/
|
|
@@ -453,4 +500,4 @@ declare class PromptBasedAgent {
|
|
|
453
500
|
}): AsyncGenerator<MCPLinkEvent>;
|
|
454
501
|
}
|
|
455
502
|
|
|
456
|
-
export { Agent, type ChatCallbacks, type ChatResult, DEFAULT_SYSTEM_PROMPT, MCPLink, type MCPLinkConfig, type MCPLinkEvent, type MCPLinkEventData, MCPLinkEventType, MCPManager, type MCPServerConfig, type MCPServerConfigSSE, type MCPServerConfigStdio, type MCPServerStatus, type MCPTool, type Message, type MessageRole, PromptBasedAgent, type
|
|
503
|
+
export { Agent, type ChatCallbacks, type ChatResult, DEFAULT_SYSTEM_PROMPT, type ImmediateResultMatcher, MCPLink, type MCPLinkConfig, type MCPLinkEvent, type MCPLinkEventData, MCPLinkEventType, MCPManager, type MCPServerConfig, type MCPServerConfigSSE, type MCPServerConfigStdio, type MCPServerStatus, type MCPTool, type Message, type MessageRole, PromptBasedAgent, type ToolCall, type ToolResult };
|