Sandbox
@posit-dev/mcptools

MCP server and client for R

mcptools lets agents use R as an MCP server, so tools like Claude Desktop, Claude Code, and Copilot Chat can run code or inspect objects in live R sessions. It also lets R connect to third-party MCP servers through ellmer, so R chat apps can pull in external context and tools.

194 stars20 forksRUpdated 26d ago
Who it's for

Builders who want their agent to read R objects, run R code, or call external MCP tools from R.

What it delivers

You can connect agent chats to live R sessions instead of copying data and code back and forth.

What it does

Serve R functions over MCP

Expose R code and tools through `mcptools::mcp_server()` for MCP-capable clients.

Attach live R sessions

Use `mcptools::mcp_session()` so tools can run against specific interactive R sessions.

Add MCP tools to ellmer chats

Load third-party MCP servers with `mcp_tools()` and pass them into `ellmer` chat objects.

Deploy to Posit Connect

Use `_server.yml` with `engine: mcptools` and deploy as MCP content with `rsconnect::deployAPI()`.

Use Claude Desktop config format

Store server definitions in `~/.config/mcptools/config.json` and reuse common MCP setup examples.

How to get it

  1. 1Or, to use with Claude Code, you might type in a terminal
    claude mcp add -s "user" r-mcptools -- Rscript -e "mcptools::mcp_server()"
  2. 2Once the configuration file has been created (by default, mcptools will look to…
    ch <- ellmer::chat_anthropic()
    ch$set_tools(mcp_tools())
    
    ch$chat("What issues are open on posit-dev/mcptools?")

README

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

Lifecycle:
experimental CRAN
status 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.

Files in the repo

Repository payload23 top-level entries
  • .github
  • .vscode
  • inst
  • man
  • pkgdown
  • R
  • tests
  • vignettes
  • _pkgdown.yml
  • .gitignore
  • .Rbuildignore
  • AGENTS.md
  • air.toml
  • CLAUDE.md
  • cran-comments.md
  • DESCRIPTION
  • LICENSE
  • LICENSE.md
  • mcptools.Rproj
  • NAMESPACE
  • NEWS.md
  • README.md
  • README.Rmd

Discussion (0)

Ask about usage, or say what you built with it

Sign in to join the discussion.

No comments yet. Be the first to say what this is good for.

More connectors

Real-time global intelligence dashboard. AI-powered news aggregation, geopolitical monitoring, and infrastructure tracking in a unified situational awareness interface

86k

High-performance code intelligence MCP server. Indexes codebases into a persistent knowledge graph — average repo in milliseconds. 158 languages, sub-ms queries, 99% fewer tokens. Single static binary, zero dependencies.

43k

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM (Claude, Gemini, Grok, DeepSeek, Ollama…) with Codex CLI, App, SDK, and Claude Code

14k
okf-memory/
okf-agent-memory

Git-native persistent memory for AI coding agents. Implements Google OKF v0.2 with sub-300µs in-memory BM25 search, embedded MCP server, and progressive disclosure. Slashes token bloat by 80% with zero external databases or dependencies. Built in pure Go.

547
tirth8205/
code-review-graph

Local-first code intelligence graph for MCP and CLI. Builds a persistent map of your codebase so AI coding tools read only what matters, with benchmarked context reductions on reviews and large-repo workflows.

31k
2akouwu/
reverify

Stop your AI from making things up — it proposes, deterministic tools decide, every claim checked against ground truth with evidence. Grounded facts and context survive resets. Reverse engineering is the proving ground. MCP server + CLI.

1.1k