From fb3031aaff48e0c8aef81348d049919bda5b7cdf Mon Sep 17 00:00:00 2001 From: Minidoracat Date: Sat, 31 May 2025 04:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20=E5=A2=9E=E5=8A=A0=20Windows=20?= =?UTF-8?q?=E7=92=B0=E5=A2=83=E4=B8=8B=E7=9A=84=20stdio=20=E4=BA=8C?= =?UTF-8?q?=E9=80=B2=E5=88=B6=E6=A8=A1=E5=BC=8F=E8=A8=AD=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E7=A2=BA=E4=BF=9D=E7=B7=A8=E7=A2=BC=E6=AD=A3=E7=A2=BA=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E4=B8=A6=E5=9C=A8=E9=9D=9E=20Windows=20=E7=B3=BB?= =?UTF-8?q?=E7=B5=B1=E4=B8=AD=E5=BC=B7=E5=88=B6=E4=BD=BF=E7=94=A8=20UTF-8?= =?UTF-8?q?=20=E7=B7=A8=E7=A2=BC=EF=BC=8C=E6=8F=90=E5=8D=87=E8=B7=A8?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=85=BC=E5=AE=B9=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mcp_feedback_enhanced/server.py | 51 ++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/mcp_feedback_enhanced/server.py b/src/mcp_feedback_enhanced/server.py index 33238f8..eac1d64 100644 --- a/src/mcp_feedback_enhanced/server.py +++ b/src/mcp_feedback_enhanced/server.py @@ -19,6 +19,7 @@ import tempfile import asyncio import base64 from typing import Annotated, List +import io from mcp.server.fastmcp import FastMCP from mcp.server.fastmcp.utilities.types import Image as MCPImage @@ -536,7 +537,55 @@ def main(): debug_log(f" 建議介面: {'Web UI' if is_remote_environment() or not can_use_gui() else 'Qt GUI'}") debug_log(" 等待來自 AI 助手的調用...") - mcp.run() + # Windows 特殊處理:設置 stdio 為二進制模式,避免編碼問題 + if sys.platform == 'win32': + debug_log("偵測到 Windows 環境,設置 stdio 二進制模式") + try: + # 設置 stdin/stdout 為二進制模式,避免 Windows 下的編碼問題 + import msvcrt + msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) + msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) + debug_log("Windows stdio 二進制模式設置成功") + + # 重新包裝 stdin/stdout 為 UTF-8 編碼的文本流 + sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8', errors='replace') + sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', errors='replace', newline='') + debug_log("Windows stdio UTF-8 包裝設置成功") + + except Exception as e: + debug_log(f"Windows stdio 設置失敗,使用預設模式: {e}") + else: + # 非 Windows 系統:確保使用 UTF-8 編碼 + try: + if hasattr(sys.stdin, 'reconfigure'): + sys.stdin.reconfigure(encoding='utf-8', errors='replace') + if hasattr(sys.stdout, 'reconfigure'): + sys.stdout.reconfigure(encoding='utf-8', errors='replace') + debug_log("非 Windows 系統 UTF-8 編碼設置成功") + except Exception as e: + debug_log(f"UTF-8 編碼設置失敗: {e}") + + # 確保 stderr 使用 UTF-8 編碼(用於 debug 訊息) + if hasattr(sys.stderr, 'reconfigure'): + try: + sys.stderr.reconfigure(encoding='utf-8', errors='replace') + debug_log("stderr UTF-8 編碼設置成功") + except Exception as e: + debug_log(f"stderr 編碼設置失敗: {e}") + + # 強制 stdout 立即刷新,確保 JSON-RPC 消息及時發送 + sys.stdout.reconfigure(line_buffering=True) if hasattr(sys.stdout, 'reconfigure') else None + + try: + mcp.run() + except KeyboardInterrupt: + debug_log("收到中斷信號,正常退出") + sys.exit(0) + except Exception as e: + debug_log(f"MCP 服務器啟動失敗: {e}") + import traceback + debug_log(f"詳細錯誤: {traceback.format_exc()}") + sys.exit(1) if __name__ == "__main__":