Hybrid tools, state & resources¶
Hybrid mode¶
Some tools are cheap, very common and simple, such as get_me or get_current_time.
For those, a direct tool call can be more convenient than writing code. expose_tools
exposes them in addition to run_code:
They appear to the LLM as github__get_me etc., with their original JSON Schema, and
return the upstream result unchanged. They are still available inside run_code, and
require_approval and usage tracking apply to them too.
Tip
Every exposed tool adds its schema to every request. Keep the list short. The point
of Code Mode is that the rest are reachable through run_code.
Persistent state¶
Models work the way people do in a notebook: fetch something, look at it, then process it in the next script. If every script starts from nothing, the model has to fetch everything again each time, or writes a follow-up that fails because its variable is gone. In our benchmark that was the most common mistake after the first fix.
In the Python agent loops this is handled for you. run_openai and run_anthropic
keep variables for the length of one conversation and clear them when it ends. In your own
loop, wrap the conversation:
In the MCP proxy it is off by default, because the proxy cannot tell where one conversation ends and the next begins. In our benchmark turning it on made no measurable difference, because a script that uses a variable from an earlier script is told what happened and corrects itself. It is still convenient when you work through data over several turns, and safe when the proxy serves a single user over stdio. With
variables survive between calls, so the model can fetch data once and explore it over several turns:
# call 1
issues = await github.list_issues(owner="me", repo="app", state="open")
len(issues)
# call 2
[i["title"] for i in issues if "bug" in [l["name"] for l in i["labels"]]]
Points to know:
- State is lost if a run times out, because the sandbox is reset. The error message says so.
- From Python,
await cm.reset_state()clears it. - With the HTTP transport, all clients of one server process share the same state.
Resources¶
Inside run_code, upstream MCP resources are available through the resources helper:
items = await resources.list() # all servers
items = await resources.list(server="docs") # one server
text = await resources.read("docs://handbook/onboarding")
list returns [{server, uri, name, description, mime_type}]. read returns the text,
or {uri, mime_type, blob, size_bytes} for binary content (a list if there are several
parts).
In MCP server mode, resources and resource templates are also passed through to the client unchanged, so users can attach them as usual.
Prompts¶
Upstream prompts are passed through to the client, so slash commands and prompt pickers
keep working. If two servers define a prompt with the same name, they're exposed as
server__name.
What isn't passed through (yet)¶
- Sampling and elicitation started by upstream servers: codemode-mcp's own approval elicitation works, but requests made by upstream servers aren't forwarded.
- Resource subscriptions and list-changed notifications: restart the proxy if an upstream server's tool list changes.
- Binary content in the model's view: images and audio returned by tools reach your
code as base64 data (
{"type": "image", "mime_type", "data", "size_bytes"}), but the model sees only what your code returns.