AI Agent Integration Guide
This guide covers how to integrate MCPR with different AI coding assistants.
Supported Agents
MCPR currently supports: - Claude Desktop - Gemini (via MCP) - GitHub Copilot (via MCP)
Claude Desktop Setup
Automatic Configuration
library(MCPR)
install_mcpr(agent = "claude")Using MCPR Tools
Once configured, your AI agent will have access to these tools:
Workflow Examples
Data Analysis Workflow
- Start R session:
mcpr_session_start() - Agent joins session:
manage_r_sessions('join', session=1) - Load data:
execute_r_code("data <- read.csv('data.csv')") - Explore:
view('session')to see loaded objects - Visualize:
show_plot("plot(data$x, data$y)") - Iterate and refine analysis
Troubleshooting
Agent can’t see tools: Restart the agent after configuration.
Permission errors: Check file paths and R library permissions.
Connection timeouts: Increase timeout in MCP configuration.
Common Issues
Working with MCPR in renv Projects
When using MCPR with AI agents (like Claude Desktop or Codex) in renv-enabled projects, you may encounter connection issues where the agent cannot find or load the MCPR package.
Problem: The MCP server fails to start or behaves
unexpectedly because renv isolates the project’s R library.
This causes the AI agent to run a different instance or version of
MCPR (installed in the global library) than the one you are
running in your interactive R session (often in the local library),
leading to communication mismatches.
Solution: Configure renv to use your global MCPR installation by creating a scaffold file before initializing renv:
-
Before running
renv::init(), create a file calledscaffold.Rin your project root:
- Initialize renv normally:
renv::init()This approach ensures that renv detects MCPR as a dependency and links to your global installation rather than trying to install a separate copy. This is similar to the recommended approach for VSCode-R with renv.
For existing renv projects:
If you already have an renv project without MCPR, you can add it:
# Install MCPR into the renv library
renv::install("path/to/MCPR", repos = NULL, type = "source")
# Or use the global installation
renv::use_package("MCPR", type = "Imports", dev = TRUE)
renv::snapshot()GitHub Copilot / Codex Configuration Issues
Some newer versions of AI coding assistants require explicit specification of the MCP server connection type.
Problem: MCP server fails to connect with error messages like “handshaking with MCP server failed” or “connection closed: initialize response”.
Solution: Ensure your configuration file explicitly
declares "type": "stdio".
Correct configuration for
~/.vscode/mcp.json or equivalent:
{
"servers": {
"MCPR": {
"type": "stdio",
"command": "R",
"args": [
"--quiet",
"--slave",
"-e",
"MCPR::mcpr_server()"
]
}
},
"inputs": []
}Key points: - The "type": "stdio" field
is required for newer MCP client implementations -
Without this field, the client may fail to establish the communication
channel - The "inputs": [] field may also be required by
some clients
Protocol Version Mismatches
Different AI coding agents support different MCP protocol versions. MCPR implements protocol version negotiation to handle this automatically.
Supported versions: - 2024-11-05
(baseline) - 2025-03-26 - 2025-06-18 -
2025-11-25 (latest)
If you encounter version-related errors, check your agent’s logs for protocol version information. MCPR should automatically negotiate the highest mutually supported version.
