Step 6: Connect your R session

Due by Friday, 25 September 2026

Claude Code has no R session of its own. It can run a script or render a Quarto document, but it cannot see the data frame you have open in RStudio. Two packages change that. mcptools connects the agent to your running RStudio session, and btw gives it the tools to look at the objects there, which is how it inspects your data on the day. This connection is specific to R and RStudio, see the note on the Prepare page. If you work in another language or editor, the agent still works, but this step may not apply.

Part 1: Install the packages

Open RStudio. The Console is the pane at the bottom left, the one with a > prompt. Click into it, type the line below, and press Enter. Wait until the download messages stop and the prompt is back:

install.packages(c("mcptools", "btw"))

Part 2: Register it with Claude Code

Open the Terminal app (Spotlight, then type Terminal). Any folder is fine, this setting is not tied to a folder. Copy the line below, paste it after the prompt, and press Enter. It answers with a line saying the server was added:

claude mcp add -s user r-btw -- Rscript -e "btw::btw_mcp_server(list('env', 'docs', 'sessioninfo'))"

Open PowerShell (Start menu, then type PowerShell). On Windows the Rscript program is usually not findable from the terminal, so you give the full path. Get it from the RStudio Console, the pane at the bottom left:

normalizePath(file.path(R.home("bin"), "Rscript.exe"))

It prints something like C:\Program Files\R\R-4.5.1\bin\Rscript.exe. Use your path, in quotes, in PowerShell:

claude mcp add -s user r-btw -- "C:\Program Files\R\R-4.5.1\bin\Rscript.exe" -e "btw::btw_mcp_server(list('env', 'docs', 'sessioninfo'))"
NoteRegistered r-mcptools in an earlier version of this page?

The earlier line started mcptools::mcp_server() without any tools, so the agent could find your session but not see inside it. Remove it and run the line above instead:

claude mcp remove -s user r-mcptools

Check, in the same terminal:

claude mcp list

The line for r-btw should end in Connected.

Part 3: Let R announce its sessions

The server needs to know which R session to talk to. One line in your R profile registers every RStudio session you start.

Back in RStudio, click into the Console and run:

file.edit("~/.Rprofile")

A file called .Rprofile opens in the editor pane at the top left. It is empty unless you have set up R this way before. Paste this line into it, then save with Cmd+S on macOS or Ctrl+S on Windows:

if (interactive()) mcptools::mcp_session()

Restart R from the menu bar: Session, Restart R. The Console clears and shows a fresh prompt. Then, in the Console, create something for the agent to look at:

x <- head(mtcars)

Part 4: Try it

Check the Environment pane at the top right of RStudio: x is listed there as 6 obs. of 11 variables. Now open a terminal at your setup repository (as in step 4), start claude, and ask:

What objects are in my R session?

If Claude Code asks before using an r-btw tool, read the prompt and allow it; in auto mode it runs without asking. The answer names x and describes it as a data frame with 6 rows and 11 columns. On the day you do the same with the workshop data loaded.

ImportantWhat this connection means

With this in place the agent can read every object in your live session, and whatever it reads is sent to the model. In manual mode the permission prompt is what stands between it and that; in auto mode a built-in check takes that role, and it does not know your data. Read what the agent proposes, or what it did, rather than approving by reflex. That habit is the point of the workshop, and this is where you start it.