Skip to content

Snapshot document and outputs in MCP execute_code#9654

Merged
mscolnick merged 3 commits into
mainfrom
manzt/snapshot-mcp
May 21, 2026
Merged

Snapshot document and outputs in MCP execute_code#9654
mscolnick merged 3 commits into
mainfrom
manzt/snapshot-mcp

Conversation

@manzt
Copy link
Copy Markdown
Collaborator

@manzt manzt commented May 21, 2026

Mirrors what /api/execute already does. cm.get_context() previously raised "NotebookDocument not available" when invoked from code run via the MCP tool, because execute_code didn't populate notebook_cells or cell_outputs on ExecuteScratchpadCommand.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 21, 2026 8:01pm

Request Review

@manzt manzt requested a review from mscolnick May 21, 2026 17:42
@manzt manzt added the enhancement New feature or request label May 21, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Architecture diagram
sequenceDiagram
    participant MCP as MCP Tool
    participant Server as Code MCP Server
    participant Session as Session
    participant Doc as Document
    participant SV as SessionView
    participant CO as CellOutputs
    participant Runtime as Runtime
    participant CM as CodeMode (cm.get_context)

    Note over MCP,CM: execute_code MCP tool call flow

    MCP->>Server: execute_code(session_id, code)
    Server->>Server: Create ScratchCellListener, acquire scratchpad_lock
    Server->>Doc: snapshot_document()
    Doc-->>Server: cell_ids, cells
    Server->>SV: get_cell_outputs(cell_ids)
    SV-->>Server: output dictionary
    Server->>SV: get_cell_console_outputs(cell_ids)
    SV-->>Server: console_outputs dictionary
    Server->>CO: CellOutputs(outputs, console_outputs)
    CO-->>Server: cell_outputs object
    Server->>Runtime: put_control_request(ExecuteScratchpadCommand with notebook_cells, cell_outputs, code, run_id)
    Runtime->>Runtime: Execute code
    Runtime->>CM: cm.get_context()
    CM->>CM: Read notebook_cells and cell_outputs from ExecuteScratchpadCommand
    CM-->>Runtime: Context available
    Runtime-->>Server: Execution complete
    Server-->>MCP: CodeExecutionResult
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread marimo/_mcp/code_server/main.py Outdated
mscolnick
mscolnick previously approved these changes May 21, 2026
Base automatically changed from manzt/cell-outputs to main May 21, 2026 19:45
@manzt manzt dismissed mscolnick’s stale review May 21, 2026 19:45

The base branch was changed.

@github-actions github-actions Bot added the bash-focus Area to focus on during release bug bash label May 21, 2026
manzt added 3 commits May 21, 2026 15:52
Mirrors what `/api/execute` already does. `cm.get_context()` previously
raised "NotebookDocument not available" when invoked from code run via
the MCP tool, because `execute_code` didn't populate `notebook_cells` or
`cell_outputs` on `ExecuteScratchpadCommand`.
Pre-commit's no-double-backticks-in-docstrings hook (and AGENTS.md)
require Markdown inline code, not reStructuredText.
Copilot AI review requested due to automatic review settings May 21, 2026 20:00
@manzt manzt force-pushed the manzt/snapshot-mcp branch from 4556a5b to 7eca734 Compare May 21, 2026 20:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aligns MCP execute_code behavior with the existing HTTP /execute flow by ensuring scratchpad executions carry a snapshot of the notebook document and per-cell outputs, enabling cm.get_context() to expose cells and their last outputs.

Changes:

  • Added snapshot_for_scratchpad(session) helper to capture (notebook_cells, cell_outputs) from a session.
  • Refactored the HTTP /execute SSE path to use the shared snapshot helper.
  • Updated MCP execute_code to populate ExecuteScratchpadCommand.notebook_cells and cell_outputs, plus added a unit test for the helper.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/_server/test_scratchpad.py Adds coverage for the new scratchpad snapshot helper.
marimo/_server/scratchpad.py Introduces snapshot_for_scratchpad() to package document + outputs for scratchpad runs.
marimo/_server/api/endpoints/execution.py Reuses the shared snapshot helper when building ExecuteScratchpadCommand for /execute.
marimo/_mcp/code_server/main.py Updates MCP execute_code to include notebook cells + outputs in the scratchpad command.

Comment on lines +72 to +88
output = CellOutput(
channel=CellChannel.OUTPUT, mimetype="text/plain", data="1"
)
console = [CellOutput.stdout("hi\n")]

session = MagicMock()
session.document.cells = (cell,)
session.document.cell_ids = (cell.id,)
session.session_view.get_cell_outputs.return_value = {cell.id: output}
session.session_view.get_cell_console_outputs.return_value = {
cell.id: console
}

notebook_cells, cell_outputs = snapshot_for_scratchpad(session)
assert notebook_cells == (cell,)
assert cell_outputs.output == snapshot({"a": output})
assert cell_outputs.console_outputs == snapshot({"a": console})
@mscolnick mscolnick merged commit 9076095 into main May 21, 2026
43 checks passed
@mscolnick mscolnick deleted the manzt/snapshot-mcp branch May 21, 2026 20:25
@github-actions
Copy link
Copy Markdown

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.7-dev87

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bash-focus Area to focus on during release bug bash enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants