This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
plumber2mcp is an R package that adds Model Context Protocol (MCP) support to Plumber APIs. It transforms existing Plumber endpoints into MCP tools that AI assistants can discover and call via JSON-RPC.
R/pr_mcp.R - Core implementation with three key functions:
pr_mcp()- Main user-facing function that adds MCP capabilities to a Plumber routercreate_mcp_handler()- Creates JSON-RPC message handlers for MCP protocolextract_plumber_tools()- Converts Plumber endpoints to MCP tool definitions
Protocol Implementation:
- HTTP transport layer (Plumber endpoints at
/mcpand/mcp/messages) - JSON-RPC 2.0 protocol handlers for
initialize,tools/list, andtools/callmethods - Automatic endpoint discovery and schema generation from function signatures
Transport Bridge:
inst/examples/stdio-wrapper.py- Python bridge that converts stdio transport (expected by MCP clients) to HTTP calls- Handles R's jsonlite serialization quirks (single-element arrays, empty objects)
-
Endpoint Transformation: Plumber endpoints become MCP tools with naming pattern
{METHOD}__{path}(e.g.,GET__echo,POST__add) -
Schema Generation: Function parameters automatically converted to JSON Schema with roxygen comment parsing for descriptions
-
Transport Abstraction: Core R implementation uses HTTP; stdio wrapper enables compatibility with standard MCP clients
# Run all tests
R -e "devtools::test()"
# Run specific test
R -e "testthat::test_file('tests/testthat/test-pr_mcp.R')"
# Run with coverage
R -e "covr::package_coverage()"# Install dev version
R -e "devtools::install()"
# Generate documentation
R -e "devtools::document()"
# Check package
R -e "devtools::check()"# Start example server
R -e "source('inst/examples/run_mcp_server.R')"
# Test HTTP endpoints directly
curl -X POST http://localhost:8000/mcp/messages \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
# Test with MCP client (requires stdio wrapper)
cd inst/examples
mcp-cli --config-file server_config.json tools- Initialize: Client sends
initializewith protocol version, receives server capabilities - Tool Discovery: Client calls
tools/list, receives array of available tools with schemas - Tool Execution: Client calls
tools/callwith tool name and arguments, receives results
R Serialization: jsonlite creates single-element arrays for scalars. The stdio wrapper (stdio-wrapper.py) handles these conversions:
["2.0"]→"2.0"for protocol fields{}→nullfor empty id fields- Ensures
contentis always an array of content items
Transport Mismatch: R implementation uses HTTP, but most MCP clients expect stdio. The Python wrapper bridges this gap.
GitHub Actions workflows:
R-CMD-check.yaml- Multi-platform R package testingtest-coverage.yaml- Code coverage reporting via Codecov