A tool is a regular function of the package. Its roxygen block is
both the R documentation and the MCP definition. @mcpTool
is what makes it a tool: add_tool() also writes
@export, so that the function is part of the R interface of
the package, and the server serves it either way. A tag that roxygen
does not know, such as a misspelled @mcpTol, only gets the
warning of roxygen: read it, the function is then not served.
#' 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
) {
# validated computation on the runs dataset lives here
}Tag specification. Tag names are camelCase, as roxygen2’s own
(@inheritParams): roxygen2 cuts a tag name at
_, . or -.
| Tag | Role |
|---|---|
@mcpTool [name] |
Exposes the function as a tool. The name defaults to the function’s;
it must be 1 to 128 characters among
A-Z a-z 0-9 _ - .. |
@mcpType <param> <expression> |
The parameter’s type, as an R expression (see below). Required for every parameter; naming a parameter the function does not have is an error. |
@mcpReturns <kind> |
Required: text, table, image,
json or content; drives serialisation. |
@mcpOutput <expression> |
A type_object() sent as the outputSchema;
needs @mcpReturns json. The result, checked against it, is
sent as structuredContent and as JSON text. |
@mcpIcon <src> [options] |
Icons for clients: an http, https or data URI, then
mimeType=, sizes= (48x48,96x96 or
any) and theme= (light or
dark). One tag per icon. |
@mcpTask <optional\|required> |
optional or required: the tool runs as a
task when the client asks (2025-11-25) or supports tasks (2026-07-28),
or only as one, in a background R process (see Tasks). |
@mcpReadOnly |
readOnlyHint: true: the tool does not modify its
environment. |
@mcpNonDestructive |
destructiveHint: false. Redundant next to
@mcpReadOnly, which already implies it. |
@mcpIdempotent |
idempotentHint: true: a second call with the same
arguments has no further effect. Redundant next to
@mcpReadOnly, which already implies it. |
@mcpClosedWorld |
openWorldHint: false: the tool does not reach external
entities. |
@mcpLimit rows <n> |
Maximum number of rows a table returns, and an error on
any other kind; the default is 50.
run_mcp_server(timeout = ) caps the duration of every call
that does not run as a task. |
@mcpConfirm <message> |
Before the tool runs, the server asks the user
<message>, where {param} stands for an
argument of the model; the tool runs only if the user accepts (see Asking the user). |
@mcpElicit <param> |
The user, not the model, gives the parameter: it leaves the schema
the model sees and goes in the form of @mcpConfirm, which
it needs. One tag per parameter. |
@mcpApp <ui://uri> |
The page of an MCP App that the client shows with the result of the tool, a resource of the server (see MCP Apps). |
@mcpWidget <function> |
A function that draws the value of the tool as an htmlwidget, sent
to the page of @mcpApp (see MCP
Apps). |
@mcpVisibility <model\|app> |
Who may call the tool: the model, the page of an MCP App, or both, the default (see MCP Apps). |
@mcpSample <param> <message> |
Before the tool runs, the server asks the model of the client for
the text of <param>, a string that leaves the schema
the model sees (see Asking the model
of the client). |
@mcpSchema <expression> |
JSON Schema 2020-12 keywords added to the root of the input schema,
such as $defs, allOf or if (see
Schema keywords). |
@mcpHeader <param> <Name> |
Mirrors the parameter into an Mcp-Param-<Name>
HTTP header for 2026-07-28 clients (see The HTTP transport). |
@mcpResource <uri> |
Exposes the function as a resource, or as a resource template when
the URI has a {param} (see Resources, prompts and
skills). |
@mcpPrompt |
Exposes the function as a prompt, which the user picks in the client (see Resources, prompts and skills). |
@mcpComplete <param> <function> |
Links an argument to a function of the package that suggests its
values, through completion/complete (see Resources, prompts and
skills). |
@mcpCsp <kind> <domain>... |
On the page of an MCP App: the domains it may reach, by kind,
connect, resource, frame or
baseUri (see MCP Apps). |
@mcpPermission <permission>... |
On the page of an MCP App: camera,
microphone, geolocation and
clipboardWrite, which the client may grant (see MCP Apps). |
@mcpDomain <domain> |
On the page of an MCP App: a dedicated origin for the page (see MCP Apps). |
@mcpBorder <true\|false> |
On the page of an MCP App: whether the client draws a border around it (see MCP Apps). |
Annotations err on the side of caution. A tool without annotation tags sends no annotations, and clients apply the MCP defaults: it may modify and destroy, is not safe to call twice, and reaches the outside world. The tags only declare what is safe, so a forgotten tag makes a client ask for confirmation more often, never less. A tool that changes a run without destroying anything, safe to call twice, and whose table has 20 rows at most:
#' Rename a run
#'
#' Gives a run of the log another name, and returns the runs of its day.
#'
#' @param date Day of the run.
#' @param name New name of the run.
#'
#' @mcpTool
#' @mcpType date type_string(format = "date")
#' @mcpType name type_string(max_length = 80)
#' @mcpReturns table
#' @mcpLimit rows 20
#' @mcpNonDestructive
#' @mcpIdempotent
#' @export
rename_run <- function(date, name) {
data.frame(
date = date,
name = name
)
}A tag whose content is structured takes an R expression, never a
grammar of its own: the roclet parses and evaluates it in a prepared
environment where mordew’s type constructors are
attached. They exist only there: mordew exports them too,
for the forms of input_form(), and the registry holds plain
R lists, so a deployed server does not depend on ellmer. An expression
stays recursive where JSON Schema is (arrays of objects, where a flat
grammar breaks down).
The constructors follow ellmer’s API and add the constraints ellmer
types lack (checked with ellmer 0.3.2: description and
required only); they can be proposed to ellmer.
| Constructor | JSON Schema 2020-12 |
|---|---|
type_string(pattern = NULL, format = NULL, min_length = NULL, max_length = NULL) |
string, with pattern, format
(email, uri, date,
date-time), minLength and
maxLength
|
type_integer(min = NULL, max = NULL) |
integer, with minimum and
maximum
|
type_number(min = NULL, max = NULL) |
number, with minimum and
maximum
|
type_boolean() |
boolean |
type_enum(values, titles = NULL) |
string, with enum, or oneOf
with a title for each value |
type_array(items, min_items = NULL, max_items = NULL) |
array of items, with minItems
and maxItems; titled choices as anyOf
|
type_object(...) |
object with the named fields |
Every constructor also takes required = TRUE and
description = NULL (.required and
.description for type_object(), as in
ellmer).
The server checks a pattern with the Perl-compatible
expressions of R, where $ also matches before a line break
that ends the text: ^[a-z]+$ accepts "abc\n".
Write ^[a-z]+$(?!\n) to refuse it: a client, which reads
the pattern as JSON Schema does, reads that one the same way.
required = FALSE matches a parameter that has a default
value in the function’s signature, and only that: the roclet fails on
any mismatch. A parameter’s description comes from its
@param, which the roclet requires; description
only describes nested types (array items, object fields). The tool’s
inputSchema is a type_object() of the
parameters, in the order of the signature: one property per parameter,
the required ones listed in required. Every
type_object() sets
additionalProperties: false, so fields it does not describe
are rejected, at every level.
The server validates the arguments against this schema, then converts
them before calling the function, so a type always reaches the tool as
the same R type: an integer becomes an R integer, a
number a double, an array of scalars a vector of their type
(an empty array included, as character(0) for strings), an
object a named list, and an array of objects or arrays a list. An
integer beyond R’s integer range, -2147483647 to 2147483647, is rejected
with an error that gives this range, rather than turned into
NA. The published schema does not carry the range: give
min and max to type_integer() to
publish bounds.
Single-word tags (@mcpReturns table,
@mcpReadOnly) stay words: one word from a closed set is not
a language.
The roxygen title becomes the tool’s title, shown to
users; the description becomes its description, read by the
model; and each @param becomes a parameter description.
Roxygen sends the paragraphs after the description to
@details: they stay in the R help, and the model never
reads them. These texts are prompts: they should say
when to use the tool, when not to, and give units and formats. The
roclet stops on a tool without title or description, and on
@mcpTool on an object that is not a function.
Return conventions
| R value | Sent to the model as |
|---|---|
character |
text content, one element per line |
data.frame |
markdown table with dimensions, truncated to
@mcpLimit rows (default 50) |
ggplot or recorded plot |
PNG image content, 800×600 at 96 dpi |
list |
JSON |
list of content_*()
|
the blocks as they are, with @mcpReturns content
|
| condition (error) | an isError result with a message the model can act
on |
A value that does not match the tool’s @mcpReturns comes
back as an isError result naming the declared kind and the
received one.
With @mcpReturns content, a tool returns a list of
blocks made by content_text(), content_image()
(a plot or an image file), content_audio() (an audio file,
base64), content_resource_link() (a link to a resource the
client can read) and content_resource() (a resource
embedded as text or as a blob), each with optional
content_annotations(): its audience ("user",
"assistant" or both), its priority from 0 to 1 and its last
change. A resource cannot return content.
Structured output
With @mcpOutput type_object(runs = type_integer()), a
json tool declares the structure of its result:
tools/list sends it as the tool’s
outputSchema, and each result goes out as
structuredContent and, for the clients that do not read it,
as the same JSON in a text block. A result that does not match the
schema comes back as an isError result naming the field,
and so does a result that is not an object, such as an empty list or a
list without names: the server never sends structured content that
breaks its schema. A vector of one value is written as a JSON scalar:
for a field that is an array, return I(x) or
as.list(x), so that one value stays an array.
A tool with a structured result, and an icon:
#' Totals of a year
#'
#' Returns the number of runs and the distance of one year.
#'
#' @param year Year of the runs.
#'
#' @mcpTool
#' @mcpType year type_integer(min = 2015)
#' @mcpReturns json
#' @mcpOutput type_object(runs = type_integer(), distance_km = type_number())
#' @mcpIcon https://runlog.example/icons/totals.png mimeType=image/png sizes=48x48
#' @export
year_totals <- function(year) {
list(
runs = 42L,
distance_km = 512.3
)
}Schema keywords
@mcpSchema list(...) adds JSON Schema 2020-12 keywords
to the root of the input schema of a tool, such as $schema,
$defs, allOf, anyOf, or
if, then and else. The client
gets them as they are; the server checks the arguments against what the
@mcpType tags describe, not against these keywords. They
cannot set type, properties,
required or additionalProperties, which the
@mcpType tags write.
A tool whose month needs its year, and
whose year a 2026-07-28 client also sends in the
Mcp-Param-Year header (see The HTTP transport):
#' Runs of a period
#'
#' Returns the runs of a year, or of a month of a year.
#'
#' @param year Year of the runs.
#' @param month Month of the runs, from 1 to 12.
#'
#' @mcpTool
#' @mcpType year type_integer(min = 2015)
#' @mcpType month type_integer(min = 1, max = 12, required = FALSE)
#' @mcpReturns table
#' @mcpSchema list(dependentRequired = list(month = list("year")))
#' @mcpHeader year Year
#' @export
period_runs <- function(year, month = NULL) {
data.frame(
year = year,
month = month
)
}