Code mode.
Code mode for MCP. Rather than loading every tool definition of its MCP servers, an agent gets one small MCP server with three tools:
searchfinds tools and shows their TypeScript signatures.executeruns the agent’s TypeScript in a Deno process with no file, network, environment or subprocess permission. Its only way out isawait tools.<server>.<tool>(input), which the gateway checks and makes. A large result is written to a file in the agent’s checkout, and the agent reads what it needs.skilllists and loads skills (folders with aSKILL.md), when the agent has any.
The package stands alone: it depends on mcp only and imports nothing of k0rtex. The k0rtex
worker embeds it in k0rtex_worker/gateway.
Using it #
It needs Deno 2 on the machine, and MCP servers that speak stateless streamable HTTP.
from pathlib import Path
from k0rtex_codemode import Agent, Gateway, Host, Upstream
class Rules(Host):
def decide(self, agent, server, tool):
return "deny" if tool.startswith("delete_") else "allow"
gateway = Gateway(
[Agent("gateway_reviewer", skills=["review"])],
[Upstream("github", "https://mcp.example.com/github", {"Authorization": "Bearer …"})],
checkout=Path("/work/checkout"),
deno="/usr/bin/deno",
host=Rules(),
skills_root=Path("/work/skills"),
)
await gateway.start()
# Give the harness gateway.commands["gateway_reviewer"] as a stdio MCP server, run the
# agent, then:
await gateway.close()Each Agent gets its own server, so the host can decide per agent. commands holds what a
harness starts for each: relay.py, which only pipes stdio to a Unix socket of the gateway.
The upstream servers’ credentials stay in the embedding process.
The host #
Subclass Host to decide and to follow what agent code does. The default allows every call.
| Method | When |
|---|---|
decide(agent, server, tool) |
Before a call, and to leave denied tools out of search: "allow", "ask" or "deny" |
permit(call) |
A call decide answered with "ask". The code’s clock stands still meanwhile. |
started(call, verdict), ended(call, status, output) |
Around every call that was not denied |
Layout #
| Module | Role |
|---|---|
server.py |
Gateway: the per-agent MCP servers and the three tools |
catalog.py |
Lists, ranks, describes and calls the upstream tools |
sandbox.py, runner.ts |
The Deno process and the JSON lines it exchanges with the gateway. Never add an --allow-* flag. |
results.py |
Result files under <checkout>/<results_root>/results/, a folder that ignores itself |
skills.py |
Lists and loads skills |
relay.py |
What a harness runs; started with python -I, so it imports nothing of the checkout |
Limits #
One execution runs for 60 seconds unless it asks for more (at most 300), in 128 MB, and makes at most 50 tool calls. A result over about 4,000 characters goes to a file; a file holds at most 1 MB and a gateway keeps at most 20 of them.
This page is backend/packages/codemode/README.md in the k0rtex repository. Suggest a change.