Skip to content

kernel keeps losing connection between add code cell and execute cell #21

@emilio-gagliardi

Description

@emilio-gagliardi

Description

add_execute_code_cell() keeps failing after adding a cell

The NbModelClient part is robust because it reconnects each time. The KernelClient part is not; it relies on a connection made at startup to persist indefinitely.

Cell Addition Works: Because NbModelClient creates a new connection each time.
Cell Execution Fails (often): Because the global KernelClient's connection may have died since the MCP server started, and there's no mechanism in add_execute_code_cell to refresh or re-establish it. The "Connection to remote host was lost. - goodbye" error you saw in the Docker logs earlier ([06/06/25 20:58:20]) was very likely the global KernelClient's connection dying. After that point, all calls to execute_cell would fail.
In summary: The code doesn't seem to have a mechanism to handle a disconnected global KernelClient. If that initial connection (established when the MCP server starts) drops, all subsequent attempts to execute code via notebook.execute_cell(cell_index, kernel) will fail because kernel is no longer in a usable state. This perfectly explains why cell additions are working (as they use a fresh NbModelClient connection) but executions are failing with a "Connection is already closed" error.

Reproduce

created a notebook file
updated the mcp_config.json
restarted the IDE
instructed agent to add a new cell with something in it
the cell is inserted but the MCP reports the kernel is disconnected and can't execute the cell

I looked at the source code and reviewed it with Gemini 2.5 Pro

Expected behavior

add + execute happens immediately

Context

  • Datalayer version: latest
  • Operating System and version: windows 11
  • Browser and version: Brave
Browser Output
I'm running the notebook with no browser command

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions