library(mcpr)
#>
#> Attaching package: 'mcpr'
#> The following object is masked from 'package:methods':
#>
#> initialize
#> The following object is masked from 'package:base':
#>
#> write
Introduction
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.
Creating a Simple MCP Server
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)
Using Standard Input/Output
The simplest way to interact with your MCP server is through standard
input/output using the serve_io
function:
# Start the server using standard input/output
serve_io(mcp_server)
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.
Testing the Server
To test your server, you can run the above code in an R script and interact with it using the command line. For example:
- Save the code in a file named
mcp_adder.R
- Run it from the command line:
Rscript mcp_adder.R
- Send a JSON-RPC request to standard input:
{"jsonrpc": "2.0", "id": 123, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 15, "b": 27}}}
The server will respond with: