协作 AI 助手(MCP)
AI 助手
@rebodocs/mcp 把用户在编辑器中能做的操作,以 51 个 Model Context Protocol 工具的形式提供给 AI 助手。“填写这份合同”或“把这个列表变成表格”都是在真实文档上完成的,排版和格式完好无损,而不是在文字副本上操作。
选择哪种方式?
同一套工具可以通过三种方式作用于文档。前两种在你的电脑上运行服务器;第三种把 Claude 放进你自己的应用。
扮演 AI 助手
真实编辑器中的示例文档,以及 AI 助手调用的工具,由服务器和聊天使用的同一套代码在其上运行。返回的内容与 AI 助手读到的一字不差。
正在打开编辑器…
段落按编号引用,与 read_document 显示的一致;每个修改文字的工具都会回答它做了什么:AI 助手不必在每一步之后重新读取整份文档。错误的参数会以一句普通的话返回,供它自行纠正。在文档的 45 个工具中,35 个会修改文档,其余只读。工具说明是服务器告诉 AI 助手的原文,为英文。
配置
适用于前两种方式:服务器会随 AI 助手一起启动。选择你的 AI 助手,复制它需要的配置。
你的 AI 助手
写入
~/Library/Application Support/Claude/claude_desktop_config.json(Windows 上为 %APPDATA%\Claude\claude_desktop_config.json)。修改后请重启它。
{
"mcpServers": {
"rebodocs": {
"command": "npx",
"args": [
"-y",
"@rebodocs/mcp",
"--root",
"/Users/you/Documents/contracts"
]
}
}
}它来自 npm,引擎作为其依赖:无需克隆任何内容。然后对 AI 助手说:“打开 /Users/you/Documents/contracts/lease.docx,为 Ana López 填写”。
服务器选项
- --root DIR
- 允许读写文件的文件夹;可以指定多个。请至少指定一个:不指定时,整台机器上的任何位置都可以。
- --fonts DIR
- 在系统字体之前查找字体的文件夹,用于排版和生成 PDF。
- --port N
- 浏览器中的编辑器连接的端口。默认为 7411,或使用 REBODOCS_MCP_PORT。
- --no-editor
- 只处理文件:不监听编辑器。
- --origin URL
- 除 localhost 外允许连接的页面。
- --host ADDR
- 用于监听其他机器上的编辑器(配合 --http 时还包括客户端)。此时需要 12 个字符以上的 --token,或 REBODOCS_MCP_TOKEN。
- --http N
- 通过 HTTP 在 http://127.0.0.1:N/mcp(Streamable HTTP)监听 AI 助手,每个助手有自己的会话,而不是使用标准输入输出。设置 --token 时,客户端以“Authorization: Bearer”提供。
- --timeout S
- 等待编辑器响应工具调用的秒数:默认为 30。
- --editor-url URL
- 你的编辑器的地址:没有编辑器连接时告诉 AI 助手,以便它说明在哪里打开。
通过 HTTP
适用于连接到服务器而不是自己启动服务器的客户端,或位于其他机器上的服务器:使用 --http 时,服务器在 /mcp 监听(协议的 Streamable HTTP),每个客户端都有自己的会话和自己打开的文件。浏览器中的编辑器对所有客户端都是同一组。
# The server, listening for assistants at http://127.0.0.1:7412/mcp
npx -y @rebodocs/mcp --root /path/to/documents --http 7412
# Claude Code, pointed at it
claude mcp add --transport http rebodocs http://127.0.0.1:7412/mcp
# From another machine: --host and a --token of 12 characters or more,
# which the client gives as "Authorization: Bearer <token>"
npx -y @rebodocs/mcp --root /srv/documents --http 7412 --host 0.0.0.0 --token "$REBODOCS_MCP_TOKEN"在你自己的编辑器中添加按钮
只有用户打开“AI”时编辑器才会连接,并且重新加载后不会记住该设置。AI 助手做的每件事都可一步撤销。
// The “AI” button, for a server on the person's machine
<DocsEditor features={{ assistant: true }} />
// …or one on another machine, started with --host and --token
<DocsEditor features={{ assistant: { url: 'http://10.0.0.5:7411', token } }} />AI 助手可以访问什么
AI 助手只能做工具允许的事,且只能在服务器允许的范围内。以下是这些限制,以及分别由谁设置。
只限你指定的文件夹
使用 --root 时,这些文件夹之外的路径会被拒绝,并告诉 AI 助手原因。不使用时,服务器可以使用这台机器允许它访问的任何文件。
保存之前不写入
打开的文件在内存中修改。由 save_document 写入,除非 AI 助手指定 overwrite,否则绝不覆盖已有文件;new_document 不接受已存在的路径。
默认只限本机
服务器监听 127.0.0.1。页面只能从 localhost 或你指定的 --origin 连接。要监听其他机器(--host),没有 12 个字符以上的 --token 就不会启动。
由用户开启
只有打开“AI”时编辑器才会连接,重新加载后不会自动再连接。每处更改都可一步撤销,视图会跳到更改处。
是建议,而不是直接修改
开启 track_changes 后,工具做出的更改会保留标记,署名“Assistant”,由用户接受或拒绝。
可以回退
对于文件,undo 可以从最近的更改往回撤销,最多 50 处;在编辑器中,每处更改都是用户可以撤销的一步。edit_document 要么完成全部编辑,要么一个也不做。
客户端知道哪些只读
每个工具都会说明它是否只读、重复执行是否会改变更多内容,以及是否可能丢失工作:只有 save_document 和 close_document 可能。客户端可以让只读工具无需确认直接运行。
在你的应用中:谁可以用、多频繁
assistantRoute 通过 allow 判断谁可以使用,并按 key 区分用户,给每个用户 perMinute 个步骤;API 密钥留在你的服务器上,对话留在页面中。
全部工具
服务器告诉 AI 助手的每个工具的说明(英文),在构建本页时从 @rebodocs/mcp 的源码中读取。“试一试”会把它加载到上面的编辑器中。
文件
服务器独有:你的应用中的聊天没有文件,因此没有这些工具。
Opens a Word file (.docx) to read it and change it. It is kept in memory: nothing is written until save_document. Gives its overview.
Starts an empty document, to write in and save. Gives it the path it will be saved to.
Writes a document to a file: Word (.docx), PDF (.pdf) or Markdown (.md), by the ending of the path. A DOCX that was opened keeps everything that was not changed exactly as it was. It does not write over a file that is there unless "overwrite" is true.
Undoes the last changes the tools made to a file that is open, one at a time or several: the way back from a mistake without opening the file again and losing the rest. Up to 50 changes. In the editor in the browser, each change is a step the person undoes there.
The documents that can be worked on now: the files that are open, with whether each has changes not saved, and whether an editor is connected.
Forgets a file that is open, with any changes that were not saved.
读取
A summary of the document: how long it is, its headings with the paragraph each is at, its template variables, its notes and its paragraph styles. Start here.
The text of the document, one paragraph a line, each with its number in brackets and, in parentheses, its style and its table cell when it has them. A long document comes in parts: ask for the next with "from".
How paragraphs look: for each, its style, alignment, indents, the room before and after it and its line spacing; then each stretch of its text that looks different, with its font, its size and whether it is bold, italic, underlined or coloured. Read it before rewriting or adding text that has to look like what is there.
How the document falls on its pages, as it prints: for each page, the paragraphs on it, how it starts and ends and how much room is left at its foot; and what may want mending: a paragraph cut by the end of a page, a heading left at the foot of one with its text on the next, a page with only a line or two, a table that runs over pages, a page with nothing on it. Read it after changes that move text, before a document goes out.
Every place a text is found, with the paragraph it is in and the words around it.
The links of the document: the text of each, where it goes and its paragraph.
The notes of the document: the id of each, who wrote it, the text it is about and what it says.
The template variables of the document, written between double braces: the name of each, how many times it is used and the formats it is given (capitals, a date, money…).
What changed between this document and another version of it in a .docx file ("path"): the paragraphs put in, taken out and changed, of the text and of its headers, footers, notes at the foot of the page and text boxes, and the notes on the text (as add_note makes), with what was taken out as [-…-] and what was put in as {+…+}. Tracked changes are compared as if accepted, as Word does. By default the file is the earlier version; with "file_is" "later", it is the later one.
文字
Puts a text in place of another, in the formatting of what was there: its font, its size and its marks. The text to find has to be inside one paragraph. An empty "replace" deletes it. To write a whole paragraph anew, as in a translation, rewrite_paragraph keeps more of its look.
Writes the whole text of a paragraph anew, as to translate it or say it otherwise, and keeps how it looks: its style, alignment, indents and spacing, and the font, size and colour most of its text has. Words that were set apart (bold, italic…) are kept so by giving the text in "segments". A paragraph with a picture is changed with replace_text instead.
Writes new paragraphs after a paragraph. Each has its text and, when wanted, a style (a heading, for one) or a list. With "like_paragraph" a new one looks like a paragraph that is there: its alignment, indents, spacing, list, and the font, size and colour of most of its text. Text between double braces, like {{cliente}}, is a template variable.
Deletes whole paragraphs, from one to another. Not for the paragraphs of a table.
Makes several changes in one go, each as the tool it names would make it, and all of them or none. Every paragraph number in them is the one read_document showed before the first edit: the numbers do not move as the edits before insert or delete paragraphs, so there is no need to read again between them. A text to find is looked for as the document is when its edit comes. Each edit is an object with "tool" (replace_text, rewrite_paragraph, insert_paragraphs, delete_paragraphs, set_style, set_list, format_text, format_paragraphs, set_link, add_note, insert_page_break, move_section, set_cell_text, renumber_clauses) and the arguments of that tool beside it. The text of the document only: for a header or a footer, use each tool alone with "area".
Moves a heading and everything under it, down to the next heading of its level or above (a clause with its text, a chapter with its clauses), to another place, as dragging it in the outline of the editor does. The paragraphs keep their look, their notes and their pictures. After it, renumber_clauses puts the numbers of the headings in order.
Puts in order the numbers written at the start of the headings of clauses, after one was put in, taken out or moved: "Primera.", "Segunda."… in words, "CLÁUSULA 3.", or "IV.", each kept as it is written (its capitals, "Primera" or "Primero", "Décima Primera" or "Undécima"). The headings of one series are those that begin the same way and have the same style; the longest is renumbered, or the one of "paragraph". It says the places of the text that name a clause by its number ("según la cláusula Tercera"), for you to check: it does not change them.
Gives paragraphs a paragraph style: a heading, a title, the normal text. get_overview lists the styles of the document.
Makes paragraphs the items of a list with bullets or with numbers, or takes them out of the list they are in.
Makes a text a link to a web page, a mail address or a telephone; or, with an empty "url", takes its link away.
格式
Changes how a text looks: its font, its size, bold, italic, underlined, struck through, its colour or the highlight behind it. The text is one found by "find", or whole paragraphs, from "from" to "to". Give only what is to change.
Changes how paragraphs sit on the page: their alignment, indents, the room before and after them, their line spacing, their tab stops, and whether they keep with the next one. Give only what is to change.
批注与修订
Puts a note on a text: a comment in the margin, as in Word, that does not change the document. The way to review a document, or to ask about something in it.
Takes a note away; the text it was about stays.
Switches tracked changes on or off, as Word's "Track changes". While on, every change made to the text, by any tool, stays marked for a person to accept or reject: what was put in as {+…+} and what was taken out as [-…-] in read_document.
The tracked changes of the document, numbered: who made each, whether it put text in, took it out, moved it or changed its formatting, the text, and the paragraph it is in. Text one person put in and another took out is two changes. accept_changes and reject_changes take these numbers.
Accepts tracked changes: what was put in stays, unmarked, and what was taken out goes. With "changes", the ones of those numbers in list_changes; with "author", all of that person's; without either, all of them. A move is accepted whole.
Rejects tracked changes: what was put in goes, and what was taken out comes back. With "changes", the ones of those numbers in list_changes; with "author", all of that person's; without either, all of them. A move is rejected whole.
模板
Puts a value in place of each template variable that is given one, everywhere in the document (its headers, footers, notes and text boxes too), in the format the variable asks for, and writes out each block given one: {{#name}} … {{/name}} is repeated for each item of a list (a row of a table, paragraphs, or text), kept for true and taken out for false; {{^name}} … {{/name}} the other way round. Dates go as YYYY-MM-DD and numbers plain (1234.5): the format writes them out. Variables and blocks with no value stay as they are.
表格与图片
Puts in a table after a paragraph, with the text of each cell. Every row must have the same number of cells.
Changes the shape or the look of a table that is there. "paragraph" is any paragraph of the cell to act on (read_document says which cell each is in). Actions: insert_row_above, insert_row_below, insert_column_left, insert_column_right, delete_row, delete_column, merge_cells (from "paragraph" to "to_paragraph"), split_cell (undoes a merge), set_column_widths (with "widths_cm", one for each column), set_row_height (with "height_cm", 0 for as tall as it holds, and "exact"), header_row_on and header_row_off (the rows down to this one repeat at the top of each page), align_cells_top, align_cells_center, align_cells_bottom, text_level, text_up and text_down (the text of the cells set level, or vertically to read from the bottom up or from the top down; its lines are as long as the row is tall), shade_cells (with "shade"), cell_lines (with "line_style", "line_width_pt" and "line_color"; "line_style" none takes them away), show_borders, hide_borders (the whole table), keep_together (the whole table on one page, as a block of signatures should be: no row is cut, and each row stays with the next), allow_split (undoes it) and delete_table. The actions on cells take "to_paragraph" for several. The text of a cell is changed with replace_text and insert_paragraphs.
Writes the text of a cell of a table, by its table, row and cell, as read_document shows them ("table 1, row 2, cell 3"): what the cell had goes, empty or not, and the text takes the look most of it had. A new line in "text" starts another paragraph in the cell. The way to fill the cells of a row just put in.
Puts in a block of signatures after a paragraph: side by side, each with its title (such as ASESOR and CLIENTE), a line to sign on and the name under it.
Puts a picture from a file (PNG, JPEG or GIF) in a paragraph of its own after a paragraph. It goes at its own size, or as wide as the text when it is wider, unless "width_cm" says otherwise.
Changes a picture that is in the document: "resize" it (with "width_cm"; its height follows), "move" it to a paragraph of its own after another (with "after_paragraph"), "align" the paragraph it is in (with "align"), "describe" it for whoever cannot see it (with "description", what a screen reader says; empty takes it away), or "delete" it. read_document shows a picture as [image], or [image: its description].
页面与分栏
Changes the size of the paper, its orientation, or its margins; and, for one section of a document of several, whether it has a header and footer of its own and whether its first page has its own. Give only what is to change. Without "section", the page of every section changes.
Gives the document a letterhead: a picture from a file (PNG, JPEG or GIF), stretched over the whole page behind the text of every page, in place of the one it had. For a PNG, the top and bottom margins grow to clear what it prints there. With "remove", takes it away; the rest of the header stays. get_overview says whether the document has one.
Sets text in columns, as a newsletter: one column, two, three, with a line between them or not. With "from" and "to" only those paragraphs go in columns, in a section of their own on the same page; with "paragraph" alone, the whole section that paragraph is in.
Makes the text from a place start at the top of the next column, in a section set in columns: before a text found with "find", or at the start of a paragraph.
Puts the number of the page in the header or the footer of every page, moves it, changes how it reads, or takes it away.
Makes the text after a paragraph start on a new page.
Puts a note at the foot of the page: its number goes in the text right after a text found with "find" (or at the end of paragraph "at_paragraph"), and the note says "text". Notes are numbered by where they are in the text.
Puts a note at the end of the document, after its text (or at the end of its section, when the document puts them there): its number (i, ii, iii) goes in the text right after a text found with "find" (or at the end of paragraph "at_paragraph"), and the note says "text". Notes at the end are numbered by where they are in the text, apart from those at the foot of the page.
Puts a text box in: a box with a thin black line, anchored to a paragraph (at the end of "after_paragraph"; 1 when left out), at the left of the column, with the text of that paragraph going round it. It is as tall as its text. Its text is "text"; each line of it a paragraph. Change it later with area "textbox".
Puts in a table of contents after a paragraph: the headings of the document, each with its page and a link to it. A document has one.
Writes the table of contents again from the headings and the pages the document has now. Do it after changing headings, or text that moves them to other pages.
保留格式
默认情况下,AI 助手看到的是文字,而不是它的样子。read_formatting 会逐段告诉它:样式、对齐、缩进、间距,以及每一段文字的字体、字号和标记。
重写一个段落(例如翻译),保留其对齐、缩进和间距,以及大部分文字原有的字体、字号和颜色。按片段提供时,原来加粗或倾斜的词仍保持原样。
使用 like_paragraph 时,新段落的格式与已有的某个段落相同。
保留被替换文字的格式。
可以在上面的编辑器中查看:“格式”读取第 4 段,“翻译并保留加粗”把第 10 段译成英文,加粗和红色的词仍保持原样,“添加同样格式的段落”会插入一个与周围一致的项目符号。你的应用中的聊天会在每次对话前这样告诉 Claude:改写前先读取格式,只有在被要求时才修改字体、字号或间距。
处理合同
合同有编号的条款、金额表格,以及不能被分页截断的签名。除了读写文字之外,这些工具就是为此而设计的。
一次调用完成多项更改,要么全部完成,要么一个也不做:替换、改写、插入、删除、样式、格式、批注、移动条款、单元格。其中的段落编号都基于同一次读取,因此 AI 助手不必每次插入后都重新读取。
在插入、删除或移动条款之后,重新排列标题开头写的编号:“Primera.”、“CLÁUSULA 3.”或“IV.”,各自保持原有写法。它会列出正文中以已变化的编号引用条款的位置,但不修改它们。
移动一个标题及其下的所有内容,就像在大纲中拖动它一样:条款保留其格式和批注。
按表格、行和单元格写入一个单元格,无论是否为空:这是填写刚插入的行的方法。
用文字描述文档的分页情况:被分页截断的段落、留在页尾的标题、只有一行的最后一页、跨两页的表格。
使用 keep_together 时,签名栏会完整地留在同一页,放不下时才整体移到下一页;format_paragraphs 用 keep_lines_together 让段落不被拆开,用 keep_with_next 让它与下一段在同一页。
上面编辑器中的“一次多项”和“分页情况”会在示例文档上运行其中的第一个和最后一个工具。
在你自己的应用中
“AI”按钮连接的是用户电脑上的服务器。要在你的应用中为所有用户提供 Claude,页面与你的后端通信,你的后端再与 Claude API 通信。
- 1
用户提问
createAssistantChat 把目前为止的对话发送到你服务器上的 POST /api/assistant。
- 2
你的路由询问 Claude
assistantRoute(在 Next.js 中为 assistantHandler)检查谁可以提问(allow)以及频率(perMinute),然后把对话连同文档工具发送给 Claude。回答会随 Claude 的生成逐步返回:每段文字和每个工具请求各一行 JSON,最后是完整的回答。
- 3
页面运行工具
Claude 请求的每个工具都会在用户打开的编辑器上运行,作为可撤销的一步;在共享房间中,其他人看到的是该用户自己的更改。
- 4
……直到完成
结果会在下一步交还给 Claude,直到它无事可做:每个问题最多 40 步(maxSteps)。对话保存在页面中;服务器不保存任何内容。
在你的服务器上
npm install @rebodocs/mcp,再加一个用你的密钥把对话交给 Claude 的路由。除非指定其他 model 或 effort,它会以中等 effort 调用 Claude Opus 5.5,并给每个用户每分钟 40 个步骤(perMinute)。不提供 key 时,用户按请求来源地址区分,而在代理之后所有人的地址都相同:请传入你会话中的用户。context 在指令之后告诉 Claude 你的应用知道的信息。toolSearch: true 让 Claude 在需要时再搜索表格、页面和审阅相关的工具,而不是每一步都读取全部工具。
import { assistantHandler, assistantRoute } from '@rebodocs/mcp/claude';
// ANTHROPIC_API_KEY in the environment.
// Next.js: app/api/assistant/route.ts
export const POST = assistantHandler({
allow: async (request) => !!(await sessionOf(request)),
key: async (request) => (await sessionOf(request))!.userId, // perMinute counts per user
context: async (request) => `The adviser is ${(await sessionOf(request))!.name}.`,
});
// A server of Node's own: answers POST /api/assistant.
const assistant = assistantRoute({ allow: (request) => isSignedIn(request) });
http.createServer(async (request, response) => {
if (await assistant(request, response)) return;
// …the rest of your server
});在页面中
import { createAssistantChat } from '@rebodocs/react';
const chat = createAssistantChat(handle.editor, { onChange: redraw });
await chat.send('Translate the document into English, keeping its format.');
chat.entries; // what to show: the person, Claude, each tool it used
chat.busy; // true while it works; chat.stop() ends it after this step你的密钥,你的账单。 运行服务器的人要为打开的页面发出的请求付费。请在 allow 中校验会话,并把 perMinute 设低一些。想看实际效果,可以运行 npm run assistant 启动演示。
注意事项
- 插入或删除后段落编号会变化。edit_document 用同一次读取的编号完成多项编辑;在它之后,或在单个工具之后,AI 助手需要重新读取才能使用旧编号。
- 工具中的长度以厘米和磅为单位,与人们的说法一致;编辑器自己的 API 以磅为单位。
- 开启修订期间,工具做出的更改也会记录为修订,署名“Assistant”。
- 你的应用中的聊天无法从路径插入图片或信头,也无法与文件比较:这些需要服务器的磁盘。
- read_pages 用文字描述文字的分页情况;没有页面图片供 AI 助手查看。
- 客户端取消调用后,服务器不再等待编辑器;编辑器已经做出的更改会保留,可一步撤销。
- 在共享房间中,AI 助手通过提问者的编辑器工作:其他人看到的更改属于该用户,但看不到对话内容。