@ai-zen/agents-sdk 1.2.0-alpha.2 → 2.0.0-alpha.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.
- package/CHANGELOG.md +77 -0
- package/README.md +397 -113
- package/README.zh.md +314 -112
- package/dist/agent-plugins/AssetTransportPlugin.d.ts +51 -0
- package/dist/agent-plugins/AssetTransportPlugin.js +145 -0
- package/dist/agent-plugins/AutoMigratePlugin.d.ts +56 -6
- package/dist/agent-plugins/AutoMigratePlugin.js +64 -15
- package/dist/agent-plugins/AutoRefreshToolsPlugin.d.ts +12 -9
- package/dist/agent-plugins/AutoRefreshToolsPlugin.js +6 -7
- package/dist/agent-plugins/ContextGuardPlugin.d.ts +32 -24
- package/dist/agent-plugins/ContextGuardPlugin.js +37 -22
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.d.ts +38 -46
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.js +37 -50
- package/dist/agent-plugins/UnknownToolHintPlugin.d.ts +15 -1
- package/dist/agent-plugins/UnknownToolHintPlugin.js +15 -1
- package/dist/config/ConfigManager.d.ts +13 -5
- package/dist/config/ConfigManager.js +58 -39
- package/dist/config/constants.d.ts +1 -1
- package/dist/config/constants.js +2 -2
- package/dist/config/factory-skills.js +2 -2
- package/dist/config/normalize.d.ts +93 -0
- package/dist/config/normalize.js +158 -0
- package/dist/crud/AgentRepository.d.ts +3 -1
- package/dist/crud/AgentRepository.js +4 -2
- package/dist/index.d.ts +22 -9
- package/dist/index.js +16 -6
- package/dist/runtime/AgentRuntime.d.ts +293 -0
- package/dist/runtime/AgentRuntime.js +457 -0
- package/dist/runtime/HandoffGuardPlugin.d.ts +85 -16
- package/dist/runtime/HandoffGuardPlugin.js +140 -19
- package/dist/runtime/HandoffService.d.ts +255 -91
- package/dist/runtime/HandoffService.js +392 -177
- package/dist/runtime/RuntimePeerSessions.d.ts +34 -0
- package/dist/runtime/RuntimePeerSessions.js +50 -0
- package/dist/runtime/Scopes.d.ts +123 -0
- package/dist/runtime/Scopes.js +169 -0
- package/dist/runtime/SdkAgent.d.ts +21 -11
- package/dist/runtime/SdkAgent.js +24 -8
- package/dist/runtime/completeOnce.d.ts +3 -3
- package/dist/runtime/completeOnce.js +3 -3
- package/dist/runtime/createAgent.d.ts +29 -7
- package/dist/runtime/createAgent.js +57 -12
- package/dist/runtime/createModel.d.ts +4 -4
- package/dist/runtime/createModel.js +6 -6
- package/dist/runtime/lastPromptTokens.d.ts +18 -0
- package/dist/runtime/lastPromptTokens.js +26 -0
- package/dist/runtime/resolveModel.d.ts +9 -9
- package/dist/runtime/resolveModel.js +8 -8
- package/dist/scope/Scope.d.ts +42 -14
- package/dist/scope/Scope.js +55 -16
- package/dist/scope/ScopePlugin.d.ts +2 -8
- package/dist/scope/disclosure.d.ts +1 -1
- package/dist/scope/disclosure.js +1 -1
- package/dist/scope/plugins/allInOne.d.ts +13 -1
- package/dist/scope/plugins/allInOne.js +9 -5
- package/dist/scope/plugins/builtin/AsyncExecTool.d.ts +12 -0
- package/dist/scope/plugins/builtin/AsyncExecTool.js +103 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.d.ts +10 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.js +90 -0
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +1 -1
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +1 -1
- package/dist/scope/plugins/builtin/DownloadFileTool.js +1 -1
- package/dist/scope/plugins/builtin/ExecTool.js +10 -199
- package/dist/scope/plugins/builtin/FindTextTool.d.ts +1 -1
- package/dist/scope/plugins/builtin/FindTextTool.js +1 -1
- package/dist/scope/plugins/builtin/GenerateImageTool.d.ts +1 -4
- package/dist/scope/plugins/builtin/GenerateImageTool.js +2 -8
- package/dist/scope/plugins/builtin/InspectFileTool.js +1 -1
- package/dist/scope/plugins/builtin/SdkCallbackTool.d.ts +4 -4
- package/dist/scope/plugins/builtin/SdkCallbackTool.js +3 -3
- package/dist/scope/plugins/builtin/SleepTool.js +2 -3
- package/dist/scope/plugins/builtin/ViewImageTool.d.ts +2 -4
- package/dist/scope/plugins/builtin/ViewImageTool.js +4 -6
- package/dist/scope/plugins/builtin/WriteTool.d.ts +7 -0
- package/dist/scope/plugins/builtin/WriteTool.js +8 -1
- package/dist/scope/plugins/builtin/asyncTasks.d.ts +41 -0
- package/dist/scope/plugins/builtin/asyncTasks.js +97 -0
- package/dist/scope/plugins/builtin/commandOutput.d.ts +22 -0
- package/dist/scope/plugins/builtin/commandOutput.js +55 -0
- package/dist/scope/plugins/builtin/commandRun.d.ts +48 -0
- package/dist/scope/plugins/builtin/commandRun.js +169 -0
- package/dist/scope/plugins/builtin/discover.d.ts +1 -3
- package/dist/scope/plugins/builtin/discover.js +1 -3
- package/dist/scope/plugins/builtin/index.d.ts +5 -7
- package/dist/scope/plugins/builtin/index.js +7 -7
- package/dist/scope/plugins/builtin/outputGuard.d.ts +1 -5
- package/dist/scope/plugins/builtin/outputGuard.js +1 -5
- package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +61 -9
- package/dist/scope/plugins/mcp/McpConnectionManager.js +174 -72
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +37 -4
- package/dist/scope/plugins/mcp/McpScopePlugin.js +100 -9
- package/dist/scope/plugins/mcp/discover.d.ts +9 -2
- package/dist/scope/plugins/mcp/discover.js +12 -14
- package/dist/scope/plugins/mcp/mcpTaskNotice.d.ts +52 -0
- package/dist/scope/plugins/mcp/mcpTaskNotice.js +8 -0
- package/dist/scope/plugins/mcp/mcpTools.d.ts +20 -2
- package/dist/scope/plugins/mcp/mcpTools.js +220 -39
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.d.ts +25 -0
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.js +32 -0
- package/dist/scope/plugins/peers/peerSessionTools.d.ts +173 -0
- package/dist/scope/plugins/peers/peerSessionTools.js +292 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +10 -4
- package/dist/scope/plugins/skills/discover.d.ts +15 -8
- package/dist/scope/plugins/skills/discover.js +15 -26
- package/dist/scope/plugins/skills/skillTools.d.ts +9 -8
- package/dist/scope/plugins/skills/skillTools.js +21 -13
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +4 -1
- package/dist/scope/plugins/subagents/discover.d.ts +7 -1
- package/dist/scope/plugins/subagents/discover.js +7 -3
- package/dist/scope/plugins/subagents/subAgentTools.d.ts +0 -1
- package/dist/scope/plugins/subagents/subAgentTools.js +14 -1
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +4 -1
- package/dist/scope/plugins/usertools/discover.d.ts +7 -8
- package/dist/scope/plugins/usertools/discover.js +17 -27
- package/dist/shared/EntityRepository.d.ts +22 -5
- package/dist/shared/EntityRepository.js +45 -29
- package/dist/shared/content-blocks.d.ts +9 -3
- package/dist/shared/content-blocks.js +12 -19
- package/dist/shared/errors.d.ts +8 -3
- package/dist/shared/errors.js +9 -4
- package/dist/shared/logger.d.ts +21 -10
- package/dist/shared/logger.js +9 -15
- package/dist/types/index.d.ts +38 -13
- package/package.json +2 -2
- package/dist/config/migrate.d.ts +0 -115
- package/dist/config/migrate.js +0 -200
- package/dist/scope/plugins/builtin/test-helpers.d.ts +0 -3
- package/dist/scope/plugins/builtin/test-helpers.js +0 -4
|
@@ -1,30 +1,65 @@
|
|
|
1
1
|
import { isAbsolute, join } from "node:path";
|
|
2
2
|
import { discoverMcpServers } from "./discover.js";
|
|
3
|
-
import { createCallMcpTool, createLoadMcpTool, createReadMcpResourceTool } from "./mcpTools.js";
|
|
3
|
+
import { createCallMcpTool, createCancelMcpTaskTool, createGetMcpTaskResultTool, createLoadMcpTool, createReadMcpResourceTool, } from "./mcpTools.js";
|
|
4
4
|
import { McpConnectionManager } from "./McpConnectionManager.js";
|
|
5
|
+
import { CANCEL_MCP_TASK_TOOL, GET_MCP_TASK_RESULT_TOOL } from "./mcpTaskNotice.js";
|
|
5
6
|
/**
|
|
6
7
|
* MCP 插件 —— 发现 mcp.json + 自持连接管理器 + 装配渐进式披露元工具。
|
|
7
8
|
*
|
|
8
9
|
* 渐进式披露:
|
|
9
|
-
* - `load_mcp`
|
|
10
|
-
* - `call_mcp_tool`
|
|
11
|
-
* - `read_mcp_resource`
|
|
12
|
-
*
|
|
10
|
+
* - `load_mcp` 枚举所有允许的 server(含 include_manifest 开关)
|
|
11
|
+
* - `call_mcp_tool` 调用已连接 server 的工具(可带任务参数)
|
|
12
|
+
* - `read_mcp_resource` 读取已连接 server 的资源
|
|
13
|
+
* - `get_mcp_task_result` 按任务 id 取回一个任务的结果
|
|
14
|
+
* - `cancel_mcp_task` 取消一个任务
|
|
15
|
+
* 五个元工具名恒上报 `tools` 维度;`mcpManager` 由本插件私有持有,
|
|
13
16
|
* 有配置路径时在构造时创建,`dispose()` 时统一断开。
|
|
17
|
+
*
|
|
18
|
+
* 每次 `discover()` 除了重读 mcp 文件,还会把连接与发现结果对账一次(见 `reconcileConnections`)。
|
|
14
19
|
*/
|
|
15
20
|
export class McpScopePlugin {
|
|
16
21
|
static ID = "mcp";
|
|
17
22
|
id = McpScopePlugin.ID;
|
|
18
23
|
paths;
|
|
19
24
|
mcpManager;
|
|
25
|
+
/** 日志口(可选):连接管理器与 `load_mcp` 那两句都走它;不注入就不记 */
|
|
26
|
+
logger;
|
|
20
27
|
servers = [];
|
|
28
|
+
/**
|
|
29
|
+
* @param options.taskNotice 任务通知的投递口(可选):不给就是任务状态没有人接,其余照常
|
|
30
|
+
*/
|
|
21
31
|
constructor(options) {
|
|
22
32
|
this.paths = options?.paths ?? [];
|
|
23
|
-
this.
|
|
33
|
+
this.logger = options?.logger;
|
|
34
|
+
this.mcpManager =
|
|
35
|
+
this.paths.length > 0
|
|
36
|
+
? new McpConnectionManager(undefined, undefined, options?.logger, options?.taskNotice)
|
|
37
|
+
: undefined;
|
|
24
38
|
}
|
|
25
39
|
async discover(scope) {
|
|
26
40
|
const files = this.paths.map((p) => (isAbsolute(p) ? p : join(scope.cwd, p)));
|
|
27
|
-
|
|
41
|
+
const discovered = await discoverMcpServers(files, { logger: this.logger });
|
|
42
|
+
await this.reconcileConnections(discovered);
|
|
43
|
+
this.servers = discovered;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 让连接跟着发现结果对账。
|
|
47
|
+
*
|
|
48
|
+
* 连接是按**定义**建立的:声明改了而连接不动,等于「配置改了但实际没生效」。所以
|
|
49
|
+
* - 这次发现里消失的 server → 断开它的连接(连同缓存的状态 / 清单一起失效);
|
|
50
|
+
* - 定义变了的 server → 断开旧连接,下一次调用时按新定义懒连;
|
|
51
|
+
* - 新增的 server → 不主动连(保持懒连接:没人调用就不起进程 / 不建链);
|
|
52
|
+
* - 定义没变的 → 一个字节都不动(连接与清单原样留着)。
|
|
53
|
+
*
|
|
54
|
+
* 于是「改了 mcp 配置」只需刷一次 Scope,不必重建它。
|
|
55
|
+
*/
|
|
56
|
+
async reconcileConnections(discovered) {
|
|
57
|
+
const manager = this.mcpManager;
|
|
58
|
+
if (!manager)
|
|
59
|
+
return;
|
|
60
|
+
for (const id of staleMcpConnections(this.servers, discovered)) {
|
|
61
|
+
await manager.disconnect(id);
|
|
62
|
+
}
|
|
28
63
|
}
|
|
29
64
|
/** 是否配置了 MCP(供外部代码判定,如 UnknownToolHintPlugin) */
|
|
30
65
|
hasConfig() {
|
|
@@ -33,7 +68,13 @@ export class McpScopePlugin {
|
|
|
33
68
|
candidates() {
|
|
34
69
|
return {
|
|
35
70
|
mcps: this.servers.map((s) => s.id),
|
|
36
|
-
tools: [
|
|
71
|
+
tools: [
|
|
72
|
+
"load_mcp",
|
|
73
|
+
"call_mcp_tool",
|
|
74
|
+
"read_mcp_resource",
|
|
75
|
+
GET_MCP_TASK_RESULT_TOOL,
|
|
76
|
+
CANCEL_MCP_TASK_TOOL,
|
|
77
|
+
],
|
|
37
78
|
};
|
|
38
79
|
}
|
|
39
80
|
instantiate(scope, filtered) {
|
|
@@ -44,7 +85,7 @@ export class McpScopePlugin {
|
|
|
44
85
|
const m = this.mcpManager;
|
|
45
86
|
const result = [];
|
|
46
87
|
if (allow.has("load_mcp") && m && filteredMcps.length > 0) {
|
|
47
|
-
result.push(createLoadMcpTool(m, filteredMcps));
|
|
88
|
+
result.push(createLoadMcpTool(m, filteredMcps, this.logger));
|
|
48
89
|
}
|
|
49
90
|
if (allow.has("call_mcp_tool") && m) {
|
|
50
91
|
// env 供 call_mcp_tool 读取 maxToolOutput,与内置工具共用同一上限口径;
|
|
@@ -54,9 +95,59 @@ export class McpScopePlugin {
|
|
|
54
95
|
if (allow.has("read_mcp_resource") && m) {
|
|
55
96
|
result.push(createReadMcpResourceTool(m, scope.env, mcps));
|
|
56
97
|
}
|
|
98
|
+
// 两条任务工具与 call_mcp_tool 同一档位:有配置(`m` 在)且这个 Scope 允许它们
|
|
99
|
+
if (allow.has(GET_MCP_TASK_RESULT_TOOL) && m) {
|
|
100
|
+
result.push(createGetMcpTaskResultTool(m, scope.env, mcps));
|
|
101
|
+
}
|
|
102
|
+
if (allow.has(CANCEL_MCP_TASK_TOOL) && m) {
|
|
103
|
+
result.push(createCancelMcpTaskTool(m, mcps));
|
|
104
|
+
}
|
|
57
105
|
return result;
|
|
58
106
|
}
|
|
59
107
|
async dispose() {
|
|
60
108
|
await this.mcpManager?.disconnectAll();
|
|
61
109
|
}
|
|
62
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* 对账结论:这些 server 的连接必须断掉。
|
|
113
|
+
*
|
|
114
|
+
* 两类:已经不在这次发现里的(配置里删了 / 被禁用),以及定义变了的(连接不能再按旧定义用)。
|
|
115
|
+
* 新增的不在结论里 —— 新增不主动连,保持懒连接。
|
|
116
|
+
*/
|
|
117
|
+
export function staleMcpConnections(previous, discovered) {
|
|
118
|
+
const nextById = new Map(discovered.map((config) => [config.id, config]));
|
|
119
|
+
const stale = [];
|
|
120
|
+
for (const before of previous) {
|
|
121
|
+
const after = nextById.get(before.id);
|
|
122
|
+
if (!after || definitionFingerprint(before) !== definitionFingerprint(after)) {
|
|
123
|
+
stale.push(before.id);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return stale;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 「定义」的指纹:哪些字段变了就算连接不能再用。
|
|
130
|
+
*
|
|
131
|
+
* 算进来的是**连接事实**:transport 以及各 transport 的落地参数(command / args / env /
|
|
132
|
+
* url / headers / oauth)。不算 `description` —— 它只影响 `load_mcp` 给人看的那段文字,
|
|
133
|
+
* 与怎么连无关,改它不该把一条好连接断掉(新描述下一次实例化就带上了)。
|
|
134
|
+
* `id` 也不进指纹:按 id 配对的,同一 id 才比。
|
|
135
|
+
*
|
|
136
|
+
* 键顺序用排序后的规范化 JSON 抹平(同一份配置不同写法不该被当成「变了」)。
|
|
137
|
+
*/
|
|
138
|
+
function definitionFingerprint(config) {
|
|
139
|
+
const { id: _id, description: _description, ...definition } = config;
|
|
140
|
+
return canonicalJson(definition);
|
|
141
|
+
}
|
|
142
|
+
/** 递归按键名排序的 JSON:对象字段顺序不算差异 */
|
|
143
|
+
function canonicalJson(value) {
|
|
144
|
+
if (Array.isArray(value))
|
|
145
|
+
return `[${value.map(canonicalJson).join(",")}]`;
|
|
146
|
+
if (value && typeof value === "object") {
|
|
147
|
+
const entries = Object.entries(value)
|
|
148
|
+
.filter(([, item]) => item !== undefined)
|
|
149
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
150
|
+
return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`).join(",")}}`;
|
|
151
|
+
}
|
|
152
|
+
return JSON.stringify(value) ?? "null";
|
|
153
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type LogSink } from "../../../shared/logger.js";
|
|
1
2
|
import type { McpServerConfig } from "../../../types/index.js";
|
|
2
3
|
/**
|
|
3
4
|
* 从 mcp.json 文件列表中发现所有 MCP server 配置。
|
|
@@ -7,6 +8,12 @@ import type { McpServerConfig } from "../../../types/index.js";
|
|
|
7
8
|
* { "mcpServers": { "server-id": { "type": "stdio", "command": "npx", ... } } }
|
|
8
9
|
* ```
|
|
9
10
|
*
|
|
10
|
-
* 按优先级从高到低传入路径列表,同名 server
|
|
11
|
+
* 按优先级从高到低传入路径列表,同名 server 靠前的文件优先(先到先得)。靠前的文件里
|
|
12
|
+
* **任何一条**占住这个名字的条目都算数,包括 `disabled: true` 的那一条:近层停用即取消远层。
|
|
11
13
|
*/
|
|
12
|
-
|
|
14
|
+
/** 发现 MCP 配置时的选项 */
|
|
15
|
+
export interface DiscoverMcpServersOptions {
|
|
16
|
+
/** 日志口(可选):某个配置文件 parse 不了时报一句;不注入就不记(库缺省沉默) */
|
|
17
|
+
logger?: LogSink;
|
|
18
|
+
}
|
|
19
|
+
export declare function discoverMcpServers(paths: string[], options?: DiscoverMcpServersOptions): Promise<McpServerConfig[]>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { promises as fs } from "node:fs";
|
|
2
|
-
import {
|
|
2
|
+
import { SILENT_LOG } from "../../../shared/logger.js";
|
|
3
3
|
/**
|
|
4
4
|
* 归一化原始 MCP server 配置为内部 McpServerConfig。
|
|
5
5
|
* 处理业界标准中的字段别名差异。
|
|
@@ -22,6 +22,10 @@ function normalizeConfig(id, raw) {
|
|
|
22
22
|
env: raw.env,
|
|
23
23
|
url: raw.url,
|
|
24
24
|
headers: raw.headers,
|
|
25
|
+
// oauth 是对象就原样带上(形状见 McpServerConfig["oauth"]),否则视为没配
|
|
26
|
+
oauth: typeof raw.oauth === "object" && raw.oauth !== null && !Array.isArray(raw.oauth)
|
|
27
|
+
? raw.oauth
|
|
28
|
+
: undefined,
|
|
25
29
|
};
|
|
26
30
|
return config;
|
|
27
31
|
}
|
|
@@ -43,17 +47,8 @@ function normalizeTransport(type, raw) {
|
|
|
43
47
|
return "http";
|
|
44
48
|
return null;
|
|
45
49
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
* 遵循业界标准格式:
|
|
50
|
-
* ```json
|
|
51
|
-
* { "mcpServers": { "server-id": { "type": "stdio", "command": "npx", ... } } }
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* 按优先级从高到低传入路径列表,同名 server 靠前的文件优先(先到先得)。
|
|
55
|
-
*/
|
|
56
|
-
export async function discoverMcpServers(paths) {
|
|
50
|
+
export async function discoverMcpServers(paths, options = {}) {
|
|
51
|
+
const logger = options.logger ?? SILENT_LOG;
|
|
57
52
|
const seen = new Set();
|
|
58
53
|
const items = [];
|
|
59
54
|
for (const path of paths) {
|
|
@@ -74,15 +69,18 @@ export async function discoverMcpServers(paths) {
|
|
|
74
69
|
const rawServer = servers[id];
|
|
75
70
|
if (!rawServer || typeof rawServer !== "object")
|
|
76
71
|
continue;
|
|
72
|
+
// 名字先认领,再决定收不收:写成 `disabled: true` 的那一条也是这一层对这个名字的
|
|
73
|
+
// 发言,远处同名的定义不该越过它生效。放到收进结果之后再认领,停用条目就不占名字
|
|
74
|
+
// —— 人写「别用」,合并出来的却是一条能用的。
|
|
75
|
+
seen.add(id);
|
|
77
76
|
const normalized = normalizeConfig(id, rawServer);
|
|
78
77
|
if (!normalized)
|
|
79
78
|
continue;
|
|
80
|
-
seen.add(id);
|
|
81
79
|
items.push(normalized);
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
82
|
catch (err) {
|
|
85
|
-
|
|
83
|
+
logger.warn(`[sdk] Failed to parse MCP config: ${path} — ${err?.message ?? err}`);
|
|
86
84
|
}
|
|
87
85
|
}
|
|
88
86
|
return items;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Agent } from "@ai-zen/agents-core";
|
|
2
|
+
/** 取任务结果那一条工具的名字:任务通知的正文里要写出它,模型才知道去哪儿取 */
|
|
3
|
+
export declare const GET_MCP_TASK_RESULT_TOOL = "get_mcp_task_result";
|
|
4
|
+
/** 取消任务那一条工具的名字 */
|
|
5
|
+
export declare const CANCEL_MCP_TASK_TOOL = "cancel_mcp_task";
|
|
6
|
+
/** 任务的终态:到这三格才算结束,才有「结果可以取了」这回事 */
|
|
7
|
+
export type McpTaskTerminalStatus = "completed" | "failed" | "cancelled";
|
|
8
|
+
/** 判定终态;中间态(`working` / `input_required`)不是「有事要人动手」,不投递 */
|
|
9
|
+
export declare function isTerminalTaskStatus(status: string): status is McpTaskTerminalStatus;
|
|
10
|
+
/**
|
|
11
|
+
* 一条任务到了终态时要投进 inbox 的事实。
|
|
12
|
+
*
|
|
13
|
+
* 只有事实,没有一句话文案:这段文字会变成会话里的一条消息给人看,而语言与用户可见文案归宿主
|
|
14
|
+
* 那一层(它才知道界面那一侧该用什么语言说话)。
|
|
15
|
+
*/
|
|
16
|
+
export interface McpTaskNotice {
|
|
17
|
+
/** 服务器名(配置里的那个 id) */
|
|
18
|
+
server: string;
|
|
19
|
+
/** 服务器给的任务 id */
|
|
20
|
+
taskId: string;
|
|
21
|
+
/** 终态 */
|
|
22
|
+
status: McpTaskTerminalStatus;
|
|
23
|
+
/** 服务器随状态给的一句说明(它没给就没有这一格) */
|
|
24
|
+
statusMessage?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 任务状态通知的投递口。
|
|
28
|
+
*
|
|
29
|
+
* 服务器推来的状态是「一条通知」,它要落到的地方是发起那次调用的那段会话的 inbox。这条路上两头
|
|
30
|
+
* 都不在 SDK 手里:谁活着、往哪张表写、推给哪个客户端,只有宿主答得出来。所以这里只留一个形状,
|
|
31
|
+
* 由宿主实现,连接管理器只认这两个方法:
|
|
32
|
+
*
|
|
33
|
+
* - {@link McpTaskNoticePort.sessionOf}:发起调用的执行体属于哪一段会话;
|
|
34
|
+
* - {@link McpTaskNoticePort.deliver}:把一条终态通知投进那段会话。
|
|
35
|
+
*
|
|
36
|
+
* SDK 这一侧不记「谁活着」:任务 id 到会话 id 的对应关系留在连接管理器里(进程没了就没了,
|
|
37
|
+
* 任务本身在服务器那边),会话此刻在不在、该不该复活,一概问这个口。
|
|
38
|
+
*/
|
|
39
|
+
export interface McpTaskNoticePort {
|
|
40
|
+
/**
|
|
41
|
+
* 这个执行体属于哪一段会话;不属于任何一段(被派出去的 subagent、一次性执行体)时是
|
|
42
|
+
* `undefined` —— 那种调用发起的任务没有会话可以回报。
|
|
43
|
+
*/
|
|
44
|
+
sessionOf(agent: Agent): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* 把一条终态通知投进那段会话的 inbox。
|
|
47
|
+
*
|
|
48
|
+
* 返回 `false` 表示那段会话已经不在(没有执行体),这条通知被丢掉了 —— 投递不创建、也不复活
|
|
49
|
+
* 会话,丢不丢、记不记日志都由实现那一侧决定,调用方拿到 `false` 只是不再往下做。
|
|
50
|
+
*/
|
|
51
|
+
deliver(sessionId: string, notice: McpTaskNotice): Promise<boolean>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** 取任务结果那一条工具的名字:任务通知的正文里要写出它,模型才知道去哪儿取 */
|
|
2
|
+
export const GET_MCP_TASK_RESULT_TOOL = "get_mcp_task_result";
|
|
3
|
+
/** 取消任务那一条工具的名字 */
|
|
4
|
+
export const CANCEL_MCP_TASK_TOOL = "cancel_mcp_task";
|
|
5
|
+
/** 判定终态;中间态(`working` / `input_required`)不是「有事要人动手」,不投递 */
|
|
6
|
+
export function isTerminalTaskStatus(status) {
|
|
7
|
+
return status === "completed" || status === "failed" || status === "cancelled";
|
|
8
|
+
}
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
+
import { type LogSink } from "../../../shared/logger.js";
|
|
2
3
|
import type { McpConnectionManager } from "./McpConnectionManager.js";
|
|
3
4
|
import type { McpServerConfig, ToolEnv } from "../../../types/index.js";
|
|
4
5
|
/**
|
|
5
6
|
* 创建 load_mcp 工具。
|
|
6
7
|
* server 枚举由 filteredMcps (完整 McpServerConfig[])自动推导,
|
|
7
8
|
* 查找配置也直接使用 filteredMcps,与枚举来源一致。
|
|
9
|
+
*
|
|
10
|
+
* @param logger 日志口(可选):报「连上了、清单多大」那两句走它;不注入就不记(库缺省沉默)
|
|
8
11
|
*/
|
|
9
|
-
export declare function createLoadMcpTool(mcpManager: McpConnectionManager, filteredMcps: McpServerConfig[]): CallbackTool;
|
|
12
|
+
export declare function createLoadMcpTool(mcpManager: McpConnectionManager, filteredMcps: McpServerConfig[], logger?: LogSink): CallbackTool;
|
|
10
13
|
/**
|
|
11
14
|
* 创建 call_mcp_tool 工具。
|
|
12
15
|
* 通过官方 Client.callTool() API 调用 MCP 服务器上的工具。
|
|
13
16
|
*
|
|
14
17
|
* 返回形态:
|
|
15
18
|
* - 仅文本 → 纯字符串;
|
|
16
|
-
* - 含媒体 →
|
|
19
|
+
* - 含媒体 → 内容块数组,按 MCP content 的原始顺序(文本块可能夹在中间);
|
|
20
|
+
* - 任务是任务时 → 一段带任务 id 的 JSON,结果不在这条路上回来(见 {@link taskHandleText})。
|
|
17
21
|
*
|
|
18
22
|
* env 仅用于读取 maxToolOutput(文本输出保护上限),与内置工具口径一致。
|
|
19
23
|
*/
|
|
@@ -22,3 +26,17 @@ export declare function createCallMcpTool(mcpManager: McpConnectionManager, env:
|
|
|
22
26
|
* 创建 read_mcp_resource 工具。
|
|
23
27
|
*/
|
|
24
28
|
export declare function createReadMcpResourceTool(mcpManager: McpConnectionManager, env: ToolEnv, filteredMcps: string[]): CallbackTool;
|
|
29
|
+
/**
|
|
30
|
+
* 创建 get_mcp_task_result 工具:按任务 id 去服务器取回结果。
|
|
31
|
+
*
|
|
32
|
+
* 结果原样交给模型:服务器存下来的就是一次工具调用的结果,与 `call_mcp_tool` 走同一套媒体映射
|
|
33
|
+
* 与输出保护(见 {@link callResultToContent})。
|
|
34
|
+
*/
|
|
35
|
+
export declare function createGetMcpTaskResultTool(mcpManager: McpConnectionManager, env: ToolEnv, filteredMcps: string[]): CallbackTool;
|
|
36
|
+
/**
|
|
37
|
+
* 创建 cancel_mcp_task 工具:让服务器停掉一个已经发起的任务。
|
|
38
|
+
*
|
|
39
|
+
* 回给模型的是任务此刻那一格(`tasks/cancel` 的应答就是这个形状)。终态通知照旧会来:
|
|
40
|
+
* 取消也是终态,到了就把那条状态搁进 inbox。
|
|
41
|
+
*/
|
|
42
|
+
export declare function createCancelMcpTaskTool(mcpManager: McpConnectionManager, filteredMcps: string[]): CallbackTool;
|