The mcpr package allows you to create Model Context
Protocol (MCP) servers in R. This vignette will guide you through
building a very simple MCP server with a basic addition tool.
Let’s create a simple MCP server that can add two numbers:
# Create a new MCP server
mcp_server <- new_server(
name = "Simple Adder",
description = "A basic MCP server that adds two numbers",
version = "1.0.0"
)
# Create a simple addition tool
add_tool <- new_tool(
name = "add",
description = "Adds two numbers together",
input_schema = schema(
properties = list(
a = property_number("First number", "First number to add", required = TRUE),
b = property_number("Second number", "Second number to add", required = TRUE)
)
),
handler = function(input) {
result <- input$a + input$b
response_text(paste("The sum is:", result))
}
)
# Add the tool to the server
mcp_server <- add_capability(mcp_server, add_tool)The simplest way to interact with your MCP server is through standard
input/output using the serve_io function:
When you run serve_io(mcp_server), your R session will
wait for input in JSON-RPC 2.0 format. You can then communicate with
your server by sending JSON-RPC requests.
To test your server, you can run the above code in an R script and interact with it using the command line. For example:
mcp_adder.RRscript mcp_adder.R{"jsonrpc": "2.0", "id": 123, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 15, "b": 27}}}The server will respond with:
Once you’re comfortable with the basics, you can:
See the other vignettes and function documentation for more details on these advanced features.