Skip to content
mgubi edited this page Nov 5, 2026 · 5 revisions

Agents and TeXmacs

How an AI agent (Claude, or any agent which speaks MCP) and a person can work on the same TeXmacs document at the same time. This page collects the approaches and the open questions; it is a place for discussion, not a decision.

The problem

People who write papers with an agent today let it edit the .tm file on disk while they have the same document open in TeXmacs. The two get in each other's way: one of them overwrites the other's changes, or works on a stale copy, and the agent cannot see the typeset result.

What we want:

  • both work on the same document at once, and each sees the other's changes as they happen;
  • the agent edits the structure of the document, not its text file;
  • the agent can check its work: typesetting errors, and how a page looks;
  • the person stays in control: they see what the agent changes, can refuse it, and can undo their own changes and the agent's separately.

What TeXmacs already has

  • The TeXmacs server and live documents (progs/server/server-live.scm, progs/client/client-live.scm): several clients edit one document; changes travel as patches with versions, and each participant's cursor is shown. Accounts and authentication exist, and a server can run locally (texmacs --server --headless --port N, see src/SERVER.md).
  • The remote tools (Tools > Remote tool, manual under doc/main/remote/): the user's side of the server, with live documents and a chat (client-chat.scm, server-chat.scm); a chat is a ready place for an agent participant.
  • A WebSocket port for the server (PR #332, still open): the server also listens for WebSockets, so that a TeXmacs in a browser, and in principle a small agent client, can reach it.
  • Scheme access to everything in a buffer: the tree, typesetting, export to PDF and images, version comparison (menu Version > Compare, shown when the versioning tool is on; compare-with-older, compare-with-newer).
  • Headless runs of TeXmacs (texmacs -headless -x ...), which typeset and export without a window.
  • The AI plug-ins: chat sessions with Claude and other models, and tools of the Tools menu which correct or translate a selection, optionally shown as a comparison whose changes the person accepts or rejects (plugins/ai/doc/ai-tools.en.tm): already the "suggestions" idea below, for a selection.
  • The LSP client, which already speaks a JSON protocol to an external process.

texmacs --mcp exists since November 2026: see MCP server design (section 8 has the steps and their status) and Help > AI agents (MCP server) in TeXmacs Upsilon.

Approaches

A. A local TeXmacs server, with the agent as one more participant

A TeXmacs server runs on the machine (or a shared one). The person opens the document as a live document. The agent reaches it through a client which joins the same live document: a headless TeXmacs with an MCP endpoint, or a small client which speaks the server's protocol.

  • For: reuses the live documents and their patch-based concurrency as they are; works with several people and several agents; the server keeps the history; does not depend on the person's TeXmacs staying open.
  • Against: a server to run and configure; the document has to become a live document; the agent's client still needs TeXmacs (headless) to typeset and to check its work, or it can only edit blindly.

B. An MCP server inside a running TeXmacs

The person's TeXmacs itself offers an MCP endpoint (localhost only, with a token). Its tools work on the buffers which are open: the agent edits the very document on screen.

  • For: nothing to deploy; the agent sees unsaved changes; typesetting and export are at hand; the quickest to try.
  • Against: only while that TeXmacs runs; one machine; concurrency with the person's typing has to be handled in the editor (same thread, same undo history); a bug in the endpoint can disturb the person's session.

C. One MCP layer, used in both ways

The two approaches can share their code: an MCP layer inside TeXmacs whose tools map to Scheme on a buffer. In a running TeXmacs it gives B; in a headless TeXmacs which joins a live document it gives A. The choice becomes one of deployment, and B can come first.

The tools an agent would need

A first list, to discuss:

  • Read: list the open documents; the outline (sections, labels); a subtree, by label or path, in two forms: the exact tree, and a compact readable text which costs the model less.
  • Edit: replace, insert or remove a subtree, carrying the version the agent read. If the document moved since, the edit is rebased (like a live patch) or refused.
  • Check: typeset and return the errors; export a page as an image to look at.
  • No general evaluation of Scheme by default: it would give an agent full control of the editor and the machine.

Open questions

  • Stable addresses. Paths in the tree shift when someone edits above them. Should the agent address labels and anchors, with paths only as hints checked against a version?
  • Suggestions or direct edits. Should the agent's changes arrive as suggestions which the person accepts or rejects (perhaps from the comparison of versions, or from the accept and reject of the AI tools), with direct editing as an option per document?
  • Undo per participant. Undoing one's own change should not undo the agent's, and the reverse. Do the live patches record their author?
  • What the model reads. The full tree is exact but long; plain text loses the structure. Which compact form? (The Markdown converter could be a start.)
  • Presence. The agent's cursor shown like another participant's; a soft claim on a section ("the agent is working on section 3") rather than locks.
  • Security. A local endpoint must listen on localhost only and require a token; a shared server needs accounts for agents.
  • Several agents on one document, and agents which work while nobody has the document open.

The design

A design which follows approach C, with an API, is on MCP server design.

Possible steps

  1. A design note on the tools and the data format (this page can become it).
  2. Read-only MCP in a running TeXmacs: outline, read, typeset check, page image.
  3. Edits, as suggestions, with the version check.
  4. The same tools in a headless TeXmacs on a live document of the server: presence, several participants.

Each step is usable on its own.

Tests from Basile Audoly (with Claude), 4 November 2026

Done in a macOS 26 VM. Scripts and logs can be shared on request.

1. Approach B in a window (Upsilon Cocoa of 2 November)

Services declared with tm-service in the person's TeXmacs, called by a 30-line client speaking the tm_link packets, while the person types with real keystrokes.

  • Undo. Changes applied with the person's author merge into the person's last undo step, even with add-undo-mark or mark-start/mark-end around them: one Cmd-Z removes the last word typed together with the agent's changes, and Cmd-Shift-Z brings back the word only, so the agent's changes are lost. With another author (new-author, set-author, commit-changes), Cmd-Z skips the agent's changes, undoes them only when nothing of the person is left, and Cmd-Shift-Z redoes them. So an author per agent is required, as the design says.
  • Cursor. tree-insert! and tree-set! keep the person's cursor with its text (paragraph inserted above, text inserted before the cursor in the same paragraph). When the agent replaces the whole paragraph of the cursor, the cursor keeps its offset.
  • Typing at the same time: six edits of the agent, three in the paragraph being typed: nothing lost, nothing doubled.

2. Shared files, PR #805 at 984ef19d (built as Cocoa S7, without #804)

Two processes with a window, separate TEXMACS_HOME_PATH, share-open-files on. The person (host) types a sentence at the end of a paragraph, one key every 80 ms. During that time the agent, with its own author, inserts a paragraph, makes two insertions in the first paragraph, and three at the start of the paragraph being typed.

  • Both processes end with the same document, with everything in it, and nothing in system/recovered.

  • Live documents in a window (#796). With live documents, typing in a window corrupts the text: letters are doubled or lost, and the view diverges from the live document. With shared files, we did not see this.

  • Undo (3.7), Cmd-Z three times in the person's window, with no crash:

    • the first Cmd-Z removes the end of the person's sentence;
    • the second also removes the agent's last insertion, made at the start of the paragraph being typed: an insertion far from the cursor, which should commute;
    • the third removes only the person's text.

    Both processes follow. So an agent's patch received during typing is not always skipped by the person's undo.

  • Detail: go-to-path called from a delayed command did not move the cursor of the window of a shared buffer (the same call worked on an unshared buffer).

3. Formats (5, 7)

Sizes in bytes of three documents of the TeXmacs documentation with formulas:

Document .tm stree on one line Markdown text
devel/source/maths 20 096 14 547 17 191 16 336
main/math/fonts/man-math-font-catalogue 34 920 15 699 28 465 19 550
devel/source/drd-model 29 296 18 163 24 745 21 435

The stree on one line is the shortest exact form. The .tm syntax is 1.4 to 2.2 times longer, mostly because of its indentation. A .tm without indentation, or the stree, would serve as the compact exact form. Counting in model tokens rather than bytes would be the next step.

4. Start-up of texmacs --mcp (4.1)

-headless of Upsilon Qt, quitting at once: 4.2 s the first time, then 1.0 to 1.1 s. Start-up, load, typesetting and PDF of one document: 3.2 s. Acceptable for a server started once per agent session.

5. Other remarks

  • Notifications (5.5). We do not know which MCP clients show a resource update to the model while it works. A note in every tool answer, saying when the document changed since the agent's last read (state, sections touched), would work with all clients; get_changes gives the details.
  • Refusals (4.4). Return pressed far from the agent's edit is refused. People press Return all the time, so rebasing across a split or join which leaves the agent's node untouched looks like the most useful improvement.
  • Configuration (4.1). claude mcp add is for Claude Code. The Claude desktop app (and Cowork through it) declares local MCP servers in its own settings. One line for each in the documentation would help.