mirror of
https://github.com/alvinunreal/tmuxai.git
synced 2026-10-12 03:49:23 +02:00
3.2 KiB
3.2 KiB
internal/mcp/
Responsibility
- Own the MCP integration layer: load/validate MCP server config, manage MCP client sessions, expose discovered tool metadata, execute remote MCP tool calls, and parse
<MCPToolCall>directives from model output. - Keep lifecycle safety (startup, reconnect, shutdown, and process cleanup) while tracking server health and availability for the rest of tmuxai.
Design
- Config types (
types.go):MCPConfigandServerConfigdescribe server transport/auth/runtime knobs;ServerInfoandToolDefmodel runtime state and tool metadata. - Lifecycle manager (
MCPManager): centralized coordinator with mutex-protected maps for:- server metadata (
servers) - active sessions (
sessions) - stdio process handles for process-group cleanup (
cmds) - in-flight request counters (
inFlight, lock-free sync.Map + atomic). Plus cached tool-definition text for prompt inclusion.
- server metadata (
- Transport/path abstraction: transport resolution defaults empty
typeto command→stdio or URL→sse, then creates SDK transport for stdio / SSE / streamable-HTTP. - Validation/parsing:
LoadConfigreads JSON, applies${VAR}expansion, validates required transport fields, and returnsnil,nilif file is absent.ParseMCPToolCallsextracts JSON calls from<MCPToolCall>...</MCPToolCall>blocks and strips them from text.
- Registry:
Registrybuilds fully-qualified namesmcp__<server>__<tool>from healthy servers for deterministic tool lookup.
Data & Control Flow
- Load phase:
DefaultConfigPath -> LoadConfig -> Validate; expansion and structural validation happen before runtime use. - Startup:
NewMCPManager(cfg)creates context and empty state;InitrunsinitServerfor each configured server (parallel). - initServer: set pending status, create transport, connect (
Client.Connect), fetch tools (Tools), then publish healthy status and tool list; failures are recorded as unhealthy with error text. - Execution:
- Caller resolves a fully-qualified name via
Registry.Lookup. ExecuteToolCallincrements in-flight counter, enforces timeout, gets session, parses arguments, calls tool, performs one reconnect retry on failure (callWithRetry), normalizes output (extractText+SanitizeResult), and returns error flag.
- Caller resolves a fully-qualified name via
- Change/recovery:
Reloadcomputes add/restart/remove sets via config diff, drains in-flight calls, tears down impacted servers, reinitializes targets, updates config.ReconnectServertargets one server;Shutdowncancels manager context, closes all sessions, and kills stdio process groups.
- Observability helpers:
GetServerInfo,ListTools,ToolDefinitionsBlock, andInvalidateCachesupport status/query and cached prompt formatting.
Integration Points
- Depends on go-sdk MCP (
github.com/modelcontextprotocol/go-sdk/mcp) for transport/session/call APIs. - Emits structured diagnostics through tmuxai/logger.
- Reads/expands environment from the host process and loads config from
${HOME}/.config/tmuxai/mcp.jsonvia stdlib OS/JSON APIs. - Invoked by higher application layers that build tool registries, execute tools on user/model requests, and trigger config reload/shutdown events.