Builds a mcp_servers list entry that launches an R subprocess running
mcptools::mcp_server() over stdio. Requires the mcptools and ellmer
packages to be installed on the system.
Arguments
- tools_script
Character(1) or NULL. Path to an
.Rscript that, when sourced, yields alist()ofellmer::tool()objects. WhenNULLonly the built-in session tools are exposed (controlled bysession_tools).- session_tools
Logical. Whether to expose the built-in
mcptoolssession-management tools (list_r_sessions,select_r_session). DefaultFALSEfor embedding use-cases.- rscript
Character(1). Absolute path to the
Rscriptbinary. Defaults to the binary of the currently running R installation (Rscripton Linux/macOS,Rscript.exeon Windows), resolved viaR.home("bin")— always an absolute path, independent ofPATH. Falls back toSys.which("Rscript")if that path does not exist. Pass an explicit path to use a specific R installation.
Value
A named list with type, command, and args suitable for use
as a value inside ClaudeAgentOptions(mcp_servers = list(...)).
Details
Defining tools
Create a standalone .R script that returns a list() of ellmer::tool()
objects (this is the value mcptools::mcp_server() accepts via its
tools argument):
# my_tools.R
library(ellmer)
list(
ellmer::tool(
fun = function(a, b) a + b,
description = "Add two numbers",
arguments = list(
a = ellmer::type_number("First number"),
b = ellmer::type_number("Second number")
)
)
)Then pass the script path:
options <- ClaudeAgentOptions(
mcp_servers = list(my_tools = r_mcp_server("my_tools.R")),
allowed_tools = "mcp__my_tools__add"
)Examples
# Create an MCP server entry from a tools script
entry <- r_mcp_server("my_tools.R")
entry$type # "stdio"
#> [1] "stdio"
entry$command # path to Rscript
#> [1] "/opt/R/4.6.0/lib/R/bin/Rscript"
# Use in ClaudeAgentOptions
if (FALSE) { # \dontrun{
opts <- ClaudeAgentOptions(
mcp_servers = list(my_tools = r_mcp_server("my_tools.R")),
allowed_tools = "mcp__my_tools__add"
)
} # }