Skip to content

feat(composer): 支持显式选择 MCP 服务器与工具 - #1409

Merged
vastsa merged 10 commits into
vastsa:mainfrom
zszz3:codex/feat-composer-mcp-1377
Oct 4, 2026
Merged

vastsa merged 10 commits into
vastsa:mainfrom
zszz3:codex/feat-composer-mcp-1377

Conversation

@zszz3

@zszz3 zszz3 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1377

使用方式

输入框的 / 菜单支持显式选择 MCP 服务器或具体工具,让用户能指出这次任务要用哪个 MCP。

输入 含义
/mcp:time 查询上海当前时间 选择 Time 服务器,让模型从该服务器允许使用的工具中完成任务
/mcp:time:get_current_time 查询上海当前时间 选择 Time 的 get_current_time 工具
/mcp:agent-recall:search_sessions 查找上周的会话 选择 AgentRecall 的会话搜索工具

选择只把命令填入输入框,不会立即执行工具。发送后,模型仍负责判断是否调用、填写参数和处理结果。只有命令而没有任务正文时,也不自动执行一个猜测的操作。具体命令 ID 以菜单为准,服务器和工具名称中的特殊字符会做 URI 编码。

具体实现

输入框目录和补全

composer-ipc.ts 从现有 User MCP runtime 读取服务器记录和工具目录,只为已启用、当前项目可用、连接就绪且有工具的服务器生成命令。获取目录复用原有连接初始化流程,因此冷启动后无需先打开 MCP 设置。

共享的 ComposerCommand 增加 mcp 类型,携带 mcpServerId 和可选的 mcpToolName。输入框复用现有斜杠菜单、搜索、鼠标和键盘补全,不新增另一套工具执行入口。目录不包含连接地址、凭据或工具参数 schema,现有命令重名时保持原有优先级。

提交时解析和校验

composer-mcp.ts 解析消息开头的 /mcp:…,并匹配 Main 在发送时重新读取的命令目录。校验依据是这条会话绑定的项目,不是界面当前打开的项目。

Main 将原始输入保留在消息的 command 字段,供历史展示;模型收到一条简短的用户选择说明及任务正文。例如,服务器选择会说明用户为本次请求选择了哪个 MCP。实际选择同时通过内部 RPC 的 mcpServerIds、mcpToolNames 传递,工具激活不依赖模型识别这句说明。

服务器已禁用、项目范围不匹配、工具不存在等情况会明确失败,不会当普通文本继续发送,也不会把无效的具体工具选择扩大为整个服务器。Desktop 管理的 MCP 选择不用于原生 Pi 会话。

Runtime 激活和模型上下文

mcp-tool-selection.ts 用目录中的服务器 ID 和完整工具名校验归属,再按当前模式过滤可用工具,不靠工具名前缀判断权限。

  • 选择服务器:激活该服务器当前模式允许的工具,不受 ToolSearch 单次结果数量限制。
  • 选择具体工具:只新增所选工具的激活状态;不存在、归属不符或模式不允许时拒绝。
  • 激活复用现有 deferred-tool 状态和工具声明同步流程,使这些工具进入模型可用集合;已激活的其他工具不会被移除。
  • 执行仍经过原有 Host 权限和模式检查。指定 MCP 不等于批准执行,也不绕过 Plan 限制。

普通消息在发起模型请求前激活。运行中的追加消息将选择与消息一起保存,等消息真正进入模型上下文时才激活并同步声明,避免提前影响正在进行的请求。未携带 MCP 选择的消息继续使用原来的按需发现方式。

主要改动在输入框命令目录、Main 提交解析和 Agent Runtime 工具激活;没有数据库迁移,没有新增 Plugin SDK 或 Rust Host 协议字段。相关 ADR、工具权限规范、组件规范和 E2E 场景同步更新。

验证

  • 当前候选提交 e13259df78cc 已合入主线 abfeeb21852e。文档检查覆盖 557 个页面、85 对中英文文档,13 项文档测试及 VitePress 构建通过。
  • 当前候选的桌面构建、类型检查通过;3527 项桌面测试、42 项 MCP runtime 回归测试,以及服务器选择、具体工具选择和 Electron 输入框交互三组 E2E 全部通过。
  • MCP E2E 使用本地 provider fixture、真实 sidecar、Host 权限流程和 stdio MCP,验证 tools/call 的结果进入后续模型请求;覆盖选择服务器后直接调用第六个工具,避免只激活 ToolSearch 首批结果。
  • 此前已用真实模型验证 Time、Filesystem、Memory、AgentRecall、Sequential Thinking、Fetch、Playwright,共 14 个服务器/具体工具代表性场景。该验证来自同步主线前的实现版本,不代表每个服务器的全部工具都已测试;本轮没有重复调用付费 API。

本轮 CI 修复针对 ADR 标题和目录索引:缺少 ADR 标题前缀及索引项同时导致 Docs 检查和 JS 流水线中的文档测试失败。修复没有修改 MCP 执行代码。

类型为 feat。仓库当前外部贡献范围限于 fix / perf,此功能是否接受需由维护者决定。

zszz3 and others added 10 commits October 5, 2026 01:19
Allow users to select enabled MCP capabilities from the composer while
preserving runtime discovery, project scope and execution permissions.
Resolve exact identities at submission and activate queued selections only
when their steering message is consumed.

Initialize MCP discovery before cold-start validation so the first command
after restart works without visiting settings. Cover selection, lifecycle,
permission and composer interactions with regression and E2E scenarios.

closes vastsa#1377
Keep the latest usable model selection across new chats and restarts,
while preserving each existing conversation binding. Expose three recent
provider/model pairs directly in the composer menu.

Remove the fixed chat-default settings controls without changing legacy
settings used by scheduled jobs or the independent image model selection.
Cover persistence, unavailable bindings and model menu interactions.
Only accepted messages update model history, so opening an old chat or
browsing model choices cannot change the next chat's inherited model.
Keep search and recent choices together, with other models expanded inline.
Recent entries missed the shared model group styles and rendered larger
than the expanded catalog. Reuse that group and check computed typography
in the Electron interaction test.
Pointer hover must not leave a model marked as the keyboard target after
moving to the inline disclosure. Clear the keyboard cursor on pointer
movement and when search loses focus, preserving native hover feedback.
The documentation gate requires an ADR heading and an index entry for
each decision record. Include both so docs validation and the recursive
workspace test job can complete.
…ection

feat(composer): 新聊天沿用上次模型并优先显示最近三项
A bare MCP selection currently sends only a tool-selection instruction, leaving the model to infer what to do. Require task text or attachment context before creating a turn while preserving attachment-only requests.
@vastsa
vastsa merged commit 4e9aef5 into vastsa:main Oct 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 请求将已连接的MCP服务器加入到输入框 / 的菜单补全当中,显性调用MCP

2 participants