Skip to contents

mordew does for MCP servers what golem does for Shiny apps: an MCP server is an R package. Each tool is an R function of the package, documented with roxygen and tested with testthat. mordew scaffolds the package, turns the roxygen documentation into an MCP tool registry, serves that registry to any MCP client (Claude Code, Claude Desktop, Codex, Copilot…), and provides the testing, security and deployment conventions needed to run all this in production. mordew does not ship a chat: a chat is a separate product, which talks to a mordew server through MCP like any other client.

The examples use runlog, a toy package that answers questions about a (fake) running log: dates, types of run, distances and durations. It is built by the steps below, and a copy of it is in the repository of mordew, under inst/runlog/.

Install

mordew is not on CRAN: install it from GitHub.

pak::pak(
  "ThinkR-open/mordew"
)

Create the package

create_mordew() creates the package and its dev/ scripts:

mordew::create_mordew(
  path = "runlog"
)

Its DESCRIPTION holds placeholders to fill before the first devtools::check(): the title, the author, the description and the license. It adds mordew::registry_roclet to the roclets of roxygen, its extensions, so that devtools::document() writes the registry of the tools, resources and prompts in R/mordew_registry.R. The dev/ scripts list the steps of the project. dev/01_start.R, run once:

# Run once, at the start of the project.

mordew::install_dev_deps()

It installs the packages mordew suggests, with devtools and rsconnect, which the next scripts call. The rest of this article runs in the folder of the package: open runlog/ as a project, or move there with setwd("runlog").

dev/02_dev.R, during development:

# Run during development.

mordew::add_tool(
  name = "name_of_tool"
)

mordew::add_resource(
  name = "name_of_resource"
)

mordew::add_prompt(
  name = "name_of_prompt"
)

mordew::add_skill(
  name = "name-of-skill"
)

mordew::add_mcp_client_config()

mordew::use_evals()

dev/03_deploy.R, before deploying:

# Run before deploying.

# Check the package
devtools::check()

# Docker: a Dockerfile that serves the package over HTTP
mordew::add_dockerfile()

# Posit Connect: _server.yml makes {mordew} the engine that serves the package
mordew::add_connect()
rsconnect::deployAPI(
  ".",
  contentCategory = "mcp"
)

inst/mordew/instructions.md holds the instructions that the server sends to clients, which add them to what the model reads. The ones of runlog:

This server answers questions about one runner's training log for 2024
and 2025: each run has a date, a type (easy, long, interval or race), a
distance in kilometres and a duration in minutes. Use its tools for any
question about these runs, and for nothing else.

Anatomy of a package

What create_mordew(), add_tool(), add_resource(), add_prompt(), add_skill(), use_evals(), add_dockerfile() and add_connect() generate:

runlog/
├── DESCRIPTION
├── NAMESPACE
├── R/
│   ├── tool_summarise_runs.R    # one file per tool
│   ├── resource_surfaces.R      # one file per resource
│   ├── prompt_review_run.R      # one file per prompt
│   ├── run.R                    # run_mcp_server()
│   └── mordew_registry.R        # generated by the roclet, do not edit
├── inst/
│   └── mordew/
│       ├── instructions.md      # server instructions sent to clients
│       ├── prompts/
│       │   └── review_run.md    # text of a prompt
│       └── skills/
│           └── year-review/
│               └── SKILL.md     # a skill: how to use the tools
├── tests/
│   ├── testthat.R
│   └── testthat/
│       ├── test-tool_summarise_runs.R  # unit, schema snapshot, extreme arguments
│       ├── test-resource_surfaces.R    # unit, read through a client
│       ├── test-prompt_review_run.R    # unit, got through a client
│       └── test-check_tools.R          # no call that can run code
├── dev/
│   ├── 01_start.R
│   ├── 02_dev.R
│   ├── 03_deploy.R
│   └── evals.R                  # behavioural evals, not run by R CMD check
├── .github/workflows/evals.yaml # runs dev/evals.R, started by hand
├── Dockerfile
└── _server.yml                  # Posit Connect

Add a tool

add_tool("summarise_runs") writes R/tool_summarise_runs.R, a function to fill:

#' Title of the tool
#'
#' What the tool does, when to use it and when not to, with units and
#' formats.
#'
#' @mcpTool
#' @mcpReturns text
#' @export
summarise_runs <- function() {
  # TODO
}

Once written, its roxygen block describes the tool to the model, and its tags tell mordew the types of its parameters and what it returns (see Writing tools):

#' Year summary
#'
#' Returns the totals of one year for each type of run: number of runs,
#' distance (km) and duration (hours). For month by month totals, use
#' summarise_runs_by_month.
#'
#' @param year Four-digit year.
#'
#' @mcpTool
#' @mcpType year type_integer(min = 2024, max = 2025)
#' @mcpReturns table
#' @mcpReadOnly
#' @mcpClosedWorld
#' @export
summarise_runs <- function(
  year
) {
  runs <- get_runs(
    year
  )
  totals(
    runs,
    list(
      type = runs$type
    )
  )
}

get_runs() is another tool of runlog, which gives the runs of a year, and totals() one of its helpers, which adds them up: both are in the copy of the repository.

add_tool() also writes its test file: a test of the function to write, a snapshot of the schema of the tool, and extreme arguments (see Testing):

test_that("summarise_runs works", {
  fail(
    "Write a test for summarise_runs()."
  )
})

test_that("summarise_runs is exposed with a stable schema", {
  mordew::expect_tool_schema(
    "summarise_runs"
  )
})

test_that("summarise_runs answers extreme arguments", {
  skip_on_cran()
  mordew::expect_hostile_safe(
    "summarise_runs",
    list()
  )
})

devtools::document() then writes the tool in the registry. The package also holds a test that no function can turn an argument into code:

test_that("no function can turn an argument into code", {
  expect_identical(
    mordew::check_tools(),
    character()
  )
})

Try it

call_tool() starts the server of the package in a separate R process, calls the tool through MCP and returns its result:

devtools::load_all()
mordew::call_tool(
  "summarise_runs",
  list(
    year = 2025
  ),
  package = "runlog"
)

run_dev() connects a model to the tools of the package under development, through ellmer or Claude Code (see Development loop):

mordew::run_dev(
  chat = ellmer::chat_anthropic()
)

Serve it

run_mcp_server(), which create_mordew() writes in R/run.R, serves the package: over stdio by default, to the client that started the R process, or over HTTP with transport = "http". Once the package is installed, Claude Code starts it with:

claude mcp add runlog -- Rscript --no-init-file -e "runlog::run_mcp_server()"

Connecting clients gives the configuration of Claude Desktop and of the other clients, and the HTTP transport how several users share one server.

Where to go next