Skip to contents

Implements Model Context Protocol server for persistent R session management. Operates through nanonext sockets for non-blocking message handling between JSON-RPC clients and R sessions, enabling tool execution routing and workspace state persistence.

Details

Server operates through layered message handling:

  • Client Layer: Handles JSON-RPC communication with MCP clients

  • Server Layer: Manages tool execution and session routing

  • Session Layer: Forwards requests to active R sessions

Note

This method should only be called in non-interactive contexts because it blocks execution

Super class

MCPR::BaseMCPR -> mcprServer

Methods

Inherited methods


Method new()

Initialize the MCP server with optional tools

Usage

mcprServer$new(registry = NULL, .tools_dir = NULL)

Arguments

registry

A ToolRegistry instance to use for tool discovery

.tools_dir

Internal parameter for specifying tools directory path

Returns

A new mcprServer instance


Method start()

Start the MCP server and begin listening for connections

Usage

mcprServer$start()

Returns

No return value (blocking call) Stop the running server with graceful shutdown and resource cleanup


Method stop()

Usage

mcprServer$stop(timeout_ms = 5000)

Arguments

timeout_ms

Timeout in milliseconds for graceful shutdown (default: 5000)

Returns

The server instance (invisibly) for method chaining


Method is_running()

Check if the server is currently running

Usage

mcprServer$is_running()

Returns

TRUE if server is running, FALSE otherwise


Method get_tools()

Get server tools in the specified format

Usage

mcprServer$get_tools(format = c("list", "json"))

Arguments

format

Character string specifying output format: "list" (default) or "json"

Returns

For "list": named list of ToolDef objects. For "json": list suitable for JSON serialization


Method get_capabilities()

Get server capabilities for MCP protocol

Usage

mcprServer$get_capabilities()

Returns

List of server capabilities


Method clone()

The objects of this class are cloneable with this method.

Usage

mcprServer$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

if (FALSE) { # \dontrun{
# Basic server initialization
server <- mcprServer$new()
server$start() # Blocking call

# Server with custom tools
my_tool <- tool(
  function(x) mean(x),
  name = "mean",
  description = "Calculate arithmetic mean",
  arguments = list(x = "number")
)
registry <- ToolRegistry$new()
registry$add_tool(my_tool)
server <- mcprServer$new(registry = registry)
server$start()

# Using convenience function
registry <- ToolRegistry$new(tools_dir = "path/to/tools")
mcpr_server(registry = registry)
} # }