Skip to contents

Implements a Model Context Protocol server with a private R runtime. Ordinary tools execute in the server process by default. When manage_r_sessions is registered, the server can optionally attach to human or secondary R sessions.

Details

Server operates through layered message handling:

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

  • Server Layer: Manages tool execution and active session state

  • Session Layer: Optionally forwards requests to attached 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,
  log_file = NULL,
  execution_timeout_secs = 300L,
  resource_registry = NULL
)

Arguments

registry

A ToolRegistry instance to use for tool discovery

.tools_dir

Internal parameter for specifying tools directory path

log_file

Optional log file path for the server logger

execution_timeout_secs

Default seconds before a forwarded request is considered timed out (default: 300). Override per-call via the timeout argument in tools like execute_r_code.

resource_registry

An MCPResourceRegistry instance. If NULL (default), a built-in registry with the MCPR plot viewer is created automatically.

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(version = NULL)

Arguments

version

Protocol version (if NULL, uses latest supported version)

Returns

List of server capabilities


Method mcp_apps_supported()

Check if the connected client supports MCP Apps

Usage

mcprServer$mcp_apps_supported()

Returns

Logical indicating MCP Apps support


Method session_manager()

Return the server-owned session manager.

Usage

mcprServer$session_manager()

Returns

A mcprSessionManager instance.


Method session_management_enabled()

Return whether this server exposes session-management capability.

Usage

mcprServer$session_management_enabled()

Returns

Logical scalar.


Method active_session_binding()

Return the active session binding owned by this server.

Usage

mcprServer$active_session_binding()

Returns

An environment describing the active binding.


Method active_session_label()

Return the active session label owned by this server.

Usage

mcprServer$active_session_label()

Returns

Character scalar.


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 tools discovered from a package or directory
registry <- ToolRegistry$new(tools_dir = "path/to/tools")
registry$search_tools()
server <- mcprServer$new(registry = registry)
server$start()

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