zygi/rstudiomcp
If you are the rightful owner of rstudiomcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The RStudio MCP server bridges RStudio sessions with Claude Code, enabling seamless interaction and execution of R code.
rstudiomcp
MCP (Model Context Protocol) server for RStudio - Bridge your RStudio session with Claude Code.
What is this?
This R package runs a local MCP server inside your RStudio session, allowing Claude Code to:
- Execute R code in your session
- Inspect objects and environments
- Read and edit open documents
- Source R scripts
- Capture plots from the Plots pane
- Access HTML content from the Viewer pane
Installation
You'll probably want Claude Code installed to use with RStudio. Claude Code installation instructions:
If you know how to use the command line, just follow the official instructions. Otherwise, ask Claude (or your preferred AI assistant) this:
Please help me install Claude Code. The latest instructions are at https://docs.claude.com/en/docs/claude-code/setup - you should fetch them. Please ask me about what system I use - Windows, WSL, MacOS, Linux, and based on that walk me through. Thanks!
RStudio MCP Extension
To install the RStudio MCP extension, run the following lines in your RStudio Console:
# Install from GitHub
install.packages("remotes")
library("remotes")
remotes::install_github("zygi/rstudiomcp")
Per-project Setup
Option 1: Auto-load
Use the RStudio Addin: "Enable Auto-load (per project)"
This adds library(rstudiomcp) to your project's .Rprofile configuration, so the server starts automatically when you open the project.
Restart your RStudio session after enabling auto-load. You should see the following output:
Starting MCP Server on http://localhost:16751
MCP Server started successfully!
Endpoint: http://localhost:16751
Transport: Streamable HTTP (JSON responses, no SSE)
Run stop_mcp_server() to stop the server
Added RStudio MCP server to C:/Users/zygi/Documents/test_proj/.mcp.json
Option 2: Manual start (For development/testing)
library(rstudiomcp)
start_mcp_server()
Usage with Claude Code
- Start the MCP server in RStudio
- Open Claude Code inside the project folder (from either the integrated RStudio terminal, or external terminal)
[!IMPORTANT] For easy setup, you should open Claude Code inside the same folder that RStudio uses as project home. (This might not be the same as the "active directory" you set in the RStudio filesystem pane.) If in doubt, look inside the RStudio console for the log message
Added RStudio MCP server to <path>/.mcp.json- this means Claude Code will automatically connect if you start it inside<path>.
- Claude Code should automatically detect the server and connect to it! You should verify that by typing
/mcpand looking at the details.
You can now ask Claude Code to interact with your R session!
Concurrent sessions
Concurrent sessions - having two RStudio windows open, both with RStudio MCP servers running - currently aren't supported out of the box. A somewhat manual way to achieve this is to NOT activate auto-load, and instead load the servers manually in the sessions by providing different ports:
library(rstudiomcp)
start_mcp_server(port=12345)
in one session, and start_mcp_server(port=12346) in another. Then start two Claude Code sessions, in the corresponding RStudio project paths, and they should just connect!
Simple Troubleshooting
If you're having problems setting this up, paste the following into Claude:
Hi! I'm trying to set up RStudio-MCP to use with a Claude Code session but it's not working.
Could you please help me debug? The project readme is at https://github.com/zygi/rstudiomcp/blob/master/README.md
(but maybe fetch it as a raw file.)
Available Tools
Claude Code can use these tools:
Code Execution
eval_r- Execute R code in the R sessionsource_active_document- Run the currently active R script (like clicking Source button). Optional: specifystart_lineandend_lineto source only specific lines
Environment Inspection
list_environments- List available R environmentslist_objects- List objects in an environmentget_object- Get detailed object information (type, class, structure, preview)get_console_history- View recent console commands
Document Editing
Note: All document editing tools work on the currently active document only. Use
create_documentto create new docs oropen_document_fileto open saved files.
create_document- Create a new document with optional file path (becomes active automatically)open_document_file- Open or refocus a saved document file by pathget_active_document- Read the active document (shows ID, path, and contents)insert_text- Insert text at cursor or specific location in the active documentreplace_text_range- Replace text with exact string matching in the active document
Visualization
get_current_plot- Capture the current plot as an imageget_latest_viewer_content- Get HTML content of the last rendered page in the Viewer pane
Configuration
Check Status
mcp_status()
Or use the RStudio Addin: "MCP Server Status"
Change Port
configure_mcp_server()
Default port is 16751.
Restart Server
restart_mcp_server()
Or use the RStudio Addin: "Restart MCP Server"
Disable Auto-start
disable_autoload()
Or use the RStudio Addin: "Disable Auto-load (per project)"
Technical Troubleshooting
"Address already in use"
The port is occupied. Restart the server:
restart_mcp_server()
If that prompts to kill a process, type yes to confirm.
Or change the port:
configure_mcp_server()
Server won't start after sleep/wake
The server may get orphaned. Use:
restart_mcp_server()
Development
See CLAUDE.md for developer notes and architecture details.
Once the server is up, you can also use MCP Inspector for debugging:
npx @modelcontextprotocol/inspector http://localhost:16751/
Technical Details
- The R package runs in RStudio's R session and starts up an HTTP MCP server on 127.0.0.1
- It also writes (or updates) a
.mcp.jsonfile in the project directory. - Once you start Claude Code, it sees this file and attempts to connect to the MCP server.
Notes:
- To allow MCP to access what's on the Viewer panel, the server needs to wrap the
viewerfunction. This is obviously hacky, I haven't seen it break anything yet but please report if you find it a real problem.
Contributing
Issues and pull requests welcome! The author isn't an R expert so some stupid decisions may have been made. This is not an official RStudio product.