r-stats-mcp

r-stats-mcp

An MCP server that provides an LLM with access to R's statistical toolkit including hypothesis tests, regression, psychometrics, survival analysis, time series, and plots through a persistent R session.

Category
访问服务器

README

r-stats-mcp

test License: MIT Python 3.12+ R 4.0+

An MCP server that gives an LLM the whole of R's statistical toolkit — hypothesis tests, regression, psychometrics, survival analysis, time series and plots — over a persistent R session.

繁體中文說明請見 README.zh-TW.md。


What makes it different

One R session, kept alive. Load a dataset once and every later tool call sees it. Fitted models are saved back into that session under a name, so you can fit a model, diagnose it, compare it against another, and then run arbitrary R on it — all without re-reading the data.

Results written for a reader, not a parser. Tools return formatted markdown tables rather than raw JSON: p-values as <.001, effect sizes with magnitude labels, confidence intervals already assembled. When an assumption is violated the output says what to do about it — a significant Levene's test points you at Welch, sparse expected counts bring in Fisher's exact test automatically.

No dead ends. 28 structured tools cover the common ground, and r_run executes arbitrary R in the same session for everything else.

You: Load survey.csv and check whether the two groups differ on score.

  data_load(path="survey.csv", name="df")
  data_inspect(data="df")
  data_transform(data="df", to_factor=["group"])
  check_assumptions(data="df", variables=["score"], group="group")
  test_ttest(data="df", y="score", group="group", nonparametric=true)

Requirements

Version Notes
R ≥ 4.0 Must be on PATH, or set R_MCP_RSCRIPT
Python ≥ 3.12 Managed by uv
uv any Install

Required R packages: jsonlite and evaluate. Everything else is needed only by the tools that use it, and each tool tells you exactly what to install when something is missing.

install.packages(c("jsonlite", "evaluate"))

For full coverage of every tool:

install.packages(c(
  "ggplot2", "ragg", "corrplot",          # plotting
  "car", "emmeans", "rstatix",            # ANOVA, post-hoc, VIF
  "psych", "GPArotation", "lavaan",       # scales, factor analysis, SEM
  "lme4", "lmerTest",                     # mixed models
  "survival", "forecast", "tseries",      # survival, time series
  "mgcv", "MASS", "nnet",                 # GAM, ordinal, multinomial
  "haven", "readxl", "openxlsx",          # SPSS/Stata/Excel
  "sandwich", "lmtest"                    # robust standard errors
))

Install

Claude Code

claude mcp add r-stats -- uvx --from git+https://github.com/Ian3738/r-stats-mcp r-stats-mcp

Claude Desktop

Add to claude_desktop_config.json (macOS: ~/Library/Application Support/Claude/, Windows: %APPDATA%\Claude\):

{
  "mcpServers": {
    "r-stats": {
      "command": "uvx",
      "args": ["--from", "git+https://github.com/Ian3738/r-stats-mcp", "r-stats-mcp"]
    }
  }
}

If the client cannot find uvx, give the absolute path (which uvx).

From a clone

git clone https://github.com/Ian3738/r-stats-mcp
cd r-stats-mcp
uv sync
claude mcp add r-stats -- "$(pwd)/run-server.sh"

Configuration

Variable Default Purpose
R_MCP_RSCRIPT Rscript from PATH Path to the R executable
R_MCP_WORKDIR Startup directory (home if /) Where relative paths in data_load resolve
R_MCP_TIMEOUT 180 Default per-call timeout in seconds

Verify the setup by asking the model to call r_session_info.


Tools

Session and code

Tool Purpose
r_run Execute arbitrary R code; returns console output and plots
r_install_packages Install packages from CRAN
r_session_info R version, platform, which statistics packages are available

Data

Tool Purpose
data_load CSV, TSV, Excel, SPSS, Stata, SAS, RDS, RData, JSON, Parquet
data_builtin Datasets bundled with R or an installed package
data_list Datasets and fitted models currently in the session
data_inspect Types, missing counts, distinct counts, first rows
data_transform Filter, derive, recode, factor conversion, sort, long/wide reshape
data_export Write out as CSV, TSV, Excel or RDS

Descriptives and assumptions

Tool Purpose
describe n, mean, SD, SE, CI, median, quartiles, skew, kurtosis — optionally by group
frequency_table Frequency tables and cross-tabulations with row percentages
check_assumptions Normality, homogeneity of variance, outliers, VIF, residual diagnostics

Hypothesis tests

Tool Purpose
test_ttest One-sample, independent and paired t-tests, with Levene, Cohen's d and rank-based equivalents
test_anova One-way, factorial, ANCOVA, repeated-measures and mixed designs, with effect sizes and post-hoc comparisons
test_categorical Chi-square independence and goodness-of-fit, Fisher, McNemar, Cramér's V, odds ratios
test_proportion One-sample and multi-group proportion tests, including the exact binomial
correlation Pearson, Spearman, Kendall; partial correlations; multiple-comparison adjustment

Regression and models

Tool Purpose
regression Linear, logistic, Poisson, negative binomial, ordinal, multinomial, mixed-effects, GAM
model_diagnostics Residual normality, heteroscedasticity, autocorrelation, VIF, influential cases
model_compare AIC, BIC, log-likelihood, ΔAIC, plus nested-model tests
model_predict Estimated marginal means, or predictions on new data

Scales and questionnaires

Tool Purpose
reliability Cronbach's α, McDonald's ω, item-total correlations, α-if-dropped
factor_analysis EFA and PCA with KMO, Bartlett, parallel analysis, rotated loadings, scree plot
sem lavaan CFA / SEM / path / growth models with fit indices, CR and AVE
mediation Bootstrap confidence intervals for indirect effects, multiple mediators supported
moderation Interaction models with ΔR², simple slopes and an interaction plot

Survival and time series

Tool Purpose
survival_analysis Kaplan-Meier with log-rank, Cox regression with the proportional-hazards test
time_series Stationarity tests, STL decomposition, automatic ARIMA/ETS, forecasts

Plotting

Tool Purpose
plot ggplot2 histogram, density, box, violin, scatter, line, bar, Q-Q, correlation heatmap

Examples

Regression with model comparison

regression(data="df", dv="score", predictors=["age","sex","group"], save_as="full")
regression(data="df", dv="score", predictors=["age"], save_as="base")
model_compare(models=["base","full"])
model_diagnostics(model="full")

Scale validation

reliability(data="df", items=["q1",...,"q10"], reverse=["q3","q7"], scale_max=5)
factor_analysis(data="df", variables=["q1",...,"q10"])
sem(data="df", type="cfa", model="anxiety =~ q1 + q2 + q3\ndepression =~ q4 + q5 + q6")

Repeated measures

data_transform(data="df", reshape={
  "direction": "long", "value_cols": ["t1","t2","t3"],
  "id_cols": ["id"], "names_to": "time", "values_to": "score"
})
test_anova(data="df", dv="score", within=["time"], id="id", posthoc="bonferroni")

Survival

survival_analysis(data="df", time="days", event="died", group="treatment",
                  type="km", times_of_interest=[180, 365])
survival_analysis(data="df", time="days", event="died",
                  covariates=["age","sex","stage"], type="cox")

How it works

MCP client  ──stdio/JSON-RPC──▶  Python server  ──NDJSON over pipes──▶  Rscript worker
                                 (tool schemas)                          (.GlobalEnv)

Transport. A long-lived Rscript process reads newline-delimited JSON requests and writes sentinel-delimited JSON responses. Plots are captured with the evaluate package — the same machinery knitr uses — so base graphics, ggplot2 and lattice all work, and are returned as inline PNGs.

Namespace hygiene. User objects live in .GlobalEnv; the server's own machinery lives in a separate .rmcp_sys environment. data_list and r_run therefore see only your data and models.

Timeouts. Two layers: R enforces its own limit with setTimeLimit(), and Python applies a hard timeout on the pipe. If the session has to be restarted, the tool says so explicitly rather than silently losing your data.

Failure messages are actionable. Passing a three-level factor to a t-test does not produce a stack trace — it tells you the levels it found and points you at test_anova.


Development

uv sync
uv run python -c "
import asyncio
from r_stats_mcp.server import server
print(len(asyncio.run(server.list_tools())), 'tools')
"

Adding a statistical tool means two edits: an R helper in src/r_stats_mcp/R/ returning list(md=, plots=), and a decorated function in server.py describing its parameters. The R files are sourced in filename order at worker startup.

src/r_stats_mcp/
├── server.py          MCP tool definitions and schemas
├── session.py         persistent R subprocess, transport, timeouts
└── R/
    ├── worker.R           protocol loop, plot capture
    ├── 00-util.R          markdown tables, formatting, session objects
    ├── 10-data.R          loading, inspection, reshaping
    ├── 20-descriptive.R   descriptives, frequencies, assumptions
    ├── 30-htest.R         t-tests, ANOVA, categorical, correlation
    ├── 40-regression.R    regression family, diagnostics, comparison
    ├── 50-psychometrics.R reliability, factor analysis, SEM, mediation
    ├── 60-survival-ts.R   survival analysis, time series
    └── 70-viz.R           ggplot2 charts

License

MIT — see LICENSE.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选