TRAE调用Pencil MCP时没有处理其error消息的hack

在这篇贴子https://forum.trae.cn/t/topic/3167/4中我提到TRAE可能没有处理MCP 的error消息

这是TRAE 的 MCP调用有BUG,似乎TRAE只处理了MCP返回的result类型为success的数据,对于result类型为error的数据,都没有处理。

可以这样验证, 当你调用pencil这个mcp时,你可以打开 面板 》编辑器 》下面的 “输出”,切换到 MCP Servers Host, 这时可以看到所有MCP的调用输出 ,然后你就会发现当pencil mcp返回error时,这里有输出 ,但是,在你贴图所示的batch_design(要展开)中,其响应为空。

这时,TRAE会认为这次的batch_design是成功的,其实是失败的。。。。。这算不算个BUG。
不知有没有什么办法让TRAE在调用MCP时,能像处理success的mcp返回数据一样处理error类型的mcp返回。

今天专门抽了一天时间写了一个mcp-proxy,该mcp工具可以运行指定的mcp,将其输出的error节点转换为result节点,以使TRAE能正常识别,这样在mcp的calltool调用失败时,TRAE可以根据其失败信息自动处理,不用像以前一样失败了还认为是成功,结果造成Pencil工具不能正常工作。

希望TRAE能更新一下,把这个问题解决一下。就不用这么麻烦的hack了。

MCP配置如下

{
  "mcpServers": {
    "pencil": {
      "command": "python.exe",
      "args": [
        "-u",
        "E:\\tmp\\xxxx\\mcp_proxy.py",
        "E:\\Program Files\\Pencil\\resources\\app.asar.unpacked\\out\\mcp-server-windows-x64.exe",
        "--app",
        "desktop"
      ],
      "env": {}
    }
  }
}

mcp_proxy.py 文件如下

import subprocess
import sys
import os
import json

sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')

def run_program():
    if len(sys.argv) < 2:
        print("Usage: python run_program.py <program> [args...]")
        return
    
    program = sys.argv[1]
    args = sys.argv[2:]
    
    try:
        if os.name == 'nt':
            quoted_program = f'"{program}"' if ' ' in program else program
            quoted_args = [f'"{arg}"' if ' ' in arg else arg for arg in args]
            cmd = ' '.join([quoted_program] + quoted_args)
            env = os.environ.copy()
            env['PYTHONIOENCODING'] = 'utf-8'
            process = subprocess.Popen(
                cmd,
                stdin=sys.stdin,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                bufsize=1,
                universal_newlines=True,
                encoding='utf-8',
                errors='replace',
                shell=True,
                env=env
            )
        else:
            cmd = [program] + args
            env = os.environ.copy()
            env['PYTHONIOENCODING'] = 'utf-8'
            process = subprocess.Popen(
                cmd,
                stdin=sys.stdin,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                bufsize=1,
                universal_newlines=True,
                encoding='utf-8',
                errors='replace',
                env=env
            )
        
        import io
        
        def process_output(pipe):
            with open('a.txt', 'a', encoding='utf-8') as f:
                for line in iter(pipe.readline, ''):
                    if line:
                        try:
                            data = json.loads(line)
                            if 'error' in data:
                                error_content = data.pop('error')
                                data['result'] = {'content': [{"type": "text", "text": json.dumps(error_content)}]}
                                output_line = json.dumps(data) + '\n'
                                f.write(output_line)
                                print(output_line, end='')
                            else:
                                f.write(line)
                                print(line, end='')
                        except json.JSONDecodeError:
                            wrapped_output = {"jsonrpc": "2.0", "id": -1, "result": {"message": line.strip()}}
                            output_line = json.dumps(wrapped_output) + '\n'
                            f.write(output_line)
                            print(output_line, end='')
        
        import threading
        stdout_thread = threading.Thread(target=process_output, args=(process.stdout,))
        stderr_thread = threading.Thread(target=process_output, args=(process.stderr,))
        
        stdout_thread.start()
        stderr_thread.start()
        
        process.wait()
        
        stdout_thread.join()
        stderr_thread.join()
        
        return process.returncode
    except Exception as e:
        print(f"Error running program: {e}", file=sys.stderr)
        return 1

if __name__ == "__main__":
    sys.exit(run_program())
1 个赞