mcpr exposes R functions through the Model Context Protocol (MCP), enabling seamless integration with AI assistants like Claude Desktop.
Installation
# install.packages("devtools")
devtools::install_github("chi2labs/mcpr")
Quick Start
Using Decorators
Create a file with decorated functions:
# analysis-tools.R
#* @mcp_tool
#* @description Calculate summary statistics for a numeric vector
#* @param x numeric vector to analyze
#* @param na.rm logical whether to remove NA values (default: TRUE)
calculate_stats <- function(x, na.rm = TRUE) {
list(
mean = mean(x, na.rm = na.rm),
median = median(x, na.rm = na.rm),
sd = sd(x, na.rm = na.rm),
min = min(x, na.rm = na.rm),
max = max(x, na.rm = na.rm)
)
}
Load and run:
server <- mcp("Analysis Server", "1.0.0")
server$mcp_source("analysis-tools.R")
server$mcp_run(transport = "http", port = 8080)
Advanced Usage
Register Existing Functions
server <- mcp_http("Stats Server", "1.0.0")
server$mcp_tool(
name = "t_test",
fn = t.test,
description = "Perform t-test"
)
server$mcp_tool(
name = "cor_test",
fn = cor.test,
description = "Correlation test"
)
Production Deployment
server <- mcp_http(
name = "Production Server",
version = "1.0.0",
host = "0.0.0.0", # Listen on all interfaces
port = 8080,
log_file = "mcp-server.log",
log_level = "info"
)