Feature: Support the remote terminal on Android hosts

The terminal tools were Windows/Linux only because the Android client had
no COMMAND_SHELL handler: the server opened a shell sub-connection and
the command was silently dropped, leaving exec_command to time out. This
adds the PTY path on the client and the matching server gating.

Client: COMMAND_SHELL on the main connection spawns a terminal thread
that opens an authenticated sub-connection, sends TOKEN_TERMINAL_START
and keeps the PTY alive until the server closes it. common/PTYHandler.h
is reused, with a minSdk-21 openpty built from posix_openpt/grantpt and
/system/bin/sh (mksh) as the shell. A new CLIENT_CAP_TERMINAL bit lets
the server reject old APKs up front with -32005.

Server: the tool masks, instructions and docs matrix accept APK. While
verifying, exec_command left a second echoed copy of the command line in
stdout: mksh forks with ECHO still on, so input written before its line
editor takes over is echoed by the line discipline in addition to the
shell's own redraw. The server now waits for the shell's first output
(the prompt) before sending the command and sentinel, which restores the
single-echo assumption the cleaning step relies on. terminal_exec on a
ready session is unaffected.

Verified on the Android 15 emulator: exec_command returns clean stdout
and real exit codes for id/uname/df/ls (including a failing command),
and terminal_open keeps cwd across commands, pipes work and close is
clean. The committed ghost.apk is stale and must be rebuilt before
release; a rebuilt APK was used for this testing.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:54 +01:00
1 parent 915c587964
commit 445bd8bbb9
9 files changed
+205 -32

No files matched your search

+11 -7
View File
@@ -2140,12 +2140,13 @@
},
{
"name": "exec_command",
"description": "在指定在线 Windows 或 Linux 主机上执行一条命令并返回 stdout 与退出码(受只读模式与命令白名单约束,默认仅允许只读命令前缀,见 McpReadonly / McpCmdWhitelist;Linux 有独立的 POSIX 默认白名单,禁用 ; 反引号 $( ${ ! 等串联/子命令字符)。",
"description": "在指定在线 Windows、Linux 或 Android 主机上执行一条命令并返回 stdout 与退出码(受只读模式与命令白名单约束,默认仅允许只读命令前缀,见 McpReadonly / McpCmdWhitelist;Linux/Android 有独立的 POSIX 默认白名单,禁用 ; 反引号 $( ${ ! 等串联/子命令字符)。Android 上 shell 是 /system/bin/sh(mksh),可用命令由 toybox 提供。",
"gate": null,
"write": true,
"platforms": [
"win",
"lnx"
"lnx",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -2189,12 +2190,13 @@
},
{
"name": "terminal_open",
"description": "在指定在线 Windows 或 Linux 主机上打开一个持久 shell 会话并返回 session_id 与终端模式。后续 terminal_exec 复用该会话(cwd/环境变量跨命令保持),用毕须 terminal_close。Linux 使用 POSIX PTY(UTF-8,is_pty=true)。",
"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)。",
"gate": "terminal",
"write": true,
"platforms": [
"win",
"lnx"
"lnx",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -2233,12 +2235,13 @@
},
{
"name": "terminal_exec",
"description": "在已打开的持久会话中执行一条命令并返回 stdout 与退出码。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"description": "在已打开的持久会话中执行一条命令并返回 stdout 与退出码。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux 与新版 Android 的 POSIX PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"gate": "terminal",
"write": true,
"platforms": [
"win",
"lnx"
"lnx",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -2292,7 +2295,8 @@
"write": true,
"platforms": [
"win",
"lnx"
"lnx",
"apk"
],
"impl": "mfc",
"inputSchema": {