Tools 与 Runnable
状态:✅ registry + list_files / read_file / write_file / edit_file / grep / execute_command / todo_write / web_fetch / web_search / browser_open(内置浏览器) MCP / Subagent / 权限见 生态(Phase 4)
Tool 接口
interface Tool<TInput = unknown, TOutput = unknown> {
name: string;
description: string;
inputSchema: JSONSchema;
execute(input: TInput, ctx: ToolContext): Promise<TOutput>;
}
interface ToolContext {
sessionId: string;
callId: string;
signal: AbortSignal;
/** 受限 Context:可 get 白名单服务,不可随意 provide */
runtime: Pick<Context, "get" | "tryGet" | "emit">;
}ToolService
const TOOLS = createServiceKey<ToolService>("tools");
interface ToolService {
register(tool: Tool): void;
list(): Tool[];
listSchemas(): ToolSchema[];
execute(call: ToolCall, ctx: ToolContext): Promise<ToolResult>;
}挂载方式:
runtime.use(
toolsPlugin({
tools: [listFilesTool("."), readFileTool("."), grepTool("."), writeFileTool("."), executeCommandTool(".")],
}),
);execute 需要 ToolContext(sessionId / callId / signal / runtime)。
执行管道
当前实现(packages/tools):
permission.visible / permission.check → execute → tool.finished | tool.failed| 事件 | 何时 |
|---|---|
permission.denied / permission.granted | 配置了权限插件时 |
tool.started | 即将执行 |
tool.finished | 成功结束 |
tool.failed | 未知工具、被拒、或抛错 |
只读 Agent = 不注册写工具,这是硬边界;prompt 只是软提示。权限插件见 生态。
MCP
进程内 ping 与官方 @modelcontextprotocol/client(stdio / 远程 HTTP)都经 McpBackend 适配成同一个 Tool。
runtime.use(mcpPlugin({ backend: inProcessPingBackend() }));
// 多服务时 mcpPlugin 只登记各服务的工具;list_resources / read_resource / get_prompt 由 bootRuntime 按需另挂远程 MCP 的工具与 stdio 一样进注册表。有资源能力时才有 list_resources / read_resource;有提示词能力时才有 get_prompt。tah chat / tah serve 里符合 skill 命名规则的提示词可用 /名字 展开(包在 <mcp_prompt> 里);与 skill 同名时用 skill。
对 Loop 而言没有「MCP 特殊路径」——只有 ToolService.execute。
Runnable:统一 Agent / Tool / Subagent
interface Runnable<I, O> {
readonly name: string;
run(input: I, ctx: Context): Promise<O>;
}| 实现 | 输入 | 输出 |
|---|---|---|
| Tool | 结构化 args | 结构化 result |
| Agent / Session | 自然语言 / 任务 | RunResult |
| Workflow | 工作流参数 | 汇总结果 |
主 Agent 调用子 Agent:
Main Agent
├── Research Agent (Runnable)
├── Coding Agent (Runnable)
└── Review Agent (Runnable)不必单独维护 SubAgentManager;注册表可以是 RunnableRegistry,Tool 只是其中一类。
内置工具候选(Coding Agent)
| Tool | Phase | 说明 |
|---|---|---|
list_files | 2 | 列目录 |
read_file | 2 | 读文件 |
write_file | 2 | 写文件(可配 permission deny) |
edit_file | 0.31 | 按原文替换片段:old_string 必须恰好出现一次,否则报出现次数;replace_all: true 全部替换;需要审批 |
todo_write | 0.31 | 任务清单:每次传整份 todos(content + pending / in_progress / completed,最多 50 项),全量替换;不需要审批。提示词要求 3 步以上的任务使用 |
web_fetch | 0.31 | 读公网网页(拒绝内网 / 回环地址,重定向逐跳检查),HTML 转纯文本,默认 3 万字符、max_chars 最多 10 万;不需要审批。见 tah.config.json |
web_search | 0.31 | 搜索网页,返回标题 / 链接 / 摘要(max_results 1–10,默认 5)和实际用的 engine。0.37.4 起不需要 key:先问 Exa,失败或没结果再查 Bing;配了 Tavily key 只用 Tavily。见下文 |
browser_open | 0.31 | 启动内置浏览器(Playwright MCP + 本机 Chrome),可带 url;浏览器没开时需要审批,已开时直接放行。打开后挂上 18 个 browser_* 工具(browser_navigate / browser_snapshot / browser_find / browser_click / browser_type / browser_fill_form / browser_take_screenshot / browser_close 等),结果超过 3 万字符截断,截图不回传给模型;browser_file_upload 每次审批,browser_close 后撤下这些工具。见 内置浏览器 |
ask_user | 0.37 | 向用户提 1–4 个选择题(每题 2–5 个选项,可多选,用户也能自己输入),等回答后把答案交回模型;只在 tah serve / tah chat 挂,--yes 不影响。见 让用户选择 |
execute_command | 9 | 工作区 cwd 起进程(非 shell)。macOS / Linux 上默认经 sandbox-exec / bwrap 限制文件系统:项目目录可写,家目录其余部分不可读,网络仍可用,环境变量仍继承。--no-sandbox 关掉。助手不在 PATH 时该次调用失败。默认 30 秒超时,模型可按次传 timeoutMs(最长 10 分钟) |
grep | 11 | 工作区正则搜索(跳过 node_modules / dist / 点文件) |
| MCP ping | 4 | inProcessPingBackend |
| MCP stdio / HTTP | 6 | stdioMcpBackend / httpMcpBackend(官方 @modelcontextprotocol/client) |
web_search 的搜索来源(0.37.4)
- 没配 key:查询发给 Exa 的公开 MCP 服务(
mcp.exa.ai,免费但限流,10 秒超时);Exa 出错、限流或 0 条结果时改查 Bing 结果页。两边都失败时报错,并建议用browser_open打开 Bing 搜索页。注意查询内容会发给这两家第三方。 - 配了 Tavily key(
tah serve的 Settings → Web search,或.env的TAVILY_API_KEY):只用 Tavily,失败直接报错,不回退。
想换别的搜索服务,可以在 tah.config.json 里挂第三方 MCP,例如 open-websearch(多引擎,首次 npx 安装较慢):
{
"extensions": {
"mcp": {
"command": "env",
"args": ["MODE=stdio", "npx", "-y", "open-websearch@latest"]
}
}
}env 写法适用于 macOS / Linux。
设计约束
- Tool 名称在 registry 内唯一
inputSchema必须能投影到 LLM function-calling schema- 执行结果进入 Session 事件日志,而不是只活在内存
- 长耗时工具必须尊重
signal