Skip to contents

Discovers and registers R functions as MCP tools through roxygen2 parsing. Scans one or more directories for functions tagged with the mcpr_tool keyword, converts documentation into structured tool specifications, and supports post-discovery filtering.

Details

Provides comprehensive tool management:

  • Multi-Directory Discovery: Scans directories for tagged functions

  • Documentation Parsing: Converts roxygen2 comments to tool specs

  • Type Mapping: Maps parameter types to MCPR specifications

  • Filtering: Include/exclude tools by name for tailored server profiles

  • Provenance: Tracks which directory contributed each tool

  • Validation: Checks for naming conflicts and protocol compliance

Methods


Method new()

Create a new ToolRegistry instance with specified configuration

Usage

ToolRegistry$new(
  tools_dir = "inst",
  pattern = "tool-.*\\.R$",
  recursive = FALSE,
  verbose = FALSE
)

Arguments

tools_dir

Character vector of directory paths to scan for tool files (default: "inst"). Each path is scanned independently; missing directories are skipped with a warning.

pattern

File pattern to match (regex) (default: "tool-.*\.R$")

recursive

Whether to search subdirectories (default: FALSE)

verbose

Enable verbose output during search (default: FALSE)

Returns

New ToolRegistry instance


Method search_tools()

Scan configured directories for functions tagged with mcpr_tool keyword. Applies any active include/exclude filters before returning results.

Usage

ToolRegistry$search_tools(force_refresh = FALSE)

Arguments

force_refresh

Force re-scanning even if tools are cached (default: FALSE)

Returns

List of MCPR tool objects (filtered if filters are active)


Method get_tools()

Return currently loaded tools (after filtering) without re-scanning

Usage

ToolRegistry$get_tools()

Returns

List of MCPR tool objects


Method get_tool_summary()

Generate data.frame summary of loaded tools with metadata

Usage

ToolRegistry$get_tool_summary()

Returns

Data.frame with columns: name, description, parameters, source_dir


Method has_tool()

Check if tool with specified name exists in registry

Usage

ToolRegistry$has_tool(name)

Arguments

name

Name of the tool to check

Returns

TRUE if tool exists, FALSE otherwise


Method get_tool()

Retrieve specific tool by name from registry

Usage

ToolRegistry$get_tool(name)

Arguments

name

Name of the tool to retrieve

Returns

MCPR tool object or NULL if not found


Method configure()

Update search configuration and reset cached tools. Accepts a character vector for tools_dir to enable multi-directory discovery. Filters set via filter() are preserved across configure() calls.

Usage

ToolRegistry$configure(tools_dir = NULL, pattern = NULL, recursive = NULL)

Arguments

tools_dir

Character vector of directory paths (optional)

pattern

New file pattern (optional)

recursive

New recursive setting (optional)

Returns

Self (invisibly) for method chaining


Method filter()

Set include/exclude filters for tool names. Filters are applied immediately to already-discovered tools and persist across subsequent search_tools() calls. Call with no arguments to clear all filters.

Usage

ToolRegistry$filter(include = NULL, exclude = NULL)

Arguments

include

Character vector of tool names to keep (allowlist). When set, only tools whose names appear in this vector are retained.

exclude

Character vector of tool names to drop (denylist). Applied after include. Cannot overlap with include.

Returns

Self (invisibly) for method chaining


Method set_verbose()

Enable or disable verbose output during search operations

Usage

ToolRegistry$set_verbose(verbose)

Arguments

verbose

TRUE to enable verbose output, FALSE to disable

Returns

Self (invisibly) for method chaining


Method print()

Print summary of ToolRegistry with directory, filter, and tool information

Usage

ToolRegistry$print()

Returns

Self (invisibly)


Method clone()

The objects of this class are cloneable with this method.

Usage

ToolRegistry$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

if (FALSE) { # \dontrun{
# Basic usage - single directory
registry <- ToolRegistry$new()
tools <- registry$search_tools()

# Multi-directory discovery
registry <- ToolRegistry$new(
  tools_dir = c(
    system.file(package = "MCPR"),
    system.file("tools", package = "myPkg")
  )
)

# Filter after discovery
registry$search_tools()
registry$filter(exclude = "execute_r_code")

# Cherry-pick tools from MCPR + all from another package
registry$filter(include = c("manage_r_sessions", "my_tool_a", "my_tool_b"))

# Inspect what you got
registry$get_tool_summary()
} # }