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.
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 ...