Skip to content
全部文章

AnyRouter Code Mode

AnyRouter 支持 Code Mode。Agent 写一段 JavaScript,调用你的 MCP 工具,只有答案会回来。

AnyRouter 支持 Code Mode

Code Mode 改变 agent 使用工具的方式。没有它时,agent 调用一个工具,读完整个结果,再调用下一个。有了它,agent 写一段 JavaScript。程序调用这些工具,把上一个结果用在下一次调用里,只返回 agent 需要读的部分。

Claude Code 对自己的工具就是这样做的。Cloudflare 把这种程序放在 isolate 里运行,所以代码到不了网络。AnyRouter Code Mode 是同一想法,用在你已经连接的服务器上。程序调用那些服务器。代码在 Cloudflare 的 isolate 里运行。你的密钥不会进入程序。

一段程序,多次调用

你仍然用一句话描述任务。Agent 来写程序。查询、过滤和写入可以是一步。

Illustrative

One tool at a time

12,800 tokens through the model

  1. list_issues(page=1)

    3,200 tokens

    tool call · 40 issues come back

  2. list_issues(page=2)

    3,200 tokens

    tool call · 40 more issues come back

  3. create_page()

    6,400 tokens

    the model copies both lists into this call

  4. …

    another tool call, and the results come back again

Code Mode

140 tokens the model reads

one program

Sandbox

  1. list_issues(page=1)

    3,200 tokens

    40 issues stay here

  2. list_issues(page=2)

    3,200 tokens

    40 issues stay here

  3. create_page()

    uses the lists already here

  • return { count, url }

    20 tokens

    this is the only result the model reads

  • One tool at a time

    12,800 tokens

    3,200 tokens from page 1, 3,200 tokens from page 2, then both lists again inside create_page.

    Code Mode

    140 tokens

    The program is about 120 tokens. The answer is 20 tokens. The 6,400 tokens of issues stay in the sandbox.

    Figure 1: Two pages of 40 issues, about 80 tokens of title and state each. One tool at a time, list_issues(page=1) and list_issues(page=2) each return 3,200 tokens, and create_page() sends both lists again. Code Mode makes the same calls inside the sandbox. The model reads the program and { count, url }. A worked example, not a measurement of a live run.
    一次 Code Mode 运行的函数体。每个名字都是从已连接的 MCP 工具生成的函数。
    const issues = await linear__list_issues({ team: "APP" })
    const open = issues.filter((issue) => issue.state === "open")
    const page = await notion__create_page({
      title: "Open issues",
      body: open.map((issue) => issue.title).join("\n"),
    })
    return { count: open.length, url: page.url }

    Agent 看到的是 { count, url }。它看不到每一条 issue,也不用手把那份列表抄进下一次调用。程序想留一句说明时,console.log 也会回来。

    两件工具完成这件事。search_tools 查找已连接的工具并返回 TypeScript 签名,declare function linear__list_issues(...)。execute 运行 JavaScript。沙箱里已经有这个函数。调用它就是用你的密钥去调用同一个 MCP 工具。目录不会先被装进提示词。

    程序做不到的事

    Isolate 没有自己的网络。每个生成的函数都会回到 AnyRouter,用你的密钥和连接去执行工具,和一次普通的 MCP 调用一样。不是 JavaScript 标识符的名字仍然走 codemode.call。

    • 密钥不能使用的工具不会被注册,所以程序调不到。
    • 程序不能随意 fetch 一个 URL,也看不到密钥。
    • 如果程序返回一个秘密,agent 就会看到它。返回答案,不要返回原始数据。

    这不是 Claude Code 自己的 Code Mode。那个功能在 Claude Code 里面运行,不经过网关。AnyRouter Code Mode 给任何你指到 AnyRouter MCP URL 的 agent 使用。

    打开它

    Code Mode 在你打开之前是关的。在仪表盘的 MCP 页打开 Code mode。或者向 PATCH /api/v1/mcp/config 发送 { "code": true }。在那之前,Code Mode 的 URL 返回 403。

    anyr 仍然连接 search mode。Search mode 一次找一个工具。它不运行程序。你想用 Code Mode 时,自己连接这个 URL。

    任何接受 Streamable HTTP URL 的客户端。
    claude mcp add --transport http anyrouter-codemode \
      "https://anyrouter.dev/api/v1/mcp?codemode=1" \
      --header "Authorization: Bearer $ANYROUTER_API_KEY"

    一次成功的运行花费工作区 $0.01 额度。失败的运行不收费。余额不够时,程序不会开始。