Runtime and process model
-
The server owns its process.
run_mcp_server()blocks the R process that calls it: it is meant to be the main loop of a dedicated process, never something run inside another app. - Transports. With stdio, each desktop client spawns its own R process: one user, no auth. With HTTP, one shared server serves many users, so it needs authentication and a concurrency strategy.
-
Concurrency. R is single-threaded: a server that
runs tools in its own process runs one call at a time.
Asynchronous execution is a hard requirement for
production: a server that blocks on every call cannot serve
several users. Over both transports, the front only reads messages and
answers
initialize,pingand the lists; tool calls, resource reads, prompt gets and completions run in a pool of mirai workers, one R process each, 1 over stdio and 4 over HTTP by default. Over stdio, a thread of nanonext reads stdin in the background, so the front keeps reading while a tool runs. See Scaling an HTTP deployment. - State. Tools must be stateless, so that any process can answer any call.
Targets
| Target | Transport | How |
|---|---|---|
| Local, desktop client | stdio |
Rscript --no-init-file -e "pkg::run_mcp_server()",
snippet from mcp_client_config()
|
| Docker | HTTP |
add_dockerfile(), built on dockerfiler as in
golem |
| Posit Connect | HTTP |
add_connect() writes _server.yml with
engine: mordew;
rsconnect::deployAPI(".", contentCategory = "mcp")
|
Posit Connect
Connect serves mordew packages through plumber2’s
_server.yml standard, as it serves mcptools: the file’s
first line names the engine package, and Connect calls that package’s
launch_server(settings, host, port), which serves until the
process stops. add_connect() writes
_server.yml with engine: mordew at the package
root and keeps it out of the build;
mordew:::launch_server() installs the deployed package into
a temporary library, then serves it over HTTP on the host and port
Connect gives, with its worker pool:
mordew::add_connect()
rsconnect::deployAPI(
".",
contentCategory = "mcp"
)claude mcp add --transport http runlog https://connect.example.com/content/<guid>/mcp \
--header "Authorization: Key <Connect API key>"Connect authenticates the clients itself, with its API keys, and
strips the Authorization header before the request reaches
the server: the server runs without a token there, so a content that
Connect opens to anyone, without a login, is served to anyone. Checked
on Connect 2026.06.1 with a mordew package:
initialize, tools/list and
tools/call over /mcp. rsconnect 1.6.0 creates
the content, then fails to upload the bundle with a 404; 1.12.0 deploys
it. Until mordew is on CRAN, Connect installs it from
GitHub, which takes the Remotes: ThinkR-open/mordew that
create_mordew() writes in the package’s
DESCRIPTION, and mordew installed from
GitHub (pak::pak("ThinkR-open/mordew")) where you deploy:
rsconnect records each dependency as it is installed there.
When a single process is enough
- stdio: never needs scaling. Each client spawns its own R process.
- HTTP for one user, or with fast tools and little traffic: one process is enough, as long as calls stay well under the timeouts.
Everything else, meaning several users at the same time or tools that take seconds, needs asynchronous execution.
Scaling an HTTP deployment
Because a process that runs tools itself runs one call at a time, there are two ways to serve concurrent calls.
A. Replicas behind a load balancer. N identical R
processes or containers, behind nginx, Traefik or a Kubernetes Service.
It works as long as the HTTP transport stays stateless. The cost is
memory: each replica loads its own copy of the data. The replicas share
the key that signs the requestState of ask()
in MORDEW_STATE_KEY, so that a retry may reach any of
them.
flowchart LR C["MCP clients"] --> LB["Load balancer
TLS, auth, least connections"] LB --> R1["R process 1
run_mcp_server()"] LB --> R2["R process 2
run_mcp_server()"] LB --> R3["R process N
run_mcp_server()"]
B. One asynchronous front, a pool of workers. A single HTTP process whose handler returns a promise instead of blocking, while tools run in a pool of mirai daemons. The front keeps accepting requests while tools compute; data can be loaded once per worker; there is a single container to deploy.
flowchart LR C["MCP clients"] --> P["Reverse proxy
TLS, auth"] P --> F["Async HTTP front
nanonext handler answers a promise"] F --> W1["mirai worker 1"] F --> W2["mirai worker 2"] F --> W3["mirai worker N"]
A + B. Several replicas, each with its own worker pool, for large deployments.
run_mcp_server(transport = "http") implements B with the
HTTP server of nanonext and mirai, and stdio runs on the same front. The
front answers initialize, ping and the lists
itself, so that they never wait behind a tool, and hands each
tools/call, resources/read,
prompts/get and completion/complete to a
worker, answering with a promise. The workers load the package before
the server serves. At most one call runs per worker; the others wait in
a bounded queue held by the front, which hands a call to mirai only when
a worker is free, so that mirai’s timeout counts the run time and not
the wait. A call that finds the queue full gets a “busy, retry later”
error at once; a call that times out, or whose worker stops, gets an
error too: an isError result for a tool call, which the
model reads, and a JSON-RPC error -32603 for the others.
A worker that stops, killed or crashed during a call, is replaced at
once: the replacement takes the front’s library paths through
R_LIBS and loads the package on its first call.
Not handled yet: a worker that ignores the interrupt at its timeout (compiled code), and per-tool limits.
Load balancer checklist
- Algorithm: least connections rather than round-robin, because call durations vary widely and each process (or worker) runs one call at a time.
-
Health check: on
GET /health, which mordew’s front answers. -
Timeouts: align the proxy timeouts with the longest
tool. nginx’s
proxy_read_timeoutdefaults to 60 seconds; a longer tool ends in a 504. -
Origin: the proxy must forward the original
Originheader, so that the server can check it. - Authentication and TLS: at the proxy (OAuth or OIDC).
-
Sticky sessions on the
MCP-Session-Idheader: a replica keeps the sessions it opened, and answers 404 to the others, which makes the client open a new one. Tools must still keep no state in memory.
Containers
add_dockerfile() builds on dockerfiler’s
dock_from_desc(), the same way golem does: it installs the
package’s dependencies, then the package, and adds what a server
needs:
- a non-root user,
mordew - the port, 8080 by default
- a health check that reads
GET /healthwith base R, so the image needs no curl - a start on
0.0.0.0throughmordew::run_mcp_server(), whatever the package’s ownrun_mcp_server()takes, with the token ofMORDEW_TOKEN; without one, for instance behind a proxy that authenticates, it warns at start-up
What it adds for runlog, after the lines from
dockerfiler:
RUN useradd --create-home mordew
USER mordew
EXPOSE 8080
HEALTHCHECK CMD Rscript -e 'readLines("http://127.0.0.1:8080/health", warn = FALSE)'
CMD ["Rscript", "-e", "mordew::run_mcp_server('runlog', transport = 'http', host = '0.0.0.0', port = 8080)"]Two things to know:
-
add_dockerfile()needs dockerfiler 1.0.1 or later, installed withpak::pak("ThinkR-open/dockerfiler")while CRAN has 1.0.0. It computes the system requirements with pak, mordew fromRemotesincluded, and runs every install withoptions(warn = 2), so a failed install fails the build. -
The package’s
Suggestsare installed too, though a server does not need them:pkgloadpullsfs, which needslibuv1-dev.
Container checklist
-
Bind to
0.0.0.0. A server listening on127.0.0.1is unreachable from outside the container. - Non-root user, read-only root file system, only temporary directories writable.
- Health check on a dedicated endpoint (see the load balancer checklist).
-
Secrets and model API keys come from environment
variables or a secret store, never baked into the image:
add_dockerfile()puts.Renvironand.envin the.dockerignore. -
Configuration through environment variables:
MORDEW_TOKEN. -
stdout is reserved for the protocol in stdio mode.
The server diverts what a tool prints to stderr, but anything written to
stdout before it starts (a
.Rprofile, a noisy.onLoad()) corrupts the JSON-RPC stream; logs go to stderr or to a file. -
Pinned versions: R version,
renv.lock, base image digest. - Resource limits (CPU, memory) per container, sized to the worker pool.
- Restricted network egress, as described in the security model.