
R as a server: Configure R-based tools with LLM-enabled apps
Source:R/server.R
, R/session.R
server.Rd
Together, these functions implement a model context protocol server for your R session.
Arguments
- tools
A list of tools created with
ellmer::tool()
that will be available from the server or a file path to an .R file that, when sourced, will return a list of tools. Any list that could be passed toChat$set_tools()
can be passed here. By default, the package won't serve any tools other than those needed to communicate with active R sessions.
Configuration
mcp_server()
should be configured with the MCP clients via the Rscript
command. For example, to use with Claude Desktop, paste the following in your
Claude Desktop configuration (on macOS, at
file.edit("~/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:
mcp_server() is not intended for interactive use.
The server interfaces with the MCP client on behalf of your R session.
Use mcp_session()
to make your R session available to the server.
Place a call to mcptools::mcp_session()
in your .Rprofile
, perhaps with
usethis::edit_r_profile()
, to make every interactive R session you start
available to the server.
See also
The "R as an MCP server" vignette at
vignette("server", package = "mcptools")
delves into further detail on setup and customization.These functions implement R as an MCP server. To use R as an MCP client, i.e. to configure tools from third-party MCP servers with ellmer chats, see
mcp_tools()
.
Examples
# should only be run non-interactively, and will block the current R process
# once called.
if (FALSE) {
# to start a server with a tool to draw numbers from a random normal:
library(ellmer)
tool_rnorm <- tool(
rnorm,
"Draw numbers from a random normal distribution",
n = type_integer("The number of observations. Must be a positive integer."),
mean = type_number("The mean value of the distribution."),
sd = type_number("The standard deviation of the distribution. Must be a non-negative number.")
)
mcp_server(tools = list(tool_rnorm))
# can also supply a file path as `tools`
readLines(system.file("example-ellmer-tools.R", package = "mcptools"))
mcp_server(tools = system.file("example-ellmer-tools.R", package = "mcptools"))
}
if (interactive()) {
mcp_session()
}