Repository navigation
Description
现象
agentdescent install <host> 给所有宿主写的 MCP 启动命令是 pinned 的 "<venv>/bin/python3" -m agentdescent.cli mcp(agentdescent/integrations/__init__.py 的 install():_Writer(dry_run, [os.path.abspath(sys.executable), "-m", "agentdescent.cli"]))。python -m 会把宿主进程的当前目录放到 sys.path[0],所以只要宿主在一个含有名为 agentdescent/ 子目录的目录里启动(最常见:放源码 checkout 的父目录,如 ~/code/ 下有 ~/code/agentdescent/),agentdescent 就被解析成那个目录(namespace package),MCP 服务器启动即崩,宿主只显示 Connection closed。
另外,cmd_mcp 的 except ImportError 兜底分支里调用的 mcp_unavailable() 本身也要 from .integrations import mcp_sdk_missing,同样失败,于是本该打印的一行「cannot start the MCP server -- ...」变成了二次异常的 traceback。
复现(main 053aeb1,OpenCode 1.x,Python 3.13)
mkdir ws && cd ws && git clone https://github.com/Birfy/agentdescent.git
python3 -m venv agentdescent/.venv && agentdescent/.venv/bin/pip install -e "./agentdescent[mcp]"
HOME=$PWD/home agentdescent/.venv/bin/agentdescent install opencode
# 在 ws/(含 agentdescent/ 子目录)里:
HOME=$PWD/home opencode mcp list
# ✗ agentdescent failed
# MCP error -32000: Connection closed
# .../.venv/bin/python3 -m agentdescent.cli mcp
cd /tmp && HOME=<同上> opencode mcp list
# ✓ agentdescent connected ← 同一配置,换个 cwd 就好了直接跑服务器命令看到的 stderr:
ImportError: cannot import name '__version__' from 'agentdescent' (unknown location)
During handling of the above exception, another exception occurred:
File ".../agentdescent/cli.py", line 1072, in cmd_mcp
why = mcp_unavailable()
File ".../agentdescent/cli.py", line 180, in mcp_unavailable
from .integrations import mcp_sdk_missing
ImportError: cannot import name 'mcp_sdk_missing' from 'agentdescent.integrations' (unknown location)
Claude Code / Codex / dsh 的文件路由写的是同一条 pinned 命令,同样受影响(宿主以项目目录为 cwd 启动 MCP 子进程)。agentdescent doctor 只看配置是否写入,会报 opencode: ok,用户无从得知。
预期
无论宿主在哪个目录启动,install 写下的 MCP 命令都能连上;万一导入失败,stderr 是一行可行动的说明而不是 traceback。
建议修法(单个 PR)
- pinned 启动器不再依赖 cwd:Python ≥ 3.11 时写
[<python>, "-P", "-m", "agentdescent.cli", "mcp"](-P不把 cwd 放进sys.path);3.9/3.10 时改用与sys.executable同一 scripts 目录下的agentdescentconsole script 绝对路径(sysconfig.get_path("scripts")),不存在时再退回现有写法。注意-I不合适(会丢 user site,pip install --user装的会找不到)。 _is_stock_mcp_command(约第 370 行)同时认新旧两种形状,保证install幂等、uninstall/doctor能识别旧配置并在重装时升级。cmd_mcp的兜底:mcp_unavailable()调用包一层 try;导入失败时打印一行,包含agentdescent实际被解析到的位置(例如agentdescent.__path__),并提示「宿主当前目录里有同名agentdescent/目录遮蔽了安装包」。- changelog.d 片段;docs/testing-the-plugins.md 的 OpenCode/Claude Code 排障段落加一句此症状。
验收
- 测试:在临时目录里建一个空的
agentdescent/子目录,以它为 cwd 运行install生成的命令(mcp换成能快速退出的doctor或直接--help也可),能正常导入;旧写法在同条件下复现失败(回归保护)。 - 测试:已有旧形状配置时再次
install被识别为 stock 并原地升级,不重复写块。 - 测试:模拟
from .mcp import serve的 ImportError 且integrations也不可导入时,cmd_mcp返回 3 且 stderr 只有一行说明、无 traceback。 - 手测:上面的复现步骤在
ws/里opencode mcp list显示connected。
Refs #165
Activity
test(install): the legacy launcher in a shadowed cwd prints one line (#…
fix(install): pin only a console script that runs this interpreter; w…
docs(changelog): #394 launcher fallback warning
ci(smoke): wheel smoke uses mcp_launcher() and launches from a shadow…
fix(install): MCP launcher no longer depends on the host's cwd (#394) (…
fix(cli): resolve --version lazily so a shadowing cwd keeps the #394 …
feat(cli): agentdescent --version; doctor reports AgentDescent versio…