Eglot is part of the package

eglot ships with Emacs 30.1, which emjupy requires. emjupy loads it, because the WebSocket transport inherits Eglot's connection class. Loading does not start anything: nothing connects until a notebook asks for completion or documentation. emjupy works without a language server.

The server next to the kernel

emjupy gets completion, documentation, definitions and the rest from a language server that sees the environment the code will run in.

jupyter-lsp runs a language server on the Jupyter server. emjupy talks LSP to it over that server's WebSocket. This is the notebook's own transport, and how JupyterLab's jupyterlab-lsp works. So:

  • Nothing written on the remote. Code goes in-band with didChange. The local shadow file is written once, for eglot to attach to.
  • No second connection. Completion and documentation make no TRAMP, ssh or directory calls: measured, they make zero remote filesystem operations. Only opening a definition on the kernel's machine uses TRAMP.
  • The right environment. The server runs beside the kernel, so it describes the packages the code will import. M-. finds your own modules, resolved against the notebook's directory.

Requirements

Two packages, in the Python environment running jupyter server:

bash
pip install jupyter-lsp 'python-lsp-server[all]'

In a conda environment file:

yaml
dependencies:
  - jupyter-lsp
  - python-lsp-server
Package Purpose
jupyter-lsp adds the /lsp/ endpoints to Jupyter
jupyterlab-lsp JupyterLab front end; requires jupyter-lsp
python-lsp-server the Python language server

jupyterlab-lsp installs no language server: alone, it gives the endpoints with nothing behind them.

Locating the language server

jupyter-lsp finds a language server by importing it: importlib.util.find_spec("pylsp"), in the interpreter that runs jupyter server. It starts the server as [sys.executable, "-m", "pylsp"]. It does not consult PATH, so it does not find a pylsp elsewhere on the machine. One installation per Jupyter server is enough, however many notebooks it serves.

eglot over the Jupyter WebSocket

Every eglot command works against the server beside the kernel: rename, formatting, code actions, find-implementation, and whatever eglot gains next. Nothing is wired up per command.

eglot reaches a server through three jsonrpc generics, and jsonrpc-connection-send dispatches on the connection class: emjupy-eglot-server sends over the notebook's WebSocket. jsonrpc-process-connection type-checks for a process, so an idle one is made and ignored.

The document has two names. One is the shadow file on this machine, which eglot manages. The other is the path the kernel sees, which the server knows. Every message is rewritten between the two, in that one place. Completion, eldoc, M-. and every other eglot command go to the shadow buffer, where eglot is attached.

With this transport the shadow file is always local, as the server is sent its contents; emjupy-shadow-host applies only to the SSH arrangement of Working with a remote server.

eglot commands in a notebook

An eglot command looks for the server of the buffer it runs in. A notebook buffer has none, so M-x eglot-rename there fails with "No current JSON-RPC connection". So the commands in emjupy-eglot-delegated-commands run in the shadow buffer instead, at the position matching point, and their edits are pulled back into the cells. They are eglot-rename, eglot-code-actions, eglot-format and the eglot-find-* family. The shadow buffer holds every cell, so a rename fixes references in all of them.

The advice does nothing outside emjupy-mode; M-x emjupy-eglot-remove-advice removes it.

Jumping to definitions

eglot installs its xref backend in the shadow buffer, so emjupy adds one in the notebook that forwards there and maps the answer back. The shadow buffer holding every cell, M-. on a call in one cell lands on its definition in another.

Remote M-. needs remote LSP

A language server reads only the files of its machine:

Language server Your own modules Standard library and packages
beside the kernel (remote) found found – the kernel's own
on this machine (fallback) not found found – but THIS machine's, not the kernel's

To work on a remote kernel on another machine, M-. needs jupyter-lsp and a language server remotely where the kernel runs (see Requirements). Without them, emjupy falls back to a server on this machine with a message. The mode line then shows LSP: this machine, in the error face, for as long as that lasts. The standard library then resolves to this machine's copy, which may be another Python. For a kernel on this machine, either server works. emjupy asks the Jupyter server once whether it has a language server, without waiting. Until it answers, no language server is attached.

M-x emjupy-lsp-diagnose says which server a notebook uses: "over the Jupyter WebSocket" is the one beside the kernel, "a local process" the fallback.

Jumping into modules beside a remote notebook

A definition reported by the server beside the kernel is a path on the kernel's machine. emjupy opens it there over TRAMP. That includes the standard library, because the kernel's Python is what the code runs against. The host is the one emjupy-remote-root names, if it names one. Otherwise it is the host of the SSH tunnel through which the server is reached, read from the ssh process on this machine. So nothing needs configuring behind ssh -L.

Sometimes emjupy has no way to reach that machine: no tunnel, and nothing configured. It then names the file it could not open, and names emjupy-remote-root as the setting that would reach it. It does not open a file of the same name on this machine. Paths from the fallback server name this machine's files, and are left as they are.

Language server requests

Requests are asynchronous. eldoc runs after every command, and a blocking request would cost a round trip per keystroke. Completion answers from the previous reply, and asks for the next one in the background. The handshake is asynchronous too: jupyter-lsp starts the server on demand, which may take seconds, and gives no answers until then. emjupy retries a socket that cannot be opened after emjupy-lsp-retry-interval seconds.

Per-command cost, measured on a twelve-cell notebook with figures:

Operation Time
cursor motion 0.0 ms
completion 0.1 ms
eldoc 1.2 ms

When a shadow file would describe the wrong machine

Sometimes the kernel runs where this Emacs cannot reach, and emjupy-shadow-host does not say how. The shadow file and its server are then local, and describe this machine: its interpreter and packages, and none of the modules next to the notebook. That is mostly right for ordinary Python, so it is the default. It is confidently wrong for anything specific to the environment, so emjupy says so once per notebook. emjupy-shadow-when-kernel-unreachable nil gives no language support instead. emjupy-shadow-host, or jupyter-lsp on the server, gives answers from the right machine.

When the language server cannot be reached

In the SSH arrangement the shadow file is remote, over TRAMP, and emjupy needs it after every command. An unreachable host would block every keystroke. So setup gets emjupy-shadow-timeout seconds (2). After a failure, emjupy does not retry for emjupy-lsp-retry-interval seconds (30), and says so once. This covers creating the file and starting the language server. Starting the server is the costly half: without the delay it would repeat per command when the host has no pylsp. Editing and running cells never need the shadow buffer; only completion, eldoc and M-. wait for the host.

Falling back

emjupy-language-support set to local chooses the older arrangement, which needs no jupyter-lsp. The language server then runs on this machine, or over SSH beside the kernel; see Working with a remote server. emjupy also falls back to this arrangement, and says so, when the server has no language server.

Checking it from the shell

bash
curl -s "http://localhost:9999/lsp/status?token=YOUR_REAL_TOKEN"

With your real token (YOUR_REAL_TOKEN is refused):

Answer Meaning
404 (an HTML page) jupyter-lsp is not installed
{"message": "Forbidden"} (403) the token was wrong, nothing more
{"version": 2, "sessions": {...}} the plumbing works; read what it lists

sessions names the language servers installed:

{"version": 2, "sessions": {"pylsp": {"status": "not_started", ...}}}

An empty sessions means the plumbing is there and no language server. emjupy-lsp-server (default "pylsp") names the one to ask for. emjupy asks /lsp/status itself when it cannot reach a server, and says which of the three it found.