R

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.

Quick Install
npx -y @posit-dev/mcptools

Overview

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:

install.packages("mcptools")

From GitHub (latest development):

# install remotes if necessary
if (!requireNamespace("remotes", quietly = TRUE)) {
  install.packages("remotes")
}
remotes::install_github("posit-dev/mcptools")

Basic configuration example (environment variables are recommended for secrets):

# set environment variables (example)
Sys.setenv(MCP_SERVER_URL = "https://mcp.example.com")
Sys.setenv(MCP_API_KEY = "your-api-key")

# or configure a client programmatically
library(mcptools)
client <- mcp_client(
  url = Sys.getenv("MCP_SERVER_URL"),
  api_key = Sys.getenv("MCP_API_KEY"),
  timeout = 60
)

Common dependencies (automatically installed): httr, jsonlite, httpuv/plumber (for local servers), and rlang.

Available Resources

Core client/server utilities (example API surface):

Function / ClassPurpose
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

  1. Call a remote model tool as a native R function
library(mcptools)
client <- mcp_client(url = "https://mcp.example.com", api_key = "TOKEN")

# turn a remote tool into an R function
summarize_text <- client$as_function("text_summarizer")

# call it like any other R function
result <- summarize_text(text = "Long article text here", max_tokens = 200)
print(result$summary)
  1. 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
  1. Expose local R functions as an MCP server
library(mcptools)

server <- mcp_server$new()

# register a simple function
server$register_tool("add_numbers", function(a, b) {
  list(result = a + b)
}, schema = list(
  inputs = list(a = "numeric", b = "numeric"),
  outputs = list(result = "numeric")
))

# start server (blocking)
server$start(port = 8080)

This enables other MCP-aware clients to discover and call add_numbers remotely.

  1. 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.