Tool: execute_code
Executes Python in a Colab runtime's Jupyter kernel and returns the captured stdout, stderr, the last expression's value, and any error.
Target exactly one of runtime (runtimes/{runtime_id}), which runs in a fresh kernel session, or session (runtimes/{runtime_id}/sessions/{session_id}), which runs in that session and shares kernel state (variables, imports, loaded data) with its other calls. A session is created if it does not already exist.
stdout and stderr stream incrementally as progress notifications and are also returned aggregated in the result.
To make an execution resumable, pass a stable executionId (a UUID). A call that outlives the streaming deadline (600s) ends with a DEADLINE_EXCEEDED error before returning a result. To recover, re-issue the identical call with the same executionId to reattach and keep streaming through to the final result. Reattaching works only until the result is returned. Reusing the same executionId after that starts a new execution. Without an executionId, an execution that outlives the deadline keeps running but cannot be reconnected to.
Clients use the standard MCP notifications/cancelled flow on an in-flight execution. When cancelled, interrupts the kernel.
Only text is returned. Rich outputs (images, plots, widgets) are not returned. Oversized output is truncated and flagged via outputTruncated. Files written to disk persist on the runtime regardless of session.
The following code sample shows how to use curl to call the execute_code MCP tool.
| Curl Request |
|---|
curl --location 'https://colaboratory.googleapis.com/mcp' \ --header 'content-type: application/json' \ --header 'accept: application/json, text/event-stream' \ --data '{ "method": "tools/call", "params": { "name": "execute_code", "arguments": { // Provide these details according to the MCP tool specification. } }, "jsonrpc": "2.0", "id": 1 }' |
Input Schema
Request message for ExecuteCode.
ExecuteCodeRequest
| JSON representation |
|---|
{ "code": string, "executionId": string, // Union field |
| Fields | |
|---|---|
code |
Required. The Python source to execute. |
executionId |
Optional. An optional client-supplied execution identifier (a UUID). Supply one to make the execution resumable: re-issuing ExecuteCode with the same Once a request carrying an |
Union field
|
|
runtime |
The runtime to execute on. Format: Each call opens a fresh kernel session, so an execution run this way can only be resumed by targeting the |
session |
The session to execute in. Format: |
Output Schema
The result of an ExecuteCode call, returned as the terminal structured_content of the response stream.
ExecuteCodeResult
| JSON representation |
|---|
{
"executionId": string,
"session": string,
"executionCount": integer,
"stdout": string,
"stderr": string,
"result": string,
"executionError": {
object ( |
| Fields | |
|---|---|
executionId |
The execution identifier this result is for, echoed from the request. Empty when the request supplied no |
session |
The session the execution ran in. Format: |
executionCount |
The Jupyter |
stdout |
Aggregated stdout across the execution. |
stderr |
Aggregated stderr across the execution. |
result |
The |
executionError |
Populated when the execution raised. Mutually exclusive with a successful completion. |
outputTruncated |
Whether the runtime truncated buffered output (size or line caps hit). When true, |
Error
| JSON representation |
|---|
{ "name": string, "value": string, "traceback": [ string ] } |
| Fields | |
|---|---|
name |
The exception class name, e.g. "NameError". |
value |
The exception message, e.g. "name 'foo' is not defined". |
traceback[] |
The formatted traceback, one frame per entry. |
Tool Annotations
Tool annotations are sent to MCP clients to describe the basic risk of a given tool. Most clients treat these hints as untrusted, but they can be used to decide when a confirmation prompt might be sent to a user.
Along with the title string, the following boolean hints are defined as follows:
readOnlyHint: If true, the tool doesn't modify its environment. Default: false.destructiveHint: If true, then the tool can perform destructive actions. If false, then the tool can only perform additive actions. Default: true.idempotentHint: If true, then calling the tool repeatedly with the same arguments will have no additional effect on its environment. Default: false.openWorldHint: If true, then the tool can interact with an 'open world' of external entities. If false, then the tool can only interact with internal entities. For example, a web search tool would be open world, while a memory tool would not be open world.
Destructive Hint: ✅ | Idempotent Hint: ❌ | Read Only Hint: ❌ | Open World Hint: ✅