11 Commits
Author SHA1 Message Date
etwen 6a85a48e6d merge: PDU MCP server (v0.3.0) 2026-06-08 10:12:24 +08:00
etwen 02f4ac49e8 feat(pdu): add PDU MCP server for AI-driven SNMP power control
New ETTerms.PduMcp stdio MCP server lets AI agents (Kiro/Claude CLI)
control SNMP PDU outlets directly. Unlike serial, SNMP is non-exclusive
so it talks to the PDU directly without bridging through the GUI.

Tools: pdu_connect / pdu_list / pdu_set_port / pdu_get_port / pdu_status
/ pdu_power_cycle / pdu_disconnect.

- McpRegistrar now registers both etterms-serial and etterms-pdu in one click
- Settings -> AI MCP shows both server paths
- publish target (PublishMcpServers) bundles both MCP servers
- bump version to 0.3.0, update About changelog, ARCHITECTURE.md, CLAUDE.md
2026-06-08 10:12:15 +08:00
etwen 89ec175669 docs: credit KKTerm (by ryantsai, MIT) in Acknowledgments 2026-06-05 13:54:29 +08:00
etwen 776509cc6c merge: high-DPI scaling & layout fixes (v0.2.2) 2026-06-05 13:33:33 +08:00
etwen a66d2a19e4 fix(ui): high-DPI scaling and layout clipping (v0.2.2)
Switch to AutoScaleMode.Dpi (PerMonitorV2) so control sizes scale by the exact DPI ratio, matching point-font rendering and eliminating text/button clipping at 125%/150%. Make toolbar, settings, sidebar buttons and About labels AutoSize; align left tab bar with workspace toolbar (both 40px); About cards now auto-grow and the left column scrolls. ActivityRail cell size is DPI-aware. No version bump.
2026-06-05 13:33:25 +08:00
etwen 68b727f38a merge: terminal resize/Shift+Enter bugfixes (v0.2.2) 2026-06-05 11:02:45 +08:00
etwen e5c10afd94 fix(terminal): stop minimize/switch from freezing ConPTY; add Shift+Enter newline (v0.2.2)
OnSizeChanged now ignores degenerate sizes (minimized / smaller than one cell), so ConPTY no longer receives a 1x1 size that collapsed full-screen TUIs like Kiro CLI. ScreenBuffer.Resize reflows both the live and saved alt-screen buffers instead of discarding the main-screen backup. TerminalInput.Map sends LF for Shift+Enter (CR for plain Enter) so the shell inserts a newline for multi-line input. Bump version to 0.2.2 and add About changelog entry.
2026-06-05 11:02:39 +08:00
etwen fc97e04846 build: add self-contained portable publish variant (v0.2.1)
PublishSerialMcp target now inherits the GUI's SelfContained setting, so a --self-contained true publish also produces a runtime-free ETTerms.SerialMcp. Document both publish variants in ARCHITECTURE.md / CLAUDE.md: framework-dependent ETTerms_v{Version}\ and portable ETTerms_v{Version}_portable\ (no .NET runtime install needed).
2026-06-05 09:33:24 +08:00
etwen 2ac260489f merge: AI MCP one-click setup (v0.2.1) 2026-06-05 08:58:20 +08:00
etwen 7ba3fadb87 feat: AI MCP one-click setup in Settings (v0.2.1)
Add Settings -> AI MCP tab with McpRegistrar to one-click register/remove the Serial MCP server into Claude Code (~/.claude.json) and Kiro (~/.kiro/settings/mcp.json); read-modify-write preserves other servers, atomic write-back. Each card shows a CLI verify command.

Add PublishSerialMcp MSBuild target so a single GUI publish auto-bundles ETTerms.SerialMcp into the \ETTerms.SerialMcp\ subfolder, aligning with McpRegistrar.ResolveServerExe(). Bump version to 0.2.1. Update ARCHITECTURE/CLAUDE/README/README.zh-TW and About changelog.
2026-06-05 08:58:02 +08:00
etwenandClaude Opus 4.8 10ec900386 docs: add v0.2.0 changelog & publish convention; drop "Version" label from title
- About: add v0.2.0 "Beta Version Release" changelog entry.
- MainForm: title bar now shows "ETTerms  vX.Y.Z" (drop the word "Version").
- ARCHITECTURE.md + CLAUDE.md: document the publish convention --
  output to src\ETTerms\Publish\ETTerms_v{Version}\, main exe renamed to
  "ETTerms v{Version}.exe", ETTerms.SerialMcp published into a subfolder.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 21:39:38 +08:00
22 changed files with 1012 additions and 113 deletions
+106 -5
View File
@@ -32,7 +32,7 @@ ETTerms 是一個給工程師 / 韌體 / 硬體驗證人員用的**單一視窗
| 祕密儲存 | **Windows Credential Manager**DPAPI / CredMan | 連線密碼、SSH key passphrase,不落地明碼 | | 祕密儲存 | **Windows Credential Manager**DPAPI / CredMan | 連線密碼、SSH key passphrase,不落地明碼 |
| PDU 控制(選用) | **SnmpSharpNet** | 沿用 MyTeraTerm PDU 控制(`pductrl` / `pduconnect` | | PDU 控制(選用) | **SnmpSharpNet** | 沿用 MyTeraTerm PDU 控制(`pductrl` / `pduconnect` |
| 日誌 | 自製 **AppLogger**(從 MyTeraTerm 移植) | 檔案 + Debug 雙輸出 | | 日誌 | 自製 **AppLogger**(從 MyTeraTerm 移植) | 檔案 + Debug 雙輸出 |
| AI / MCP 整合(選用) | **stdio MCP server**(官方 C# SDK `ModelContextProtocol` | 獨立行程,但不自己開 port——經本機 named pipe 橋接 GUI 持有的 serial session,把收發暴露成 AI 可呼叫工具(Kiro CLI / Claude CLI),見 [AI / MCP Integration](#ai--mcp-integrationserial-mcp-server) | | AI / MCP 整合(選用) | **stdio MCP server**(官方 C# SDK `ModelContextProtocol` | 兩個獨立 server`ETTerms.SerialMcp`不自己開 port經本機 named pipe 橋接 GUI 持有的 serial session)與 `ETTerms.PduMcp`(直接打 SNMP 控制 PDU 插座,不需 GUI);把收發 / 電源控制暴露成 AI 可呼叫工具(Kiro CLI / Claude CLI),見 [AI / MCP Integration](#ai--mcp-integrationserial-mcp--pdu-mcp-server) |
| 打包 | `dotnet publish` + (選用)Inno Setup / MSIX | 單機安裝,current-user | | 打包 | `dotnet publish` + (選用)Inno Setup / MSIX | 單機安裝,current-user |
> **與舊版 MyTeraTerm 的關鍵差異:** 舊版是把真正的 `ttermpro.exe`TeraTerm)嵌進 Panel,靠 **com0com 虛擬 COM 對**攔截 serial 來跑腳本。ETTerms 改走**全原生**SSH.NET 做 SSH、`System.IO.Ports` 做 serial、自繪 VT100 控制項做終端機畫面,**不再依賴外部 TeraTerm exe,也不再需要 com0com**。腳本引擎從「驅動 com0com bridge」改成「驅動原生 `ISessionChannel`」。 > **與舊版 MyTeraTerm 的關鍵差異:** 舊版是把真正的 `ttermpro.exe`TeraTerm)嵌進 Panel,靠 **com0com 虛擬 COM 對**攔截 serial 來跑腳本。ETTerms 改走**全原生**SSH.NET 做 SSH、`System.IO.Ports` 做 serial、自繪 VT100 控制項做終端機畫面,**不再依賴外部 TeraTerm exe,也不再需要 com0com**。腳本引擎從「驅動 com0com bridge」改成「驅動原生 `ISessionChannel`」。
@@ -160,6 +160,7 @@ ETTerms/
└── Infrastructure/ └── Infrastructure/
├── AppLogger.cs # 日誌 (port 自 MyTeraTerm) ├── AppLogger.cs # 日誌 (port 自 MyTeraTerm)
├── AppSettings.cs # 使用者偏好 (JSON, %LocalAppData%\ETTerms\settings.json) ├── AppSettings.cs # 使用者偏好 (JSON, %LocalAppData%\ETTerms\settings.json)
├── McpRegistrar.cs # ✅ 一鍵把 Serial MCP 註冊/移除到 Claude Code / Kiro 設定檔(Settings → AI MCP
└── NativeTheme.cs # 深色標題列 (DWM) └── NativeTheme.cs # 深色標題列 (DWM)
└── ETTerms.SerialMcp/ # ✅ Serial MCP serverstdio)——不自己開 port,經 named pipe 橋接 GUI └── ETTerms.SerialMcp/ # ✅ Serial MCP serverstdio)——不自己開 port,經 named pipe 橋接 GUI
@@ -355,7 +356,16 @@ ScriptRunner.RunAsync(scriptText, activeChannel)
--- ---
## AI / MCP IntegrationSerial MCP Server ## AI / MCP IntegrationSerial MCP + PDU MCP Server
ETTerms 提供**兩個獨立的 stdio MCP server**給 AI agentKiro CLI / Claude CLI):
- **`ETTerms.SerialMcp`** — 收發 serial。COM port 獨佔,故由 GUI 唯一持有、MCP 經本機 named pipe 橋接(見下方)。
- **`ETTerms.PduMcp`** — 控制 SNMP PDU 電源插座。SNMP(UDP) 非獨佔,故 MCP **直接打 SNMP**,不需 GUI 在跑、也不經 pipe。
兩者都能用 GUI **Settings → AI MCP** 一鍵 Setup`McpRegistrar` 會同時註冊 `etterms-serial``etterms-pdu`)。
### Serial MCP Server
> 讓 **Kiro CLI / Claude CLI** 等 AI agent 收發 serial**且使用者能在 ETTerms GUI 即時看到 AI 的每筆收發**。 > 讓 **Kiro CLI / Claude CLI** 等 AI agent 收發 serial**且使用者能在 ETTerms GUI 即時看到 AI 的每筆收發**。
> >
@@ -411,7 +421,11 @@ ETTerms GUI(單一行程,唯一開 COM3 的人)
- **可視性:** AI 的 TX 在 GUI 以 `[AI]` 標色,與使用者手打的輸入區分;RX 兩邊同源。 - **可視性:** AI 的 TX 在 GUI 以 `[AI]` 標色,與使用者手打的輸入區分;RX 兩邊同源。
- **安全:** 本機、無雲、不碰 credential;pipe 僅限本機行程,只搬 serial bytes。 - **安全:** 本機、無雲、不碰 credential;pipe 僅限本機行程,只搬 serial bytes。
### 註冊(Kiro CLI ### 註冊(兩種方式
**方式 A — 一鍵設定(推薦,v0.2.1):** GUI **Settings → AI MCP** 分頁,對 Claude Code / Kiro 各按 **Setup** 即可。`McpRegistrar` 以 read-modify-write 把 `etterms-serial` 寫進該 CLI 的使用者層級設定檔(Claude Code:`~/.claude.json`Kiro`~/.kiro/settings/mcp.json`),保留檔內其他既有 MCP server,原子寫回避免壞檔。註冊的執行檔路徑指向 `<ETTerms.exe>\ETTerms.SerialMcp\ETTerms.SerialMcp.exe`(與 publish 慣例對齊,必定存在);卡片同時顯示 CLI 驗證指令,按 **Remove** 可移除。
**方式 B — 手動 CLI**
```powershell ```powershell
kiro-cli mcp add --name serial --command dotnet ` kiro-cli mcp add --name serial --command dotnet `
@@ -420,6 +434,33 @@ kiro-cli mcp add --name serial --command dotnet `
或寫進 agent.json 的 `mcpServers`Claude CLI 則用其對應的 `mcpServers` 設定。**使用前提:先在 ETTerms GUI 開好要操作的 serial 連線**,AI 才能 `serial_attach` 上去。註冊後即可對 AI 說「列出目前 serial session → 接上 COM3 → 送指令看回應」。 或寫進 agent.json 的 `mcpServers`Claude CLI 則用其對應的 `mcpServers` 設定。**使用前提:先在 ETTerms GUI 開好要操作的 serial 連線**,AI 才能 `serial_attach` 上去。註冊後即可對 AI 說「列出目前 serial session → 接上 COM3 → 送指令看回應」。
### PDU MCP Serverv0.3.0
> 讓 AI agent 直接控制 SNMP PDU 的電源插座,**典型用途:測試中自動 power-cycle DUT**。
**關鍵設計:直接打 SNMP,不經 GUI 橋接。** 與 serial 不同,PDU 走 SNMPUDP)**非獨佔**——多個行程可同時對同一台 PDU 下命令。因此 `ETTerms.PduMcp` 不需要像 serial 那樣繞 GUI 的 named pipe,而是內含一份精簡版 `PduController`OID 邏輯複製自 GUI 的 `Scripting/Pdu/PduController`,診斷改走 stderr 以免污染 stdio JSON-RPC)直接與 PDU 對話。**好處:GUI 不必開著,AI 也能控制 PDU;最少程式碼、最穩。**
```
Kiro/Claude CLI ── 啟動子行程 ETTerms.PduMcpstdio / JSON-RPC
└─ SnmpSharpNet ──(SNMP/UDP 161)──► PDUiPoMan II/III
```
連線狀態(device IP → controller)以行程內單例 `PduRegistry` 保存,跨工具呼叫保留,直到 `pdu_disconnect` 或行程結束。
**暴露的工具:**
| 工具 | 參數 | 說明 |
|------|------|------|
| `pdu_connect` | ip | 以 SNMP 連線並驗證 PDU 回應,成功回傳 model name;控制前必須先呼叫 |
| `pdu_list` | — | 列出本 session 已連線的 PDU(依 IP |
| `pdu_set_port` | ip, port, on | 將某插座開(on=true)/關(off=false) |
| `pdu_get_port` | ip, port | 讀單一插座的狀態 / 電流(mA) / 功率(W) |
| `pdu_status` | ip | 讀全部 12 個插座的狀態 / 電流 / 功率 |
| `pdu_power_cycle` | ip, port, offSeconds? | 關 → 等 offSeconds → 開(重啟 DUT |
| `pdu_disconnect` | ip | 解除本 session 的 PDU 連線(不改變插座狀態) |
> 所有工具回傳統一的 `{ "ok": bool, "result"/"error": ... }` JSON。SNMP community 目前沿用 GUI 版的 `"private"`。
--- ---
## Key Constraints & Business Rules ## Key Constraints & Business Rules
@@ -480,13 +521,73 @@ cd F:\10_AI\ETTerms
dotnet build dotnet build
dotnet run --project src\ETTerms\ETTerms.csproj dotnet run --project src\ETTerms\ETTerms.csproj
# 5. 打包(後期 Phase # 5. 打包(見下方「Publish / 打包慣例」
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained false dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained false `
-o src\ETTerms\Publish\ETTerms_v<Version>
# 後續可用 Inno Setup / MSIX 做安裝程式 # 後續可用 Inno Setup / MSIX 做安裝程式
``` ```
--- ---
## Publish / 打包慣例
> 每次發 Release 都依此規則產出,方便辨識版本與一併攜帶 MCP server。
**輸出位置(固定):** `src\ETTerms\Publish\ETTerms_v{Version}\`
- `{Version}` 取自 `src\ETTerms\ETTerms.csproj``<Version>`(例:`0.2.0`)。
- 資料夾命名:`ETTerms_v{Version}`(前綴 `v`,例:`ETTerms_v0.2.0`)。
- `Publish/` 已 gitignore`publish/`),**不入 git**。
**內容結構:**
```
ETTerms_v0.3.0\
├── ETTerms v0.3.0.exe # 主程式 apphost,改名為「ETTerms v{Version}.exe」
├── ETTerms.dll + 各相依 dll # SSH.NET / SQLite / SnmpSharpNet / System.IO.Ports …
├── ETTerms.SerialMcp\ # Serial MCP server,獨立發佈到子資料夾(相依 dll 與 GUI 隔離)
│ ├── ETTerms.SerialMcp.exe
│ └── ETTerms.SerialMcp.dll + 相依
└── ETTerms.PduMcp\ # PDU MCP serverv0.3.0),同樣獨立發佈到子資料夾
├── ETTerms.PduMcp.exe
└── ETTerms.PduMcp.dll + 相依(含 SnmpSharpNet
```
**規則:**
1. **GUI publish 會自動帶上兩個 MCP server**`ETTerms.csproj``PublishMcpServers` target`AfterTargets="Publish"`),會把 `ETTerms.SerialMcp``ETTerms.PduMcp` 一併發佈到各自的 `<publish>\<server>\` **子資料夾**(與 GUI 相依 dll 隔離)。因此**只要發佈 GUI 一個指令**即可,不必再單獨發 MCP。
- 對齊 `McpRegistrar.ResolveServerExe()`:它解析的 `<ETTerms.exe>\<server>\<server>.exe` 因此**必定存在**,AI MCP 一鍵設定寫進去的路徑才不會落空。
- MCP 子發佈會**跟隨 GUI 的 `SelfContained` 設定**target 內以 `$(SelfContained)` 傳入):框架相依版的 MCP 也框架相依;portable 版的 MCP 也免 runtime。
2. **主 exe 改名**`dotnet publish` 產生的 `ETTerms.exe` 重新命名為 **`ETTerms v{Version}.exe`**。
- 可安全改名:.NET apphost 內部記錄要載入的 `ETTerms.dll`,**不靠自身檔名**,改名後仍正常啟動。
### 兩種發佈版本(兩個都要產出)
| 版本 | 資料夾 | 指令旗標 | 需 .NET Runtime | 用途 |
|------|--------|----------|-------------------|------|
| **A. 框架相依(預設)** | `ETTerms_v{Version}\` | `--self-contained false` | ✅ 需先裝 .NET 8 Desktop Runtime | 體積小;給已具備 runtime 的環境 |
| **B. Portable(免安裝)** | `ETTerms_v{Version}_portable\` | `--self-contained true` | ❌ 不需要,runtime 已內含 | 體積大(約 240MB);給受 MIS 管控、不便裝 runtime 的環境,解壓即用、免系統管理員權限 |
> ⚠️ **不要開 trimming**`PublishTrimmed`):WinForms 大量用反射,trim 後易在執行時出錯。
**PowerShell 範例(一次產出兩種):**
```powershell
$ver = ([regex]::Match((Get-Content src\ETTerms\ETTerms.csproj -Raw), '<Version>([^<]+)</Version>')).Groups[1].Value
# A. 框架相依版 → ETTerms_v{Version}\
$root = "src\ETTerms\Publish\ETTerms_v$ver"
if (Test-Path $root) { Remove-Item $root -Recurse -Force }
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained false -o $root
Rename-Item (Join-Path $root "ETTerms.exe") "ETTerms v$ver.exe"
# B. Portable 免安裝版 → ETTerms_v{Version}_portable\runtime 已內含;MCP 也跟著 self-contained
$proot = "src\ETTerms\Publish\ETTerms_v${ver}_portable"
if (Test-Path $proot) { Remove-Item $proot -Recurse -Force }
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained true -o $proot
Rename-Item (Join-Path $proot "ETTerms.exe") "ETTerms v$ver.exe"
```
> 兩版的 `ETTerms.SerialMcp\` 與 `ETTerms.PduMcp\` 子資料夾都由 `PublishMcpServers` target 自動產生;portable 版的 MCP 也是 self-contained,故 AI MCP 功能在無 runtime 環境同樣可用。
---
## Development Phases ## Development Phases
> 依使用者要求「**前面先把 GUI 做出來,後面再慢慢補功能**」排序: > 依使用者要求「**前面先把 GUI 做出來,後面再慢慢補功能**」排序:
+23 -3
View File
@@ -10,6 +10,10 @@ ETTerms 是一個 **C# .NET 8 WinForms** 的原生 Windows 終端機工作台,
**進度:** Phase 15 ✅、Phase 6 ✅(TTL 引擎 + Group 同步,SSH 待驗收)、Phase 7 ✅(Settings/About)、Phase 8 ✅(PDU + Shell/ConPTY + SFTP + Settings 擴充)、Phase 9 ✅(Serial MCP server**GUI 持有 COM portMCP 經本機 named pipe 橋接**,AI 收發的資料即時以 `[AI]` 標色顯示在 GUI)。打包待指示。 **進度:** Phase 15 ✅、Phase 6 ✅(TTL 引擎 + Group 同步,SSH 待驗收)、Phase 7 ✅(Settings/About)、Phase 8 ✅(PDU + Shell/ConPTY + SFTP + Settings 擴充)、Phase 9 ✅(Serial MCP server**GUI 持有 COM portMCP 經本機 named pipe 橋接**,AI 收發的資料即時以 `[AI]` 標色顯示在 GUI)。打包待指示。
**v0.3.0** 新增 **`ETTerms.PduMcp`**stdio MCP server):讓 AI agent 直接控制 SNMP PDU 插座。與 serial 不同,PDU 走 SNMP(UDP) **非獨佔**,故 PduMcp **直接打 SNMP、不經 GUI 橋接**(內含精簡版 `PduController`OID 邏輯複製自 GUI 版,log 走 stderr),GUI 不開著也能用。工具:`pdu_connect` / `pdu_list` / `pdu_set_port` / `pdu_get_port` / `pdu_status` / `pdu_power_cycle` / `pdu_disconnect`,回傳統一 `{ok, result/error}` JSON;連線狀態以行程內單例 `PduRegistry`IP→controller)保存。`McpRegistrar` 改為多 server**Settings → AI MCP 一鍵同時註冊 `etterms-serial``etterms-pdu`**`ETTerms.csproj` 的 publish target 更名 `PublishMcpServers`GUI publish 會把兩個 MCP 各自帶到 `\ETTerms.SerialMcp\``\ETTerms.PduMcp\` 子資料夾。
**v0.2.1** 新增 GUI **Settings → AI MCP** 分頁(`McpRegistrar`):對 Claude Code`~/.claude.json`)與 Kiro`~/.kiro/settings/mcp.json`**一鍵 Setup / Remove** 註冊 `etterms-serial` MCP serverread-modify-write 保留檔內其他設定、原子寫回;卡片附 CLI 驗證指令。`ETTerms.csproj` 加 publish target`AfterTargets=Publish`),GUI publish 會自動把 MCP server 帶到子資料夾,與 `McpRegistrar.ResolveServerExe()` 解析路徑對齊。
**v0.2.0** Phase 9 完成 — `ETTerms.SerialMcp`stdio MCP server+ GUI `SerialBridgeServer`named pipe `\\.\pipe\etterms-serial`)上線,提供 `serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach` 五個工具,AI 的 TX 在 GUI 以 `[AI]` 標色即時 echo;視窗 / 工作列 / About 改用 Choco 圖示,標題列顯示版本號。見 [docs/serial-mcp-guide.md](docs/serial-mcp-guide.md)。 **v0.2.0** Phase 9 完成 — `ETTerms.SerialMcp`stdio MCP server+ GUI `SerialBridgeServer`named pipe `\\.\pipe\etterms-serial`)上線,提供 `serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach` 五個工具,AI 的 TX 在 GUI 以 `[AI]` 標色即時 echo;視窗 / 工作列 / About 改用 Choco 圖示,標題列顯示版本號。見 [docs/serial-mcp-guide.md](docs/serial-mcp-guide.md)。
**v0.1.2** 新增 `sprintf2`TeraTerm 相容 C printf 格式化);`wait` 改為命中關鍵字後須等裝置安靜(`SettleMs` 預設 300ms)才接受並取「最後一次」出現,排除輸出中途的指令回顯(避免腳本搶跑)。 **v0.1.2** 新增 `sprintf2`TeraTerm 相容 C printf 格式化);`wait` 改為命中關鍵字後須等裝置安靜(`SettleMs` 預設 300ms)才接受並取「最後一次」出現,排除輸出中途的指令回顯(避免腳本搶跑)。
@@ -25,7 +29,7 @@ ETTerms 是一個 **C# .NET 8 WinForms** 的原生 Windows 終端機工作台,
- **連線儲存:** SQLite`Microsoft.Data.Sqlite` - **連線儲存:** SQLite`Microsoft.Data.Sqlite`
- **密碼儲存:** Windows Credential Manager(不落地明碼) - **密碼儲存:** Windows Credential Manager(不落地明碼)
- **PDU** SnmpSharpNetiPoMan II/III via SNMP - **PDU** SnmpSharpNetiPoMan II/III via SNMP
- **AI / MCP(選用):** stdio MCP server`ETTerms.SerialMcp`官方 C# SDK `ModelContextProtocol`)。**不自己開 COM port**而是經本機 named pipe 接上 GUI 持有的 serial session把 serial 收發暴露給 Kiro CLI / Claude CLIAI 的 TX/RX 同步顯示在 GUI - **AI / MCP(選用):** 兩個 stdio MCP server(官方 C# SDK `ModelContextProtocol`)。`ETTerms.SerialMcp`**不自己開 COM port**,經本機 named pipe 接上 GUI 持有的 serial sessionAI 的 TX/RX 同步顯示在 GUI。`ETTerms.PduMcp`v0.3.0):**直接打 SNMP** 控制 PDU 插座,非獨佔故不需 GUI 在跑。皆暴露給 Kiro CLI / Claude CLI
- **設定持久化:** JSON → `%LocalAppData%\ETTerms\settings.json` - **設定持久化:** JSON → `%LocalAppData%\ETTerms\settings.json`
## 常用指令 ## 常用指令
@@ -38,16 +42,31 @@ dotnet run --project src\ETTerms\ETTerms.csproj
# 加套件 # 加套件
dotnet add src\ETTerms package SSH.NET dotnet add src\ETTerms package SSH.NET
# 打包 # 打包(見「Publish / 打包慣例」)—— 兩種版本都產出,輸出到 src\ETTerms\Publish\
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained false # GUI publish 會「自動」把 ETTerms.SerialMcp 與 ETTerms.PduMcp 一併發到各自的子資料夾
# ETTerms.csproj 的 PublishMcpServers targetAfterTargets=Publish),且 MCP 跟隨 GUI 的 self-contained 設定。
$ver = ([regex]::Match((Get-Content src\ETTerms\ETTerms.csproj -Raw), '<Version>([^<]+)</Version>')).Groups[1].Value
# A. 框架相依版(需目標機已裝 .NET 8 Desktop Runtime)→ ETTerms_v{Version}\
$root = "src\ETTerms\Publish\ETTerms_v$ver"
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained false -o $root
Rename-Item (Join-Path $root "ETTerms.exe") "ETTerms v$ver.exe"
# B. Portable 免安裝版(runtime 內含,免裝、免管理員)→ ETTerms_v{Version}_portable\
$proot = "src\ETTerms\Publish\ETTerms_v${ver}_portable"
dotnet publish src\ETTerms\ETTerms.csproj -c Release -r win-x64 --self-contained true -o $proot
Rename-Item (Join-Path $proot "ETTerms.exe") "ETTerms v$ver.exe"
# 註冊 Serial MCP server(給 AI agent 操作 serial # 註冊 Serial MCP server(給 AI agent 操作 serial
# 推薦:GUI Settings → AI MCP 分頁,對 Claude Code / Kiro 按 Setup 一鍵註冊(McpRegistrar)。
# 或手動 CLI
kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj" kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj"
``` ```
## 開發慣例 ## 開發慣例
- **命名:** PascalCase 類別 / 方法,`_camelCase` 私有欄位;檔名 = 類別名。 - **命名:** PascalCase 類別 / 方法,`_camelCase` 私有欄位;檔名 = 類別名。
- **Publish / 打包:** 輸出到 `src\ETTerms\Publish\`;主 exe 改名為 `ETTerms v{Version}.exe``ETTerms.SerialMcp``ETTerms.PduMcp` 一併發到其下 `ETTerms.SerialMcp\``ETTerms.PduMcp\` 子資料夾(且跟隨 GUI 的 self-contained 設定)。**兩種版本都產出**:框架相依 `ETTerms_v{Version}\``--self-contained false`,需裝 .NET 8 Desktop Runtime)+ portable 免安裝 `ETTerms_v{Version}_portable\``--self-contained true`runtime 內含)。不要開 trimmingWinForms 反射)。詳見 [ARCHITECTURE.md](ARCHITECTURE.md#publish--打包慣例)。
- **分層:** UI`App/`)只認 `ISessionChannel` 抽象,不直接相依 SSH.NET / SerialPort。 - **分層:** UI`App/`)只認 `ISessionChannel` 抽象,不直接相依 SSH.NET / SerialPort。
- **執行緒:** channel I/O 在背景;所有 UI 更新一律 `Control.Invoke` 回 UI thread。 - **執行緒:** channel I/O 在背景;所有 UI 更新一律 `Control.Invoke` 回 UI thread。
- **commit** 走 Conventional Commits`feat:` / `fix:` / `refactor:` …)。 - **commit** 走 Conventional Commits`feat:` / `fix:` / `refactor:` …)。
@@ -68,5 +87,6 @@ kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerm
- **`src/ETTerms/`** — 主應用程式(WinForms 視窗外殼 + 連線 / 終端機 / 腳本引擎)。 - **`src/ETTerms/`** — 主應用程式(WinForms 視窗外殼 + 連線 / 終端機 / 腳本引擎)。
- **`src/ETTerms.SerialMcp/`** — ✅ stdio MCP server(給 AI agent 收發 serial)。獨立行程,但**不直接開 COM port**:經本機 named pipe 連到 GUI 的 `SerialBridgeServer`,由 GUI 代為讀寫實體 portnet8.0 console + `ModelContextProtocol` SDK。 - **`src/ETTerms.SerialMcp/`** — ✅ stdio MCP server(給 AI agent 收發 serial)。獨立行程,但**不直接開 COM port**:經本機 named pipe 連到 GUI 的 `SerialBridgeServer`,由 GUI 代為讀寫實體 portnet8.0 console + `ModelContextProtocol` SDK。
- **`src/ETTerms.PduMcp/`** — ✅ stdio MCP server(給 AI agent 控制 SNMP PDUv0.3.0)。獨立行程,**直接打 SNMP**(內含精簡版 `PduController`),不經 GUI、GUI 不開著也能用;net8.0 console + `ModelContextProtocol` + `SnmpSharpNet`
- **`For_AI/`** — AI 協作素材與**參考專案**(`KKTerm-main` UI 參考、`MyTeraTerm` Script 參考)。整個資料夾 gitignored,僅供開發對照。 - **`For_AI/`** — AI 協作素材與**參考專案**(`KKTerm-main` UI 參考、`MyTeraTerm` Script 參考)。整個資料夾 gitignored,僅供開發對照。
- 本專案**無 `secret/` 資料夾**:沒有伺服端祕密 / DB 密碼 / compile-time secret,連線密碼一律走 Windows Credential Manager。 - 本專案**無 `secret/` 資料夾**:沒有伺服端祕密 / DB 密碼 / compile-time secret,連線密碼一律走 Windows Credential Manager。
+1
View File
@@ -2,5 +2,6 @@
<Folder Name="/src/"> <Folder Name="/src/">
<Project Path="src/ETTerms/ETTerms.csproj" /> <Project Path="src/ETTerms/ETTerms.csproj" />
<Project Path="src/ETTerms.SerialMcp/ETTerms.SerialMcp.csproj" /> <Project Path="src/ETTerms.SerialMcp/ETTerms.SerialMcp.csproj" />
<Project Path="src/ETTerms.PduMcp/ETTerms.PduMcp.csproj" />
</Folder> </Folder>
</Solution> </Solution>
+25 -13
View File
@@ -4,7 +4,7 @@
> A native Windows terminal workspace (C# .NET 8 WinForms) — **SSH**, **Serial Port**, and **local Shell (ConPTY)** in one window, with a **TTL scripting engine** ported from MyTeraTerm for automation, plus an optional **Serial MCP server** that lets AI agents (Kiro CLI / Claude CLI) drive the serial port directly. Standalone, no cloud, no login. > A native Windows terminal workspace (C# .NET 8 WinForms) — **SSH**, **Serial Port**, and **local Shell (ConPTY)** in one window, with a **TTL scripting engine** ported from MyTeraTerm for automation, plus an optional **Serial MCP server** that lets AI agents (Kiro CLI / Claude CLI) drive the serial port directly. Standalone, no cloud, no login.
![version](https://img.shields.io/badge/version-0.1.0-blue.svg) ![platform](https://img.shields.io/badge/platform-Windows-0078D6.svg?logo=windows&logoColor=white) ![.NET](https://img.shields.io/badge/.NET-8.0-512BD4.svg?logo=dotnet&logoColor=white) ![UI](https://img.shields.io/badge/UI-WinForms-5C2D91.svg) ![SSH](https://img.shields.io/badge/SSH-SSH.NET-success.svg) ![Serial](https://img.shields.io/badge/Serial-System.IO.Ports-success.svg) ![status](https://img.shields.io/badge/status-WIP-orange.svg) ![license](https://img.shields.io/badge/license-MIT-green.svg) ![version](https://img.shields.io/badge/version-0.2.1-blue.svg) ![platform](https://img.shields.io/badge/platform-Windows-0078D6.svg?logo=windows&logoColor=white) ![.NET](https://img.shields.io/badge/.NET-8.0-512BD4.svg?logo=dotnet&logoColor=white) ![UI](https://img.shields.io/badge/UI-WinForms-5C2D91.svg) ![SSH](https://img.shields.io/badge/SSH-SSH.NET-success.svg) ![Serial](https://img.shields.io/badge/Serial-System.IO.Ports-success.svg) ![status](https://img.shields.io/badge/status-beta-yellow.svg) ![license](https://img.shields.io/badge/license-MIT-green.svg)
--- ---
@@ -65,10 +65,11 @@
- Control PDU outlets over SNMP (`SnmpSharpNet`) to power-cycle devices during tests - Control PDU outlets over SNMP (`SnmpSharpNet`) to power-cycle devices during tests
- Script commands: `pduconnect` / `pductrl` - Script commands: `pduconnect` / `pductrl`
- 🤖 **AI / MCP integration (optional, planned)** - 🤖 **AI / MCP integration (optional)**
- The **GUI owns the COM port**; a standalone stdio **Serial MCP server** bridges to it over a local named pipe for **Kiro CLI / Claude CLI** - The **GUI owns the COM port**; a standalone stdio **Serial MCP server** bridges to it over a local named pipe for **Kiro CLI / Claude CLI**
- Tools: `serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach` - Tools: `serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach`
- AI's serial TX/RX shows live in the GUI tagged `[AI]` — open the port in the GUI first, then let the AI attach - AI's serial TX/RX shows live in the GUI tagged `[AI]` — open the port in the GUI first, then let the AI attach
- **One-click setup** in **Settings → AI MCP**: register the server into Claude Code / Kiro with a single button
- 📊 **Session RX logging** - 📊 **Session RX logging**
- `logopen` / `logwrite` / `logclose` write session output to file - `logopen` / `logwrite` / `logclose` write session output to file
@@ -271,7 +272,7 @@ wait 'login: '
## 🤖 AI / MCP Integration ## 🤖 AI / MCP Integration
> 🔜 **Planned (Phase 9).** Let AI agents (**Kiro CLI / Claude CLI**) send/receive on a serial port **while you watch it live in the ETTerms GUI**. > Let AI agents (**Kiro CLI / Claude CLI**) send/receive on a serial port **while you watch it live in the ETTerms GUI**.
**Key design: the GUI owns the COM port; the MCP server never opens it.** A COM port can be opened by only one process at a time, so rather than the MCP server grabbing it, the GUI is the sole owner and runs a local **named pipe server** (`SerialBridgeServer`). The standalone `ETTerms.SerialMcp` (launched by Kiro/Claude CLI) is a thin client: every `write` / `read` is forwarded over the pipe to the GUI, which does the real port I/O. AI's TX is echoed into the terminal tagged `[AI]`, so RX/TX flow through the GUI channel and what you see is exactly what the AI sees. **Key design: the GUI owns the COM port; the MCP server never opens it.** A COM port can be opened by only one process at a time, so rather than the MCP server grabbing it, the GUI is the sole owner and runs a local **named pipe server** (`SerialBridgeServer`). The standalone `ETTerms.SerialMcp` (launched by Kiro/Claude CLI) is a thin client: every `write` / `read` is forwarded over the pipe to the GUI, which does the real port I/O. AI's TX is echoed into the terminal tagged `[AI]`, so RX/TX flow through the GUI channel and what you see is exactly what the AI sees.
@@ -287,7 +288,11 @@ Full closed loop — all inside the ETTerms GUI:
| `serial_read` | waitFor?, timeoutMs? | Drain accumulated RX; optionally wait for a substring / timeout | | `serial_read` | waitFor?, timeoutMs? | Drain accumulated RX; optionally wait for a substring / timeout |
| `serial_detach` | — | Unbind (does **not** close the GUI's port) | | `serial_detach` | — | Unbind (does **not** close the GUI's port) |
Register in Kiro CLI: ### One-click setup (recommended)
In **Settings → AI MCP**, click **Setup** on the Claude Code or Kiro card to register the Serial MCP server into that CLI's user-level config (`~/.claude.json` / `~/.kiro/settings/mcp.json`) with a single button. Your other MCP servers are preserved, and each card shows the CLI command to verify the connection. **Remove** unregisters it.
### Manual setup
```powershell ```powershell
kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj" kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj"
@@ -377,8 +382,8 @@ ETTerms/
│ ├── Sessions/ # Connection abstraction: ISessionChannel / SshChannel / SerialChannel / ShellChannel │ ├── Sessions/ # Connection abstraction: ISessionChannel / SshChannel / SerialChannel / ShellChannel
│ ├── Connections/ # Connection data: Connection / ConnectionStore(SQLite) / CredentialVault │ ├── Connections/ # Connection data: Connection / ConnectionStore(SQLite) / CredentialVault
│ ├── Scripting/ # TTL engine: TTLInterpreter / ScriptRunner / GroupSyncContext / Pdu │ ├── Scripting/ # TTL engine: TTLInterpreter / ScriptRunner / GroupSyncContext / Pdu
│ └── Infrastructure/ # AppLogger / AppSettings / NativeTheme │ └── Infrastructure/ # AppLogger / AppSettings / McpRegistrar / NativeTheme
└── src/ETTerms.SerialMcp/ # 🔜 Serial MCP server (stdio) — lets AI agents drive the serial port └── src/ETTerms.SerialMcp/ # Serial MCP server (stdio) — lets AI agents drive the serial port
``` ```
**Core design:** every connection implements `ISessionChannel` (`Write(byte[])` + `event DataReceived`). `TerminalView` and `TTLInterpreter` only know this abstraction, so SSH / Serial / Shell look identical to upper layers — the key to "one script engine driving multiple connection types." See [ARCHITECTURE.md](ARCHITECTURE.md). **Core design:** every connection implements `ISessionChannel` (`Write(byte[])` + `event DataReceived`). `TerminalView` and `TTLInterpreter` only know this abstraction, so SSH / Serial / Shell look identical to upper layers — the key to "one script engine driving multiple connection types." See [ARCHITECTURE.md](ARCHITECTURE.md).
@@ -397,18 +402,25 @@ ETTerms/
## 📜 Version History ## 📜 Version History
### v0.1.0 (in development) ### v0.2.1
- **AI MCP one-click setup** — new **Settings → AI MCP** tab registers the Serial MCP server into Claude Code (`~/.claude.json`) or Kiro (`~/.kiro/settings/mcp.json`) with a single button; existing MCP servers are preserved, and each card shows the verify command
- Publish now auto-bundles `ETTerms.SerialMcp` into the app folder, so the registered path always resolves
### v0.2.0
- **Phase 9: Serial MCP server** (`ETTerms.SerialMcp`) — AI agents (Kiro CLI / Claude CLI) send/receive on the serial port while you watch it live in the GUI
- GUI is the sole COM-port owner; the MCP server bridges over a local named pipe (`SerialBridgeServer`) and never opens the port itself
- Tools: `serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach`; AI's TX echoed into the terminal tagged `[AI]`
- New app icon (window / taskbar / executable); version shown in the title bar
### v0.1.0
- Phases 18 complete: window shell, connection sidebar, tiling workspace, Serial / SSH / local Shell (ConPTY) / SFTP - Phases 18 complete: window shell, connection sidebar, tiling workspace, Serial / SSH / local Shell (ConPTY) / SFTP
- Owner-drawn VT100 terminal rendering - Owner-drawn VT100 terminal rendering
- TTL scripting engine (ported from MyTeraTerm) + Group sync execution - TTL scripting engine (ported from MyTeraTerm) + Group sync execution
- PDU power control (SNMP) - PDU power control (SNMP)
- Settings / About - Settings / About
- Packaging TBD
**Planned**
- Phase 9: Serial MCP server (`ETTerms.SerialMcp`) — let AI agents (Kiro CLI / Claude CLI) drive the serial port directly
--- ---
@@ -420,7 +432,7 @@ Licensed under the **MIT License** — see [LICENSE](LICENSE).
## 🙏 Acknowledgments ## 🙏 Acknowledgments
- **[KKTerm](https://github.com/)** — UI design reference (Activity Rail + tabbed workspace + Saved Connections) - **[KKTerm](https://github.com/ryantsai/KKTerm)** — by ryantsai (MIT) — UI design reference (Activity Rail + tabbed workspace + Saved Connections)
- **MyTeraTerm** — source of the TTL scripting engine and `AppLogger` - **MyTeraTerm** — source of the TTL scripting engine and `AppLogger`
- **[SSH.NET](https://github.com/sshnet/SSH.NET)**, **[SnmpSharpNet](http://www.snmpsharpnet.com/)** — open-source connectivity / SNMP libraries - **[SSH.NET](https://github.com/sshnet/SSH.NET)**, **[SnmpSharpNet](http://www.snmpsharpnet.com/)** — open-source connectivity / SNMP libraries
- **Microsoft** — .NET 8, WinForms, ConPTY, Credential Manager - **Microsoft** — .NET 8, WinForms, ConPTY, Credential Manager
+25 -13
View File
@@ -4,7 +4,7 @@
> 原生 Windows 終端機工作台(C# .NET 8 WinForms)—— 一個視窗整合 **SSH**、**Serial Port**、**本機 Shell (ConPTY)** 連線,內建從 MyTeraTerm 移植的 **TTL 腳本引擎**做自動化,並提供選用的 **Serial MCP server**,讓 AI agentKiro CLI / Claude CLI)直接操作 serial port。單機、無雲、無登入系統。 > 原生 Windows 終端機工作台(C# .NET 8 WinForms)—— 一個視窗整合 **SSH**、**Serial Port**、**本機 Shell (ConPTY)** 連線,內建從 MyTeraTerm 移植的 **TTL 腳本引擎**做自動化,並提供選用的 **Serial MCP server**,讓 AI agentKiro CLI / Claude CLI)直接操作 serial port。單機、無雲、無登入系統。
![version](https://img.shields.io/badge/version-0.1.0-blue.svg) ![platform](https://img.shields.io/badge/platform-Windows-0078D6.svg?logo=windows&logoColor=white) ![.NET](https://img.shields.io/badge/.NET-8.0-512BD4.svg?logo=dotnet&logoColor=white) ![UI](https://img.shields.io/badge/UI-WinForms-5C2D91.svg) ![SSH](https://img.shields.io/badge/SSH-SSH.NET-success.svg) ![Serial](https://img.shields.io/badge/Serial-System.IO.Ports-success.svg) ![status](https://img.shields.io/badge/status-WIP-orange.svg) ![license](https://img.shields.io/badge/license-MIT-green.svg) ![version](https://img.shields.io/badge/version-0.2.1-blue.svg) ![platform](https://img.shields.io/badge/platform-Windows-0078D6.svg?logo=windows&logoColor=white) ![.NET](https://img.shields.io/badge/.NET-8.0-512BD4.svg?logo=dotnet&logoColor=white) ![UI](https://img.shields.io/badge/UI-WinForms-5C2D91.svg) ![SSH](https://img.shields.io/badge/SSH-SSH.NET-success.svg) ![Serial](https://img.shields.io/badge/Serial-System.IO.Ports-success.svg) ![status](https://img.shields.io/badge/status-beta-yellow.svg) ![license](https://img.shields.io/badge/license-MIT-green.svg)
--- ---
@@ -65,10 +65,11 @@
- 透過 SNMP`SnmpSharpNet`)控制 PDU 插座,測試中遠端電源循環 - 透過 SNMP`SnmpSharpNet`)控制 PDU 插座,測試中遠端電源循環
- 腳本指令:`pduconnect` / `pductrl` - 腳本指令:`pduconnect` / `pductrl`
- 🤖 **AI / MCP 整合(選用,規劃中** - 🤖 **AI / MCP 整合(選用)**
- **COM port 由 GUI 持有**;獨立的 stdio **Serial MCP server** 經本機 named pipe 橋接過去,供 **Kiro CLI / Claude CLI** 使用 - **COM port 由 GUI 持有**;獨立的 stdio **Serial MCP server** 經本機 named pipe 橋接過去,供 **Kiro CLI / Claude CLI** 使用
- 工具:`serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach` - 工具:`serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach`
- AI 的 serial 收發會即時顯示在 GUI(標 `[AI]`)——先在 GUI 開好 port,再讓 AI attach - AI 的 serial 收發會即時顯示在 GUI(標 `[AI]`)——先在 GUI 開好 port,再讓 AI attach
- **一鍵設定**:在 **設定 → AI MCP** 一個按鈕就把 server 註冊進 Claude Code / Kiro
- 📊 **連線 RX 日誌** - 📊 **連線 RX 日誌**
- `logopen` / `logwrite` / `logclose` 將工作階段輸出寫檔 - `logopen` / `logwrite` / `logclose` 將工作階段輸出寫檔
@@ -271,7 +272,7 @@ wait 'login: '
## 🤖 AI / MCP 整合 ## 🤖 AI / MCP 整合
> 🔜 **規劃中(Phase 9)。** 讓 AI agent**Kiro CLI / Claude CLI**)收發 serial**而且你能在 ETTerms GUI 即時看到 AI 的每筆收發**。 > 讓 AI agent**Kiro CLI / Claude CLI**)收發 serial**而且你能在 ETTerms GUI 即時看到 AI 的每筆收發**。
**關鍵設計:COM port 由 GUI 唯一持有,MCP server 不自己開 port。** 一個 COM port 同時只能被一個行程開啟;因此不讓 MCP server 自己開,而是 **GUI 當 port 的唯一擁有者**,在 GUI 內跑一支本機 **named pipe server**`SerialBridgeServer`)。獨立的 `ETTerms.SerialMcp`(由 Kiro/Claude CLI 啟動)退化成瘦客戶端:所有 `write` / `read` 都經 pipe 轉發給 GUI,由 GUI 代為讀寫實體 port。AI 的 TX 會以 `[AI]` 標色 echo 進終端機,RX/TX 都流經 GUI channel**你看到的就是 AI 看到的**。 **關鍵設計:COM port 由 GUI 唯一持有,MCP server 不自己開 port。** 一個 COM port 同時只能被一個行程開啟;因此不讓 MCP server 自己開,而是 **GUI 當 port 的唯一擁有者**,在 GUI 內跑一支本機 **named pipe server**`SerialBridgeServer`)。獨立的 `ETTerms.SerialMcp`(由 Kiro/Claude CLI 啟動)退化成瘦客戶端:所有 `write` / `read` 都經 pipe 轉發給 GUI,由 GUI 代為讀寫實體 port。AI 的 TX 會以 `[AI]` 標色 echo 進終端機,RX/TX 都流經 GUI channel**你看到的就是 AI 看到的**。
@@ -287,7 +288,11 @@ wait 'login: '
| `serial_read` | waitFor?, timeoutMs? | 取出累積的 RX;可等待子字串或逾時 | | `serial_read` | waitFor?, timeoutMs? | 取出累積的 RX;可等待子字串或逾時 |
| `serial_detach` | — | 解除綁定(**不會**關閉 GUI 的 port | | `serial_detach` | — | 解除綁定(**不會**關閉 GUI 的 port |
在 Kiro CLI 註冊: ### 一鍵設定(推薦)
**設定 → AI MCP**,對 Claude Code 或 Kiro 卡片按 **Setup**,一個按鈕就把 Serial MCP server 寫進該 CLI 的使用者層級設定檔(`~/.claude.json` / `~/.kiro/settings/mcp.json`)。你既有的其他 MCP server 會被保留,卡片並顯示用來驗證連線的 CLI 指令。按 **Remove** 即可移除。
### 手動設定
```powershell ```powershell
kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj" kiro-cli mcp add --name serial --command dotnet --args "run --project src\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj"
@@ -377,8 +382,8 @@ ETTerms/
│ ├── Sessions/ # 連線抽象:ISessionChannel / SshChannel / SerialChannel / ShellChannel │ ├── Sessions/ # 連線抽象:ISessionChannel / SshChannel / SerialChannel / ShellChannel
│ ├── Connections/ # 連線資料:Connection / ConnectionStore(SQLite) / CredentialVault │ ├── Connections/ # 連線資料:Connection / ConnectionStore(SQLite) / CredentialVault
│ ├── Scripting/ # TTL 引擎:TTLInterpreter / ScriptRunner / GroupSyncContext / Pdu │ ├── Scripting/ # TTL 引擎:TTLInterpreter / ScriptRunner / GroupSyncContext / Pdu
│ └── Infrastructure/ # AppLogger / AppSettings / NativeTheme │ └── Infrastructure/ # AppLogger / AppSettings / McpRegistrar / NativeTheme
└── src/ETTerms.SerialMcp/ # 🔜 Serial MCP serverstdio)—— 讓 AI agent 直接操作 serial port └── src/ETTerms.SerialMcp/ # Serial MCP serverstdio)—— 讓 AI agent 直接操作 serial port
``` ```
**核心設計:** 所有連線都實作 `ISessionChannel``Write(byte[])` + `event DataReceived`)。`TerminalView``TTLInterpreter` 只認得這個抽象,因此 SSH / Serial / Shell 對上層完全一致——這是「同一套腳本引擎驅動多種連線」的關鍵。詳見 [ARCHITECTURE.md](ARCHITECTURE.md)。 **核心設計:** 所有連線都實作 `ISessionChannel``Write(byte[])` + `event DataReceived`)。`TerminalView``TTLInterpreter` 只認得這個抽象,因此 SSH / Serial / Shell 對上層完全一致——這是「同一套腳本引擎驅動多種連線」的關鍵。詳見 [ARCHITECTURE.md](ARCHITECTURE.md)。
@@ -397,18 +402,25 @@ ETTerms/
## 📜 版本紀錄 ## 📜 版本紀錄
### v0.1.0(開發中) ### v0.2.1
- **AI MCP 一鍵設定** —— 新增 **設定 → AI MCP** 分頁,一個按鈕就把 Serial MCP server 註冊進 Claude Code`~/.claude.json`)或 Kiro`~/.kiro/settings/mcp.json`);保留檔內其他既有 MCP server,卡片並顯示驗證指令
- Publish 會自動把 `ETTerms.SerialMcp` 一併打包進 app 資料夾,註冊寫入的路徑必定存在
### v0.2.0
- **Phase 9Serial MCP server**`ETTerms.SerialMcp`)—— AI agentKiro CLI / Claude CLI)可在 serial port 收發,而你在 GUI 即時看著
- COM port 由 GUI 唯一持有;MCP server 經本機 named pipe`SerialBridgeServer`)橋接,自己不開 port
- 工具:`serial_list` / `serial_attach` / `serial_write` / `serial_read` / `serial_detach`AI 的 TX 在終端機以 `[AI]` 標色 echo
- 新增 app 圖示(視窗 / 工作列 / 執行檔);標題列顯示版本號
### v0.1.0
- Phase 1–8 完成:視窗外殼、連線側欄、平鋪工作區、Serial / SSH / 本機 Shell (ConPTY) / SFTP - Phase 1–8 完成:視窗外殼、連線側欄、平鋪工作區、Serial / SSH / 本機 Shell (ConPTY) / SFTP
- 自繪 VT100 終端機渲染 - 自繪 VT100 終端機渲染
- TTL 腳本引擎(移植自 MyTeraTerm+ Group 同步執行 - TTL 腳本引擎(移植自 MyTeraTerm+ Group 同步執行
- PDU 電源控制(SNMP - PDU 電源控制(SNMP
- Settings / About - Settings / About
- 打包待規劃
**規劃中**
- Phase 9Serial MCP server`ETTerms.SerialMcp`)—— 讓 AI agentKiro CLI / Claude CLI)直接操作 serial port
--- ---
@@ -420,7 +432,7 @@ ETTerms/
## 🙏 致謝 ## 🙏 致謝
- **[KKTerm](https://github.com/)** — UI 設計參考(Activity Rail + 分頁工作區 + Saved Connections - **[KKTerm](https://github.com/ryantsai/KKTerm)** — by ryantsaiMITUI 設計參考(Activity Rail + 分頁工作區 + Saved Connections
- **MyTeraTerm** — TTL 腳本引擎與 `AppLogger` 來源 - **MyTeraTerm** — TTL 腳本引擎與 `AppLogger` 來源
- **[SSH.NET](https://github.com/sshnet/SSH.NET)**、**[SnmpSharpNet](http://www.snmpsharpnet.com/)** — 開源連線 / SNMP 函式庫 - **[SSH.NET](https://github.com/sshnet/SSH.NET)**、**[SnmpSharpNet](http://www.snmpsharpnet.com/)** — 開源連線 / SNMP 函式庫
- **Microsoft** — .NET 8、WinForms、ConPTY、Credential Manager - **Microsoft** — .NET 8、WinForms、ConPTY、Credential Manager
+20
View File
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>ETTerms.PduMcp</RootNamespace>
<AssemblyName>ETTerms.PduMcp</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ModelContextProtocol" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="SnmpSharpNet" Version="0.9.7">
<NoWarn>NU1701</NoWarn>
</PackageReference>
</ItemGroup>
</Project>
+92
View File
@@ -0,0 +1,92 @@
using System.Net;
using SnmpSharpNet;
namespace ETTerms.PduMcp;
/// <summary>
/// PDU controller for iPoMan II/III models via SNMP (slim copy of the GUI's
/// ETTerms.Scripting.Pdu.PduController, ported into the standalone MCP server).
///
/// Differences from the GUI version:
/// • No dependency on ETTerms.Infrastructure.AppLogger — diagnostics go to stderr
/// (stdout is reserved for the MCP JSON-RPC stream).
/// • Adds <see cref="GetModelName"/> so tools can surface the device identity.
///
/// SNMP is connectionless (UDP) and non-exclusive, so this talks to the PDU directly
/// without bridging through the GUI.
/// </summary>
public sealed class PduController : IDisposable
{
private readonly string _ip;
private const string Community = "private";
private const int SnmpPort = 161;
private const int Timeout = 3000;
public string Ip => _ip;
public PduController(string ip) => _ip = ip;
/// <summary>Reads the PDU model/name OID; non-empty containing "PDU" means reachable.</summary>
public string? GetModelName() => SnmpGet(".1.3.6.1.4.1.2468.1.4.2.1.1.4");
public bool CheckConnection()
{
var name = GetModelName();
Log($"CheckConnection {_ip}: name='{name ?? "<null>"}'");
return !string.IsNullOrEmpty(name) && name.Contains("PDU");
}
public bool SetPortOn(int port) => SnmpSet(PortControlOid(port), new Integer32(3));
public bool SetPortOff(int port) => SnmpSet(PortControlOid(port), new Integer32(4));
/// <summary>true = on, false = off, null = unknown/unreachable.</summary>
public bool? GetPortState(int port)
{
var r = SnmpGet(PortStateOid(port));
return r == "3" ? true : (r == "2" || r == "4") ? false : null;
}
public int? GetPortCurrent(int port) => int.TryParse(SnmpGet(PortCurrentOid(port)), out int v) ? v : null;
public double? GetPortPowerWatts(int port) => int.TryParse(SnmpGet(PortPowerOid(port)), out int v) ? v / 10.0 : null;
private static string PortControlOid(int port) => $".1.3.6.1.4.1.2468.1.4.2.1.3.2.4.1.2.{port}";
private static string PortStateOid(int port) => $".1.3.6.1.4.1.2468.1.4.2.1.3.2.3.1.2.{port}";
private static string PortCurrentOid(int port) => $".1.3.6.1.4.1.2468.1.4.2.1.3.2.3.1.3.{port}";
private static string PortPowerOid(int port) => $".1.3.6.1.4.1.2468.1.4.2.1.3.2.3.1.5.{port}";
private bool SnmpSet(string oid, AsnType value)
{
try
{
var param = new AgentParameters(new OctetString(Community)) { Version = SnmpVersion.Ver1 };
using var target = new UdpTarget((IPAddress)new IpAddress(_ip), SnmpPort, Timeout, 1);
var pdu = new SnmpSharpNet.Pdu(PduType.Set);
pdu.VbList.Add(new Oid(oid), value);
var result = (SnmpV1Packet)target.Request(pdu, param);
return result?.Pdu.ErrorStatus == 0;
}
catch (Exception ex) { Log($"SNMP SET {oid} exception: {ex.Message}"); return false; }
}
private string? SnmpGet(string oid)
{
try
{
var param = new AgentParameters(new OctetString(Community)) { Version = SnmpVersion.Ver1 };
using var target = new UdpTarget((IPAddress)new IpAddress(_ip), SnmpPort, Timeout, 1);
var pdu = new SnmpSharpNet.Pdu(PduType.Get);
pdu.VbList.Add(new Oid(oid));
var result = (SnmpV1Packet)target.Request(pdu, param);
if (result == null) { Log($"SNMP GET {oid}: no response (timeout)"); return null; }
if (result.Pdu.ErrorStatus != 0) { Log($"SNMP GET {oid}: ErrorStatus={result.Pdu.ErrorStatus}"); return null; }
foreach (var v in result.Pdu.VbList) return v.Value.ToString();
}
catch (Exception ex) { Log($"SNMP GET {oid} exception: {ex.Message}"); }
return null;
}
// stdio MCP serverstdout 專供 JSON-RPC,診斷一律走 stderr。
private static void Log(string msg) => Console.Error.WriteLine($"[PDU] {msg}");
public void Dispose() { }
}
+31
View File
@@ -0,0 +1,31 @@
using System.Collections.Concurrent;
namespace ETTerms.PduMcp;
/// <summary>
/// Process-wide registry of connected PDUs, keyed by IP. The MCP server is long-lived,
/// so connections established by <c>pdu_connect</c> persist across tool calls.
/// </summary>
public sealed class PduRegistry
{
public static readonly PduRegistry Instance = new();
/// <summary>iPoMan II/III outlet count.</summary>
public const int PortCount = 12;
private readonly ConcurrentDictionary<string, PduController> _pdus = new(StringComparer.OrdinalIgnoreCase);
public PduController GetOrAdd(string ip) =>
_pdus.GetOrAdd(ip.Trim(), static k => new PduController(k));
public bool TryGet(string ip, out PduController pdu) =>
_pdus.TryGetValue(ip.Trim(), out pdu!);
public bool Remove(string ip)
{
if (_pdus.TryRemove(ip.Trim(), out var pdu)) { pdu.Dispose(); return true; }
return false;
}
public IReadOnlyCollection<string> ConnectedIps => _pdus.Keys.ToArray();
}
+132
View File
@@ -0,0 +1,132 @@
using System.ComponentModel;
using System.Text;
using System.Text.Json;
using ModelContextProtocol.Server;
namespace ETTerms.PduMcp;
/// <summary>
/// MCP tools exposed to the AI for controlling an SNMP PDU (iPoMan II/III).
///
/// Unlike the serial bridge, the PDU is reached directly over SNMP — the ETTerms GUI
/// does NOT need to be running. Connections are held per-IP and persist for the lifetime
/// of this MCP server. Always <c>pdu_connect</c> an IP first, then control its ports.
/// </summary>
[McpServerToolType]
public static class PduTools
{
private static readonly JsonSerializerOptions Json = new() { WriteIndented = false };
[McpServerTool, Description("Connect to a PDU over SNMP by IP and verify it responds. Required before controlling ports. Returns the model name on success.")]
public static Task<string> pdu_connect(
[Description("PDU IP address, e.g. 192.168.1.21")] string ip)
{
ip = (ip ?? "").Trim();
if (ip.Length == 0) return Err("ip is required");
var pdu = PduRegistry.Instance.GetOrAdd(ip);
var model = pdu.GetModelName();
bool ok = !string.IsNullOrEmpty(model) && model.Contains("PDU");
if (!ok)
{
PduRegistry.Instance.Remove(ip);
return Err($"no SNMP response from PDU at {ip} (check IP/network/community)");
}
return Ok(new { ip, model });
}
[McpServerTool, Description("List PDUs currently connected in this MCP session (by IP).")]
public static Task<string> pdu_list()
=> Ok(new { connected = PduRegistry.Instance.ConnectedIps });
[McpServerTool, Description("Turn a PDU outlet on or off. The PDU must be connected first via pdu_connect.")]
public static Task<string> pdu_set_port(
[Description("PDU IP address")] string ip,
[Description("Outlet/port number (1-12)")] int port,
[Description("true = ON, false = OFF")] bool on)
{
if (!TryResolve(ip, port, out var pdu, out var error)) return Err(error);
bool ok = on ? pdu.SetPortOn(port) : pdu.SetPortOff(port);
if (!ok) return Err($"SNMP set failed for {ip} port {port}");
return Ok(new { ip, port, state = on ? "on" : "off" });
}
[McpServerTool, Description("Read a single outlet's state, current (mA) and power (W).")]
public static Task<string> pdu_get_port(
[Description("PDU IP address")] string ip,
[Description("Outlet/port number (1-12)")] int port)
{
if (!TryResolve(ip, port, out var pdu, out var error)) return Err(error);
return Ok(PortSnapshot(pdu, port));
}
[McpServerTool, Description("Read the state, current (mA) and power (W) of all outlets on the PDU.")]
public static Task<string> pdu_status(
[Description("PDU IP address")] string ip)
{
if (!PduRegistry.Instance.TryGet(ip, out var pdu))
return Err($"PDU {ip} not connected. Call pdu_connect first.");
var ports = new List<object>();
for (int p = 1; p <= PduRegistry.PortCount; p++)
ports.Add(PortSnapshot(pdu, p));
return Ok(new { ip, ports });
}
[McpServerTool, Description("Power-cycle an outlet: turn it OFF, wait offSeconds, then turn it ON. Useful for rebooting a DUT.")]
public static async Task<string> pdu_power_cycle(
[Description("PDU IP address")] string ip,
[Description("Outlet/port number (1-12)")] int port,
[Description("Seconds to stay off before powering back on")] int offSeconds = 5)
{
if (!TryResolve(ip, port, out var pdu, out var error)) return await Err(error);
if (offSeconds < 0) offSeconds = 0;
if (!pdu.SetPortOff(port)) return await Err($"SNMP off failed for {ip} port {port}");
await Task.Delay(offSeconds * 1000);
if (!pdu.SetPortOn(port)) return await Err($"SNMP on failed for {ip} port {port}");
return await Ok(new { ip, port, action = "power_cycle", offSeconds, state = "on" });
}
[McpServerTool, Description("Disconnect a PDU from this MCP session (does not change outlet states).")]
public static Task<string> pdu_disconnect(
[Description("PDU IP address")] string ip)
=> Ok(new { ip, removed = PduRegistry.Instance.Remove(ip) });
// ── helpers ──
private static bool TryResolve(string ip, int port, out PduController pdu, out string error)
{
pdu = null!;
error = "";
if (port < 1 || port > PduRegistry.PortCount)
{
error = $"port must be 1-{PduRegistry.PortCount}";
return false;
}
if (!PduRegistry.Instance.TryGet(ip, out pdu))
{
error = $"PDU {ip} not connected. Call pdu_connect first.";
return false;
}
return true;
}
private static object PortSnapshot(PduController pdu, int port)
{
var state = pdu.GetPortState(port);
return new
{
port,
state = state == true ? "on" : state == false ? "off" : "unknown",
currentMilliAmps = pdu.GetPortCurrent(port),
powerWatts = pdu.GetPortPowerWatts(port)
};
}
private static Task<string> Ok(object payload)
{
var node = new { ok = true, result = payload };
return Task.FromResult(JsonSerializer.Serialize(node, Json));
}
private static Task<string> Err(string message)
=> Task.FromResult(JsonSerializer.Serialize(new { ok = false, error = message }, Json));
}
+17
View File
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
// stdio MCP server:把 SNMP PDU 控制暴露給 AIKiro / Claude CLI)。
// 與 ETTerms.SerialMcp 不同,PDU 走 SNMP(UDP) 非獨佔,故直接打 SNMP,不需 GUI 在跑。
var builder = Host.CreateApplicationBuilder(args);
// stdio 傳輸:stdout 專供 JSON-RPClog 一律走 stderr,否則會污染協議。
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
+48 -20
View File
@@ -21,7 +21,7 @@ public sealed class AboutView : UserControl
var left = new FlowLayoutPanel var left = new FlowLayoutPanel
{ {
Dock = DockStyle.Left, Width = 380, FlowDirection = FlowDirection.TopDown, Dock = DockStyle.Left, Width = 380, FlowDirection = FlowDirection.TopDown,
WrapContents = false, AutoScroll = false, Padding = new Padding(20), WrapContents = false, AutoScroll = true, Padding = new Padding(20),
BackColor = Theme.WorkspaceBack BackColor = Theme.WorkspaceBack
}; };
@@ -73,7 +73,7 @@ public sealed class AboutView : UserControl
{ {
var header = new Label var header = new Label
{ {
AutoSize = false, Width = 500, Height = 22, AutoSize = true,
Text = $"● v{entry.Version} · {entry.Title} ({entry.Date:yyyy-MM-dd})", Text = $"● v{entry.Version} · {entry.Title} ({entry.Date:yyyy-MM-dd})",
ForeColor = Theme.Accent, Font = Theme.UiFontBold, ForeColor = Theme.Accent, Font = Theme.UiFontBold,
Margin = new Padding(0, 8, 0, 2) Margin = new Padding(0, 8, 0, 2)
@@ -83,7 +83,7 @@ public sealed class AboutView : UserControl
{ {
right.Controls.Add(new Label right.Controls.Add(new Label
{ {
AutoSize = false, Width = 500, Height = 20, AutoSize = true,
Text = $" • {change}", Text = $" • {change}",
ForeColor = Theme.Text, Font = Theme.UiFont, ForeColor = Theme.Text, Font = Theme.UiFont,
Margin = new Padding(0) Margin = new Padding(0)
@@ -99,18 +99,17 @@ public sealed class AboutView : UserControl
private static Panel MakeCard(int width, int height, Action<FlowLayoutPanel> build) private static Panel MakeCard(int width, int height, Action<FlowLayoutPanel> build)
{ {
var card = new Panel // 卡片本身就是 TopDown 的 AutoSize FlowLayoutPanel:高度永遠長到容得下內容
// (高 DPI 字變大也不會被裁),MinimumSize 維持 100% 的設計尺寸。
var card = new FlowLayoutPanel
{ {
Width = width, Height = height, Margin = new Padding(0, 0, 0, 12), FlowDirection = FlowDirection.TopDown, WrapContents = false,
AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(width, height),
Margin = new Padding(0, 0, 0, 12),
BackColor = Theme.TabBack, Padding = new Padding(12) BackColor = Theme.TabBack, Padding = new Padding(12)
}; };
var flow = new FlowLayoutPanel build(card);
{
Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown,
WrapContents = false, BackColor = Theme.TabBack, AutoSize = false
};
build(flow);
card.Controls.Add(flow);
return card; return card;
} }
@@ -149,24 +148,31 @@ public sealed class AboutView : UserControl
{ {
return new Label return new Label
{ {
AutoSize = false, Width = 310, Height = 20, AutoSize = true,
Text = text, Font = font, ForeColor = color, Text = text, Font = font, ForeColor = color,
TextAlign = align, Margin = new Padding(0) TextAlign = align, Margin = new Padding(0, 1, 0, 1)
}; };
} }
private static Panel MakeRow(string label, string value) private static Control MakeRow(string label, string value)
{ {
var row = new Panel { Width = 310, Height = 20, Margin = new Padding(0, 2, 0, 0), BackColor = Color.Transparent }; var row = new FlowLayoutPanel
{
AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
FlowDirection = FlowDirection.LeftToRight, WrapContents = false,
Margin = new Padding(0, 2, 0, 0), BackColor = Color.Transparent
};
row.Controls.Add(new Label row.Controls.Add(new Label
{ {
Text = value, AutoSize = false, Width = 230, Height = 20, Dock = DockStyle.Right, Text = label, AutoSize = true, MinimumSize = new Size(70, 0),
ForeColor = Theme.Text, Font = Theme.UiFont, TextAlign = ContentAlignment.MiddleRight ForeColor = Theme.TextDim, Font = Theme.UiFont,
TextAlign = ContentAlignment.MiddleLeft, Margin = new Padding(0, 0, 8, 0)
}); });
row.Controls.Add(new Label row.Controls.Add(new Label
{ {
Text = label, AutoSize = false, Width = 70, Height = 20, Dock = DockStyle.Left, Text = value, AutoSize = true,
ForeColor = Theme.TextDim, Font = Theme.UiFont, TextAlign = ContentAlignment.MiddleLeft ForeColor = Theme.Text, Font = Theme.UiFont,
TextAlign = ContentAlignment.MiddleLeft, Margin = new Padding(0)
}); });
return row; return row;
} }
@@ -177,6 +183,28 @@ public sealed class AboutView : UserControl
private static readonly ChangelogEntry[] Changelog = private static readonly ChangelogEntry[] Changelog =
[ [
new("0.3.0", new DateOnly(2026, 6, 8), "feat: PDU MCP server",
[
"New ETTerms.PduMcp server: AI agents (Kiro / Claude CLI) can now control SNMP PDU outlets directly.",
"Tools: pdu_connect / pdu_list / pdu_set_port / pdu_get_port / pdu_status / pdu_power_cycle / pdu_disconnect.",
"PDU runs over SNMP, so the AI can power-cycle a DUT without the GUI session being open.",
"Settings → AI MCP now registers both Serial and PDU MCP servers with one click.",
]),
new("0.2.2", new DateOnly(2026, 6, 5), "Bugfix — terminal stability",
[
"Fixed: the terminal no longer freezes after minimizing or switching tabs (notably in PowerShell / Kiro).",
"Added: Shift+Enter inserts a newline in the shell, so you can type multi-line commands.",
]),
new("0.2.1", new DateOnly(2026, 6, 4), "AI MCP one-click setup",
[
"New Settings → AI MCP tab: register the Serial MCP server into Claude Code or Kiro with one click.",
"Lets AI agents drive the serial port for you.",
]),
new("0.2.0", new DateOnly(2026, 6, 4), "Beta Version Release",
[
"AI agents can now send/receive on the serial port while you watch it live in the GUI.",
"New app icon and version shown in the title bar.",
]),
new("0.1.0", new DateOnly(2026, 6, 3), "Initial Release", new("0.1.0", new DateOnly(2026, 6, 3), "Initial Release",
[ [
"Beta Version: expect bugs and missing features. Feedback welcome!", "Beta Version: expect bugs and missing features. Feedback welcome!",
+16 -1
View File
@@ -17,7 +17,9 @@ public sealed class ActivityRail : UserControl
private RailView _active = RailView.Terminal; private RailView _active = RailView.Terminal;
private RailView? _hover; private RailView? _hover;
private const int ItemSize = 56; private const int BaseItemSize = 56;
/// <summary>依目前螢幕 DPI 縮放的格子大小(owner-draw 不會自動縮放像素,需自己換算)。</summary>
private int ItemSize => (int)Math.Round(BaseItemSize * (DeviceDpi / 96.0));
private static readonly (RailView view, string glyph, string tip)[] Items = private static readonly (RailView view, string glyph, string tip)[] Items =
{ {
(RailView.Terminal, "▤", "Terminal"), (RailView.Terminal, "▤", "Terminal"),
@@ -37,6 +39,19 @@ public sealed class ActivityRail : UserControl
Cursor = Cursors.Hand; Cursor = Cursors.Hand;
} }
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Width = ItemSize; // handle 建立後 DeviceDpi 才正確,依實際 DPI 重設寬度
}
protected override void OnDpiChangedAfterParent(EventArgs e)
{
base.OnDpiChangedAfterParent(e);
Width = ItemSize; // 拖到不同縮放的螢幕時跟著調整
Invalidate();
}
[System.ComponentModel.DesignerSerializationVisibility( [System.ComponentModel.DesignerSerializationVisibility(
System.ComponentModel.DesignerSerializationVisibility.Hidden)] System.ComponentModel.DesignerSerializationVisibility.Hidden)]
public RailView ActiveView public RailView ActiveView
+9 -4
View File
@@ -49,10 +49,11 @@ public sealed class ConnectionSidebar : UserControl
BackColor = Theme.SidebarBack; BackColor = Theme.SidebarBack;
// ── Tab bar (Sessions / SFTP) ── // ── Tab bar (Sessions / SFTP) ──
// 高度 40 與右側 Workspace 工具列對齊,讓「CONNECTIONS / 連線清單」與右側分頁列同一條基準線
var tabBar = new FlowLayoutPanel var tabBar = new FlowLayoutPanel
{ {
Dock = DockStyle.Top, Height = 30, BackColor = Theme.RailBack, Dock = DockStyle.Top, Height = 40, BackColor = Theme.RailBack,
Padding = new Padding(4, 3, 4, 0), WrapContents = false Padding = new Padding(4, 7, 4, 0), WrapContents = false
}; };
var sessionsPanel = new Panel { Dock = DockStyle.Fill, BackColor = Theme.SidebarBack }; var sessionsPanel = new Panel { Dock = DockStyle.Fill, BackColor = Theme.SidebarBack };
@@ -63,7 +64,9 @@ public sealed class ConnectionSidebar : UserControl
{ {
var b = new Button var b = new Button
{ {
Text = text, Width = 70, Height = 24, FlatStyle = FlatStyle.Flat, Text = text, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(70, 24), Padding = new Padding(8, 2, 8, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont,
Margin = new Padding(0, 0, 4, 0), Cursor = Cursors.Hand Margin = new Padding(0, 0, 4, 0), Cursor = Cursors.Hand
}; };
@@ -217,7 +220,9 @@ public sealed class ConnectionSidebar : UserControl
var sshCombo = new ComboBox { Width = 120, DropDownStyle = ComboBoxStyle.DropDownList, BackColor = Theme.TabBack, ForeColor = Theme.Text, Font = Theme.UiFont }; var sshCombo = new ComboBox { Width = 120, DropDownStyle = ComboBoxStyle.DropDownList, BackColor = Theme.TabBack, ForeColor = Theme.Text, Font = Theme.UiFont };
var connectBtn = new Button var connectBtn = new Button
{ {
Text = "Connect", Width = 64, Height = 24, FlatStyle = FlatStyle.Flat, Text = "Connect", AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(64, 24), Padding = new Padding(8, 2, 8, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, Cursor = Cursors.Hand, Margin = new Padding(4, 0, 0, 0) ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, Cursor = Cursors.Hand, Margin = new Padding(4, 0, 0, 0)
}; };
connectBtn.FlatAppearance.BorderColor = Theme.SerialColor; connectBtn.FlatAppearance.BorderColor = Theme.SerialColor;
+4 -2
View File
@@ -18,8 +18,10 @@ partial class MainForm
{ {
SuspendLayout(); SuspendLayout();
AutoScaleDimensions = new SizeF(7F, 15F); // 用 Dpi 自動縮放:尺寸縮放比例 = 螢幕 DPI 比例,與點數字型的實際渲染比例一致,
AutoScaleMode = AutoScaleMode.Font; // 避免「字放大了、容器沒放大」造成的高 DPI 文字/按鈕被裁切。
AutoScaleDimensions = new SizeF(96F, 96F);
AutoScaleMode = AutoScaleMode.Dpi;
ClientSize = new Size(1100, 700); ClientSize = new Size(1100, 700);
MinimumSize = new Size(720, 480); MinimumSize = new Size(720, 480);
BackColor = Theme.WorkspaceBack; BackColor = Theme.WorkspaceBack;
+1 -1
View File
@@ -39,7 +39,7 @@ public partial class MainForm : Form
var v = Assembly.GetExecutingAssembly().GetName().Version; var v = Assembly.GetExecutingAssembly().GetName().Version;
var versionStr = v != null ? $"{v.Major}.{v.Minor}.{v.Build}" : "0.1.0"; var versionStr = v != null ? $"{v.Major}.{v.Minor}.{v.Build}" : "0.1.0";
Text = $"ETTerms Version v{versionStr}"; Text = $"ETTerms v{versionStr}";
} }
private void BuildLayout() private void BuildLayout()
+199 -35
View File
@@ -5,7 +5,7 @@ using ETTerms.Scripting.Pdu;
namespace ETTerms.App; namespace ETTerms.App;
/// <summary>Settings page with tabs: Terminal / PDU.</summary> /// <summary>Settings page with tabs: Terminal / PDU / AI MCP.</summary>
public sealed class SettingsView : UserControl public sealed class SettingsView : UserControl
{ {
public SettingsView() public SettingsView()
@@ -20,22 +20,22 @@ public sealed class SettingsView : UserControl
Padding = new Padding(4, 4, 4, 0), WrapContents = false Padding = new Padding(4, 4, 4, 0), WrapContents = false
}; };
var terminalPage = BuildTerminalTab();
var pduPage = BuildPduTab();
terminalPage.Dock = DockStyle.Fill;
pduPage.Dock = DockStyle.Fill;
pduPage.Visible = false;
var body = new Panel { Dock = DockStyle.Fill, BackColor = Theme.WorkspaceBack }; var body = new Panel { Dock = DockStyle.Fill, BackColor = Theme.WorkspaceBack };
body.Controls.Add(terminalPage); var pages = new List<Panel>();
body.Controls.Add(pduPage);
Button? activeBtn = null; Button? activeBtn = null;
Button MakeTab(string text, Panel page) Button MakeTab(string text, Panel page)
{ {
page.Dock = DockStyle.Fill;
page.Visible = false;
body.Controls.Add(page);
pages.Add(page);
var b = new Button var b = new Button
{ {
Text = text, AutoSize = false, Width = 80, Height = 26, FlatStyle = FlatStyle.Flat, Text = text, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(70, 26), Padding = new Padding(10, 2, 10, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont,
Margin = new Padding(0, 0, 4, 0), Cursor = Cursors.Hand Margin = new Padding(0, 0, 4, 0), Cursor = Cursors.Hand
}; };
@@ -43,8 +43,7 @@ public sealed class SettingsView : UserControl
b.FlatAppearance.MouseOverBackColor = Theme.Hover; b.FlatAppearance.MouseOverBackColor = Theme.Hover;
b.Click += (_, _) => b.Click += (_, _) =>
{ {
terminalPage.Visible = page == terminalPage; foreach (var p in pages) p.Visible = p == page;
pduPage.Visible = page == pduPage;
if (activeBtn != null) activeBtn.BackColor = Theme.TabBack; if (activeBtn != null) activeBtn.BackColor = Theme.TabBack;
b.BackColor = Theme.TabActiveBack; b.BackColor = Theme.TabActiveBack;
activeBtn = b; activeBtn = b;
@@ -52,17 +51,16 @@ public sealed class SettingsView : UserControl
return b; return b;
} }
var termBtn = MakeTab("Terminal", terminalPage); var termBtn = MakeTab("Terminal", BuildTerminalTab());
var pduBtn = MakeTab("PDU", pduPage);
tabBar.Controls.Add(termBtn); tabBar.Controls.Add(termBtn);
tabBar.Controls.Add(pduBtn); tabBar.Controls.Add(MakeTab("PDU", BuildPduTab()));
tabBar.Controls.Add(MakeTab("AI MCP", BuildAiMcpTab()));
// Set initial active
termBtn.BackColor = Theme.TabActiveBack;
activeBtn = termBtn;
Controls.Add(body); Controls.Add(body);
Controls.Add(tabBar); Controls.Add(tabBar);
// Set initial active tab
termBtn.PerformClick();
} }
// ═══ Terminal Tab ═══ // ═══ Terminal Tab ═══
@@ -275,26 +273,190 @@ public sealed class SettingsView : UserControl
} }
} }
// ═══ AI MCP Tab ═══
private Panel BuildAiMcpTab()
{
var page = new Panel { BackColor = Theme.WorkspaceBack, Padding = new Padding(20) };
var flow = new FlowLayoutPanel
{
Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown,
WrapContents = false, BackColor = Theme.WorkspaceBack, AutoScroll = true
};
flow.Controls.Add(new Label
{
Text = "AI MCP Integration", AutoSize = true,
ForeColor = Theme.Accent, Font = Theme.UiFontBold, Margin = new Padding(0, 0, 0, 4)
});
flow.Controls.Add(new Label
{
Text = "One-click register the ETTerms MCP servers (Serial + PDU) into your AI CLI's\n" +
"user-level config. Serial: ETTerms owns the COM port, the AI drives it through a\n" +
"local named pipe (open a Serial session first). PDU: the AI controls outlets\n" +
"directly over SNMP — no GUI session required.",
AutoSize = false, Width = 600, Height = 64,
ForeColor = Theme.TextDim, Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 8)
});
// Resolved MCP server exes (serial + pdu)
foreach (var (name, exe, exists) in McpRegistrar.ServerInfos())
{
flow.Controls.Add(new Label
{
Text = $"{name}: {exe}",
AutoSize = false, Width = 600, Height = 20,
ForeColor = exists ? Theme.SerialColor : Color.FromArgb(210, 150, 120),
Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 2)
});
}
if (!McpRegistrar.ServerExeExists())
{
flow.Controls.Add(new Label
{
Text = "⚠ Some servers not built yet — publish the app (or build the MCP projects). Setup still writes the expected paths.",
AutoSize = false, Width = 600, Height = 20,
ForeColor = Color.FromArgb(210, 150, 120), Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 4)
});
}
flow.Controls.Add(MakeSpacer(10));
flow.Controls.Add(BuildMcpTargetCard(McpTarget.Claude));
flow.Controls.Add(MakeSpacer(10));
flow.Controls.Add(BuildMcpTargetCard(McpTarget.Kiro));
page.Controls.Add(flow);
return page;
}
/// <summary>單一 AI 目標(Claude / Kiro)的設定卡:狀態 + Setup / Remove + CLI 驗證指令。</summary>
private Panel BuildMcpTargetCard(McpTarget target)
{
var card = new Panel
{
Width = 600, Height = 196, BackColor = Theme.TabBack,
Padding = new Padding(14), Margin = new Padding(0, 0, 0, 4)
};
var col = new FlowLayoutPanel
{
Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown,
WrapContents = false, BackColor = Theme.TabBack, AutoSize = false
};
var title = new Label
{
Text = McpRegistrar.DisplayName(target), AutoSize = true,
ForeColor = Theme.Text, Font = Theme.UiFontBold, Margin = new Padding(0, 0, 0, 2)
};
var status = new Label { AutoSize = true, Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 2) };
var pathLbl = new Label
{
Text = $"Config: {McpRegistrar.ConfigPath(target)}",
AutoSize = false, Width = 560, Height = 18,
ForeColor = Theme.TextDim, Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 6)
};
var setupBtn = MakeButton("Setup", Theme.Accent);
var removeBtn = MakeButton("Remove", Color.FromArgb(210, 120, 120));
setupBtn.Margin = new Padding(0, 0, 8, 0);
var btnRow = new FlowLayoutPanel
{
FlowDirection = FlowDirection.LeftToRight, AutoSize = true,
WrapContents = false, BackColor = Theme.TabBack, Margin = new Padding(0, 0, 0, 8)
};
btnRow.Controls.Add(setupBtn);
btnRow.Controls.Add(removeBtn);
var verifyLbl = new Label
{
Text = "Verify in your CLI:", AutoSize = true,
ForeColor = Theme.TextDim, Font = Theme.UiFont, Margin = new Padding(0, 0, 0, 2)
};
var verifyBox = new TextBox
{
Multiline = true, ReadOnly = true, Width = 560, Height = 56,
BackColor = Color.FromArgb(20, 20, 24), ForeColor = Color.FromArgb(200, 200, 200),
BorderStyle = BorderStyle.FixedSingle, Font = new Font("Cascadia Mono", 9f),
Text = McpRegistrar.VerifyHint(target)
};
void Refresh()
{
bool reg = McpRegistrar.IsRegistered(target);
status.Text = reg ? "● Configured" : "○ Not configured";
status.ForeColor = reg ? Theme.SerialColor : Theme.TextDim;
removeBtn.Enabled = reg;
}
setupBtn.Click += (_, _) =>
{
try
{
McpRegistrar.Register(target);
Refresh();
MessageBox.Show(this,
$"{McpRegistrar.DisplayName(target)} is now configured.\n\n" +
"Restart your AI CLI (or open a new session), then run the verify command shown below.",
"AI MCP", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, $"Failed to write config:\n{ex.Message}",
"AI MCP", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
};
removeBtn.Click += (_, _) =>
{
try
{
McpRegistrar.Unregister(target);
Refresh();
MessageBox.Show(this,
$"Removed from {McpRegistrar.DisplayName(target)}.\nRestart your AI CLI for it to take effect.",
"AI MCP", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, $"Failed to update config:\n{ex.Message}",
"AI MCP", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
};
Refresh();
col.Controls.Add(title);
col.Controls.Add(status);
col.Controls.Add(pathLbl);
col.Controls.Add(btnRow);
col.Controls.Add(verifyLbl);
col.Controls.Add(verifyBox);
card.Controls.Add(col);
return card;
}
// ── Helpers ── // ── Helpers ──
private const int LabelWidth = 150; // 標籤欄固定寬度 private const int LabelWidth = 150; // 標籤欄最小寬度
private const int InputWidth = 200; // 所有輸入框統一寬度 private const int InputWidth = 200; // 所有輸入框統一寬度
private static Panel MakeRow(string label, Control control) private static Control MakeRow(string label, Control control)
{ {
var row = new Panel { Width = LabelWidth + InputWidth, Height = 32, Margin = new Padding(0, 3, 0, 3) }; // 用 FlowLayout + AutoSize 標籤取代絕對定位,讓高 DPI 下標籤變寬也不會被裁、且對齊穩定
var row = new FlowLayoutPanel
// 統一輸入框寬度 + 左緣對齊(不再 Dock.Right,避免右對齊造成左緣參差)
control.Width = InputWidth;
int top = (row.Height - control.Height) / 2;
control.Location = new Point(LabelWidth, top < 0 ? 0 : top);
row.Controls.Add(control);
row.Controls.Add(new Label
{ {
Text = label, AutoSize = false, Width = LabelWidth, Height = row.Height, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
Location = new Point(0, 0), ForeColor = Theme.Text, Font = Theme.UiFont, FlowDirection = FlowDirection.LeftToRight, WrapContents = false,
TextAlign = ContentAlignment.MiddleLeft Margin = new Padding(0, 3, 0, 3), BackColor = Color.Transparent
}); };
var lbl = new Label
{
Text = label, AutoSize = true, MinimumSize = new Size(LabelWidth, 0),
ForeColor = Theme.Text, Font = Theme.UiFont,
TextAlign = ContentAlignment.MiddleLeft, Margin = new Padding(0, 6, 8, 0)
};
if (control.Width <= 0) control.Width = InputWidth;
row.Controls.Add(lbl);
row.Controls.Add(control);
return row; return row;
} }
@@ -302,7 +464,9 @@ public sealed class SettingsView : UserControl
{ {
var b = new Button var b = new Button
{ {
Text = text, Width = 90, Height = 28, FlatStyle = FlatStyle.Flat, Text = text, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(90, 28), Padding = new Padding(10, 2, 10, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont,
Cursor = Cursors.Hand, Margin = new Padding(8, 0, 0, 0) Cursor = Cursors.Hand, Margin = new Padding(8, 0, 0, 0)
}; };
+9 -4
View File
@@ -52,7 +52,8 @@ public sealed class WorkspaceView : UserControl
BackColor = Theme.WorkspaceBack; BackColor = Theme.WorkspaceBack;
// 頂部工具列容器:左側為 Layout/Run 群組(Fill),右側為 Log AllDock Right // 頂部工具列容器:左側為 Layout/Run 群組(Fill),右側為 Log AllDock Right
var topBar = new Panel { Dock = DockStyle.Top, Height = 38, BackColor = Theme.RailBack }; // 高度 40 與左側 ConnectionSidebar 的 tab bar 對齊,且容得下 AutoSize 按鈕(不被裁切)
var topBar = new Panel { Dock = DockStyle.Top, Height = 40, BackColor = Theme.RailBack };
_toolbar = new FlowLayoutPanel _toolbar = new FlowLayoutPanel
{ {
@@ -374,7 +375,9 @@ public sealed class WorkspaceView : UserControl
{ {
var b = new Button var b = new Button
{ {
Text = label, AutoSize = false, Size = new Size(44, 26), FlatStyle = FlatStyle.Flat, Text = label, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(40, 26), Padding = new Padding(8, 2, 8, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont,
Margin = new Padding(3, 0, 3, 0), Cursor = Cursors.Hand Margin = new Padding(3, 0, 3, 0), Cursor = Cursors.Hand
}; };
@@ -384,11 +387,13 @@ public sealed class WorkspaceView : UserControl
return b; return b;
} }
private static Button MakeActionButton(string text, int width, int leftMargin, EventHandler onClick) private static Button MakeActionButton(string text, int minWidth, int leftMargin, EventHandler onClick)
{ {
var b = new Button var b = new Button
{ {
Text = text, AutoSize = false, Size = new Size(width, 26), FlatStyle = FlatStyle.Flat, Text = text, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink,
MinimumSize = new Size(minWidth, 26), Padding = new Padding(10, 2, 10, 2),
FlatStyle = FlatStyle.Flat,
ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont, ForeColor = Theme.Text, BackColor = Theme.TabBack, Font = Theme.UiFont,
Margin = new Padding(leftMargin, 0, 3, 0), Cursor = Cursors.Hand Margin = new Padding(leftMargin, 0, 3, 0), Cursor = Cursors.Hand
}; };
+24 -1
View File
@@ -10,10 +10,13 @@
<AssemblyName>ETTerms</AssemblyName> <AssemblyName>ETTerms</AssemblyName>
<!-- 版本資訊 --> <!-- 版本資訊 -->
<Version>0.2.0</Version> <Version>0.3.0</Version>
<Product>ETTerms</Product> <Product>ETTerms</Product>
<Company>ETTerms Project</Company> <Company>ETTerms Project</Company>
<!-- 高 DPIPerMonitorV2,讓不同縮放比例的螢幕都能正確縮放、避免文字/按鈕被裁切 -->
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
<!-- 視窗 / 工作列 / exe 圖示 --> <!-- 視窗 / 工作列 / exe 圖示 -->
<ApplicationIcon>Assets\Choco_256x256.ico</ApplicationIcon> <ApplicationIcon>Assets\Choco_256x256.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
@@ -32,4 +35,24 @@
<PackageReference Include="System.IO.Ports" Version="8.0.0" /> <PackageReference Include="System.IO.Ports" Version="8.0.0" />
</ItemGroup> </ItemGroup>
<!--
發佈 GUI 時,自動把 MCP serversSerial + PDU)一併發佈到 <publish>\<server>\ 子資料夾。
這樣單一 `dotnet publish src\ETTerms` 就會產生完整自洽的 bundle,
且 McpRegistrar.ResolveServerExe() 解析的 <ETTerms.exe>\<server>\<server>.exe 必定存在。
刻意放子資料夾:與 GUI 的相依 dll 隔離,避免互相覆蓋。
MCP 跟隨 GUI 的 self-contained 設定:框架相依版 → MCP 也框架相依;portable(self-contained)版 → MCP 也免 runtime。
-->
<Target Name="PublishMcpServers" AfterTargets="Publish">
<PropertyGroup>
<_McpRid Condition="'$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</_McpRid>
<_McpRid Condition="'$(RuntimeIdentifier)' == ''">win-x64</_McpRid>
<_McpSelfContained Condition="'$(SelfContained)' != ''">$(SelfContained)</_McpSelfContained>
<_McpSelfContained Condition="'$(SelfContained)' == ''">false</_McpSelfContained>
</PropertyGroup>
<Message Importance="high" Text="[ETTerms] Publishing ETTerms.SerialMcp -> $(PublishDir)ETTerms.SerialMcp (self-contained=$(_McpSelfContained))" />
<Exec Command="dotnet publish &quot;$(MSBuildThisFileDirectory)..\ETTerms.SerialMcp\ETTerms.SerialMcp.csproj&quot; -c $(Configuration) -r $(_McpRid) --self-contained $(_McpSelfContained) -o &quot;$(PublishDir)ETTerms.SerialMcp&quot;" />
<Message Importance="high" Text="[ETTerms] Publishing ETTerms.PduMcp -> $(PublishDir)ETTerms.PduMcp (self-contained=$(_McpSelfContained))" />
<Exec Command="dotnet publish &quot;$(MSBuildThisFileDirectory)..\ETTerms.PduMcp\ETTerms.PduMcp.csproj&quot; -c $(Configuration) -r $(_McpRid) --self-contained $(_McpSelfContained) -o &quot;$(PublishDir)ETTerms.PduMcp&quot;" />
</Target>
</Project> </Project>
+199
View File
@@ -0,0 +1,199 @@
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace ETTerms.Infrastructure;
/// <summary>支援一鍵設定 MCP 的 AI CLI 目標。</summary>
public enum McpTarget { Claude, Kiro }
/// <summary>
/// 把 ETTerms 的 MCP servers<c>ETTerms.SerialMcp</c> 與 <c>ETTerms.PduMcp</c>)一鍵註冊 /
/// 移除到各 AI CLI 的「使用者層級」MCP 設定檔。採 read-modify-write,保留檔內其他既有伺服器。
///
/// - Claude Code<c>~/.claude.json</c> 頂層 <c>mcpServers</c>entry 需 <c>type:"stdio"</c>。
/// - Kiro<c>%USERPROFILE%\.kiro\settings\mcp.json</c> 頂層 <c>mcpServers</c>。
///
/// 兩個 server 一起註冊 / 移除(一鍵同時設定 serial 與 pdu)。
/// </summary>
public static class McpRegistrar
{
/// <summary>一個可被註冊的 MCP server 描述:CLI 內名稱 + 發佈子資料夾 + 執行檔名。</summary>
public sealed record McpServer(string Name, string PublishFolder, string ExeName);
/// <summary>ETTerms 提供的所有 MCP servers。</summary>
public static readonly IReadOnlyList<McpServer> Servers = new[]
{
new McpServer("etterms-serial", "ETTerms.SerialMcp", "ETTerms.SerialMcp.exe"),
new McpServer("etterms-pdu", "ETTerms.PduMcp", "ETTerms.PduMcp.exe"),
};
public static string DisplayName(McpTarget t) => t switch
{
McpTarget.Claude => "Claude Code",
McpTarget.Kiro => "Kiro",
_ => t.ToString()
};
/// <summary>該 AI CLI 的使用者層級 MCP 設定檔路徑。</summary>
public static string ConfigPath(McpTarget t)
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
return t switch
{
McpTarget.Claude => Path.Combine(home, ".claude.json"),
McpTarget.Kiro => Path.Combine(home, ".kiro", "settings", "mcp.json"),
_ => throw new ArgumentOutOfRangeException(nameof(t))
};
}
/// <summary>給使用者在 CLI 確認是否設定成功的指令(多行)。</summary>
public static string VerifyHint(McpTarget t) => t switch
{
McpTarget.Claude =>
"claude mcp list\r\n" +
"# 應看到:etterms-serial / etterms-pdu ✓ Connected\r\n" +
"# 細節: claude mcp get etterms-pdu",
McpTarget.Kiro =>
"kiro-cli mcp list\r\n" +
"kiro-cli mcp status --name etterms-pdu\r\n" +
"# 或在 Kiro IDE:點 ghost 圖示開 MCP Servers 面板查看狀態",
_ => ""
};
/// <summary>找出某個 MCP server 執行檔路徑(找不到回傳最可能的位置作為註冊值)。</summary>
public static string ResolveServerExe(McpServer server)
{
var baseDir = AppContext.BaseDirectory;
var candidates = new List<string>
{
Path.Combine(baseDir, server.PublishFolder, server.ExeName), // 發佈版(子資料夾)
Path.Combine(baseDir, server.ExeName), // 同層
};
// 開發版 fallbacksrc\ETTerms\bin\<cfg>\net8.0-windows → src\<folder>\bin\<cfg>\net8.0
try
{
var binCfg = new DirectoryInfo(baseDir); // ...\net8.0-windows
var config = binCfg.Parent?.Name ?? "Debug"; // Debug / Release
var srcDir = binCfg.Parent?.Parent?.Parent?.Parent; // ...\src
if (srcDir != null)
candidates.Add(Path.Combine(srcDir.FullName, server.PublishFolder, "bin", config, "net8.0", server.ExeName));
}
catch { /* 路徑推導失敗就略過開發版 fallback */ }
foreach (var c in candidates)
if (File.Exists(c)) return c;
return candidates[0]; // 都找不到 → 回發佈版預期位置
}
/// <summary>所有 server 執行檔是否都存在。</summary>
public static bool ServerExeExists() => Servers.All(s => File.Exists(ResolveServerExe(s)));
/// <summary>列出每個 server 的解析路徑與是否存在(給 UI 顯示)。</summary>
public static IEnumerable<(string Name, string Exe, bool Exists)> ServerInfos()
{
foreach (var s in Servers)
{
var exe = ResolveServerExe(s);
yield return (s.Name, exe, File.Exists(exe));
}
}
/// <summary>該目標是否已註冊「全部」ETTerms MCP servers。</summary>
public static bool IsRegistered(McpTarget t)
{
try
{
var path = ConfigPath(t);
if (!File.Exists(path)) return false;
var servers = (JsonNode.Parse(File.ReadAllText(path)) as JsonObject)?["mcpServers"] as JsonObject;
if (servers == null) return false;
return Servers.All(s => servers[s.Name] != null);
}
catch { return false; }
}
/// <summary>註冊(或更新)所有 ETTerms MCP servers 到該目標設定檔。</summary>
public static void Register(McpTarget t)
{
var path = ConfigPath(t);
var dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
var root = LoadRoot(path);
if (root["mcpServers"] is not JsonObject servers)
{
servers = new JsonObject();
root["mcpServers"] = servers;
}
foreach (var s in Servers)
servers[s.Name] = BuildEntry(t, s);
WriteRoot(path, root);
AppLogger.Info($"MCP registered to {DisplayName(t)} at {path}");
}
/// <summary>從該目標設定檔移除所有 ETTerms MCP servers。</summary>
public static void Unregister(McpTarget t)
{
var path = ConfigPath(t);
if (!File.Exists(path)) return;
var root = LoadRoot(path);
if (root["mcpServers"] is JsonObject servers)
{
bool changed = false;
foreach (var s in Servers)
changed |= servers.Remove(s.Name);
if (changed)
{
WriteRoot(path, root);
AppLogger.Info($"MCP unregistered from {DisplayName(t)} at {path}");
}
}
}
private static JsonObject BuildEntry(McpTarget t, McpServer server)
{
var exe = ResolveServerExe(server);
return t switch
{
// Claude Codestdio server 需 type 欄位
McpTarget.Claude => new JsonObject
{
["type"] = "stdio",
["command"] = exe,
["args"] = new JsonArray()
},
// Kirolocal server,附 env / disabled / autoApprove 預設
McpTarget.Kiro => new JsonObject
{
["command"] = exe,
["args"] = new JsonArray(),
["env"] = new JsonObject(),
["disabled"] = false,
["autoApprove"] = new JsonArray()
},
_ => throw new ArgumentOutOfRangeException(nameof(t))
};
}
/// <summary>讀入設定檔為可變 JSON 物件;不存在回空物件。檔案存在但格式錯誤則丟例外(不覆蓋使用者資料)。</summary>
private static JsonObject LoadRoot(string path)
{
if (!File.Exists(path)) return new JsonObject();
var text = File.ReadAllText(path);
if (string.IsNullOrWhiteSpace(text)) return new JsonObject();
if (JsonNode.Parse(text) is JsonObject obj) return obj;
throw new InvalidDataException($"{path} 不是有效的 JSON 物件,為避免覆蓋資料已中止。請手動檢查該檔。");
}
/// <summary>原子寫回(先寫 .tmp 再 replace),避免半寫壞檔。</summary>
private static void WriteRoot(string path, JsonObject root)
{
var json = root.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
var tmp = path + ".tmp";
File.WriteAllText(tmp, json);
if (File.Exists(path)) File.Replace(tmp, path, null);
else File.Move(tmp, path);
}
}
+19 -10
View File
@@ -270,20 +270,29 @@ public sealed class ScreenBuffer
{ {
cols = Math.Max(1, cols); rows = Math.Max(1, rows); cols = Math.Max(1, cols); rows = Math.Max(1, rows);
if (cols == Cols && rows == Rows) return; if (cols == Cols && rows == Rows) return;
var ng = new Cell[rows][]; _screen = Reflow(_screen, rows, cols);
for (int r = 0; r < rows; r++) // alt screen 啟用中也要一併 reflow 保存的主畫面,否則 resize 後 ExitAlt 會還原失敗、
{ // 畫面殘留 TUI 內容。不可把 _mainScreen 丟掉。
ng[r] = new Cell[cols]; if (_mainScreen != null) _mainScreen = Reflow(_mainScreen, rows, cols);
for (int c = 0; c < cols; c++)
ng[r][c] = (r < Rows && c < Cols) ? _screen[r][c] : BlankDefault();
}
_screen = ng;
Rows = rows; Cols = cols; Rows = rows; Cols = cols;
_top = 0; _bottom = Rows - 1; _top = 0; _bottom = Rows - 1;
CursorRow = Math.Clamp(CursorRow, 0, Rows - 1); CursorRow = Math.Clamp(CursorRow, 0, Rows - 1);
CursorCol = Math.Clamp(CursorCol, 0, Cols - 1); CursorCol = Math.Clamp(CursorCol, 0, Cols - 1);
_wrapPending = false; _wrapPending = false;
_mainScreen = null; }
if (AltActive) { /* alt 下 resize:重建 alt 畫面 */ }
/// <summary>把字格陣列重排到新尺寸,保留左上重疊區,其餘填預設空白。</summary>
private Cell[][] Reflow(Cell[][] src, int rows, int cols)
{
int oldRows = src.Length;
var ng = new Cell[rows][];
for (int r = 0; r < rows; r++)
{
ng[r] = new Cell[cols];
int oldCols = r < oldRows ? src[r].Length : 0;
for (int c = 0; c < cols; c++)
ng[r][c] = (c < oldCols) ? src[r][c] : BlankDefault();
}
return ng;
} }
} }
+5 -1
View File
@@ -9,6 +9,11 @@ public static class TerminalInput
{ {
public static byte[]? Map(KeyEventArgs e, bool appCursor) public static byte[]? Map(KeyEventArgs e, bool appCursor)
{ {
// Enter:一般送 CR(\r) 視為「送出」;Shift+Enter 送 LF(\n),讓 PSReadLine / Kiro 等
// 視為「插入換行」以便輸入多行指令,而不是直接送出。
if (e.KeyCode == Keys.Enter)
return new[] { (byte)(e.Shift ? '\n' : '\r') };
string? seq = e.KeyCode switch string? seq = e.KeyCode switch
{ {
Keys.Up => appCursor ? "\x1bOA" : "\x1b[A", Keys.Up => appCursor ? "\x1bOA" : "\x1b[A",
@@ -21,7 +26,6 @@ public static class TerminalInput
Keys.Delete => "\x1b[3~", Keys.Delete => "\x1b[3~",
Keys.PageUp => "\x1b[5~", Keys.PageUp => "\x1b[5~",
Keys.PageDown => "\x1b[6~", Keys.PageDown => "\x1b[6~",
Keys.Enter => "\r",
Keys.Tab => "\t", Keys.Tab => "\t",
Keys.Escape => "\x1b", Keys.Escape => "\x1b",
Keys.Back => "\x7f", Keys.Back => "\x7f",
+7
View File
@@ -59,6 +59,13 @@ public sealed class TerminalView : UserControl
protected override void OnSizeChanged(EventArgs e) protected override void OnSizeChanged(EventArgs e)
{ {
base.OnSizeChanged(e); base.OnSizeChanged(e);
// 視窗最小化、或分頁/分割切換的瞬間,ClientSize 會變成 0/極小。此時若把退化尺寸
// (會被算成 1×1)送給 PTY,ConPTY 下的全螢幕 TUI(如 Kiro CLI)會誤以為終端機只剩
// 1×1 而停止重繪、輸入也像「卡住」。直接忽略這種尺寸,還原後再以正常尺寸重繪即可。
if (ClientSize.Width < _cellW || ClientSize.Height < _cellH) return;
if (FindForm() is { WindowState: FormWindowState.Minimized }) return;
int cols = VisibleCols, rows = VisibleRows; int cols = VisibleCols, rows = VisibleRows;
if (cols == _lastCols && rows == _lastRows) return; if (cols == _lastCols && rows == _lastRows) return;
_lastCols = cols; _lastRows = rows; _lastCols = cols; _lastRows = rows;