代码级联调用
代码级联调用是 code mode 重要使用场景之一,其不再按照单个工具或者 MCP 的方式进行工具调用,而是把 MCP Tool 当作方法进行调用,可以自行编排业务逻辑。可以减少模型往复传输次数而减少大量 token 。
示例场景
在以下示例中 github.list_commits 是 github 官方 MCP 工具,其可以查询某个项的提交记录。
Agent 执行的任务是统计 agents 提交次数,如果单独使用 github.list_commits 工具,模型需要获取每页的请求信息后进行汇总,实际有 1503 提交,按照每页 50 次,也会有 30 页,需要执行 30 次请求后汇总信息。中间会产生大量的 Token 消耗。在很多场景下次数上限是不可控的,一次次请求就变的更加不现实。
在 code mode 模式下, github.list_commits MCP 工具会转化为 API 、模型会生成以下代码,只需要一次请求就可以汇总出数据。
在复杂的业务场景下 code mode 更加方便并且可以节省大量 Token。
async () => { … const owner = "jiweigang1"; const repo = "agents"; const perPage = 50; let page = 1; let total = 0; const authorMap = new Map(); while (true) { const commits = await codemode.github.list_commits({ owner, repo, perPage, page, fields: ["sha", "author"] }); if (!Array.isArray(commits) || commits.length === 0) break; total += commits.length; for (const c of commits) { const login = c.author?.login || c.commit?.author?.name || "Unknown"; authorMap.set(login, (authorMap.get(login) || 0) + 1); } if (commits.length < perPage) break; page++; } return { total, pages: page, byAuthor: Array.from(authorMap.entries()).sort((a, b) => b[1] - a[1]) }; } 项目:jiweigang1/agents默认分支:main总提交次数:1503 提交次数 Top 10(按 GitHub 作者统计): ┌──────┬─────────────────────┬──────────┐ │ 排名 │ 作者 │ 提交次数 │ ├──────┼─────────────────────┼──────────┤ │ 1 │ threepointone │ 917 │ │ 2 │ mattzcarey │ 124 │ │ 3 │ github-actions[bot] │ 119 │ │ 4 │ whoiskatrin │ 92 │ │ 5 │ cjol │ 40 │ │ 6 │ dependabot[bot] │ 24 │ │ 7 │ deathbyknowledge │ 24 │ │ 8 │ ben-reitz │ 15 │ │ 9 │ ghostwriternr │ 13 │ │ 10 │ Muhammad-Bin-Ali │ 10 │ └──────┴─────────────────────┴──────────┘ 你本人(jiweigang1)在这个项目中的提交次数:2 次。
欢迎使用 Hawa Code https://www.hawacode.com/