English | 中文
MCP server for interactive R sessions. Let AI agents execute R code in a persistent R session — local or remote via SSH.
Two modes:
- Spawn mode (default): AI starts a new R process and controls it exclusively
- Attach mode: AI connects to an existing R session (e.g., your RStudio Server session) for human-AI collaboration with shared variables
CodeWhale is a Rust-based coding agent with first-class MCP support. It's the recommended client for agentR.
git clone https://github.com/yuumeHuang/agentR.git
cd agentR
npm install
npm run buildLaunch CodeWhale TUI, then run:
/mcp add stdio agentR node --arg "/absolute/path/to/agentR/dist/index.js"
Set environment variables with:
/mcp env agentR AGENT_R_SSH_HOST your-server.com
/mcp env agentR AGENT_R_SSH_USER username
/mcp env agentR AGENT_R_SSH_PASSWORD password
Edit ~/.deepseek/mcp.json:
Spawn mode (AI creates a new remote R session via SSH):
{
"servers": {
"agentR": {
"command": "node",
"args": ["/absolute/path/to/agentR/dist/index.js"],
"env": {
"AGENT_R_SSH_HOST": "your-server.com",
"AGENT_R_SSH_USER": "username",
"AGENT_R_SSH_PASSWORD": "password"
}
}
}
}Attach mode (AI joins your existing RStudio Server session):
{
"servers": {
"agentR": {
"command": "node",
"args": ["/absolute/path/to/agentR/dist/index.js"],
"env": {
"AGENT_R_MODE": "attach",
"AGENT_R_SSH_HOST": "your-server.com",
"AGENT_R_SSH_USER": "username",
"AGENT_R_SSH_PASSWORD": "password",
"AGENT_R_ATTACH_PORT": "9876"
}
}
}
}/mcp list # Should show agentR as enabled
/mcp tools agentR # Should list 6 tools: r_execute, r_inspect, r_plot, etc.
Attach mode lets you and the AI agent share the same R session. Variables, data frames, and plots are shared in real-time.
install.packages(c("httpuv", "jsonlite"))Copy R/agentR.R to your server (e.g., via SCP or RStudio file upload).
source("agentR.R")
agentR_serve(9876)RStudio console remains fully interactive — you can keep typing commands.
Use the attach mode config above with AGENT_R_MODE=attach and AGENT_R_ATTACH_PORT=9876.
agentR_stop()| Tool | Description |
|---|---|
r_execute |
Execute R code in the persistent session |
r_inspect |
Inspect R objects (str, class, dim, summary, head, glimpse) |
r_plot |
Generate plots and return as PNG images |
r_install_packages |
Install R packages |
r_session_info |
Get R version, loaded packages, working directory |
r_reset |
Reset the R session (kill + restart) |
| Variable | Default | Description |
|---|---|---|
AGENT_R_MODE |
spawn |
spawn = new R process, attach = join existing session |
AGENT_R_PATH |
R |
Path to R binary |
AGENT_R_TIMEOUT |
60000 |
Idle timeout in ms — resets on each output chunk (sliding window) |
AGENT_R_MAX_TOTAL_TIMEOUT |
600000 |
Absolute max execution time in ms regardless of output |
AGENT_R_SSH_HOST |
— | SSH host (enables SSH mode) |
AGENT_R_SSH_PORT |
22 |
SSH port |
AGENT_R_SSH_USER |
— | SSH username |
AGENT_R_SSH_PASSWORD |
— | SSH password |
AGENT_R_SSH_KEY |
— | SSH private key (file path or content) |
AGENT_R_SSH_PASSPHRASE |
— | Passphrase for private key |
AGENT_R_ATTACH_PORT |
9876 |
Port for attach mode (httpuv server) |
Spawn Mode Attach Mode
┌────────────────┐ ┌─────────────────────┐
│ AI Agent │ │ Human (RStudio) │
│ (MCP client) │ │ ┌───────────────┐ │
└───────┬────────┘ │ │ R console │ │
│ │ │ httpuv :9876 │ │
▼ │ └───────┬───────┘ │
┌────────────────┐ └──────────┼──────────┘
│ agentR MCP │ SSH tunnel │
│ server │◄───────────────────────────┤
│ (Node.js) │ │
└───────┬────────┘ │
│ SSH exec ("R") SSH forwardOut │
▼ ▼
┌────────────────┐ ┌────────────────────┐
│ New R process │ │ Existing R session│
│ (exclusive) │ │ (shared) │
└────────────────┘ └────────────────────┘
- Node.js >= 18
- R >= 4.0 (on local or remote machine)
- For attach mode:
httpuvandjsonliteR packages - For SSH: remote server accessible via password or key auth
MIT