Serves the tools, resources and prompts of an installed mordew package.
It blocks the R process, so it is meant to be the main loop of a
dedicated process. Over stdio, it serves the MCP client that started the
R process: reads requests from stdin and writes responses to stdout until
the client closes stdin. Over HTTP, it serves the Streamable HTTP
transport of MCP on host and port. Over both, the front answers
initialize, ping and the lists itself and hands each tool call,
resource read, prompt get and completion to a pool of mirai workers, so
that it keeps reading requests while tools run.
Usage
run_mcp_server(
package,
transport = c("stdio", "http"),
host = "127.0.0.1",
port = 8000,
workers = if (identical(transport, "http")) 4 else 1,
timeout = 60,
queue = 8,
token = Sys.getenv("MORDEW_TOKEN"),
completions = 10,
tool_calls = 10,
tasks = max(1, workers - 1),
task_requests = 20
)Arguments
- package
Name of the installed package whose tools, resources and prompts to serve, or the path of an R script that ends with
mcp_server(): the front and each worker source it once, from its folder.- transport
"stdio"or"http".- host
Address to listen on over HTTP:
"127.0.0.1"serves this machine only, as the MCP specification recommends for a local server.- port
Port to listen on over HTTP.
- workers
Number of R processes that run the calls, each with the package loaded once: 1 over stdio and 4 over HTTP by default.
- timeout
Seconds a call may run before it is stopped and answered with an error. It counts from the start of the run, not the time spent in the queue.
- queue
Number of calls that may wait for a free worker. A call that comes when the queue is full gets a "busy, retry later" error at once.
- token
Secret that every HTTP request but
GET /healthmust send asAuthorization: Bearer <token>, or get a 401. Taken from theMORDEW_TOKENenvironment variable by default;NULLor""serves without a token. Serving on another host than127.0.0.1,localhostor::1without one prints a message.- completions
Completion requests answered per second to each session, as the MCP specification asks servers to limit them; the others get an error asking to retry later. The requests that come without a session share one count.
Infanswers them all.- tool_calls
Tool calls run per second for each session, counted as the completions are; the others get an error result asking to retry later.
Infruns them all.- tasks
Tasks that may run at once: a tool tagged
@mcpTaskruns as a task when the client asks, in a worker it holds until it ends, kept for the time to live the client asks, at most an hour. By default one less thanworkers, and at least 1, so that a worker stays free for the calls: with more, the tasks may hold every worker, and the calls then wait for one of them to end. A task that comes beyond gets an error asking to retry later.- task_requests
Task requests (
tasks/get,tasks/resultandtasks/cancel, together) answered per second to each session, counted as the completions are; the others get an error asking to retry later.Infanswers them all.