Testing is at the core of mordew. There are three levels, plus security tests.
| Level | Question answered | Tooling | Where it runs |
|---|---|---|---|
| 1. Unit | Does the function compute the right result? | testthat |
R CMD check, CI |
| 2. Contract | Is the tool exposed correctly over MCP? | mordew helpers + testthat snapshots |
R CMD check, CI |
| 3. Behaviour | Does the model pick the right tool with the right arguments? | evals through the claude CLI |
separate CI job, before releases |
| Security | Does the tool resist hostile arguments? | mordew helpers |
R CMD check, CI |
Level 2, the contract, is the one nobody covers today. It checks that:
- every parameter is documented and typed, and every typed parameter exists
- the generated JSON schema matches a snapshot, so that any change to a tool’s interface shows up in review as a breaking change
- return values serialise to valid MCP content
- errors come back as
isErrorresults, not as crashes - a full round-trip works through a real MCP client over stdio
test_that("summarise_runs is exposed with a stable schema", {
expect_tool_schema(
tool = "summarise_runs"
)
})
test_that("summarise_runs answers through a real MCP client", {
skip_on_cran()
res <- call_tool(
tool = "summarise_runs",
args = list(
year = 2025
),
transport = "stdio"
)
expect_false(
res$isError
)
})add_tool() writes the schema snapshot test with the
tool. call_tool() starts the package’s server in a separate
R process and talks to it through mordew’s stdio client,
one server per call so that no state leaks between tests: under
devtools::test() the server loads the development version
with pkgload::load_all(), under R CMD check it
runs the installed package. A protocol error (an unknown tool) is an R
error; a tool error is an isError result, which
expect_tool_error() checks, with an optional pattern on its
message. read_resource() reads a resource the same way,
through resources/read, and returns the result, a list with
contents; an unknown URI, or a resource that fails, is an R
error. expect_resource() reads it and expects its text to
match a pattern; add_resource() writes that test with the
resource. get_prompt() and expect_prompt() do
the same for a prompt, through prompts/get, and
add_prompt() writes that test with the prompt.
expect_structured_output() calls a tool and checks its
structuredContent against its @mcpOutput, as
the specification asks clients to.
With client = "inspector", call_tool()
makes the same call through the MCP Inspector, the reference client of
the MCP project, run with npx in its CLI mode, pinned to
the version checked. The server and mordew’s client share
one reading of the protocol, so a mistake in it would pass unnoticed
between them: a client written by others catches it. Both clients give
the same result shape, so a test can compare them.
client = "inspector" is not supported on Windows yet, hence
the skip_on_os():
test_that("summarise_runs answers the same through the MCP Inspector", {
skip_on_cran()
skip_on_os(
"windows"
)
args <- list(
year = 2025
)
expect_identical(
call_tool(
"summarise_runs",
args,
client = "inspector"
),
call_tool(
"summarise_runs",
args
)
)
})Level 3, behaviour, uses evals: each case is a user
prompt with the tool calls to accept, scored over several runs. Evals
need the claude CLI logged in or an API key, cost one
request per run and are not deterministic, so they live in
dev/, out of R CMD check, and run in a
separate CI job started by hand, before releases and whenever a tool
description, the system prompt or the model changes.
# dev/evals.R, written by use_evals()
cases <- list(
mordew::eval_case(
"Show me my long runs in 2024.",
mordew::tool_call(
"get_runs",
year = 2024,
type = "long"
)
),
mordew::eval_case(
"What will the weather be in Brussels tomorrow?"
)
)
results <- mordew::run_evals(
cases,
runs = 5
)
mordew::assert_pass_rate(
results
)run_evals() asks each question through a Claude Code
session restricted to the package’s server, in its development version,
stops when the session did not load that server, and checks every call
the model makes: each argument on its own terms (partial()
accepts part of a name), any of the answers of the case, all the calls
of an answer, and no call for a case without an answer. A call matches a
tool_call() when it has its tool and exactly its arguments:
an argument the model adds, even an optional one, makes it another call.
Neither the order of the calls nor the calls made besides those of the
answer are compared. It flags a run that takes more calls than the
longest answer. assert_pass_rate() stops when a case passes
in less than 80% of its runs: a rate per case, so that a case that
always fails cannot hide among the others. When
ANTHROPIC_API_KEY is set, as in the CI workflow, the
session runs with --bare, which leaves out the developer’s
CLAUDE.md, memory, hooks and plugins: that run is the
reference. Without it, --bare would ignore a subscription
login, so a local run on a subscription still reads that configuration.
The claude CLI inherits R’s environment: a key in
.Renviron makes local runs use it, billed per token, even
when the shell has none.
Security tests feed hostile arguments to each tool.
Wrong types and values outside the schema never reach it: validation
rejects them first, for every tool. What the schema accepts does reach
it, so expect_hostile_safe(), which add_tool()
writes in the tests of each tool, calls it with valid arguments, then
replaces one at a time by each extreme value its schema accepts: an
empty or a 100,000-character string, the bounds of a number, or the
largest numbers a message carries when it has none. Every call must get
an answer, success or isError: a server that hangs, or a
tool that stops the worker running it, fails the test. A success can
still leak data, which no generic test can judge: paths outside the
allowed scope are kept out by a pattern on the argument and
by OS-level isolation in production. check_tools(), which
the package template runs in a test, reads the code of every function of
the package and flags the calls through which an argument could become
code (eval(), parse(), source(),
system() and similar). It only sees direct calls to these
functions in the body of a function: a function passed as a value
(lapply(x, eval)), a call by name
(do.call("system", ...)), a call in a default argument, or
another way to run code (pipe(), glue::glue(),
rlang::eval_tidy(), processx::run()) goes
unnoticed, so a clean result is not a proof.
The helpers: call_tool(),
expect_tool_error(), read_resource(),
expect_resource(), get_prompt(),
expect_prompt(), expect_structured_output(),
expect_tool_schema(), use_evals(),
eval_case(), tool_call(),
partial(), run_evals(),
assert_pass_rate(), check_tools(),
expect_hostile_safe(),
expect_conformance().
expect_conformance() serves the package over HTTP and
runs scenarios of the official conformance suite against it, through
npx: by default the twelve that need no tool of the suite,
such as server-initialize, tools-list or
dns-rebinding-protection. It stops when the suite ran no
check, as when npx fails without network.
mcp_client(), mcp_request() and
mcp_close() talk to any MCP server over stdio, as
call_tool() does inside.
The official conformance suite
Rscript dev/conformance.R builds a test package from
dev/conformance/, with the tools, resources and prompts
that the official MCP conformance suite calls, serves it over HTTP, and
runs version 0.2.0-alpha.12 of
npx @modelcontextprotocol/conformance server against it; it
needs Node, and its arguments go to the suite, such as
--scenario tools-list. The suite runs its scenarios at
once, so the server answers up to 100 tool calls and 1000 task requests
per second there, and runs 32 tasks at once. Its 72 checks of 2025-11-25
pass, and with --requirements 2026-07-28, its 194 checks of
2026-07-28, those of the tasks extension and of the pending scenarios
included.