goose/documentation/docs/guides/managing-tools/code-mode.md
Elias Posen 8631caa890
Use Port of Context (pctx) for code mode (#6765)
Signed-off-by: Elias Posen <elias@posen.ch>
Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Adrian Cole <adrian@tetrate.io>
2026-02-03 12:15:49 -05:00

3.7 KiB

sidebar_position title sidebar_label description
3 Code Mode Code Mode Understanding how Code Mode handles tool discovery and tool calling

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Code Mode is a method of interacting with MCP tools programmatically instead of calling them directly. Code Mode is particularly useful when working with many enabled extensions, as it can help manage context window usage more efficiently.

:::info This functionality requires the built-in Code Execution extension to be enabled. :::

Code Mode controls how tools are discovered and called:

  • Tools from enabled extensions are discovered on-demand and loaded into context as needed
  • Multiple tool calls are batched in one execution
  • Intermediate results are chained (output from one tool as input to the next)

How Code Mode Works

The Code Execution extension is an MCP server that uses the MCP protocol to expose three foundational meta-tools. When Code Execution is enabled, goose switches to Code Mode. For every request, the LLM writes JavaScript code that goose runs in a sandbox environment to:

  • Discover available tools from your enabled extensions (if needed)
  • Learn how to work with the tools it needs for the current task
  • Call those tools programmatically to complete the task

Traditional vs. Code Mode Tool Calling

Traditional MCP tool calling and Code Mode are two different approaches to the same goal: giving goose access to tools.

Aspect Traditional Code Mode
Tool Discovery All tools from enabled extensions, for example:
developer.shell
developer.text_editor
github.list_issues
github.get_pull_request
slack.send_message
• ... potentially many more
Code Execution extension's meta-tools:
list_functions
get_function_details
execute

The LLM uses these tools to discover tools from other enabled extensions as needed
Tool Calling • Sequential tool calls
• Each result sent to the LLM before the next call
• May require tool discovery calls
• Multiple tool calls batched in one execution
• Intermediate results are chained and processed locally
Context Window Every LLM call includes all tool definitions from enabled extensions Every LLM call includes the 3 meta-tool definitions, plus any tool definitions previously discovered in the session
Best For • 1-3 enabled extensions
• Simple tasks using 1-2 tools
• 5+ extensions
• Well-defined multi-step workflows

:::info Text-Only Results Code Mode only supports text content from tool results. Images, binary data, and other content types are ignored. :::

Additional Resources

import ContentCardCarousel from '@site/src/components/ContentCardCarousel'; import gooseCodeMode from '@site/blog/2025-12-15-code-mode-mcp/header-image.jpg'; import notMcpReplacement from '@site/blog/2025-12-21-code-mode-doesnt-replace-mcp/header-image.png';

<ContentCardCarousel items={[ { type: 'blog', title: 'Code Mode for MCP', description: 'Learn about the code execution approach to MCP tool calling.', thumbnailUrl: gooseCodeMode, linkUrl: '/goose/blog/2025/12/15/code-mode-mcp', date: '2025-12-15', duration: '5 min read' }, { type: 'blog', title: 'Code Mode Doesn't Replace MCP', description: 'Understanding how Code Mode and MCP work together.', thumbnailUrl: notMcpReplacement, linkUrl: '/goose/blog/2025/12/21/code-mode-doesnt-replace-mcp', date: '2025-12-21', duration: '8 min read' } ]} />