dsh-codex-mcp:让 Codex 通过 MCP 把编码任务派给 DSH headless

前言

在 DSH 里写代码、跑命令,通常由 DSH 自己的模型通过 ctx.tools 完成。若你同时在用 Codex CLI,希望由 Codex 发起任务、读回结构化结果,再自行跑验收命令,普通 DSH 工具对 Codex 不可见——能力必须离开 DSH 进程,以 MCP 等形式暴露给外部调用方。

GEB-lab/dsh-codex-mcp 是运行在 DSH 宿主进程内的 DSH host plugin:在本地拉起仅监听回环地址的 Streamable HTTP MCP 服务,Codex 经 Bearer 鉴权调用 MCP 工具,插件再拉起本机已安装的 DSH CLI(--profile headless --json),由 DSH 读写代码、执行命令并流式输出 NDJSON。写代码的仍是 DSH;Codex 只负责派发任务、解析返回、自己重跑验收。

社区目录页:skillhub.cn 插件条目。目录为独立站点,与 DeepSeek / 幻方无官方从属关系。

这是什么

一句话:DSH 进程内的 MCP 桥——Codex 经 http://127.0.0.1:39391/mcp(默认)把开发任务交给 DSH headless,无需每次手写 shell 包装脚本。

维护者:GEB-lab。许可证:MIT(README 标明 zero runtime dependencies)。package.json 当前版本为 0.1.1。

调用链(摘自 README):

Codex CLI
  └─ MCP (Streamable HTTP + Bearer token)      http://127.0.0.1:39391/mcp
       └─ DSH host plugin dsh-codex-mcp        (lives inside the DSH process)
            └─ node <dsh install>/lib/bin.js --profile headless --json
                 └─ DSH reads/writes code, runs commands, streams NDJSON events

为何做成插件而不是 DSH 内置工具

README 说明:常规 DSH agent 工具只对 DSH 自己的模型可见,Codex 无法调用。该插件在宿主加载时通过 ctx.effect 启动 HTTP MCP 端点,卸载时关闭;自行实现 Streamable HTTP 与 legacy JSON-RPC,不依赖 MCP SDK 或 @deepseek-ai/* 包,避免在 profile 内解析额外依赖;子进程 DSH 路径优先从宿主安装旁的 @deepseek-ai/dsh 的 lib/bin.js 定位,或由 DSH_APP_ROOT / dshCli 指定。

核心功能

MCP 工具(三个)

工具 作用
dsh_env() 无副作用、不消耗模型 token;返回插件、MCP URL/token、DSH CLI 与 profile、默认 cwd/超时、并发状态,以及可粘贴的 Codex config.toml 片段
dsh_run(task, cwd?, timeout_sec?, include_events?) 新建 headless 会话;task 经 stdin 传入,避免引号与 argv 长度问题;成功时返回 final answer、session id、cwd、exit、turn_end、token 用量、步数与耗时
dsh_resume(session_id, task, cwd?, timeout_sec?) 在同一会话上续聊;cwd 须与首次 dsh_run 一致,否则 DSH 拒绝启动

结果与失败语义

  • 结构化字段包含最终回答、session、cwd、exit、turn_end、每轮 token、步数、时长;失败时附带 stderr 尾部与分类原因。
  • 以下情况插件按 MCP 错误返回:超时、子进程非零退出、turn_end 非 completed、未收到 final 事件、最终回答为空——不轻信 DSH 自述的「已完成」。
  • 超时会对进程树做 kill(Windows 为 taskkill /T /F,其他平台为进程组),避免残留 agent。
  • maxConcurrentRuns 默认 4,超额调用快速失败,不静默排队。
  • 默认仅绑定 127.0.0.1;Bearer token 首次启动生成(32 随机字节,文件权限 0600),默认路径 $DSH_HOME/codex-mcp.token,校验使用 sha256 + timingSafeEqual。
  • 工具描述支持中英(locale/en.json、locale/zh.json),配置项 locale 默认 zh。

环境要求

项 要求
DSH 桌面或 CLI 安装,且 @deepseek-ai/dsh 支持 --profile headless --json
Node ^22.19.0 \|\| >=24.0.0(使用宿主自带 Node)
凭证 ~/.dsh profile 中已配置模型 API Key(GUI Models 或凭证服务)
Profile 需有 headless profile;桌面 profile 不接受 --json

安装与启用

在 DSH 侧添加插件(命令来自 README,注意 --profile desktop):

# from GitHub (recommended)
dsh plugin --profile desktop add github:GEB-lab/dsh-codex-mcp

# from a release tarball
dsh plugin --profile desktop add https://github.com/GEB-lab/dsh-codex-mcp/releases/latest/download/dsh-codex-mcp.tgz

# local checkout, for development (changes apply after a host restart)
dsh plugin --profile desktop add link:/absolute/path/to/dsh-codex-mcp

dsh plugin 会转发到对应 profile 的包管理器,并在 dsh.profile.bundles 中选中新 bundle。若 HMR 开启则立即加载,否则需重启 DSH。

验证 MCP 是否就绪:

curl -i http://127.0.0.1:39391/mcp -X POST \
  -H "authorization: Bearer $(cat ~/.dsh/codex-mcp.token)" \
  -H "content-type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{}}}'

无 curl 时可用仓库内冒烟脚本(node scripts/smoke.mjs 仅测协议与 dsh_env;加 --task 会消耗 token),脚本会打印本机适用的 config.toml 片段。

Codex 侧配置

编辑 ~/.codex/config.toml:

[mcp_servers.dsh_codex]
url = "http://127.0.0.1:39391/mcp"
http_headers = { Authorization = "Bearer <contents of $DSH_HOME/codex-mcp.token>" }
startup_timeout_sec = 30
tool_timeout_sec = 3600
enabled = true

执行 codex mcp list 应看到 dsh_codex … enabled Bearer token。

可选:插件配置

在 bundle 默认或 ~/.dsh/profiles/<profile>/cordis.patch.yml 中覆盖 dsh-codex-mcp 的 config,例如:

- id: dsh-codex-mcp
  name: dsh-codex-mcp
  config:
    port: 39400
    defaultCwd: 'D:\code'

常用键:host(默认 127.0.0.1,非回环需 allowNonLoopback: true)、port(默认 39391,0 为自动选端口)、path(默认 /mcp)、defaultTimeoutSec(默认 3600)、permissionMode(默认 danger-full-access)、maxConcurrentRuns(默认 4)等;完整表格见 GitHub README「Configuration」一节。

典型用法

在 Codex 中可向模型说明:用 dsh_run 在指定目录完成改动,返回后由 Codex 自己跑测试。README 示例(路径按你的环境替换):

Use dsh_run to have DSH, in C:\work\proj, add typed input support to src/sum.mjs plus three node:test cases; only touch src/ and test/. After it returns, run node --test test\*.test.mjs yourself and report the result.

dsh_run 成功时文本结果中会包含 session: 与续聊提示,例如:

--- continue this session ---
dsh_resume({ "session_id": "session-1a2b3c4d-...", "task": "<next task>" })

适用场景与注意

适合谁:已同时使用 DSH(headless + 已配 API Key)与 Codex CLI,希望用 Codex 编排多步开发、把具体改码交给 DSH,并由 Codex 保留验收与汇报链路的工作流场景。

安全与权限:插件以当前 DSH 宿主进程的权限运行子 headless agent;默认 permissionMode 为 danger-full-access。安装前请阅读仓库源码与 MIT 许可证,确认 token 文件与仅回环绑定策略符合你的环境。非回环绑定需显式配置且仍有对端地址校验(详见 README「Security model」)。

分类:社区目录将该插件归为 工作流。

结尾

dsh-codex-mcp 把「Codex 派发、DSH 执行」固定在本地 MCP 回环上,用三个工具和环境探测降低接线成本,并对超时、退出码与空结果做统一失败分类。需要细节、配置表与安全说明时,以仓库 README 为准。

  • 目录页:https://www.skillhub.cn/plugins/GEB-lab/dsh-codex-mcp
  • GitHub:https://github.com/GEB-lab/dsh-codex-mcp
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye