Skip to content

Commit 4741b83

Browse files
armishclaude
andcommitted
Fix critical issues with resources functionality
ISSUES FIXED: 1. Installed packages resource error: 'subscript out of bounds' - Problem: installed.packages() doesn't always have 'Title' column - Solution: Check available columns before subsetting - Now safely handles packages with missing Title information 2. All help resources showing same content (closure bug) - Problem: All help functions captured same topic variable from loop - Solution: Use force() to create proper closure for each topic - Each help resource now shows correct, unique content VALIDATION: - /r/packages now returns package list without errors - /help/mean shows 'Arithmetic Mean' (not 'Fitting Linear Models') - /help/lm shows 'Fitting Linear Models' - /help/summary shows 'Object Summaries' - All 45 resources tests still passing Critical fixes for production-ready resources functionality. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 04dfdeb commit 4741b83

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

R/pr_mcp.R

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,16 @@ pr_mcp_help_resources <- function(pr, topics = NULL) {
464464
pr = pr,
465465
uri = "/r/packages",
466466
func = function() {
467-
pkgs <- installed.packages()[, c("Package", "Version", "Title")]
468-
capture.output(print(as.data.frame(pkgs), row.names = FALSE))
467+
pkgs <- installed.packages()
468+
# Check which columns are available
469+
available_cols <- colnames(pkgs)
470+
desired_cols <- c("Package", "Version")
471+
if ("Title" %in% available_cols) {
472+
desired_cols <- c(desired_cols, "Title")
473+
}
474+
475+
pkg_info <- pkgs[, desired_cols, drop = FALSE]
476+
capture.output(print(as.data.frame(pkg_info), row.names = FALSE))
469477
},
470478
name = "Installed R Packages",
471479
description = "List of installed R packages with versions and descriptions",
@@ -478,12 +486,18 @@ pr_mcp_help_resources <- function(pr, topics = NULL) {
478486
#' Create a help function for a specific topic
479487
#' @noRd
480488
create_help_function <- function(topic) {
489+
# Force evaluation of topic to create proper closure
490+
force(topic)
491+
481492
function() {
493+
# Use the captured topic value
494+
current_topic <- topic
495+
482496
tryCatch({
483497
# Get help content
484-
help_file <- utils::help(topic, try.all.packages = TRUE)
498+
help_file <- utils::help(current_topic, try.all.packages = TRUE)
485499
if (length(help_file) == 0) {
486-
return(paste("No help found for topic:", topic))
500+
return(paste("No help found for topic:", current_topic))
487501
}
488502

489503
# Create a temporary file to capture clean text output
@@ -504,25 +518,25 @@ create_help_function <- function(topic) {
504518

505519
return(clean_text)
506520
} else {
507-
return(paste("Error: Could not generate help text for", topic))
521+
return(paste("Error: Could not generate help text for", current_topic))
508522
}
509523
}, error = function(e) {
510524
# Fallback: try to get basic help information
511525
tryCatch({
512526
# Simple approach - just get function signature and description
513-
func_obj <- get(topic, envir = globalenv())
527+
func_obj <- get(current_topic, envir = globalenv())
514528
if (is.function(func_obj)) {
515529
paste(
516-
paste("Function:", topic),
530+
paste("Function:", current_topic),
517531
paste("Usage:", paste(deparse(args(func_obj)), collapse = " ")),
518532
"Use help() in R console for full documentation.",
519533
sep = "\n"
520534
)
521535
} else {
522-
paste("Topic:", topic, "- Use help() in R console for documentation.")
536+
paste("Topic:", current_topic, "- Use help() in R console for documentation.")
523537
}
524538
}, error = function(e2) {
525-
paste("Help topic:", topic, "- Use help() in R console for documentation.")
539+
paste("Help topic:", current_topic, "- Use help() in R console for documentation.")
526540
})
527541
})
528542
}

0 commit comments

Comments
 (0)