MCP Connector

Execute R Code with AI Assistants

Let Claude run R code and inspect live R sessions, or connect R (via ellmer) to third-party MCP servers - bidirectional MCP for R.

Works with rclaudegithubvscodepositron

91
Spark score
out of 100
Updated 16 days ago
Version 1.0.0
Models
universal

Add 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

01

Allow AI assistants to run R code in your interactive sessions.

02

Integrate third-party MCP servers with R applications.

03

Connect R to AI assistants like Claude Desktop and GitHub Copilot.

04

Execute R code based on natural language prompts.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-r-mcptools | bash

Overview

R mcptools MCP Server

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

mcptools implements the Model Context Protocol in R, working in two directions: it lets MCP clients like Claude Desktop, Claude Code, and VS Code Copilot Chat run R code and inspect objects in your live, running R sessions (R as an MCP server); and it lets R code register third-party MCP servers (GitHub, Confluence, Google Drive, etc.) as tools for an ellmer chat object (R as an MCP client).

When to use - and when NOT to

Use it when you want an AI assistant to answer questions grounded in your actual R session state - for example, asking about the earliest year in a data frame you have loaded, and having the assistant call a tool that inspects that live object and returns real summary statistics instead of guessing. Use the client side when you're writing R code with ellmer and want to pull in context from external MCP servers into a chat or app (e.g. shinychat, querychat). Do not confuse this with acquaint or btw - this package used to be called acquaint and shipped a default toolset for R-as-server; that default toolset has moved to the btw package (use btw::btw_mcp_server()/btw::btw_mcp_session() for that functionality now), while mcptools itself supports configuring arbitrary R functions as tools.

Capabilities

R as an MCP server: mcp_server() starts a server exposing configurable R-function tools; mcp_session() called inside a specific R session (e.g. added to .Rprofile) registers that session so models can access its variables and run code in it. Multiple concurrent R sessions/projects can be connected simultaneously.

R as an MCP client: mcp_tools() reads a Claude-Desktop-format config file (default ~/.config/mcptools/config.json) listing third-party MCP servers, and returns a list of ellmer tools to pass directly to chat$set_tools().

How to install

install.packages("mcptools")
# or development version
pak::pak("posit-dev/mcptools")

For Claude Desktop (R as server), add to claude_desktop_config.json:

{
  "mcpServers": {
    "r-mcptools": {
      "command": "Rscript",
      "args": ["-e", "mcptools::mcp_server()"]
    }
  }
}

Or for Claude Code: claude mcp add -s "user" r-mcptools -- Rscript -e "mcptools::mcp_server()". Then call mcptools::mcp_session() inside any R session you want models to access. For R as a client, create the config file listing your third-party MCP servers (in Claude Desktop's config format), then call mcp_tools() and pass the result to ellmer::chat_anthropic()$set_tools().

Who it's for

R users who want Claude or another AI assistant to run code and inspect objects in their live R sessions, or R developers building ellmer-based chat apps who want to pull in context from external MCP servers.

Source README

mcptools A hexagonal logo showing a bridge connecting two portions of a forested meadow.

Lifecycle:experimental
CRANstatus
R-CMD-check

mcptools implements the Model Context
Protocol
in R. There are two sides to
mcptools:

R as an MCP server:

A system architecture diagram showing three main components: Client (left), Server (center), and Session (right). The Client box lists AI coding assistants including Claude Desktop, Claude Code, Copilot Chat in VS Code, and Positron Assistant. The Server is initiated with `mcp_server()` and contains tools for R functions like reading package documentation, running R code, and inspecting global environment objects. Sessions can be configured with `mcp_session()` and can optionally connect to interactive R sessions, with two example projects shown: 'Some R Project' and 'Other R Project'.

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:

An architecture diagram showing the Client (left) with R code using the ellmer library to create a chat object and then setting tools from mcp with `mcp_tools()`, and the Server (right) containing third-party tools including GitHub (for reading PRs/Issues), Confluence (for searching), and Google Drive (for searching). Bidirectional arrows indicate communication between the client and server components.

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 forested data
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.

A screencast of a chat with Claude. After the question is asked, a tool called 'describe data frame' is called with the `data_frame` argument set to `forested`. The results are returned from mcptools as json, which the model then integrates into its response: 'Based on the data structure, I can see there's a `year` column with values ranfing from 1995 to 2024. The earliest recorded sample in the `forested` data is from 1995.'

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.