Files
SimpleRemoter/server/go/mcp/tools.go
T
yuanyuanxianganddeepseek-flash 808747f9d2 Feature(Go): terminal_close, idle sweep and legacy-client guard
Add the terminal_close tool so a caller can release a shell session
explicitly instead of waiting for it to age out. The tool is idempotent
for the session it names, rejects a session_id that does not match the
live session with -32002, and runs without the per-device lock so it can
interrupt a terminal_exec call that is still polling on the same device.

Give hub leases a generation counter: the generation is bumped when a
pending lease is created and carried across a device re-login. MCP-side
closes pass the generation, so a stale session can no longer tear down a
shell a human operator opened after the device reconnected. The reaper
runs on a 30 s ticker (shortened when TermIdle is smaller), skips
sessions that are still opening, and reclaims idle sessions as well as
commands overdue past the one-hour busy cap.

The pre-bind terminal-close path in cmd no longer closes
unconditionally; it touches only a pending lease, because an unbound
sub-connection carries no lease identity and may be a stale orphan.

Covered by unit tests for close semantics, lock exemption, reaper
lifecycle and lease-generation ownership, plus a live run against a
Debug ghost: close before and during a command, reopen, idle reap and
mismatched-id rejection all behave as specified.

Co-Authored-By: deepseek-flash
2026-11-12 21:53:54 +01:00

89 lines
2.8 KiB
Go

package mcp
import (
_ "embed"
"encoding/json"
)
// tools_minimal.json is generated from mcp/tools.json by
// tools/mcp_tools/gen_go_tools.py — never edit it by hand. It is embedded so
// tools/list is a compile-time constant: no disk I/O, no drift between the
// schema the model sees and the schema the handlers were written against.
//
//go:embed tools_minimal.json
var toolsJSON []byte
// writeTools are hidden from tools/list in readonly mode and rejected with
// -32006 if called anyway (design §3.1: readonly exposes 7 of 9 tools).
var writeTools = map[string]bool{
"terminal_exec": true,
"terminal_close": true,
}
// unlockedTools run without the per-device lock. terminal_close must be usable
// while a terminal_exec call is still waiting on the same device: that call
// holds the lock for its whole timeout, and a close that answers -32003 could
// never interrupt it.
var unlockedTools = map[string]bool{
"terminal_close": true,
}
// toolDef holds one published definition plus its name, so filtering does not
// re-parse the JSON on every tools/list call.
type toolDef struct {
name string
raw json.RawMessage
}
type toolSet struct {
defs []toolDef
names map[string]bool
}
// buildToolSet validates the embedded payload once at startup. A malformed
// file is a build defect, so panic loudly rather than serve a broken list.
func buildToolSet(raw []byte) *toolSet {
var raws []json.RawMessage
if err := json.Unmarshal(raw, &raws); err != nil {
panic("mcp: embedded tools_minimal.json is not a JSON array: " + err.Error())
}
ts := &toolSet{defs: make([]toolDef, 0, len(raws)), names: make(map[string]bool, len(raws))}
for _, r := range raws {
var head struct {
Name string `json:"name"`
}
if err := json.Unmarshal(r, &head); err != nil || head.Name == "" {
panic("mcp: embedded tool definition without a name")
}
if ts.names[head.Name] {
panic("mcp: duplicate embedded tool " + head.Name)
}
ts.names[head.Name] = true
ts.defs = append(ts.defs, toolDef{name: head.Name, raw: r})
}
return ts
}
// visible returns the tool definitions to publish, preserving tools.json
// order. The raw bytes are passed through untouched, so descriptions and
// schemas stay exactly what mcp/tools.json says (no annotations, like MFC).
func (ts *toolSet) visible(readonly bool) []json.RawMessage {
out := make([]json.RawMessage, 0, len(ts.defs))
for _, def := range ts.defs {
if readonly && writeTools[def.name] {
continue
}
out = append(out, def.raw)
}
return out
}
// known reports whether name is a tool this server defines, including tools
// hidden by readonly mode. Unknown names are -32601; hidden ones are -32006.
func (ts *toolSet) known(name string) bool { return ts.names[name] }
// toolsListResult is the MCP tools/list payload.
type toolsListResult struct {
Tools []json.RawMessage `json:"tools"`
}