雨天小六

读懂 Codex(4.13):Plugins、Apps 和 Recommended Plugins 的上下文注入

· 更新于 2026-08-02 · 专栏:读懂 Codex

#Codex#Agent Runtime#Prompt#上下文工程#软件架构

Plugin 本身不是一种模型可调用工具,而是 Skills、MCP servers 和 Apps 的本地打包单位。Prompt 需要同时解释这种关系、暴露已安装能力,并在条件允许时把未安装推荐项作为 contextual user 信息注入。三类信息的角色与触发条件不同。

通用 Plugin 说明与显式提及

AvailablePluginsInstructions 用 developer 角色说明命名、触发和能力来源。用户显式提及某 Plugin 后,build_plugin_injections 会从已加载能力摘要中反查该 Plugin 对应的可见 MCP server、已启用 App 与 Skill 前缀,生成更具体的 Developer hint。

Plugin 提及解析到 Skill、MCP 和 App 能力提示
图 4.13-1:Plugin 只负责归组和来源追踪。真正进入 ToolSpec 的仍是 MCP/App 工具,真正按需加载的仍是 Skill。
def build_plugin_hint(plugin, mcp_tools, apps):
    servers = sorted({t.server for t in mcp_tools if plugin in t.plugin_names})
    enabled_apps = sorted({a.label for a in apps if a.enabled and plugin in a.plugin_names})
    return render_plugin_capabilities(
        display_name=plugin.display_name,
        skill_prefix=plugin.prefix,
        mcp_servers=servers,
        apps=enabled_apps,
    )

集合使用有序去重,避免同一 server 因多个 tool 重复出现。Apps 只有启用项会进入显式提示。

只有 Apps 与 Plugins feature 同时启用,并开启 ToolSuggest 或 RecommendedPlugins 时,Manager 才按认证状态、禁用工具和客户端名计算候选。渲染器最多取 50 项,以 <recommended_plugins> 标记包围,并使用 user 角色。

Recommended Plugins 经功能开关、认证和策略过滤后注入
图 4.13-2:推荐列表不是无条件广告位。它经过功能开关和策略过滤,且只告诉模型有哪些候选,不会自动安装或启用。
async def recommended_plugin_fragment(features, manager, config, auth):
    if not features.apps or not features.plugins:
        return None
    if not (features.tool_suggest or features.recommended_plugins):
        return None
    candidates = await manager.recommend(config, auth, disabled=config.disabled_tools)
    return RecommendedPluginsFragment(candidates[:50]) if candidates else None

Apps 的双重边界

App 可以贡献模型可见说明,也可以通过 Codex Apps MCP server 暴露工具。是否出现在提示里与是否通过执行策略启用必须分别判断;工具暴露还要检查 connector ID、destructive/open-world annotation 和配置层策略。

测试需区分:Plugin 通用说明、显式 mention hint、推荐列表和实际 App ToolSpec。不能看到某 Plugin 名称就断言相应工具已安装。

源码与测试锚点

  • codex-rs/core/src/context/available_plugins_instructions.rs
  • codex-rs/core/src/context/recommended_plugins_instructions.rs
  • codex-rs/core/src/plugins/injection.rsrender.rs
  • codex-rs/core/src/mcp_tool_exposure.rs:App 工具策略过滤。

评论


← 返回文章列表