使用说明
btwExtra
基于{btw}包构建的额外MCP工具和助手 将R会话与LLM连接。
安装(开发)
来自GitHub:
remotes::install_github("brianmsm/btwExtra")
当前工具
btwExtra_tool_env_run_r_code:escape舱口运行任意R代码 在连接的会话中。模拟R控制台(打印可见 值,跳过赋值)并将输出截断为20行 违约;集max_output_lines = -1禁用截断。更喜欢 更具体btw_tool_*/btwExtra_tool_*工具可用时。
示例用法
通过ellmer聊天注册btw+btwExtra工具:
library(ellmer)
ch <- ellmer::chat_anthropic()
ch$register_tools(c(btw::btw_tools(), btwExtra::btwExtra_tools()))
# Ask the model to use the run-R-code tool; output will be truncated by default.
ch$respond("Run 1 + 1 in R and show the result.")
快速本地检查面向用户的工具(聊天之外):
res <- btwExtra::btwExtra_tool_env_run_r_code("library(dplyr); mtcars %>% filter(cyl == 6) %>% summarise(value = mean(hp))")
res
## <btwExtra::BtwExtraToolResult>
## @ value : chr " value\n1 122.2857"
## @ error : NULL
## @ extra :List of 1
## .. $ data:List of 5
## .. ..$ status : chr "ok"
## .. ..$ console : chr [1:2] " value" "1 122.2857"
## .. ..$ result_class : chr NA
## .. ..$ n_output_lines : int 2
## .. ..$ max_output_lines: int 20
## @ request: NULL
截断通知:默认情况下,“长输出截断”指导为 每个会话显示一次。要始终显示它:
options(btwExtra.show_truncation_notice_once = FALSE)
MCP配置
使用btw+btwExtra组合工具:
[mcp_servers.r-btw]
command = "Rscript"
args = [
"-e",
"btwExtra::btwExtra_mcp_server()" # btw::btw_tools() + btwExtra::btwExtra_tools()
]
如果需要,您可以传递一组自定义工具,例如仅来自btw的文档 加上btwExtra的所有内容:
args = [
"-e",
"btwExtra::btwExtra_mcp_server(tools = c(btw::btw_tools('docs'), btwExtra::btwExtra_tools()))"
]
或者,您可以致电 btw::btw_mcp_server() 直接通过
组合工具:
args = [
"-e",
"btw::btw_mcp_server(tools = c(btw::btw_tools('docs'), btwExtra::btwExtra_tools()))"
]
Claude代码或游标的MCP配置
Claude CLI:
claude mcp add -s "user" r-btw -- Rscript -e "btwExtra::btwExtra_mcp_server()"
游标/VS代码风格JSON:
{
"r-btw": {
"command": "Rscript",
"args": ["-e", "btwExtra::btwExtra_mcp_server()"]
}
}
非常重要:注册R会话
您想与MCP一起使用的任何R会话都必须调用
mcptools::mcp_session() 因此MCP服务器可以发现并连接到
最简单的方法是将此呼叫添加到您的 .Rprofile (用户-或
项目级别),因此它在会话开始时自动运行。
项目级别 .Rprofile (放置在项目根目录中),或用户级别
.Rprofile:
- 窗户:
C:/Users/<USER>/Documents/.Rprofile - macOS/Linux:
~/.Rprofile
示例内容:
if (interactive() && requireNamespace("mcptools", quietly = TRUE)) {
mcptools::mcp_session()
}
这将自动注册任何交互式R会话
mcptools,使其可供MCP客户端使用(例如Codex、ChatGPT、,
等等)。
