A server in a script
A server can also be an R script rather than a package: it ends with
mcp_server(), which gathers tools, resources and prompts
declared with mcp_tool(), mcp_resource() and
mcp_prompt(). Each takes a name, the function, a title, a
description, the descriptions of the arguments, and the same roxygen
tags a package uses, written as text, so the same checks apply. The
script reads these tags with roxygen2 each time it starts: roxygen2 must
be installed where the server runs, which mordew alone
does not do.
runs <- read.csv("runs.csv")
mordew::mcp_server(
"runlog",
"1.0.0",
"Answers questions about a running log.",
tools = list(
mordew::mcp_tool(
"longest_runs",
function(n) utils::head(runs[order(-runs$distance_km), ], n),
"Longest runs",
"Returns the n longest runs, longest first.",
params = c(n = "Number of runs, from 1 to 50."),
tags = c("@mcpType n type_integer(min = 1, max = 50)", "@mcpReturns table")
)
)
)run_mcp_server("server.R") serves it, over stdio or
HTTP, and call_tool(package = "server.R") calls its tools
in tests. The front and each worker source the script once, from its
folder, so a tool sees what the script defines before it, such as
runs above. mcp_server() also takes the
title, description and
website_url of the server, which a package takes from its
DESCRIPTION, and its icons, which a package takes from the
@mcpIcon tags of the roxygen block that documents
"_PACKAGE"; clients get them in serverInfo.
mcp_server(skills = "skills") serves the skills of the
folder skills/ next to the script, as a package serves
those of inst/mordew/skills/ (see Skills).
The stdio client
mcp_client(), mcp_request() and
mcp_close() talk to any MCP server over stdio, written in R
or not, as call_tool() and run_dev() do
inside. mcp_client() starts the server as a child process
and opens a 2025-11-25 session; mcp_request() sends a
request and returns its result, and an error of the server stops with
its message; mcp_close() closes the session and stops the
server:
client <- mordew::mcp_client(
"Rscript",
c(
"--no-init-file",
"-e",
"runlog::run_mcp_server()"
)
)
mordew::mcp_request(
client,
"tools/list"
)
mordew::mcp_close(
client
)timeout is the number of seconds to wait for each
answer, 30 by default. The client answers the questions of the server
with answer, sample and roots, as
call_tool() does, and declares only the capabilities it got
an answer for.
On Windows, writing a request larger than the pipe buffer to a server that does not read its stdin blocks until the server reads or exits: the timeout only applies to the answer.