@betteroffice/agents

中文文档
0.3.0发布于 2026/10/27Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 7648

Search, inspect, propose, preview, and export Office document edits through typed tools and MCP.

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. MCP server
  4. SDK
  5. Entry points
  6. MCP tools
  7. Edits and references
  8. Limits
  9. Documentation

@betteroffice/agents

Search, read, edit and preview DOCX, XLSX and PPTX files from an agent, through a TypeScript SDK or the betteroffice-mcp server. Agents work on large files the way they work on source code: grep, read a small region, make an exact edit, check the result, export.

No model provider, API key or agent framework is required.

Install

bun add @betteroffice/agents

Requires Node.js 22 or later.

Usage

MCP server

bunx --package @betteroffice/agents betteroffice-mcp --root /absolute/path/to/documents

The server speaks MCP over stdin/stdout, so connect it from an MCP client. For example, with Codex:

codex mcp add betteroffice -- /absolute/path/to/node_modules/.bin/betteroffice-mcp --root /absolute/path/to/documents
flagdescription
--root <directory>Required. Every file path is resolved against it, including symlinks.
--read-onlyExposes only the inspection tools: no proposals, edits, acceptance or export.

Exports always write a new file and never overwrite an existing one. Path checks are not an operating-system sandbox, so use a workspace directory you control.

SDK

import { readFile, writeFile } from 'node:fs/promises';
import { openDocx } from '@betteroffice/agents';
import { renderDocxPage } from '@betteroffice/agents/render';

const doc = await openDocx(await readFile('report.docx'), {
  name: 'report.docx',
  renderer: renderDocxPage,
});

try {
  const [hit] = doc.grep({ query: '€4.2 million' }).matches;
  if (!hit) throw new Error('Revenue text not found');
  const proposal = doc.propose({
    author: 'report-agent',
    note: 'Update the executive summary revenue',
    edits: [{ match: hit.match, newText: '€5.1 million' }],
  });

  const preview = await doc.render(1, proposal.id);
  await writeFile('preview.png', preview.png);
  console.log(doc.review(proposal.id), await doc.verify(proposal.id));
  await writeFile('report-revised.docx', await doc.export(proposal.id), { flag: 'wx' });
} finally {
  doc.close();
}

Exporting a pending proposal leaves the document unchanged. To apply it, call await doc.accept(proposal.id) and then doc.export() without an ID. For Word tracked changes, call doc.accept(id, { tracked: true, date: '2026-09-27T12:00:00Z' }).

Entry points

exportdescription
openDocx(bytes, { name?, renderer? })Opens a DOCX file in its own session.
attachDocx(session, options?)Wraps a live editor's YrsSession. Closing the adapter leaves the session open.
openXlsx(bytes, { name?, wasm? })Opens an XLSX workbook: cell reads and cell proposals.
attachXlsx(handle, options?)Wraps a live WorkbookHandle.
openPptx(bytes, { name?, wasm? })Opens a PPTX presentation: outline, slide reads, text proposals, atomic edit batches and preview.
attachPptx(handle, options?)Wraps a live PresentationHandle.
renderDocxPage (from @betteroffice/agents/render)Node canvas renderer for DOCX page previews.
renderPptxSlideNode canvas renderer for PPTX slide previews.
createOfficeMcpServer({ root, renderer?, readOnly? }) (from @betteroffice/agents/mcp)The MCP server, for hosts that connect their own transport.

DOCX and PPTX documents stage text edits with grep, read and propose; workbooks use readCells and proposeCells instead. All adapters share review, accept, reject, verify, export and close. openXlsx and openPptx load the bundled WebAssembly in Node; in a browser, pass the module bytes as wasm. DOCX previews in a browser need a custom DocumentRenderer instead of the Node canvas renderer.

MCP tools

toolpurpose
office_filesList DOCX, XLSX and PPTX files, subdirectories and open document IDs.
office_openOpen a file and return its document ID, format and capabilities.
office_outlinePage through DOCX paragraphs, PPTX slide paragraphs or XLSX sheets.
office_grepLiteral search with context; returns match IDs or cell handles.
office_readRead a paragraph or a cell field; DOCX reads include run formatting.
office_cellsXLSX: read an A1 range with values, formulas and cell handles.
office_proposeDOCX/PPTX: stage 1-32 text replacements as { match, newText }.
office_propose_cellsXLSX: stage 1-32 whole-cell inputs or formulas.
office_reviewList proposals or show one proposal's before/after changes.
office_acceptApply a proposal as one undo step; DOCX can keep it as tracked changes.
office_rejectDiscard a pending proposal.
office_renderDOCX: PNG of a current or proposed page.
office_verifySave and reopen in memory; reports which checks ran.
office_exportWrite accepted edits or a pending proposal to a new file.
office_closeRelease an open document and its proposals.
docx_editDOCX: atomic direct edits of paragraph styles, bold/italic/underline, paragraphs, table rows, cell merges, comments (add, reply, resolve or reopen) and page setup with insert_section_break and set_section; runFormat: 'anchor' copies the first plain run on insertion.
xlsx_outline, xlsx_read_rangeXLSX: sheets, used ranges, tables and defined names; range reads with a version.
xlsx_write_range, xlsx_format, xlsx_sortXLSX: versioned value and formula writes, formatting and sorting.
xlsx_list_defined_names, xlsx_edit_defined_namesXLSX: list complete names with pagination; set or delete workbook and sheet scoped names atomically, including hidden flags.
xlsx_edit_grid, xlsx_edit_sheetsXLSX: insert or delete rows and columns; add, rename, delete or move sheets.
xlsx_set_auto_filterXLSX: set, extend or remove a sheet's filter buttons; table filters are refused.
xlsx_list_charts, xlsx_read_chart_text, xlsx_edit_chartsXLSX: inspect charts on worksheets and chartsheets and page through complete titles, names and references; replace or remove titles and edit series names, category refs and value refs atomically.
xlsx_list_conditional_formats, xlsx_read_conditional_format_formula, xlsx_edit_conditional_formatsXLSX: list bounded rule previews and page through complete formulas; add or delete cellIs and expression rules with a differential format.
xlsx_find_replace, xlsx_previewXLSX: find and replace displayed text; PNG of a range.
pptx_outline, pptx_read_slidePPTX: slides, layouts and shapes; shape tree, paragraphs, runs and notes. office_outline with story: 'comments' lists discussions; office_read reads full comment text.
pptx_edit, pptx_previewPPTX: atomic text, slide, layout mapping, shape, image, notes, comment (add_comment, reply_comment, resolve_comment) and table structure edits (insert_table_row, delete_table_row, insert_table_column, delete_table_column); PNG of a slide.

Edits and references

  • References (ref, match, revision, version) belong to the open session. Reopening a file creates new ones. Page, slide and paragraph numbers are one-based; text offsets are zero-based.
  • DOCX and PPTX search is literal and case-insensitive by default. XLSX office_grep searches displayed values case-sensitively.
  • A proposal holds 1-32 separate replacements and is validated on a temporary copy before one atomic update. A changed target makes it stale (STALE_TARGET): read again and propose again.
  • New text takes the formatting of the first replaced character. To keep several styles in a paragraph, replace the individual spans instead of the whole paragraph.
  • Paragraph breaks, inline embeds and existing tracked changes are protected from text proposals.
  • A committed XLSX or PPTX edit invalidates all outstanding handles, cursors and proposals for that file.
  • Results are paginated (nextCursor, nextOffset, nextStart). A page of results is never an exhaustive search.

Limits

  • Reads return at most 16,000 UTF-16 units and 100 formatting runs; lists return at most 100 items.
  • The MCP server opens at most 10 files of up to 64 MiB each and keeps at most 64 proposals per document. All state is in memory: export before closing the client.
  • MCP messages over stdin are capped at 1 MiB.
  • verify confirms that the saved file reopens. It does not check visual fidelity or whether the edit is semantically correct; use reads and previews for that.
  • Previews use the BetterOffice layout engine and bundled fonts and are not pixel-identical to Office. Install @betteroffice/fonts-cjk for CJK glyphs.

Documentation

版本与发布记录

@betteroffice/agents 共发布 4 个版本,首次发布于 2026/10/23;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/10/2712 天前
0.2.02026/10/2613 天前
0.1.02026/10/2315 天前
0.0.0-stage2026/10/2315 天前

关于 @betteroffice/agents 的常见问题

@betteroffice/agents 是什么?

Search, inspect, propose, preview, and export Office document edits through typed tools and MCP.

如何安装 @betteroffice/agents?

在项目目录运行 npm install @betteroffice/agents,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 betteroffice-mcp,可以用 npx betteroffice-mcp 直接运行。

@betteroffice/agents 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@betteroffice/agents 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@betteroffice/agents 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@betteroffice/agents 可以商用吗?

@betteroffice/agents 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@betteroffice/agents 的下载量有多少?

根据 npm 官方下载统计,@betteroffice/agents 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,648 次,日均约 255 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@betteroffice/agents 还在维护吗?

最新版本 0.3.0 发布于 2026/10/27(12 天前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/10/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。