Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Statscraft 0.61.0, release v0.61.0

MCP 服务器

statscraft mcp 让 AI 智能体通过工具而不是 shell 命令来使用 Statscraft。它通过标准输入和输出使用 Model Context Protocol(MCP,模型上下文协议),因此任何能运行本地 MCP 服务器的智能体应用都可以使用它:Claude Desktop、Claude Code、Codex(包括 ChatGPT 应用中的 Codex)、Cursor、VS Code(配合 Copilot)等。它随 statscraft 命令行一起提供,无需另外安装,数据也从不离开您的计算机。

连接智能体最简单的方法是 statscraft connect,它会找到您已安装的智能体应用,并把 Statscraft 添加到每个应用中。Claude Desktop 也可以双击 MCP 包来安装,Claude Code 则可以通过插件安装;参见连接您的 AI 智能体。如需手动设置,请给智能体提供命令 statscraft mcp;如果 statscraft 不在您的 PATH 上,请写出它的完整路径。

工具

工具输入返回内容
describefile(.sav 或 .zsav 文件),或 session字典和计数,对会话还会返回它保存的内容;与 statscraft describe --json 的 JSON 相同
list_commands无每条受支持的命令及其一行摘要,与 statscraft help 相同
command_helpcommand一条命令的语法、规则和示例,与 statscraft help COMMAND 相同
check_syntaxsyntax 或 file,以及可选的 lines、session、folder、vega_lite、encoding检查结果,与 statscraft check --json 相同;不保存也不写入任何内容
run_syntax与 check_syntax 相同,另加可选的 xlsx、xlsx_one_sheet、xlsx_no_titles、plugins、plugin_templates、charts 和 chart_format;或用 window(可选 lines)代替 syntax 和 file运行结果,与 statscraft run --json 相同;会话保存新的状态,trail 说明其语法保存在何处
describe_pluginprogram(插件的绝对路径)插件的名称、版本和设置,每项设置附带其类型、默认值以及保存它的数据文件属性
check_plugin_updateprogram(插件的绝对路径)插件是否提供更新的版本,与 statscraft update --check --plugin 相同:plugin、current、latest、update、notes;不安装任何东西
window_status无Statscraft 窗口是否已打开,若已打开,则给出其数据集和输出文档;还有 platform(windows、macos、linux),智能体在 WSL 中运行时另有 wsl_distro
list_syntax_windows无打开的窗口中的语法窗口:id、name、file、unsaved、lines,以及它是否为 Agent 窗口、指定窗口或用户最近使用的窗口
read_syntaxwindow语法窗口的文本,每行附带行号
edit_syntaxwindow、mode、text,以及 lines 或 before更改文本:replace 替换 lines 指定的行(文本为空则删除这些行),insert 插入到第 before 行之前,append 追加,或 replace_all 全部替换;返回新文本所占的行
new_syntax_window可选的 name 和 text一个新的语法窗口,为空或包含给定文本
open_syntax_filepath在语法窗口中打开的 .sps 文件,或已打开该文件的窗口
save_syntaxwindow,可选的 path 和 overwrite把窗口写入它自己的文件或 path
close_syntaxwindow,可选的 discard关闭语法窗口,与在该窗口中选择 File > Close 相同
export_outputpath,可选的 folder、format、one_sheet、selected、hidden把打开的窗口的输出写入 Excel 工作簿、网页或 PDF,与窗口的 File > Export 和 statscraft export 相同
read_output可选的 document打开的窗口中某个输出窗口的内容,与 statscraft output --json 相同:每个标题、表格和图表,以及它是否隐藏或选中,还有该窗口显示哪些类型
undo无撤销打开的窗口中最近一次更改数据的运行,与窗口的 Edit > Undo 和 statscraft undo 相同;在 undone 下给出那次运行的语法;每次调用再往前撤销一步
redo无在打开的窗口中重做最近一次撤销的运行,与窗口的 Edit > Redo 和 statscraft redo 相同;在 redone 下给出那次运行的语法
add_chartchart,可选的 session 和 vega_lite检查图表并把它添加到指定输出文档,返回内容与一次运行的结果相同;文本以 chart added to output document NAME 开头
run_codecode, a JavaScript async arrow functionwhat the function returned, as JSON under value, with ok, logs and error; see Code mode
apply_plansyntax, database (sqlite, postgres, mysql or dolt), location, dataset, optional apply, actor (required to apply), expected_branch, expected_head, profilethe JSON of statscraft plan with a database and --name: the plan, plan_hash and source_hash, and after an apply applied; see Editing a dataset in a database
  • syntax 是一条或多条命令的文本;file 是 .sps 文件的绝对路径。两者给出其一。
  • lines 把运行限制在某个行范围内的命令,例如 "12-20"。行号仍按整段文本计算。
  • folder 是语法中相对文件名的起始绝对文件夹。默认:您的主文件夹。为保险起见,请在语法中使用绝对路径。
  • xlsx、xlsx_one_sheet、xlsx_no_titles、plugins、plugin_templates、vega_lite、encoding、charts 和 chart_format 的作用与命令行中同名的 run 选项相同。路径请写成绝对路径;相对的 charts 文件夹从 folder 开始。导出全部输出介绍工作簿,使用插件的表格介绍插件。
  • 在 WSL 中运行、使用 Windows 上 Statscraft 的智能体,可以在参数中给出 Linux 路径;服务器会转换它们。在语法内部,文件名必须是 Windows 路径。参见 WSL 中的智能体。
  • window 用 id 或名称指定打开的窗口中的一个语法窗口,与 list_syntax_windows 显示的相同。语法窗口工具只在窗口打开时可用;窗口未打开时,它们会失败并提示 “no Statscraft window is running”。

一次运行或检查会以结构化内容返回命令行中介绍的 JSON,并以 Markdown 文本返回表格和图表,附带一行摘要,供只显示文本的应用使用。语法错误或运行错误是正常结果:ok 为 false,errors 给出行、列和预期的内容,智能体可以据此修正语法并重试。服务器完全无法执行的调用,例如文件缺失或参数错误,会被标记为工具错误。

服务器在连接时还会把工作流程发送给智能体,并以提示词 analysis 的形式提供:先描述数据,查阅每条命令,先检查,分小步运行,阅读 ok、errors 和警告,并报告结果表格中的数字,而不是自行计算。

每一步都留下语法

智能体通过 run_syntax 所做的一切都保留为可读的语法,用户可以检查、修改,并在没有智能体的情况下再次运行:

  • 在打开的窗口中,每次成功的运行都会添加到名为 Agent 的语法窗口中。该窗口在第一次运行时打开;如果用户关闭了它,下一次运行会打开一个新的。每次运行都位于一行带时间的注释之下,例如 * Agent 14:32.,后面是实际运行的语法,一字不差。用户可以像在任何语法窗口中一样编辑它,用 Ctrl+Enter 运行其中的行,并保存它。从命令行用 statscraft run -s gui 发送的运行也会进入这里。
  • 从语法窗口发起的运行(带 window 的 run_syntax)不会被复制:该窗口的文本本身就是它的记录。
  • 在文件会话中,每次成功的运行都会添加到会话文件夹中的 syntax.sps,位于一行带日期和时间的注释之下。
  • 失败的运行(ok 为 false)没有更改任何东西,也不留下任何东西;检查同样如此。
  • 结果中的 trail 以及文本中的 syntax kept in: 一行说明语法保存到了哪里:syntax window Agent,或文件的路径。

您自己的图表

过程会绘制自己的图表。当用户想要一张没有任何过程能生成的图表时,例如把两次运行得到的去年数字和今年数字并排展示,add_chart 会把它放进输出,并按 Statscraft 的方式绘制:

  • chart 可以是 run_syntax 在 charts 下返回的图表 JSON(智能体因此可以拿来某个过程的图表,修改其标题、标签、类型或数字),也可以是一个简单的 Vega-Lite 规范:一个视图,行内联在 data.values 中,标记为 bar、line、area、point 或 arc。两者都在作为输入的图表中介绍。
  • 数值按原样绘制:不做任何计数、求和或分组。数字应当来自结果表格。
  • 在打开的窗口中,图表进入指定输出窗口,用户可以在那里查看、复制它,在 Excel、Word 或 PowerPoint 中打开它,并与其他输出一起保存或导出。结果中的 commands[0].document 给出该文档的名称。
  • 该工具运行语法 BEGIN CHART(Statscraft 的一项扩展),其中包含图表的 JSON。这段语法像任何运行一样被保存:在 Agent 语法窗口中,或在文件会话的 syntax.sps 中,因此再次运行它会再次显示该图表。
  • 文件会话在调用之间把输出保存在其文件夹中。先添加图表,准备好后再通过 run_syntax 用 OUTPUT EXPORT 或 OUTPUT SAVE 写出文档。
  • 无法绘制的图表、拼错的字段,或 Statscraft 不读取的 Vega-Lite 功能,都是需要修正的错误,与语法错误一样:ok 为 false,消息会指出问题所在。此时不会添加任何内容。

Code mode

run_code takes code, a JavaScript async arrow function, and runs it in a sandbox inside the server. There every other tool is a function of the object statscraft, with the tool’s name and its arguments as one object, and returns what the tool returns: statscraft.describe({ file }), statscraft.run_syntax({ session, syntax }), statscraft.read_output({}) and so on. An agent uses it when one step depends on the result of another, or when a run gives far more than the agent needs: the script runs the syntax, keeps the numbers it wants and returns only those, all in one call.

  • Each function returns the tool’s structured result, the same JSON the tool gives, or its text for list_commands and command_help. A run with ok false comes back as it is; a tool error (a bad argument, a missing file) is thrown as an Error with the tool’s message, which the script can catch.
  • A table in run_syntax’s result has title, columns (names, the first row_headers of them for the row labels) and rows, each an array in the order of columns: text for a label, a raw number, null for an empty cell. The chi-square table of a CROSSTABS, for one, has the columns ["Statistic", "Value", "df", "Asymptotic Sig. (2-tailed)"] and a first row like ["Pearson Chi-Square", 39.48, 16, 0.00093], so rows[0][3] is the Pearson significance.
  • The reply has ok, value (what the function returned, as JSON), logs (the lines console.log wrote) and error (its message, and line and column in code where they are known). When the script throws or reaches a limit, ok is false.
  • Data still change only through syntax: every run_syntax the script makes is kept in the trail as a direct call is (Every step leaves syntax). The script itself is not kept.
  • The sandbox has no files, network, modules, timers, environment or processes. The script reaches only the statscraft object, so it can do what the tools can do and nothing more. run_code is not in the object: a script cannot start another script.
  • Limits, each ending the script with an error that names it: 60 seconds of the script’s own time (time inside tool calls does not count, so a long analysis is not cut short), 512 MB of memory and a 1 MB stack.
  • statscraft code runs the same scripts from the command line.

An example. A staff survey has questions q1 to q12 and a department variable dept. Which questions did more than 5% of the staff skip, and does skipping depend on the department?

async () => {
  const session = 'staff';
  statscraft.run_syntax({ session, syntax: "GET FILE='/home/anna/data/staff_survey.sav'.\n" });
  const data = statscraft.describe({ session });
  const skipped = data.variables
    .filter(v => /^q\d+$/.test(v.name))
    .filter(v => 1 - v.n_valid / data.case_count > 0.05)
    .map(v => v.name);
  const sig = {};
  for (const name of skipped) {
    const syntax = `COMPUTE skip = MISSING(${name}).\nCROSSTABS /TABLES=skip BY dept /STATISTICS=CHISQ.\n`;
    const run = statscraft.run_syntax({ session, syntax });
    if (!run.ok) return run.errors;
    const table = run.commands[1].tables.find(t => t.title.startsWith('Chi-Square Tests'));
    sig[name] = table.rows[0][3];
  }
  return { skipped, pearson_sig: sig };
}

The reply carries a few names and numbers instead of every table, and the session’s trail holds each COMPUTE and CROSSTABS the script ran.

使用插件的表格

插件会美化一次运行在工作簿中生成的 CTABLES 表格:颜色、注释、列宽以及其他外观。智能体通过 describe_plugin 获取插件的设置。每项设置保存在一个数据文件属性中,例如 plugin_acme_tables_confidence,运行时从活动数据的该属性中读取每项设置,没有时使用插件的默认值。要更改某项设置,智能体在表格之前运行 DATAFILE ATTRIBUTE ATTRIBUTE=plugin_acme_tables_confidence('90').(是/否写作 Yes 或 No,颜色写作 #rrggbb,样式写成 JSON);SAVE 会把它与数据一起保存。然后:

{ "file": "/home/anna/jobs/tables.sps", "session": "work", "xlsx": "/home/anna/jobs/tables.xlsx",
  "xlsx_one_sheet": true, "plugins": ["/home/anna/plugins/acme-tables"] }

插件失败则运行失败,并给出插件的消息。在打开的窗口中,plugins 的作用相同:设置取自运行之后窗口活动数据的属性,并且只有本次运行的工作簿使用插件的外观。窗口自身的输出仍使用 Edit > Settings > Plugins 中的插件,export_output 写出的就是这些。

语法窗口

窗口打开时,智能体可以像用户一样在用户的语法窗口中工作:

  • list_syntax_windows 和 read_syntax 显示其中的内容,包括未保存的更改。行号与运行时计算的行号一致。
  • edit_syntax 更改编辑器中的文本;用户会立即看到更改,并可以在该编辑器中用 Ctrl+Z 撤销。它从不运行任何东西,也不写入任何文件。
  • 带 window 的 run_syntax 运行该窗口的文本,带 lines 时运行这些行涉及的命令,与在该窗口中使用 Run 和 Ctrl+Enter 完全相同:相对文件名从该窗口的文件开始,错误会标记在其页边。
  • new_syntax_window 和 open_syntax_file 会打开一个窗口;智能体打开的窗口会接收从对话框粘贴的语法,与用户打开的窗口一样。Agent 窗口从不接收。
  • save_syntax 把窗口写入它自己的文件,或写入 path。除非 overwrite 为 true,否则它不会替换任何其他已有文件,并且绝不替换另一个语法窗口所持有的文件。
  • close_syntax 关闭窗口。有未保存更改的窗口会保持打开,除非 discard 为 true;程序的最后一个窗口总是保留,因为关闭它会退出 Statscraft。

当用户要求修正或解释其语法时,智能体会读取窗口,说明它打算更改什么,用 edit_syntax 进行更改,然后从窗口中运行。

export_output 像 File > Export 一样把指定输出窗口写入 path:format 为 xlsx、html 或 pdf(默认:根据扩展名),one_sheet 用于 Excel,selected 只导出选中的表格,hidden 同时导出隐藏的项目。相对的 path 从 folder 开始。它会替换已有文件。

undo 撤销打开的窗口中最近一次更改数据的运行,与窗口中的 Edit > Undo 相同;输出保留。每次调用再往前撤销一步,最多 10 步。redo 重做最近一次撤销的运行,与 Edit > Redo 相同;撤销后若又更改了数据,就不能再重做。没有可撤销或可重做的内容时,两者都会失败。文件会话不支持这两项。

read_output 让智能体看到用户在输出窗口中看到的内容:指定输出窗口,或输出文档 document。其结构化内容是 statscraft output --json 的 JSON;其文本是一行摘要加上表格,与 run_syntax 给出的形式相同,隐藏或选中的表格都有相应标记。

Editing a dataset in a database

apply_plan edits a dataset stored in an OpenStatSpec database in place, as statscraft plan --apply does: the same dataset and the same table, with no copy.

  • syntax takes only the bounded subset of plan: RECODE, VARIABLE LABELS, VALUE LABELS, ADD VALUE LABELS, FORMATS, VARIABLE LEVEL, simple COMPUTE and IF, and EXECUTE. profile picks 0.1, 0.2 or 0.3 (the default).
  • location is the absolute path of a SQLite file, or the URL of a PostgreSQL, MySQL or Dolt database. A URL with a password is refused; the server takes the password from STATSCRAFT_DB_PASSWORD in its own environment.
  • Without apply, or with apply false, the tool only previews: it reads the dataset’s dictionary, lowers the syntax to a plan and writes nothing. With apply true it applies the plan in one transaction with one audit row naming actor, which an apply requires so that every audit row names who made the change.
  • On Dolt, an apply needs expected_branch and expected_head: the database must be on that branch at that commit, with a clean working set. The tool never commits, branches or resets; the reply has "committed": false and the commit is yours. Put all edits of one step into one plan.
  • Errors carry a stable code under error. A call the tool refuses before connecting, such as an apply without actor or a URL with a password, has the code usage. When the syntax is at fault they also carry line and column, and come as a normal result for the agent to fix; others, such as an unknown dataset or a database that cannot be reached, are tool errors.

Preview first, then apply. After an apply, read the dataset back from the database, for example with GET DATA /TYPE=OPENSTATSPEC in run_syntax, instead of assuming what the edit did.

会话与窗口

会话在调用之间保存数据、设置、宏和命名数据集,与 statscraft run -s NAME 相同(参见会话)。

  • 当 Statscraft 窗口打开时,不带 session 的调用会发送到窗口(会话 gui)。您会看到每个表格出现在指定输出窗口中,每条命令出现在日志中,并且可以在智能体的各步之间继续在窗口中工作。
  • 当没有打开的窗口时,不带 session 的调用使用名为 work 的会话。
  • MCP 服务器的文件会话位于您的用户数据文件夹中,而不是智能体应用启动服务器时所在的文件夹:在 Linux 上是 ~/.local/share/statscraft/sessions(或 $XDG_DATA_HOME/statscraft/sessions),在 macOS 上是 ~/Library/Application Support/Statscraft/sessions,在 Windows 上是 %LOCALAPPDATA%\Statscraft\sessions。删除某个会话的文件夹即可重新开始,也可以让智能体使用一个新的会话名称。

安全性

  • The server can do what the command line can do, and nothing more. Syntax can read files, and it can write them with SAVE, AGGREGATE OUTFILE, OUTPUT EXPORT or the xlsx argument. check_syntax writes nothing; nothing is written without one of those, apart from the read that GET DATA records in an OpenStatSpec database and the edits apply_plan applies there (below).
  • The tools tell the agent app which of them only read (describe, list_commands, command_help, check_syntax, window_status, list_syntax_windows, read_syntax, read_output), which change only the window’s syntax windows (edit_syntax, new_syntax_window, open_syntax_file) or add to its output (add_chart, which writes no file), that close_syntax can lose unsaved syntax and undo and redo change the data, that run_syntax, save_syntax and export_output can change files, that apply_plan can change a dataset in a database, and that run_code can do whatever the other tools can. Agent apps ask you before a tool runs, unless you allowed it; allow run_syntax only for agents you trust with your files.
  • GET DATA /TYPE=OPENSTATSPEC reads a dataset from an OpenStatSpec database, which may be a server on the network. The syntax never holds the password: a URL with one is refused, and the server or the window takes it from STATSCRAFT_DB_PASSWORD in its own environment. The read is logged in the database’s catalog, except on Dolt and in check_syntax, which write nothing there. apply_plan reaches a database in the same way, with the password from the same variable.
  • 服务器只与启动它的应用通信,通过标准输入和输出进行。它不打开任何网络端口。对窗口的调用使用窗口的本地连接,详见 AI 智能体。