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 Model Context Protocol (MCP) allows you to register custom tools
with various AI clients. This vignette demonstrates how to create a
simple MCP server with mcpr
and register it with popular
clients including Claude Code, Cursor, OpenAI GPTs, and LangChain.
Creating a Simple Math Calculator Server
Let’s create a simple MCP server with a basic calculator tool:
# Create a new MCP server
math_server <- new_server(
name = "r-calculator",
description = "A simple calculator that performs basic arithmetic operations",
version = "1.0.0"
)
# Create a calculator tool
calculator <- new_tool(
name = "math_calculator",
description = "Performs basic arithmetic operations",
input_schema = schema(
properties = properties(
operation = property_enum(
"Operation",
"Math operation to perform",
enum = c("add", "subtract", "multiply", "divide"),
required = TRUE
),
a = property_number("First number", "First operand", required = TRUE),
b = property_number("Second number", "Second operand", required = TRUE)
)
),
handler = function(input) {
result <- switch(input$operation,
"add" = input$a + input$b,
"subtract" = input$a - input$b,
"multiply" = input$a * input$b,
"divide" = input$a / input$b
)
response_text(paste("Result:", result))
}
)
# Add the tool to the server
math_server <- add_capability(math_server, calculator)
Save this code to a file, for example
calculator_server.R
, and add code to serve it at the
end:
# At the end of calculator_server.R, add:
serve_io(math_server)
Registering with MCP Clients
Claude Code
Claude Code supports registering MCP tools using the
claude mcp
command:
# Register the MCP server
claude mcp add r-calculator -- Rscript /path/to/calculator_server.R
# List registered MCP servers
claude mcp list
# Use the registered tool
claude "Add 2 to 40"
Claude will automatically discover and use the registered MCP server’s capabilities.
Cursor
Cursor supports registering MCP tools via its configuration:
- Open Cursor settings
- Navigate to the “Tools” section
- Add a new tool with:
- Name: r-calculator
- Command: Rscript /path/to/calculator_server.R
- Save the settings
Now when you ask Cursor to perform calculations, it will have access to your MCP tool.
OpenAI GPT
For OpenAI GPTs, you’ll need to run your MCP server over HTTP:
# Modify your calculator_server.R to use HTTP instead of IO
serve_http(math_server, port = 8080)
Then register it with a custom GPT:
- Create a custom GPT in the GPT Builder
- In the “Configure” tab, add an “Action”
- Set the Authentication to “None”
- Set the API URL to your server (e.g., http://localhost:8080)
- Import schema from URL: http://localhost:8080/openapi.json
- Save your GPT
Your custom GPT will now be able to use your MCP calculator.
LangChain
LangChain provides adapters specifically for MCP integration:
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_tool_calling_agent
from langchain_openai import ChatOpenAI
# Connect to your R-based MCP server
# You need to run your server with serve_http for this approach
mcp_client = MultiServerMCPClient(
servers=[{"url": "http://localhost:8080"}]
)
# Get tools from the MCP server
tools = mcp_client.get_tools()
# Create a LangChain agent with the MCP tools
llm = ChatOpenAI(model="gpt-4o")
agent = create_tool_calling_agent(llm, tools)
# Use the agent
response = agent.invoke({"input": "What is 10 multiplied by 5?"})
print(response["output"])
Alternatively, you can use the process-based approach:
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_tool_calling_agent
from langchain_openai import ChatOpenAI
# Connect to your R-based MCP server using process
mcp_client = MultiServerMCPClient(
servers=[{"command": ["Rscript", "/path/to/calculator_server.R"]}]
)
# The rest is the same as above
tools = mcp_client.get_tools()
llm = ChatOpenAI(model="gpt-4o")
agent = create_tool_calling_agent(llm, tools)
Conclusion
With mcpr
, you can create MCP servers that register
seamlessly with various AI clients. This enables AI tools to leverage
R’s statistical and data processing capabilities.
For more advanced usage, consider:
- Adding multiple tools to your server
- Creating resources to serve data
- Implementing prompts for text generation
- Handling more complex data types in your tools
See the other vignettes and function documentation for more details on these advanced features.