R Mcptools: SDK for MCP Server Integration
Build R-based MCP server integrations and access third-party MCP server functions as native R functions with the R Mcptools SDK.
npx -y @posit-dev/mcptoolsOverview
R Mcptools is an R SDK that simplifies building and integrating with MCP (Model Context Protocol) servers. It provides client helpers to call third-party MCP-hosted functions as native R functions, plus server-side utilities to expose R functions over the MCP protocol. The package is intended for developers who want to wire R code into distributed model ecosystems, wrap third-party model endpoints, or create lightweight MCP services for reuse.
Using Mcptools you can treat remote tools as first-class R functions, manage authentication and HTTP interaction, and scaffold a minimal MCP server in a few lines. This reduces boilerplate (serialization, request/response handling, retries) and makes it easier to test, share, and orchestrate model-backed tools from R scripts, packages, or shiny apps.
Features
- Client utilities to discover and call MCP tools as native R functions
- Server scaffolding to expose R functions via the MCP protocol
- Automatic (de)serialization of JSON payloads and typed arguments
- Authentication and connection configuration helpers
- Tool registration and metadata management for discoverability
- Vignettes and examples for common scenarios (wrapping LLMs, image models, utilities)
Installation / Configuration
Install from CRAN (if available) or the GitHub repository:
From CRAN:
From GitHub (latest development):
# install remotes if necessary
if (!) {
}
remotes::
Basic configuration example (environment variables are recommended for secrets):
# set environment variables (example)
# or configure a client programmatically
client <-
Common dependencies (automatically installed): httr, jsonlite, httpuv/plumber (for local servers), and rlang.
Available Resources
Core client/server utilities (example API surface):
| Function / Class | Purpose |
|---|---|
| mcp_client(…) | Create a client object configured for an MCP server |
| client$list_tools() | Retrieve available tools and metadata from server |
| client$get_tool(name) | Inspect a tool’s signature / schema |
| client$as_function(name) | Return an R wrapper function that calls the tool |
| client$call(name, params) | Programmatic tool invocation |
| mcp_server$new(…) | Create a server instance (for exposing tools) |
| server$register_tool(name, func, schema) | Register an R function as an MCP tool |
| server$start(port = 8000) | Run the MCP server locally |
| server$stop() | Stop the running server |
See the package vignettes and the examples/ directory in the GitHub repo for in-depth walkthroughs.
Use Cases
- Call a remote model tool as a native R function
client <-
# turn a remote tool into an R function
summarize_text <- client$
# call it like any other R function
result <-
- Wrap a third-party MCP tool inside a package
- Use client$list_tools() to discover tools and signatures
- Create thin wrapper functions that validate inputs and call client$call()
- Export wrappers from your package so users call them naturally
- Expose local R functions as an MCP server
server <- mcp_server$
# register a simple function
server$
# start server (blocking)
server$
This enables other MCP-aware clients to discover and call add_numbers remotely.
- CI / Integration testing
- Start a local MCP server in CI with server$start(port = 0) (OS-assigned port)
- Run integration tests that exercise client$as_function wrappers
- Stop server in teardown to keep CI clean
Best Practices
- Prefer environment variables or a secrets manager for API keys; avoid hard-coding credentials.
- Use client$as_function(…) to centralize serialization and error handling.
- Provide minimal JSON Schemas when registering tools to improve discoverability and validation.
- Run a local MCP server in development to iterate quickly before deploying.
Where to find help
- GitHub: https://github.com/posit-dev/mcptools — issues, examples, and source
- Package vignettes and examples/ directory for usage patterns
- Submit issue reports or feature requests on the repository
The Mcptools SDK is intended to remove routine plumbing for MCP integrations so developers can focus on model logic and application wiring. The APIs above provide straightforward primitives for both client-side consumption and server-side exposure of MCP tools.