fix(client): MCP execute_command 执行 --help 类命令无输出 - #99
Open
sss12365 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
修复通过 MCP 调用
execute_command("bof --help")(以及任何--help/-h/help形态)始终返回Command executed successfully (no output)的问题。终端中直接执行--help有完整输出。Root Cause
两个原因(均在
client/core/):execute_command捕获不到 Cobra 的 help 文本execute_command→executeCommandWithTaskWait(utils.go:204)用client.Stdout.Range(start, now)截取输出。client.Stdout是NewStdoutWrapper(os.Stdout)环形缓冲 wrapper(external/IoM-go/client/log.go:16),只有写入该 wrapper 的内容才能被.Range()截到。--help走cmd.OutOrStdout(),全仓库没有任何cmd.SetOut(client.Stdout),help 直接打到原始os.Stdout,绕过 wrapper →.Range()截到空串 → MCP 返回 "no output"。module list/whoami走 console 日志路径(logs.Log.SetOutput(client.Stdout),console.go:247),所以能被截到。MCP server 的误导性 Tip
mcp.go:448在每次search_commands返回末尾硬编码Tip: Use execute_command("<command> --help") for detailed usage.,即 MCP 主动指引 AI 使用一条必然返回空的路径。
Fix
在
execute_commandhandler 中短路 help 请求:识别--help/-h/help,用
cmd.SetOut(&buf)+cmd.Help()将 help 文本渲染进缓冲后返回,绕开 stdout 捕获通道。唯一改动文件:
client/core/mcp.go(+135 / −1,其中 −1 为replimport 的 goimports 位置调整):bytes、github.com/chainreactors/IoM-go/client(RemoveANSI)、github.com/kballard/go-shellquoteisHelpRequest(识别 help 请求)、renderCobraHelp(缓冲捕获 help 并去 ANSI,空则 fallback 到既有generateCommandDoc)、menuRoot、activeMenuRoot、renderCommandHelp(跨 implant/client 两个菜单解析目标命令;支持 MAL 插件:命名空间拆分;与命令执行共享commandExecMu串行化)registerExecuteCommandTool中isHelpRequest命中且渲染成功时直接返回 help 文本解析失败(help 为空)时自然落回原
executeCommand路径,对非 help 命令零影响。Test Plan
execute_command("bof --help")返回完整 help 文本execute_command("help")返回当前活动菜单的命令树uac-bypass:elevatedcom --help)可正常解析whoami、module list等)行为与改动前一致