Search⌘ K
AI Features

Code Execution with MCP and Write-and-Execute Offloading

Explore how to use MCP write-and-execute tools to run verbose scripts on a server, retain full logs, and return compact handoffs. Measure when offloading reduces token usage, validate response contracts, and identify the security limits required for model-written code.

We'll cover the following...

In the last two lessons, workers handed results through a shared state, and MCP servers provided their tools. The analyst worker computed the incident’s impact and returned two numbers. But real analysis code rarely prints just two numbers. A careful script checks its work several ways and prints every step, so a person can follow it. If that output comes back into the agent’s context, all of those steps come with it.

In this lesson, we’ll measure that cost. Then we’ll build a write-and-execute tool on an MCP server: it saves the code to a file, runs it, keeps the full output in a log on disk, and returns only the lines that hold results. We’ll check the response against the handoff contract from Lesson 6.2, see that the tool’s own design still decides what comes back, and find out when the extra step isn’t worth it.

A script that prints every step

Here’s the analyst’s impact script. It counts the minutes over the SLO and the requests during them, checks the count three ways, and prints every minute along the way. At the end, it prints two lines that start with RESULT. Those are the only lines the next worker needs.

impact_script.py · Python 3.14.0
import contextlib
import io
from incident_metrics import P99_MS, REQUESTS_PER_MIN, SLO_P99_MS
from render_request import count_tokens
IMPACT_SCRIPT = f'''
p99, requests, slo = {P99_MS}, {REQUESTS_PER_MIN}, {SLO_P99_MS}
over = [i for i, value in enumerate(p99) if value > slo]
for i, value in enumerate(p99): # method 1: walk every minute
print(f"14:{{i:02d}} p99={{value}} over_slo={{value > slo}} requests={{requests[i]}}")
total = sum(requests[i] for i in over)
print("method 2: sum over flagged minutes =", total)
print("method 3: total minus unaffected =", sum(requests) - sum(r for i, r in enumerate(requests) if i not in over))
print("edge check: a threshold of 10000 ms flags", sum(v > 10000 for v in p99), "minutes")
print(f"RESULT checkout.minutes_over_slo={{len(over)}}")
print(f"RESULT checkout.requests_over_slo={{total}}")
'''
if __name__ == "__main__":
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
exec(IMPACT_SCRIPT, {}) # run in process, the way the analyst did so far
full = stdout.getvalue()
needed = "\n".join(line for line in full.splitlines() if line.startswith("RESULT"))
print(f"{len(full.splitlines())} lines printed, {count_tokens(full)} tokens")
print(f"result lines only: {count_tokens(needed)} tokens\n{needed}")

In the code above:

  • Lines 6–17: We write the script as a string, with the metrics filled in, so it can be saved to a file and run on its own later.

  • Lines 9–10: We print one line per minute inside the script.

  • Lines 11–14: We check the total in two more ways and run an edge check.

  • Lines 15–16: We print the two result lines.

  • Lines 20–22: We run the script ...