Free Intensive Nov 21–22Free 2-day hands-on Vibecoding Intensive on Nov 21–22 at 16:00 MSK (Sat–Sun) · Live Cohort 3 starts Nov 23Join Free →Cohort 3 (Nov 23)
ReleaseInsight · On RadarHabr AI→ original

A Lawyer Taught an AI Agent to Make Surgical Edits in Word with Track Changes

A lawyer got tired of copying edits from an AI chat into contracts. Instead of a fixed set of commands, he gave the model a Python sandbox. The agent parses the XML inside the .docx and makes targeted edits with Track Changes: it changes "30" to "45" without rewriting the paragraph. For files with unpredictable structure, that flexibility proved more practical than ready-made operations.

AI-processed from Habr AI; edited by Hamidun News
A Lawyer Taught an AI Agent to Make Surgical Edits in Word with Track Changes
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Key metric #1The Veqtor Workspace server gives the model four MCP tools and Python without network access
Key metric #2It works with Claude and OpenAI models; the server has no built-in model
Key metric #3The original file is preserved, and a human still checks the final result in Word

Anyone who has worked with contracts knows the routine: you discuss a good wording with an AI, then manually hunt for the clause in Word, replace the text, check the neighboring provisions, and add a comment. The author of this story, a lawyer, decided to eliminate exactly that step. He succeeded, but not on the first try, and the path is more interesting than the result: it shows where the line falls between pre-written operations and code that an agent writes on the fly.

Why the first version ran into the file format

At first, the author went for strict control. He built an MCP server with a set of predefined operations on a document. MCP is needed here so that a connected model can call the server's tools. The model decides what to change, and ordinary code carefully performs the replacement: it finds the text, checks the original state, makes the edit, and makes sure nothing extra is touched.

The problem is that .docx is a ZIP archive of XML parts. A phrase that looks like a single line in Word is often split into several fragments inside. A comment may sit next to it, part of the text may be marked as inserted and part as deleted, and all of it may sit inside a table cell. For a lawyer, that is an ordinary contract after several rounds of negotiation. For a program that has to replace a couple of words without breaking other people's revisions, it is a pile of special cases.

Every new document required extending the rules and checks. At some point the author realized he was building an ever more complex Word editor, even though the task was more modest: stop transferring edits by hand.

Second version: the server provides the environment, the model writes the code

So he flipped the design. Modern models write code well, including code to parse a specific file, so why limit them to a list of operations a human managed to implement? That is how Veqtor Workspace came about. The server gives access to selected documents and a Python environment, while the agent studies the file, parses the XML when necessary, and writes code for the specific change. If the code crashes, the agent gets the error and fixes it.

The server has four MCP tools:

  • workspace_info explains how the working environment is set up;
  • document_task creates a task with a working copy of the document and saves the result;
  • run_python executes the code the model wrote;
  • check_docx compares the original with the result and, if needed, generates a preview.

The restrictions remain on the server side. Python runs locally, without network access, and only with the selected files and a separate working folder. The original version is preserved, and before writing the result, the server checks whether the original changed while the agent was working. There is no language model in the server: the one it is connected to does the reasoning, whether that is Claude or an OpenAI model.

What came out of it, and the catches

According to the author, in tests with Claude and OpenAI models, the agent read documents, found the right passage, and prepared a file with edits in Track Changes mode. What matters most to a lawyer is exactly how the changes look. If a deadline changes from 30 to 45 days, you need to see the number replaced, not a deleted and reinserted paragraph; otherwise the other side has to compare the entire text. The new setup produces targeted edits that can be reviewed in Word, then accepted or rejected.

The format of the request is also telling: "replace 30 days with 45, keep the other wording, formatting, existing revisions and comments, make the change in track changes mode." Having the final number 45 in the text is not enough; what's needed is a document that people will keep working with.

The author names the limitations himself:

  • the model may choose a poor way to make an edit, touch extra text, or misread earlier revisions;
  • XML validation does not replace reviewing the file in Word itself;
  • the server works with the saved file and does not see unsaved changes in an open window;
  • the code runs locally, but with a cloud model, the text it reads goes to the model through tool-call results, which matters for confidential contracts;
  • flexibility costs time and tokens: the agent has to figure out the file, write code, and sometimes correct itself, while a ready-made function for a known case is faster and more predictable.

Practical takeaway

The author divides responsibility this way: file access, preservation of originals, and writing the result stay under the server's control, the model chooses how to read and edit, and a human reviews the final result. For developers, this is a workable pattern for any format with a "messy" internal structure: Excel, PowerPoint, PDF. If the inputs are too varied to describe every scenario in advance, it is cheaper to give the agent a sandbox and strict access boundaries, and to keep deterministic functions for frequent, well-known cases. The author calls himself a vibe coder and asks professionals to suggest where to draw the line between the model's freedom and pre-implemented operations.

Story

Key Questions Answered

Why the first version ran into the file format?

At first, the author went for strict control. He built an MCP server with a set of predefined operations on a document. MCP is needed here so that a connected model can call the server's tools. The model decides what to change, and ordinary code carefully performs the replacement: it finds the text, checks the original state, makes the edit, and makes sure nothing extra is touched. The problem is that .docx is a ZIP archive of XML parts.

What are the key details on Second version: the server provides the environment, the model writes the code?

So he flipped the design. Modern models write code well, including code to parse a specific file, so why limit them to a list of operations a human managed to implement? That is how Veqtor Workspace came about. The server gives access to selected documents and a Python environment, while the agent studies the file, parses the XML when necessary, and writes code for the specific change. If the code crashes, the agent gets the error and fixes it.

What came out of it, and the catches?

According to the author, in tests with Claude and OpenAI models, the agent read documents, found the right passage, and prepared a file with edits in Track Changes mode. What matters most to a lawyer is exactly how the changes look. If a deadline changes from 30 to 45 days, you need to see the number replaced, not a deleted and reinserted paragraph; otherwise the other side has to compare the entire text. The new setup produces targeted edits that can be reviewed in Word, then accepted or rejected.

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Hands-On AI Engineering · Hamidun Academy

Build & Ship Production AI Agents Without Boilerplate Code

Start your free 72-hour trial at Hamidun Academy (academy.hamidun.com) — interactive lessons on Claude Code & Cursor, live Tech Radar, and production agent templates.

Start 72h Free Trial →Vibe-Coding Intensive
What do you think?
Loading comments…