Skip to content

MCP server improvements: NDJSON transport + kernel_execute + md⇄ipynb sync#14

Merged
junjihashimoto merged 3 commits into
mainfrom
feature/mcp-server-improvements
Jun 12, 2026
Merged

MCP server improvements: NDJSON transport + kernel_execute + md⇄ipynb sync#14
junjihashimoto merged 3 commits into
mainfrom
feature/mcp-server-improvements

Conversation

@junjihashimoto

Copy link
Copy Markdown
Contributor

Summary

Three coherent improvements to xlean-mcp that together let an MCP host (Claude Code, Cursor, …) drive a Lean workflow end-to-end — speak the spec-correct framing, share env with the live notebook kernel, and keep .md / .ipynb in sync.

  • NDJSON stdio framing (a2b7dcb) — the previous Content-Length: framing was LSP-style, not MCP-stdio-style, so every host that follows the MCP spec (Claude Code, Cursor, the official TS/Python SDKs) hung at ~30 s connection timeout. Switch readMessage / writeMessage to newline-delimited JSON per spec.
  • kernel_execute Lean bridge (a7aecfe) — adds a Lean-only HTTP/1.1 + RFC 6455 WebSocket client on top of Std.Internal.UV.TCP and uses it to drive the running xlean kernel via Jupyter Server's REST + channels endpoints. Lets lean_eval skip the fresh-process shell-out and instead share env / MIME outputs with the user's notebook session. ~470 LOC pure Lean, no new lake deps, no new C.
  • markdown_to_notebook / notebook_to_markdown tools (427eb3c) — wrap the existing Convert library as MCP tools so an agent can roundtrip .md.ipynb from a single call (and the user's JupyterLab tab refreshes via jupyter-collaboration without a manual reload).

Plus a small LeanSession polish: lean_eval now invokes lake env lean when the cwd has a lakefile.lean/lakefile.toml, so import Sparkle etc. resolve transitive deps instead of failing with "unknown module prefix".

Test plan

  • cat /tmp/probe.json | xlean-mcp returns the MCP initialize reply within a second (no timeout)
  • tools/call kernel_execute against a running Jupyter Server returns Jupyter-shape outputs including text/plain, text/html, image/svg+xml
  • tools/call markdown_to_notebook writes a valid .ipynb from a tutorial .md
  • tools/call notebook_to_markdown round-trips a notebook back to Markdown without losing cell content
  • tools/call lean_eval inside a Lake project resolves deps via lake env lean

The previous transport framed every message with
`Content-Length: N\r\n\r\n<body>` (LSP style).  The MCP stdio
spec is one JSON-RPC message per line, so hosts that follow the
spec — Claude Code, Cursor, the official TS/Python SDKs — send
`{...}\n` and block waiting for `{...}\n` back.  Our server then
hangs in the header read loop looking for a `Content-Length:`
prefix that never arrives, and the host times out at ~30 s
("Failed to reconnect").

Switch readMessage/writeMessage to NDJSON: one trimmed line in,
one compressed line + `\n` out (`Json.compress` already removes
internal whitespace, so concatenating `\n` is safe).

Verified by spawning the rebuilt `xlean-mcp` under `docker exec
-i` from Claude Code's spawn path: initialize / tools/list /
tools/call lean_eval all round-trip in <1s.
Adds a `kernel_execute` MCP tool that drives the running xeus-lean
kernel via Jupyter Server's REST + WebSocket surface, so an MCP
host (Claude Code, Cursor, …) shares env with the user's notebook
session: definitions persist across calls, and MIME outputs
(text/html, image/svg+xml) emitted by the cell appear in the
user's browser too.

Implementation is fully self-contained in stdlib Lean:

  Net/HTTP.lean  — sync HTTP/1.1 client over Std.Internal.UV.TCP.
                   Just enough to POST /api/kernels + GET friends.
  Net/WS.lean    — RFC 6455 WebSocket client (handshake, masked
                   send, frame parse).  Localhost / no-auth /
                   text frames only — the surface the Jupyter
                   channels endpoint actually uses.
  KernelBridge.lean — Jupyter messaging codec (execute_request →
                       iopub absorption → stop on parent's
                       status:idle).

Zero new C, zero new lake deps; libuv FFI is what the Lean runtime
already links.  Net/HTTP and Net/WS are sized for the Jupyter
Server contract, not a general-purpose client (no TLS, no chunked,
no fragment reassembly).

Also: `LeanSession.eval` now uses `lake env lean` when the cwd has
a `lakefile.lean`/`lakefile.toml`, so a `lean_eval` inside a Lake
project resolves transitive deps instead of failing with "unknown
module prefix 'Sparkle'".
Wraps the existing `Convert` library (the one `xlean-convert`
ships) as MCP tools so an agent can keep `.md` ⇄ `.ipynb` in
sync from a single tool call instead of editing the Markdown,
shelling out to `xlean-convert`, and waiting for the user to
reload — particularly painful inside Jupyter Real-Time
Collaboration where the `.ipynb` is the live source of truth.

  markdown_to_notebook(md_path, ipynb_path, lean_path?)
    Re-render an .md to its sibling .ipynb (and optionally the
    lean:percent file `lake build` ingests).  Same pipeline as
    the CLI.

  notebook_to_markdown(ipynb_path, md_path)
    Inverse: walk the .ipynb's cells, emit a Markdown chapter
    where `code` cells become ```lean fences.  Round-trips with
    markdown_to_notebook; useful when the agent edited cells
    via notebook_edit and needs the change to land in the .md
    source the next Docker build picks up.

Both tools live entirely in `XLean.MCP.ConvertTools`; no
changes to the `Convert` lib itself, so the CLI binary stays
byte-identical.
@junjihashimoto junjihashimoto merged commit 568d4b5 into main Jun 12, 2026
7 checks passed
@junjihashimoto junjihashimoto deleted the feature/mcp-server-improvements branch June 12, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant