Feature(mcp): let terminal_exec open or reuse a session implicitly

terminal_exec no longer requires session_id. Given a command and no
session_id, it reuses the host's persistent terminal session or opens one
through the same path as terminal_open, and reports session_id, is_pty and
reused in the result so the caller can keep using it. Polling still
requires the session token.

terminal_open now returns the existing session with reused:true instead of
-32003 when one is already active. An implicitly created session is
invisible to the caller, so rejecting would silently lock the host's single
terminal slot and leave exec_command failing with a busy error. A session
that is still starting, or one owned by a one-shot exec_command, still
answers -32003.

The server instruction line that told models to always pair terminal_open
with terminal_close is updated accordingly.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 16:01:13 +01:00
1 parent 3fe3a11494
commit 0378e96470
3 files changed
+132 -19

No files matched your search

+16 -5
View File
@@ -2190,7 +2190,7 @@
},
{
"name": "terminal_open",
"description": "在指定在线 Windows、Linux 或 Android 主机上打开一个持久 shell 会话并返回 session_id 与终端模式。后续 terminal_exec 复用该会话(cwd/环境变量跨命令保持),用毕须 terminal_close。Linux/Android 使用 POSIX PTY(UTF-8,is_pty=true),Android 的 shell 是 /system/bin/sh(mksh)。",
"description": "在指定在线 Windows、Linux 或 Android 主机上打开一个持久 shell 会话并返回 session_id 与终端模式。后续 terminal_exec 复用该会话(cwd/环境变量跨命令保持),用毕须 terminal_close。若该主机已有持久会话(包括 terminal_exec 隐式创建的),直接复用并返回 reused:true,不会报 -32003。Linux/Android 使用 POSIX PTY(UTF-8,is_pty=true),Android 的 shell 是 /system/bin/sh(mksh)。",
"gate": "terminal",
"write": true,
"platforms": [
@@ -2221,6 +2221,10 @@
"description": "true=PTY(UTF-8)(Windows ConPTY / Linux PTY);false=老 cmd 管道(GBK)",
"type": "boolean"
},
"reused": {
"description": "true=该主机已有持久会话,本次直接复用(未新建);false=本次新建",
"type": "boolean"
},
"session_id": {
"description": "持久会话 token,后续 terminal_exec / terminal_close 用",
"type": "string"
@@ -2235,7 +2239,7 @@
},
{
"name": "terminal_exec",
"description": "在已打开的持久会话中执行一条命令,或轮询取回正在运行的命令输出。带 command 时执行并在 timeout_ms 内等待;等待超时返回 done:false 与已产生的部分输出,会话不销毁(命令仍在远端运行)。不带 command、只传 session_id 时进入轮询模式:继续等待并取回增量输出,直到 done:true(此时有 exit_code)。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux 与新版 Android 的 POSIX PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"description": "在已打开的持久会话中执行一条命令,或轮询取回正在运行的命令输出。带 command 时执行并在 timeout_ms 内等待;等待超时返回 done:false 与已产生的部分输出,会话不销毁(命令仍在远端运行)。session_id 可省略:自动复用该主机已有的持久会话,没有就隐式打开一个(结果里有 session_id 与 reused),不必先 terminal_open。不带 command、只传 session_id 时进入轮询模式:继续等待并取回增量输出,直到 done:true(此时有 exit_code)。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux 与新版 Android 的 POSIX PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"gate": "terminal",
"write": true,
"platforms": [
@@ -2259,7 +2263,7 @@
"type": "integer"
},
"session_id": {
"description": "terminal_open 返回的 session_id",
"description": "会话 token。带 command 时可省略:自动复用该主机已有的持久会话,没有就在内部打开一个(结果里有 session_id 与 reused)。轮询模式必须提供",
"type": "string"
},
"timeout_ms": {
@@ -2268,8 +2272,7 @@
}
},
"required": [
"id",
"session_id"
"id"
],
"type": "object"
},
@@ -2291,6 +2294,14 @@
"description": "退出码(done=true 时提供):PTY 客户端(Windows ConPTY / Linux PTY)为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(shell 进程退出)",
"type": "integer"
},
"is_pty": {
"description": "true=PTY(UTF-8)(Windows ConPTY / Linux PTY);false=老 cmd 管道(GBK)",
"type": "boolean"
},
"reused": {
"description": "true=复用了该主机已有的持久会话;false=本次隐式新建(省略 session_id 时)",
"type": "boolean"
},
"session_id": {
"description": "会话 token,原样回传,便于续接轮询",
"type": "string"