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

MCP server: security and the rules for agents' content

This page explains the security model of texmacs --mcp (see MCP server design): what an agent may and may not write into a document, why, what was tried and failed before the present rules, and what is still open. The code is on the branch wip_agents; the reference with every rule and every table is src/docs/mcp-server.md there, section "The tools". State of 5 November 2026: steps 2 and 3 and step 4, stage A (modes, proposals, the host's filter) are audited clean at e8f55e1e79 and merged into upsilon_texmacs; stage B (trusted agents) was reverted at e00d757d2f (section 7). Limits measured at the end of step 3: the worst accepted content exports in about 3 s and 0.5 GB.

1. The question

Design section 5.6 says: an agent's edit must not become a way to run code in the person's TeXmacs, or to read files into the person's documents or their exports. The server offers no evaluation of Scheme and no access to files outside the documents; the question is whether the content the agent writes can do it anyway.

It can, because TeXmacs documents are active:

  • macros run Scheme through extern; links run scripts when clicked (script:, action); include, image, pattern read files while typesetting;
  • the style and the document's own macros decide what a tag does: <foo|x> may be a link to x, an include of x, or a call of a macro named x;
  • the exporters write markup as raw LaTeX and HTML: a label a}\input{/etc/passwd}\x{ became \label{a}\input{/etc/passwd}\x{} in the .tex (#808), a colour red" onmouseover="..." became an attribute in the HTML (#809).

Why it matters, when agents can run commands anyway

  • The agent's permissions. Claude Code and similar clients ask before running a command or reading a file outside the project; an MCP tool call that edits a document is usually allowed more freely. If an edit could carry an extern, a script: link or an include of ~/.ssh/id_ed25519, writing a document would bypass those prompts.
  • Prompt injection. The text an agent writes is often shaped by what it read: a paper, a web page, an email, a bibliography entry. A filter on the content limits what such text can make the agent do to the person's files.
  • Documents travel. A document goes to coauthors, is exported to LaTeX and compiled (with shell escape for some), published as HTML. Something planted today acts later, somewhere else, for someone else.
  • The person may not look. In direct mode an agent's edit is applied at once, possibly in a part of the document the person does not read again (a folded section, the running title).

2. What the process itself may do

  • texmacs --mcp is a headless TeXmacs started by the agent's client, speaking JSON-RPC on its standard input and output; nothing else reaches the protocol (all other output goes to a log, rotated).
  • It opens any .tm file the user can read, as a text editor would, unless TEXMACS_MCP_ROOTS limits the folders (paths with .. are refused; the real path, symbolic links resolved, is checked and loaded). TEXMACS_MCP_READONLY removes the editing tools.
  • It never evaluates code given by the agent: stree content is read as data; no tool runs Scheme; no plugin sessions.
  • With shared files (#805; on by default; the agent is read only for a file it cannot join), it joins the person's TeXmacs through a local link with a token, in a folder only the user can read. An agent is marked as such when it joins.
  • The importers (LaTeX, Markdown) and the typesetter run in this process on the agent's input: a crash or a hang there is the agent's process, but an edit that is saved or shared reaches the person's TeXmacs.

3. The rules for content

The filter refuses the whole call, changes nothing, and gives the reason. It has rules that do not depend on running anything (static), and an extra check on the expansion.

3.1 Definitions belong to the person

  • The agent cannot edit inside an assign, set, reset, macro, xmacro, provide, drd-props, with-package and the like, nor replace or delete a node that holds one. It can insert next to them.
  • New content cannot define anything: no assign, no macro, no package, no style change, and no macro of the style that defines a name taken from its argument (new-theorem, new-remark, new-env, new-list...: <new-remark|strong|...> would redefine strong).

Why. The meaning of every tag of the document depends on the definitions. If the agent could change a value or a macro body, an edit far from any link could turn the person's existing links into javascript: ones (this happened in audits: a <set|dst|https://ok.org> edited to javascript:..., used by a macro of the person; a macro reached through a name built with merge). Freezing the definitions makes every other check local.

3.2 Dangerous macros are not called

  • The macros of the document and of its style are analysed without evaluating them, down to the primitives they call, transitively. A parameter of a macro is sensitive when it reaches the destination of a link, a file, code (a script, an extern of an unreviewed function, specific, latex...), the name of a macro, or the name of a definition.
  • The agent's content may not call a macro at a sensitive parameter, anywhere (also inside arg, value, compound, with...).
  • Exception: a short table of standard macros that pass an argument to a link or a file (href, slink, hlink*, include, the note macros, the manuals' links). Their link and file arguments are checked where the call is written, like a link or a file of the agent's. A macro of the table loses the exemption if the document redefines anything it calls.
  • extern: a macro of the style that calls <extern|function|...> runs a Scheme function on its arguments. Those arguments are data only for the 31 functions on a reviewed list (doc-data, ext-map, natbib-get, screens-bar...: tree builders, no files, no commands, no unbounded work); every other function makes its macro dangerous. A test scans the standard styles and packages so that a new extern cannot go unnoticed.

Why. A list of forbidden tags cannot know what the style and the person's macros do with their arguments. The analysis follows the definitions as they are, in the document being edited, so the person's own macros are covered, and a standard macro is accepted exactly when it does nothing sensitive with the agent's text.

3.3 Links, files, names that go to an export

  • A link destination is a plain string with no scheme (a relative address, #label) or http, https, mailto; also the help links tmfs://help/article/tm/doc/.... Absolute paths and .. in links are accepted: a link only acts when the person clicks it.
  • A file (image, include, pattern...) is a plain relative name, no .., no scheme, resolved (symbolic links followed) inside the roots or the folder of the document; or an http(s) image.
  • Strings that become names in an export (labels, references, citation keys, with values, file names, addresses) refuse backslashes, braces, quotes, <, >, control characters, and for labels and files % # $ & ^ ~.
  • LaTeX given by the agent is scanned before import for the commands that read files, run programs or hide a command name (\input, \write18, \catcode, \csname, \def...). \documentclass, \usepackage, \bibliography are accepted with plain names and dropped (the document's style says them).

Why. These strings leave TeXmacs: into a .tex someone compiles, an HTML page someone opens. The exporters now escape them too (#808, #809), but the filter keeps them clean at the source, also for exporters and versions that do not.

3.4 Raw code is the person's

  • No extern, script, action, hybrid, eval, compound, latex, or specific for another program in new content, and no edit inside such a node of the person (deleting it is allowed).

Why. The person may have raw LaTeX or a script in their document; the agent changing its text (\input{a.tex} → \input{/etc/passwd}) is as dangerous as writing it.

3.5 Limits

  • No loops (while, for-each, range); numbers given to the environment have at most 4 digits before the point; the values that multiply the work are capped: par-columns ≤ 12, font-size ≤ 8, font-base-size ≤ 100, magnification ≤ 4 (their product matters: 10 × 20 took 15 s and 1.9 GB), dpi ≤ 2400.
  • The expansion check runs under a step budget; recursion of macros stops when the stack is nearly full (a guard in the evaluator and the typesetter, for everybody: PR #812).
  • Any error or exceeded budget refuses the edit.

Why. An edit is saved into the person's file and typeset in the person's TeXmacs. In audits, a two-word edit (<tiled|0|a|b>) looped for ever, 9999 columns took over a minute and 2 GB, a self-recursive macro crashed the process.

3.6 Checked against the whole document, before and after

  • The edit is made tentatively in the editor's transaction; the edited block is checked, as written and as expanded in the document's environment (after the person's definitions, inside the person's withs); only new violations count, compared by their full content.
  • So the person's own links, scripts and raw LaTeX stay and do not block the agent's edits around them, and a change of their content is a new violation.
  • A refused edit is cancelled before anything is sent to a host: the person's TeXmacs never sees it, even briefly.

Why. Checking only the snippet the agent sends misses edits whose meaning comes from where they land: replacing the destination of an existing link by javascript:... sends just a string.

4. What was tried, and why it failed

Each rule above comes from an audit that broke the previous version. The lesson in short: the filter must reason about what the document means, not about tag names, and must not trust TeXmacs's evaluator to tell what the typesetter will do.

  1. A list of forbidden tags (script, extern, include...): the names used were partly wrong (include* is the real tag), and above all every style has macros that wrap these primitives (hlink*, tmdoc-include, ~30 found by a scan).
  2. A classification of all 370 primitives: complete, but macros of the style still passed their arguments to links; <with|em|<value|href>|...> made em an alias of href; unknown tags were written by the LaTeX exporter as raw commands (\verbatiminput{/etc/passwd}).
  3. An allowlist of tags plus checks of the expansion (texmacs-exec): the snippet was checked alone, so an edit of a leaf of an existing link, or an insertion inside the person's with, passed.
  4. Checking the document after the edit, by expansion: the evaluator is not the typesetter. Content that made the evaluator stop with an error (<arg|<foo|javascript:...>>) was not checked inside, but the typesetter drew a live javascript: link into the PDF. Names built by the style's own macros (inc-counter) were invisible.
  5. Static analysis with a hand-made allowlist: whitelisted macros could reach a helper the person had redefined; extern functions trusted as data included ext-map (<map|<merge|inc|lude>|<tuple|/etc/hosts>> typeset the file into the PDF) and a table builder that looped on zero columns; definers could redefine strong.

Each round also found false refusals of ordinary writing (\usepackage{amsmath}, labels with colons, smart-ref, folds, marginal-note in the journal styles, chapter in book), fixed as they came: normal writing now passes in 16 styles (generic, article, book, beamer, seminar, exam, letter, tmdoc, amsart, elsart, ifac, jsc, svjour, and article with the citation and smart-ref packages), a full LaTeX article and a Markdown document.

5. Bugs of TeXmacs found on the way (for everybody)

  • LaTeX export wrote labels, keys, URLs, lengths, names raw: \input and \write18 could be injected through a label. Fixed in #808 (merged).
  • HTML export wrote attribute values raw: attributes and javascript: links could be injected. Fixed in #809 (merged).
  • HTML export turned pattern backgrounds into white when the image was not found. Fixed in #810 (merged).
  • A self-recursive macro crashed TeXmacs (stack overflow in typesetting and export). #812 (merged): a stack guard; deep terminating recursion now stops with an error at about 870 levels on macOS (it crashed at about 1390).
  • <with> with no argument crashed the typesetter; headless undo crashed on a beep with no application. Fixed on wip_agents.

6. Limits and what is not covered

  • This is a filter on what the agent writes, not a sandbox. Documents the person opened are not scanned; the importers and the typesetter run in the agent's process.
  • The local link is not a security boundary. A process of the same user with a shell can claim to be interactive, write the file on disk, or run anything the user can. The host treats a participant as an agent when it is not interactive or its id begins with agent. (whatever else it claims), unless the person marks it as a person, and checks the shape of every patch and recomputes its inverses, but this protects against content written through the usual paths, not against a hostile process of the user.
  • What the person's TeXmacs filters. Since step 4 (stage A) the host applies the same rules to the patches of participants it treats as agents, so an agent that writes its own client of the local link is filtered too. A participant is treated as an agent when it is not interactive or its id begins with agent., unless the person marks it as a person; only a host that is itself interactive filters.
  • Click-only links: links to absolute paths, .., or tmfs://help/... are accepted; they act only when clicked, and do no more than opening a file to view.
  • Hard links and a narrow time window: a hard link inside the roots to a file outside is followed; a file swapped for a symbolic link between the check and the load would be read.
  • The analysis over-approximates: rarely used macros of the styles may be refused although harmless (the tiled tables, some literate and algorithm declarations, select...). The list is in src/docs/mcp-server.md.
  • Platforms: tested on macOS with Qt and s7; not yet with Guile, Vue, Cocoa, Windows or in the browser.

7. Open questions

  • Asking the person, and a trusted agent: built, audited, set aside (5 November). The idea (the maintainer's, 4 November): the first time an agent's edit breaks a rule, the person is asked, in a dialog of their TeXmacs or through MCP elicitation when nobody has the file open, and answers refuse, allow this edit, trust this agent for the session, or for this file from now on (remembered in the person's preferences, never in the document). Levels: style (definitions, packages) and links, files and names in exports, granted separately; code never (decided 4 November), nor the limits of work, nor the modes. Trust visible and revocable in Tools > Agents; in propose mode a trusted edit is still a proposal.

    It was built on wip_agents (commits e884dcb7e0, 4d13edb6c5, edbf61f312) and audited three times. In the audits, code, limits and modes were never granted on any path tried, and an agent could not grant itself anything. But each audit found a new way for a trusted agent to do more than the person had seen:

    • Allow once spent on another edit: the grant was keyed by an id the client chooses; a client of the local link showed the person one link and had a different one applied.
    • Changing the meaning of the person's text: with style trust, <assign|strong|<macro|x|<hlink|<arg|x>|https://...>>> turns every existing bold text into a link. Fixed by making any change of an existing name a "redefinition", asked every time; then provide, drd-props, use-package, macros of the style reached only through other macros, set and reset had to be added; the third audit then found that a new macro whose body redefines an existing one passed as a plain style question (and calling it was not asked at all), and that primitives (<drd-props|concat|arity|5>) and tags the text already used undefined were not counted as existing; these were never fixed.
    • Cost: checking the uses of a redefined macro multiplied the work by the number of uses (a chain of helper macros froze the person's TeXmacs, or took 2 GB), until one budget covered all of them.
    • The question itself: while waiting for the person's answer, the server kept serving the other participants of the shared file and sent them the edit before it was approved; a refused edit stayed in their copy.
    • Preferences: a name with a quote or a backslash made all remembered grants unreadable.

    Each fix closed the hole it was written for, and the next audit found another: the language of TeXmacs documents (macros that define macros, styles, packages, settings, the DRD) is too rich to describe "what this edit may change" by rules. Decision (5 November): stage B is reverted for the moment (e00d757d2f; the branch is back to stage A, e8f55e1e79, audited clean). Agents keep the stage A behaviour: the rules of section 3 are never lifted, and the safeguard is the person's review of proposals. If trust comes back, the lesson is to grant it on what the person sees, the result, not on rules about the source: e.g. show the change of the typeset document, or let an agent define only names in a namespace of its own that the person's text does not use.

  • Propose mode (step 4, done). The filter still applies to proposals: refused content never becomes a proposal, because a proposal shows the markup, not what it does (a javascript: link looks harmless).

  • Enforcement by the host (step 4, done). The person's TeXmacs applies the same rules to the patches of participants it treats as agents (every participant that is not interactive or whose id begins with agent., unless the person marks it; only a host that is itself interactive filters), and checks the shape of every peer's patch.

  • Sandboxing the process (macOS sandbox, seccomp on Linux): would make "the agent's process cannot read outside the roots" a guarantee of the system, not of TeXmacs.

  • Maintenance. The rules depend on the styles: a new style macro with a sensitive argument is caught by the analysis, a new extern function by a test; a new primitive must be classified (a test checks that every primitive is in the table).