MCP server improvements: NDJSON transport + kernel_execute + md⇄ipynb sync#14
Merged
Merged
Conversation
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.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/.ipynbin sync.a2b7dcb) — the previousContent-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. SwitchreadMessage/writeMessageto newline-delimited JSON per spec.kernel_executeLean bridge (a7aecfe) — adds a Lean-only HTTP/1.1 + RFC 6455 WebSocket client on top ofStd.Internal.UV.TCPand uses it to drive the running xlean kernel via Jupyter Server's REST + channels endpoints. Letslean_evalskip 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_markdowntools (427eb3c) — wrap the existingConvertlibrary as MCP tools so an agent can roundtrip.md⇄.ipynbfrom a single call (and the user's JupyterLab tab refreshes viajupyter-collaborationwithout a manual reload).Plus a small
LeanSessionpolish:lean_evalnow invokeslake env leanwhen the cwd has alakefile.lean/lakefile.toml, soimport Sparkleetc. resolve transitive deps instead of failing with "unknown module prefix".Test plan
cat /tmp/probe.json | xlean-mcpreturns the MCPinitializereply within a second (no timeout)tools/call kernel_executeagainst a running Jupyter Server returns Jupyter-shape outputs includingtext/plain,text/html,image/svg+xmltools/call markdown_to_notebookwrites a valid.ipynbfrom a tutorial.mdtools/call notebook_to_markdownround-trips a notebook back to Markdown without losing cell contenttools/call lean_evalinside a Lake project resolves deps vialake env lean