library(mcpr)
#>
#> Attaching package: 'mcpr'
#> The following object is masked from 'package:methods':
#>
#> initialize
#> The following object is masked from 'package:base':
#>
#> writeThe 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.
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:
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 supports registering MCP tools via its configuration:
Now when you ask Cursor to perform calculations, it will have access to your MCP tool.
For OpenAI GPTs, you’ll need to run your MCP server over HTTP:
Then register it with a custom GPT:
Your custom GPT will now be able to use your MCP calculator.
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)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:
See the other vignettes and function documentation for more details on these advanced features.