Skip to main content

Code Mode MCP in goose

· 4 min read
Alex Hancock
Software Engineer

code mode MCP in goose!

There is an emerging approach to MCP tool calling referred to as "sandbox mode" or "code mode". These ideas were initially presented by Cloudflare in their Code Mode: the better way to use MCP post and Anthropic in their Code execution with MCP: Building more efficient agents posts. Since the approach and the benefits are clearly laid out in those posts I will summarize them here.

The approach

Summary

  • Instead of exposing the tools directly to the model an MCP client application can:
    • Generate a programmatic interface to these same tools (typically JS or TS powered)
    • Provide a limited set of tools to the model (search available modules/tool source code, read the source code for a tool, and then a tool to execute some code)
    • Run the code the model generates to call the programmatic API in a sandboxed environment for safety

Benefits

  • The model can progressively discover relevant tools, without all server and tool definitions in the context window from the beginning
  • The model can chain tool call results into inputs to further tool calls without the intermediate results needing to flow back to the model - this saves on tokens and avoids exposing potentially sensitive data to the model unnecessarily
  • The models pre-training datasets have made them very efficient at analyzing large programmatic APIs and writing code to call them, as compared to having been trained only on contrived examples of MCP tool calling

In goose

In v1.17.0 of goose, we've introduced an open source implementation of this idea in a new platform extension called: Code Execution. Our implementation generates a JavaScript interface representing the connected MCP tools and then lets the model write code to run against it in boa which is an embeddable Javascript engine. One neat feature of boa we were able to take advantage of was the concept of NativeFunction.

In boa, a NativeFunction is something which exposes a function in the embedded JavaScript environment which calls back into a natively implemented rust function. This is perfect for the calls originating in JS and then routing the tool call to the underlying MCP server with ease!

Help us evaluate it

Our hope is that we improve tool calling performance and handling of large numbers of tools in goose, but also provide an open source implementation of this emerging approach.

  • Try out the feature by enabling the "Code Execution" extension in v1.17.0 or later of goose by clicking extensions on the left side of the desktop app or running goose configure on cli
  • Please give us feedback on how it works for you by joining our discord.
  • The source code is here

Kudos to my colleague Mic Neale for collaborating with me on the implementation!