// Package bridge records Hermes' visits through the app's MCP endpoint. Hermes // holds the connection to the Core's MCP hub; the app never does. In Phase 0 // a visit is a check-in that proves the route works and lists the hub's // tools; later phases add the CRM queue on the same route. package bridge import ( "database/sql" "encoding/json" "errors" "strings" "time" "supermarket/internal/db" ) // Checkin is one visit from Hermes. type Checkin struct { At time.Time Agent string MCPHubTools []string Note string } // hermesPrefix is how Hermes names an MCP server's tools in its own catalog. const hermesPrefix = "mcp__mcphub__" // HubToolNames returns the names as the hub itself gives them: agents may // report their own prefixed names (Hermes: mcp__mcphub__espocrm_get_record), // and later phases look tools up by the hub's name (espocrm_get_record). func HubToolNames(names []string) []string { out := make([]string, 0, len(names)) for _, n := range names { n = strings.TrimPrefix(strings.TrimSpace(n), hermesPrefix) if n != "" { out = append(out, n) } } return out } // Record stores a check-in. func Record(d *db.DB, c Checkin) error { if c.MCPHubTools != nil { c.MCPHubTools = HubToolNames(c.MCPHubTools) } tools, err := json.Marshal(c.MCPHubTools) if err != nil { return err } if c.MCPHubTools == nil { tools = []byte("[]") } _, err = d.Exec(`INSERT INTO bridge_checkins (agent, mcphub_tools, note) VALUES (?, ?, ?)`, c.Agent, string(tools), c.Note) if err != nil { return err } // Keep the last 100; older ones say nothing new. _, err = d.Exec(`DELETE FROM bridge_checkins WHERE id NOT IN (SELECT id FROM bridge_checkins ORDER BY id DESC LIMIT 100)`) return err } // Last returns the latest check-in, or nil if Hermes never checked in. func Last(d *db.DB) (*Checkin, error) { var at, tools string c := &Checkin{} err := d.QueryRow(`SELECT at, agent, mcphub_tools, note FROM bridge_checkins ORDER BY id DESC LIMIT 1`). Scan(&at, &c.Agent, &tools, &c.Note) if errors.Is(err, sql.ErrNoRows) { return nil, nil } if err != nil { return nil, err } c.At, _ = time.Parse(time.RFC3339, at) _ = json.Unmarshal([]byte(tools), &c.MCPHubTools) return c, nil } // CheckinPrompt is what to ask Hermes to test the route end to end. Hermes // must reach both MCP servers for it to work. const CheckinPrompt = `Run the Supermarket bridge check: list the names of all the tools you have from the "mcphub" MCP server, then call the "bridge_checkin" tool of the "supermarket" MCP server with agent "hermes", mcphub_tools set to that list, and a one-line note on anything that failed. Don't call any other tool.`