Execute R Code with AI Assistants
An R package implementing MCP both ways: R as an MCP server for Claude/Copilot to run your R code, and R as an MCP client via ellmer.
1.0.3Add to Favorites
Why it matters
Enable AI assistants to execute R code directly within your interactive R sessions, bridging the gap between natural language queries and R's powerful data analysis capabilities.
Outcomes
What it gets done
Allow AI assistants to run R code in your interactive sessions.
Integrate third-party MCP servers with R applications.
Connect R to AI assistants like Claude Desktop and GitHub Copilot.
Execute R code based on natural language prompts.
Source
Get it from source
Spark does not host a copy of it.
Open sourceReports
Agent outcome reports
No reports yet
Overview
mcptools implements bidirectional MCP support in R - exposing live R sessions and functions as MCP tools for clients like Claude, and letting R code register external MCP servers as ellmer chat tools. Use it when Claude needs to run code or inspect objects in a live R session, or when building an ellmer-based R chat app that needs external MCP server context. The package's former default toolset now lives in the btw package instead.
What it does
Implements the Model Context Protocol in R, in both directions. R as an MCP server: when configured with mcptools, MCP-enabled tools like Claude Desktop, Claude Code, and VS Code GitHub Copilot can run R code in your live sessions to answer questions - it supports arbitrary R functions, and the companion btw package adds a default toolset for browsing installed-package documentation, inspecting global-environment objects, and retrieving session/platform metadata. R as an MCP client: register third-party MCP servers (GitHub, Confluence, Google Drive, and others) with ellmer chats to pull additional context into apps like shinychat and querychat.
Inputs and outputs
Install from CRAN:
install.packages("mcptools")
Or the development version: pak::pak("posit-dev/mcptools"). Configure Claude Desktop (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
{ "mcpServers": { "r-mcptools": { "command": "Rscript", "args": ["-e", "mcptools::mcp_server()"] } } }
Or Claude Code: claude mcp add -s "user" r-mcptools -- Rscript -e "mcptools::mcp_server()". To expose a specific interactive R session, call mcptools::mcp_session() inside it (optionally from .Rprofile to register every session automatically). It can also be deployed to Posit Connect as an HTTP MCP server via a _server.yml with engine: mcptools.
Who it's for
R users who want AI coding assistants to execute real R code against their live sessions and objects, and R developers who want to pull third-party MCP tools (GitHub, Confluence, Google Drive) into R-based chat apps. CRAN package, experimental lifecycle stage.
Example, as an MCP client
Once a config file exists (mcptools looks for ~/.config/mcptools/config.json by default), mcp_tools() returns a list of ellmer tools that can be passed straight into $set_tools():
ch <- ellmer::chat_anthropic()
ch$set_tools(mcp_tools())
ch$chat("What issues are open on posit-dev/mcptools?")
Example, as an MCP server
Without mcptools, Claude can't run R code or talk to a live interactive R session at all. With it connected, asking Claude "From what year is the earliest recorded sample in the forested data in my Positron session?" triggers a describe data frame tool call with data_frame set to forested; mcptools routes that request to the open Positron session, gets back summary statistics as JSON, and Claude integrates the real answer into its response - in this case, that the earliest recorded sample is from 1995, read directly off the live data's year column rather than guessed. The lifecycle badge on the package marks it experimental, and it is distributed and developed on GitHub under posit-dev, the same organization behind Positron and the ellmer/shinychat/querychat ecosystem it plugs into.
Source README
mcptools 
mcptools implements the Model Context
Protocol in R. There are two sides to
mcptools:
R as an MCP server:
When configured with mcptools, MCP-enabled tools like Claude Desktop, Claude Code, and VS Code GitHub Copilot can run R code in the sessions you have running to answer your questions. While the package supports configuring arbitrary R functions, you may be interested in the btw package’s integrated support for mcptools, which provides a default set of tools to to peruse the documentation of packages you have installed, check out the objects in your global environment, and retrieve metadata about your session and platform.
R as an MCP client:
Register third-party MCP servers with ellmer chats to integrate additional context into e.g. shinychat and querychat apps.
Installation
Install mcptools from CRAN with:
install.packages("mcptools")
You can install the development version of mcptools like so:
pak::pak("posit-dev/mcptools")
R as an MCP server
mcptools can be hooked up to any application that supports MCP. For
example, to use with Claude Desktop, you might paste the following in
your Claude Desktop configuration (on macOS, at
~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"r-mcptools": {
"command": "Rscript",
"args": ["-e", "mcptools::mcp_server()"]
}
}
}
Or, to use with Claude Code, you might type in a terminal:
claude mcp add -s "user" r-mcptools -- Rscript -e "mcptools::mcp_server()"
Then, if you’d like models to access variables in specific R sessions,
call mcptools::mcp_session() in those sessions. (You might include a
call to this function in your .Rprofile, perhaps using
usethis::edit_r_profile(), to automatically register every session you
start up.)
To deploy an HTTP MCP server to Posit Connect, add a _server.yml file
with engine: mcptools and a tools file:
engine: mcptools
tools: tools.R
Deploy the directory as an R API and mark it as MCP content:
rsconnect::deployAPI(".", contentCategory = "mcp")
If the content URL is https://connect.example.com/content/abc123/, use
https://connect.example.com/content/abc123/mcp as the MCP endpoint.
If you cannot set contentCategory = "mcp" during deployment, set the
MCP category in Connect after deploying and set minimum processes to at
least 1.
R as an MCP client
mcptools uses the Claude Desktop configuration file format to register third-party MCP servers, as most MCP servers provide setup instructions for Claude Desktop in their documentation. For example, here’s what the official GitHub MCP server configuration would look like:
{
"mcpServers": {
"github": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR_TOKEN>"
}
}
}
}
Once the configuration file has been created (by default, mcptools will
look to file.path("~", ".config", "mcptools", "config.json")),
mcp_tools() will return a list of ellmer tools which you can pass
directly to the $set_tools() method from ellmer:
ch <- ellmer::chat_anthropic()
ch$set_tools(mcp_tools())
ch$chat("What issues are open on posit-dev/mcptools?")
Example
In Claude Desktop, I’ll write the following:
“From what year is the earliest recorded sample in the
foresteddata in my Positron session?”
Without mcptools, Claude couldn’t get far here; by default, it can’t run R code and doesn’t have any way to “speak to” my interactive R sessions.
Using the package, the model asks to describe the data frame using a structure that will show summary statistics from the data. mcptools will appropriately route the request to the open Positron session, forwarding the results back to the model for it to situate in a response.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.