Skip to contents

R6 class for registering custom MCP resources that the server exposes via the resources/list and resources/read MCP protocol methods. Mirrors the ergonomics of ToolRegistry but registration is imperative rather than file-scanned.

Details

Each registered resource carries a zero-argument resource_reader callback that the server invokes when the client issues a resources/read request. Readers may return either a simple list(text = ..., mimeType = ...) / list(blob = ..., mimeType = ...) shape (which the registry wraps into a full MCP contents envelope) or a complete list(contents = list(...)) shape.

Methods


Method new()

Initialize an empty resource registry.

Usage


Method register()

Register a resource entry.

Usage

MCPResourceRegistry$register(
  uri,
  name,
  resource_reader,
  description = NULL,
  mimeType = NULL,
  title = NULL,
  annotations = NULL,
  size = NULL,
  meta = NULL,
  mcp_app_only = FALSE,
  overwrite = FALSE
)

Arguments

uri

Non-empty character scalar identifying the resource.

name

Non-empty character scalar with a human-readable name.

resource_reader

Zero-argument function returning the resource contents.

description

Optional description string.

mimeType

Optional default MIME type for the resource.

title

Optional title string.

annotations

Optional annotations list passed through to the descriptor.

size

Optional size hint.

meta

Optional _meta list passed through to the descriptor.

mcp_app_only

Logical; if TRUE, the resource is only listed/read for clients that announce MCP Apps support.

overwrite

Logical; if TRUE, replace an existing entry with the same URI.

Returns

The registry, invisibly.


Method list()

List resource descriptors visible to the calling client.

Usage

MCPResourceRegistry$list(mcp_apps_supported = TRUE)

Arguments

mcp_apps_supported

Logical; if FALSE, mcp_app_only entries are excluded.

Returns

A list of resource descriptors suitable for the MCP resources/list response.


Method read()

Read a resource by URI.

Usage

MCPResourceRegistry$read(uri, mcp_apps_supported = TRUE)

Arguments

uri

The URI to read.

mcp_apps_supported

Logical; if FALSE, mcp_app_only entries return NULL.

Returns

A list shaped like the MCP resources/read result, or NULL if not found.


Method has()

Check whether a URI is registered.

Usage

MCPResourceRegistry$has(uri, mcp_apps_supported = TRUE)

Arguments

uri

The URI to check.

mcp_apps_supported

Logical; if FALSE, mcp_app_only entries report FALSE.

Returns

Logical.


Method clone()

The objects of this class are cloneable with this method.

Usage

MCPResourceRegistry$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

if (FALSE) { # \dontrun{
reg <- MCPResourceRegistry$new()
reg$register(
  uri = "data://hello",
  name = "Hello",
  resource_reader = function() list(text = "hi", mimeType = "text/plain")
)
server <- mcprServer$new(resource_registry = reg)
} # }