Skip to contents

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 /health must send as Authorization: Bearer <token>, or get a 401. Taken from the MORDEW_TOKEN environment variable by default; NULL or "" serves without a token. Serving on another host than 127.0.0.1, localhost or ::1 without 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. Inf answers 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. Inf runs them all.

tasks

Tasks that may run at once: a tool tagged @mcpTask runs 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 than workers, 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/result and tasks/cancel, together) answered per second to each session, counted as the completions are; the others get an error asking to retry later. Inf answers them all.

Value

Nothing: it serves until its input closes over stdio, and until the process stops over HTTP.

Examples

if (FALSE) { # \dontrun{
# Over stdio, as an MCP client starts the server of the package runlog:
run_mcp_server(
  "runlog"
)
# Over HTTP, on this machine:
run_mcp_server(
  "runlog",
  transport = "http"
)
} # }