RStudio MCP服务器
用于RStudio Desktop集成的模型上下文协议(MCP)服务器,可实现VSCode和其他MCP客户端的无缝R包开发、测试和jamovi模块开发。
特性
R包开发
- 测试:使用运行包测试
devtools::test()或单独的测试文件testthat::test_file() - 文档:生成文档
devtools::document()(氢氧根2) - 建筑:使用构建包
devtools::build() - 检查:运行R CMD检查
devtools::check() - 加载中:使用加载包功能
devtools::load_all()用于交互式开发
Jamovi模块开发
- 构建:使用构建jamovi模块
jmvtools::build() - 检查:使用验证jamovi模块
jmvtools::check()
通用R功能
- 代码执行:运行任意R代码
- 包管理:安装并列出软件包
- 工作空间检查:列出并检查R工作空间对象
快速开始
自动设置(推荐)
运行自动安装脚本以检查先决条件并配置所有内容:
# Clone the repository
git clone
cd rstudio-mcp-server
# Run automated setup
npm run setup安装脚本将:
- 检查Node.js和R安装
- 安装npm依赖项
- 构建服务器
- 验证R包要求
- 指导您完成MCP客户端配置
手动安装
如果您更喜欢手动设置:
- 安装先决条件
- Node.js v18或更高版本() - R 3.6或更高版本,在PATH中使用Rscript - Git(用于克隆)
- 克隆和构建
git clone
cd rstudio-mcp-server
npm install
npm run build- 安装R软件包
install.packages(c("devtools", "testthat", "roxygen2"))- 配置MCP客户端 (见下面的配置部分)
配置
适用于带克劳德代码的VSCode
添加到您的Claude Code MCP设置文件中(通常 ~/.config/claude-code/mcp_settings.json 在Linux/Mac或 %APPDATA%\claude-code\mcp_settings.json 在Windows上):
{
"mcpServers": {
"rstudio": {
"command": "node",
"args": ["/path/to/rstudio-mcp-server/build/index.js"]
}
}
}适用于Cline或其他MCP客户
为您的客户端添加到相应的MCP设置文件中:
{
"mcpServers": {
"rstudio": {
"command": "node",
"args": ["/absolute/path/to/rstudio-mcp-server/build/index.js"]
}
}
}可用工具
r_execute
执行任意R代码。
参数:
code(必填):要执行的R代码working_dir(可选):执行工作目录
例子:
{
"code": "summary(mtcars)",
"working_dir": "/path/to/project"
}r_test_package
使用devtools::test()对R包运行测试。
参数:
package_path(必填):R包目录的路径filter(可选):测试过滤器模式(正则表达式)
例子:
{
"package_path": "/path/to/mypackage",
"filter": "test-myfunction"
}r_test_file
运行特定的测试文件。
参数:
test_file(必填):测试文件的路径
例子:
{
"test_file": "/path/to/mypackage/tests/testthat/test-myfunction.R"
}r_check_package
对包运行R CMD检查。
参数:
package_path(必填):R包目录的路径args(可选):R CMD检查的其他参数
例子:
{
"package_path": "/path/to/mypackage",
"args": "--as-cran"
}r_document_package
使用roxygen2为R包生成文档。
参数:
package_path(必填):R包目录的路径
r_build_package
构建一个R包。
参数:
package_path(必填):R包目录的路径binary(可选):构建二进制包(默认值:false)
r_load_all
将所有功能加载到一个包中以进行交互式开发。
参数:
package_path(必填):R包目录的路径
r_install_package
从CRAN或本地路径安装R包。
参数:
package(必需):包名称或路径dependencies(可选):安装依赖项(默认值:true)
r_list_packages
列出所有已安装的R软件包。
r_workspace_ls
列出R工作空间中的对象。
参数:
pattern(可选):过滤对象名称的模式
jamovi_build_module
构建一个jamovi模块。
参数:
module_path(必填):jamovi模块目录的路径install(可选):构建后安装模块(默认值:false)
jamovi_check_模块
检查jamovi模块是否存在问题。
参数:
module_path(必填):jamovi模块目录的路径
用法示例
快速入门示例
配置后,您可以通过自然语言与R进行交互:
测试R连接:
- “你能列出所有已安装的R软件包吗?”
- “运行的是哪个版本的R?”
- “执行此R代码:print(sessionInfo())”
软件包开发:
- “运行此包的测试”
- “为我的包生成文档”
- “检查此包是否通过R CMD检查”
- “构建此包”
代码执行:
- “执行此R代码:摘要(mtcars)”
- “安装tidyverse软件包”
- “显示工作区中有哪些对象”
详细工作流程
1.启动新包
You: "I'm creating a new R package called 'datautils'. Can you help me set it up?"
Claude: [Guides you through using usethis::create_package()]
You: "Now document the package"
Claude: [Uses r_document_package tool]
Output: ✔ Writing 'NAMESPACE'
✔ Writing 'datautils.Rd'
You: "Check if it passes R CMD check"
Claude: [Uses r_check_package tool]
Output: ── R CMD check results ─────────────────────
0 errors ✔ | 0 warnings ✔ | 0 notes ✔2.测试驱动开发
You: "Run all tests in the package"
Claude: [Uses r_test_package tool]
Output: ✔ | F W S OK | Context
✔ | 5 | validation
✔ | 8 | data-processing
✔ | 3 | output-formatting
You: "Run only tests matching 'validation'"
Claude: [Uses r_test_package with filter: "validation"]
Output: ✔ | F W S OK | Context
✔ | 5 | validation
You: "Run the specific test file test-validation.R"
Claude: [Uses r_test_file tool]
Output: Test results for tests/testthat/test-validation.R
✔ | 5 tests passed3.调试失败测试
You: "Run the package tests"
Claude: [Uses r_test_package tool]
Output: ✖ | F W S OK | Context
✖ | 1 4 | validation
────────────────────────────────────────────
Failure (test-validation.R:15:3): input validation works
Expected TRUE, got FALSE
You: "Can you execute this R code to debug:
source('R/validation.R');
validate_input(c(1, 2, NA))"
Claude: [Uses r_execute tool]
Output: Error: NA values not allowed
You: "Now I see the issue. Let me fix the code and re-run tests..."4.软件包发布工作流程
You: "I'm ready to release my package. Let's go through the checklist."
Claude: "I'll help you verify everything:
1. First, let's run the tests"
[Uses r_test_package - shows all passing]
Claude: "2. Generating documentation"
[Uses r_document_package]
Claude: "3. Running R CMD check"
[Uses r_check_package with args: "--as-cran"]
Claude: "4. Building the package"
[Uses r_build_package]
Output: ✔ Building 'datautils_1.0.0.tar.gz'
You: "Perfect! Can you also check for any spelling issues?"
Claude: [Uses r_execute with "spelling::spell_check_package()"]5.Jamovi模块开发
You: "I'm working on a jamovi module. Can you check it for issues?"
Claude: [Uses jamovi_check_module tool]
Output: ✔ Checking module structure
✔ Validating .a.yaml files
✔ Checking R functions
✔ Verifying UI definitions
You: "Build and install the module"
Claude: [Uses jamovi_build_module with install: true]
Output: ✔ Building module
✔ Installing to jamovi
Module 'mymodule' installed successfully
You: "Now I'll test it in jamovi..."6.交互式数据分析
You: "Can you execute this analysis:
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarize(
mean_mpg = mean(mpg),
mean_hp = mean(hp)
)"
Claude: [Uses r_execute tool]
Output: # A tibble: 3 × 3
cyl mean_mpg mean_hp
1 4 26.7 82.6
2 6 19.7 122.
3 8 15.1 209.
You: "Now create a linear model of mpg vs hp"
Claude: [Uses r_execute tool]
Output: Call:
lm(formula = mpg ~ hp, data = mtcars)
Coefficients:
(Intercept) hp
30.09886 -0.068237.包装安装和管理
You: "Install the ggplot2 package"
Claude: [Uses r_install_package tool]
Output: Installing package into '/home/user/R/library'
✔ Package 'ggplot2' successfully installed
You: "List all packages containing 'test' in the name"
Claude: [Uses r_list_packages tool, then filters]
Output: Packages matching 'test':
- testthat (3.2.0)
- testit (0.13)
- covr (3.6.1)8.处理多个包
You: "I have three packages I'm developing. Can you run tests on all of them?
- /path/to/package1
- /path/to/package2
- /path/to/package3"
Claude: "I'll test each package:
Package 1:
[Uses r_test_package for package1]
✔ All 15 tests passed
Package 2:
[Uses r_test_package for package2]
✔ All 22 tests passed
Package 3:
[Uses r_test_package for package3]
✖ 2 tests failed
[Shows failure details]
Two packages passed, but package3 has failing tests. Would you like me to show the specific failures?"9.开发环境设置
You: "I'm setting up a new development machine. What R packages do I need?"
Claude: "Let me check what's installed"
[Uses r_list_packages tool]
Claude: "I see you're missing some key development packages. I can install:
- devtools (package development)
- testthat (testing)
- roxygen2 (documentation)
- usethis (workflow automation)
Should I install these?"
You: "Yes, please install them"
Claude: [Uses r_install_package for each package]
✔ All development packages installed10.CI/CD准备
You: "I want to make sure my package will pass CI checks. Run all quality checks."
Claude: "I'll run a comprehensive check:
1. Tests:
[Uses r_test_package]
✔ All 45 tests passed
2. Documentation:
[Uses r_document_package]
✔ Documentation up to date
3. R CMD check:
[Uses r_check_package with args: "--as-cran"]
✔ 0 errors | 0 warnings | 0 notes
4. Package build:
[Uses r_build_package]
✔ Package builds successfully
Your package is ready for CI! All checks passed."常见用例
代码更改后的快速测试:
"Run the tests"完整的预提交检查:
"Run tests, update docs, and run R CMD check"安装开发依赖项:
"Install devtools, testthat, and roxygen2"调试特定功能:
"Execute this code: debugonce(my_function); my_function(test_data)"检查测试覆盖率:
"Execute: covr::package_coverage()"拼写检查文档:
"Execute: spelling::spell_check_package()"发展
开发观察模式:
npm run watch故障排除
R未找到
确保R和Rscript在您的系统PATH中:
which Rscript # Linux/Mac
where Rscript # WindowsWindows PATH设置:
- 查找您的R安装(通常
C:\Program Files\R\R-4.x.x\bin) - 添加到路径:
- 在“开始”菜单中搜索“环境变量” - 编辑系统变量下的“路径” - 添加新条目: C:\Program Files\R\R-4.x.x\bin - 重新启动终端/VCode
多个R版本:
- 确保正确的R版本在PATH中位于首位
- 请检查:
Rscript --version
未找到的devtools/test
安装所需的R软件包:
install.packages(c("devtools", "testthat", "roxygen2"))找不到jamovi工具
安装 jmvtools 需要特别考虑。看 JMVTOOLS_INSTALLATION.md 详细说明。
快速摘要:
- 选项1:安装Rtools(Windows)或构建工具(Mac/Linux),然后
install.packages("jmvtools") - 选项2:使用jamovi捆绑的R(推荐给jamovi开发人员)
- 选项3:安装预构建的二进制文件
常见问题: jmvtools 需要编译工具:
Error: package 'jmvtools' is not available有关特定于平台的解决方案,请参阅专门的jmvtools指南。
构建失败
TypeScript错误:
# Clean rebuild
rm -rf build node_modules
npm install
npm run build权限错误:
# Linux/Mac
chmod +x build/index.js
# Windows: Run terminal as Administrator if neededMCP客户端连接问题
服务器未出现在MCP客户端中:
- 验证配置文件中的JSON语法(使用JSON验证器)
- 使用绝对路径(非相对路径:
~/或.\) - 完全重新启动MCP客户端
- 检查客户端日志中的错误消息
Windows路径格式:
{
"args": ["D:\\path\\to\\server\\build\\index.js"] // Use double backslashes
// OR
"args": ["D:/path/to/server/build/index.js"] // Use forward slashes
}贡献
欢迎投稿!请随时提交问题或拉取请求。
许可证
麻省理工学院
