Skip to content
mgubi edited this page Nov 4, 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 4 November 2026: step 3 of the plan (editing, direct mode), audited clean at a7a98074e7 after many rounds, not merged. Limits measured at the end: 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), 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 (open, audited clean): 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 person's TeXmacs does not filter what it receives. As host of a shared file it applies an agent's patches as it applies any participant's. An agent that bypasses texmacs --mcp (writes its own client of the local link) is not filtered. To do in step 4: the host applies the same rules to patches of participants marked as agents.
  • 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 (proposed by the maintainer, 4 November). The first time an agent's edit breaks a rule, the edit is not simply refused: the person is asked, and can let it through or trust the agent. Points of the design:
    • Who decides. Only the person, never the agent's process: a process cannot grant itself trust. When the person has the file open, the agent's server sends the refused edit to the person's TeXmacs (the host), which shows a dialog. When the file is not open anywhere else, the server asks through the MCP client with an elicitation (elicitation/create, MCP 2025-06-18), which the client shows to its human user; a client without elicitation gets a refusal, as now.
    • What the dialog shows. The rule broken and the content concerned, as the person would see it (the address of the link, the file, the macro defined, the raw LaTeX), not only a tag name; and the document.
    • The answers. Refuse (the default, and the answer when nobody answers within a time); allow this edit; trust this agent for this document for the rest of the session; trust it for this document from now on (remembered by the person's TeXmacs, not in the document, so that a document received from someone else carries no trust).
    • Levels of trust. Not all rules are equal: (1) style: definitions, packages, new theorems and the like; (2) links and files: any link scheme, files outside the folder; (3) code: extern, scripts, raw LaTeX and HTML. A trusted agent gets level 1, and level 2 or 3 only if asked for explicitly in the dialog. The limits of work (loops, sizes, the budget, the stack guard) and the escaping of the exporters stay in every case.
    • Fatigue. One question per document and session, with the content shown, so that the person answers knowingly, rather than a question per edit.
    • Trust is visible. The person's TeXmacs shows that an agent is trusted for a document (in the footer or the menu of agents of step 4) and can withdraw it.
    • With proposals (step 4): in propose mode an edit that breaks a rule arrives as a proposal marked with the rule it breaks; accepting it is the consent.
  • Propose mode (step 4). If an agent's edits arrive as proposals the person accepts or rejects, should the filter still apply, or only warn? The proposal shows the markup, not what it does: a javascript: link looks harmless in a proposal. Proposed: keep the filter, and show refused content as a warning in the proposal.
  • Enforcement by the host (section 6): the same rules in the person's TeXmacs for patches of agents.
  • 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).

Clone this wiki locally