shinymcp
   
shinymcp转换 闪亮 应用程序进入 主控程序 应用 --呈现的交互式UI 直接在Claude Desktop等AI聊天界面内。
安装
您可以从以下网址安装shinymcp的开发版本 与:
# install.packages("pak")
pak::pak("JamesHWade/shinymcp")快速开始
MCP应用程序有两个部分: 用户界面组件 在聊天中呈现 接口,以及 工具 当输入发生变化时,运行R代码。使用 标准闪亮或bslib输入——桥接器通过以下方式自动检测它们 将工具参数名称与元素匹配 id 属性。
library(shinymcp)
library(bslib)
ui `, ``,
等等) `id` 匹配工具参数名称。这意味着您可以使用
您已经知道的Shiny和bslib输入:
sidebar( shiny::selectInput("species", "Species", choices), shiny::numericInput("n", "Count", value = 10), shiny::checkboxInput("trend", "Show trend line") )
对于边缘情况(id与arg名称不匹配,自定义小部件),使用
`mcp_input()` 要显式标记一个元素:
mcp_input(shiny::radioButtons("fmt", "Format", c("summary", "head")), id = "fmt")
shinymcp还提供轻量级 `mcp_select()`, `mcp_text_input()`,
等等,它们在没有Shiny的JS运行时生成最小的HTML。这些是
适用于简单的应用程序,并且是自动转换管道
生成。
### 输出
|闪亮| shinymcp|注释|
|----|----|----|
| `textOutput()` / `verbatimTextOutput()` | `mcp_text()` |渲染在 `
` 使用等宽字体|
| `plotOutput()` | `mcp_plot()` |工具返回base64编码的PNG|
| `tableOutput()` | `mcp_table()` |工具返回HTML表格字符串|
| `htmlOutput()` | `mcp_html()` |工具返回原始HTML|
您还可以使用以下命令将任何标签转换为输出目标 `mcp_output()`:
mcp_output(tags$pre(id = "result"), type = "text")
## 例子
shinymcp附带了示例应用程序:
Minimal: mcp_select() + text output
system.file("examples", "hello-mcp", "app.R", package = "shinymcp")
Native shiny/bslib inputs with auto-detection + mcp_input()/mcp_output()
system.file("examples", "bslib-inputs", "app.R", package = "shinymcp")
Full dashboard: Palmer Penguins with native shiny inputs, ggplot2, and
summary statistics
system.file("examples", "penguins", "app.R", package = "shinymcp")
## 运作原理
MCP应用程序在AI聊天界面中的沙盒iframe内渲染。A.
轻量级JavaScript桥(无npm依赖)处理
通过postMessage/JSON-RPC进行通信:
1. 用户更改输入→ 桥接器自动检测哪些表单元素
输入(通过将工具参数名称与元素id匹配)并收集
所有值
1. Bridge发送了一个 `tools/call` 向主机发出请求
1. 主机将呼叫代理到MCP服务器(您的R进程)
1. R工具函数运行,返回结果
1. 桥接器使用响应更新输出元素
该桥还实现了MCP Apps初始化握手
(`ui/initialize`)、自动调整通知大小和拆卸处理。
## 相关套餐
- [埃尔默](https://ellmer.tidyverse.org/) --shinymcp的LLM框架
用于工具定义
- [bslib](https://rstudio.github.io/bslib/) --基于Bootstrap的布局和
UI的主题化
- [mcptools](https://github.com/posit-dev/mcptools) --MCP服务器
框架(shinymcp通过资源支持对此进行了扩展)
- [副手](https://github.com/JamesHWade/deputy) --代理AI框架
具有AI辅助Shiny转换的捆绑技能