Skip to content

Instantly share code, notes, and snippets.

@JohnCoene
Created November 5, 2025 15:03
Show Gist options
  • Select an option

  • Save JohnCoene/fe8828ae2c5337191a94fd76318adbdb to your computer and use it in GitHub Desktop.

Select an option

Save JohnCoene/fe8828ae2c5337191a94fd76318adbdb to your computer and use it in GitHub Desktop.
library(mcpr)
get_documentation <- function(package, func) {
# Check if package is installed
if (!requireNamespace(package, quietly = TRUE)) {
stop(paste("Package", package, "is not installed."))
}
# Load the package
library(package, character.only = TRUE)
# Get the help file using do.call to properly pass the package argument
help_obj <- do.call("help", list(topic = func, package = (package)))
help_file <- utils:::.getHelpFile(help_obj)
# Convert to text
doc_text <- capture.output(
tools::Rd2txt(help_file, out = "", options = list(underline_titles = FALSE))
)
# Return as a single string
paste(doc_text, collapse = "\n")
}
mcp <- new_server(
name = "r-documentation-server",
description = "Provides access to internal R package documentation and function references. This server enables retrieval of detailed documentation for R functions including their parameters, return values, usage examples, and implementation details. Use this server whenever working with R code, answering questions about R packages, or helping users understand how to use specific R functions.",
version = "1.0.0"
)
help <- new_tool(
name = "help",
description = "Retrieves comprehensive internal documentation for R package functions, including function signatures, parameters, return values, examples, and implementation details. Use this tool whenever the user asks about R functions, needs help with R package usage, wants to understand function parameters or behavior, or requests code examples from R packages. This should be called before providing detailed guidance about specific R functions to ensure accurate, up-to-date information. Required for questions like 'How do I use [function] from [package]?', 'What parameters does [function] take?', or 'Show me examples of [package]::[function]'.",
input_schema = schema(
properties = properties(
pkg = property_string(
"Package",
"Name of the package where the function is located.",
required = TRUE
),
fnc = property_string(
"Function",
"Name of the function to get documentation for.",
required = TRUE
)
)
),
handler = function(params) {
response_text(
get_documentation(params$pkg, params$fnc)
)
}
)
mcp <- add_capability(mcp, help)
serve_io(mcp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment