From 1d4041541dbace058846ca9b82d48c7ae76d5f83 Mon Sep 17 00:00:00 2001 From: Minidoracat Date: Fri, 6 Jun 2025 20:08:39 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=20MCP=5FWEB=5FPO?= =?UTF-8?q?RT=20=E7=92=B0=E5=A2=83=E8=AE=8A=E6=95=B8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E5=84=AA=E5=8C=96=20Web=20UI=20=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E9=82=8F=E8=BC=AF=EF=BC=8C=E7=A2=BA=E4=BF=9D?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E7=AF=84=E5=9C=8D=E6=9C=89=E6=95=88=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E4=B8=A6=E6=9B=B4=E6=96=B0=E7=9B=B8=E9=97=9C=E6=96=87?= =?UTF-8?q?=E6=AA=94=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.zh-CN.md | 4 ++- README.zh-TW.md | 4 ++- src/mcp_feedback_enhanced/test_web_ui.py | 40 ++++++++++++++++++------ src/mcp_feedback_enhanced/web/main.py | 23 ++++++++++++-- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/README.zh-CN.md b/README.zh-CN.md index e079282..20bdc0d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -106,7 +106,8 @@ uvx mcp-feedback-enhanced@latest test "timeout": 600, "env": { "FORCE_WEB": "true", - "MCP_DEBUG": "false" + "MCP_DEBUG": "false", + "MCP_WEB_PORT": "8765" }, "autoApprove": ["interactive_feedback"] } @@ -134,6 +135,7 @@ uvx mcp-feedback-enhanced@latest test |------|------|-----|------| | `FORCE_WEB` | 强制使用 Web UI | `true`/`false` | `false` | | `MCP_DEBUG` | 调试模式 | `true`/`false` | `false` | +| `MCP_WEB_PORT` | Web UI 端口 | `1024-65535` | `8765` | ### 测试选项 ```bash diff --git a/README.zh-TW.md b/README.zh-TW.md index 6515cab..4320f90 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -106,7 +106,8 @@ uvx mcp-feedback-enhanced@latest test "timeout": 600, "env": { "FORCE_WEB": "true", - "MCP_DEBUG": "false" + "MCP_DEBUG": "false", + "MCP_WEB_PORT": "8765" }, "autoApprove": ["interactive_feedback"] } @@ -134,6 +135,7 @@ uvx mcp-feedback-enhanced@latest test |------|------|-----|------| | `FORCE_WEB` | 強制使用 Web UI | `true`/`false` | `false` | | `MCP_DEBUG` | 調試模式 | `true`/`false` | `false` | +| `MCP_WEB_PORT` | Web UI 端口 | `1024-65535` | `8765` | ### 測試選項 ```bash diff --git a/src/mcp_feedback_enhanced/test_web_ui.py b/src/mcp_feedback_enhanced/test_web_ui.py index 5891ab3..863df70 100644 --- a/src/mcp_feedback_enhanced/test_web_ui.py +++ b/src/mcp_feedback_enhanced/test_web_ui.py @@ -122,17 +122,37 @@ def test_web_ui(keep_running=False): debug_log(t('test.messages.webUiModuleImportFailed', error=str(e))) return False, None - # Find free port + # Check for environment variable port setting + env_port = os.getenv("MCP_WEB_PORT") + if env_port: + try: + custom_port = int(env_port) + if 1024 <= custom_port <= 65535: + debug_log(t('test.messages.foundAvailablePort', port=custom_port)) + debug_log(f"使用環境變數 MCP_WEB_PORT 指定的端口: {custom_port}") + test_port = custom_port + else: + debug_log(f"MCP_WEB_PORT 值無效 ({custom_port}),必須在 1024-65535 範圍內") + # Find free port as fallback + test_port = find_free_port() + debug_log(t('test.messages.foundAvailablePort', port=test_port)) + except ValueError: + debug_log(f"MCP_WEB_PORT 格式錯誤 ({env_port}),必須為數字") + # Find free port as fallback + test_port = find_free_port() + debug_log(t('test.messages.foundAvailablePort', port=test_port)) + else: + # Find free port + try: + test_port = find_free_port() + debug_log(t('test.messages.foundAvailablePort', port=test_port)) + except Exception as e: + debug_log(t('test.messages.findPortFailed', error=str(e))) + return False, None + + # Test manager creation (不傳遞 port 參數,讓 WebUIManager 自己處理環境變數) try: - free_port = find_free_port() - debug_log(t('test.messages.foundAvailablePort', port=free_port)) - except Exception as e: - debug_log(t('test.messages.findPortFailed', error=str(e))) - return False, None - - # Test manager creation - try: - manager = WebUIManager(port=free_port) + manager = WebUIManager() # 讓 WebUIManager 自己處理端口邏輯 debug_log(t('test.messages.webUiManagerCreateSuccess')) except Exception as e: debug_log(t('test.messages.webUiManagerCreateFailed', error=str(e))) diff --git a/src/mcp_feedback_enhanced/web/main.py b/src/mcp_feedback_enhanced/web/main.py index 15266c8..ce85ac3 100644 --- a/src/mcp_feedback_enhanced/web/main.py +++ b/src/mcp_feedback_enhanced/web/main.py @@ -37,8 +37,27 @@ class WebUIManager: def __init__(self, host: str = "127.0.0.1", port: int = None): self.host = host - # 優先使用固定端口 8765,確保 localStorage 的一致性 - self.port = port or find_free_port(preferred_port=8765) + + # 確定偏好端口:環境變數 > 參數 > 預設值 8765 + preferred_port = 8765 + + # 檢查環境變數 MCP_WEB_PORT + env_port = os.getenv("MCP_WEB_PORT") + if env_port: + try: + custom_port = int(env_port) + if 1024 <= custom_port <= 65535: + preferred_port = custom_port + debug_log(f"使用環境變數指定的端口: {preferred_port}") + else: + debug_log(f"MCP_WEB_PORT 值無效 ({custom_port}),必須在 1024-65535 範圍內,使用預設端口 8765") + except ValueError: + debug_log(f"MCP_WEB_PORT 格式錯誤 ({env_port}),必須為數字,使用預設端口 8765") + else: + debug_log(f"未設定 MCP_WEB_PORT 環境變數,使用預設端口 {preferred_port}") + + # 優先使用指定端口,確保 localStorage 的一致性 + self.port = port or find_free_port(preferred_port=preferred_port) self.app = FastAPI(title="MCP Feedback Enhanced") # 重構:使用單一活躍會話而非會話字典