Connect Hermes to the Core's MCP hub and to Supermarket
Hermes runs in CoreDesktop's sandbox, with its own network, so it can't reach 127.0.0.1:8470. The app now also serves MCP on a Unix socket in its data folder (/data/mcp.sock), which Hermes reaches with socat. HTTP /mcp with the bearer token remains for other agents. hermes/setup.sh asks for the user's own hub token (the Core now issues per-user tokens), checks it with the hub, and stores it in Hermes' .env. The hub only speaks SSE, and Hermes' Streamable HTTP attempt can use up its connect time, so the setup sets transport: sse. It also warns when Hermes' gateway is still running with the old servers. Check-ins store the hub's own tool names, without Hermes' mcp__mcphub__ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/db"
|
||||
@@ -21,8 +22,28 @@ type Checkin struct {
|
||||
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
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHubToolNames(t *testing.T) {
|
||||
got := HubToolNames([]string{
|
||||
"mcp__mcphub__espocrm_get_record",
|
||||
" espocrm_search_records ",
|
||||
"mcp__mcphub__",
|
||||
"",
|
||||
"nextcloud_list_files",
|
||||
})
|
||||
want := []string{"espocrm_get_record", "espocrm_search_records", "nextcloud_list_files"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("HubToolNames = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,17 @@
|
||||
// Package mcpserver exposes the app's data and actions to agents (Hermes,
|
||||
// Claude Code) over MCP at /mcp, behind a bearer token.
|
||||
// Package mcpserver exposes the app's data and actions to agents over MCP:
|
||||
// at /mcp behind a bearer token (Claude Code), and on a Unix socket in the
|
||||
// data folder (Hermes).
|
||||
package mcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
@@ -20,9 +25,7 @@ import (
|
||||
// Handler returns the /mcp handler. Requests without "Authorization: Bearer
|
||||
// <token>" are refused.
|
||||
func Handler(d *db.DB, token, version string) http.Handler {
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "supermarket", Version: version}, nil)
|
||||
addTools(server, d)
|
||||
|
||||
server := newServer(d, version)
|
||||
h := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server },
|
||||
&mcp.StreamableHTTPOptions{Stateless: true, JSONResponse: true})
|
||||
|
||||
@@ -37,6 +40,57 @@ func Handler(d *db.DB, token, version string) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func newServer(d *db.DB, version string) *mcp.Server {
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "supermarket", Version: version}, nil)
|
||||
addTools(server, d)
|
||||
return server
|
||||
}
|
||||
|
||||
// ServeSocket serves MCP on a Unix socket at path, one stdio-style session
|
||||
// (newline-delimited JSON-RPC) per connection, until ctx ends.
|
||||
//
|
||||
// This is how Hermes reaches the app. CoreDesktop runs Hermes in a sandbox
|
||||
// with its own network, where 127.0.0.1:8470 isn't this computer's, so Hermes
|
||||
// runs `socat STDIO UNIX-CONNECT:<path>` as a stdio MCP server instead. The
|
||||
// socket has no token: it is only usable by you (the umask makes it 0700),
|
||||
// and anything that can open it can read mcp-token in the same folder anyway.
|
||||
func ServeSocket(ctx context.Context, d *db.DB, version, path string, log *slog.Logger) error {
|
||||
// A socket left by the last run would make Listen fail.
|
||||
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
ln, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(path, 0o600); err != nil {
|
||||
ln.Close()
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
ln.Close()
|
||||
}()
|
||||
|
||||
server := newServer(d, version)
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
err := server.Run(ctx, &mcp.IOTransport{Reader: conn, Writer: conn})
|
||||
if err != nil && ctx.Err() == nil && !errors.Is(err, net.ErrClosed) {
|
||||
log.Debug("MCP socket session ended", "err", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func jsonResult(v any) (*mcp.CallToolResult, error) {
|
||||
raw, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package mcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"supermarket/internal/bridge"
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
// Hermes reaches the app through the socket with `socat STDIO UNIX-CONNECT:`,
|
||||
// which is a plain stdio MCP session over the connection.
|
||||
func TestServeSocket(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
d, err := db.Open(filepath.Join(dir, "app.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
path := filepath.Join(dir, "mcp.sock")
|
||||
// A socket left over from the last run must not stop the next one.
|
||||
if err := os.WriteFile(path, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- ServeSocket(ctx, d, "test", path, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
}()
|
||||
|
||||
var conn net.Conn
|
||||
for i := 0; i < 100; i++ {
|
||||
if conn, err = net.Dial("unix", path); err == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fi, err := os.Stat(path); err != nil || fi.Mode().Perm() != 0o600 {
|
||||
t.Errorf("socket mode = %v, %v; want 0600", fi.Mode().Perm(), err)
|
||||
}
|
||||
|
||||
client := mcp.NewClient(&mcp.Implementation{Name: "test"}, nil)
|
||||
session, err := client.Connect(ctx, &mcp.IOTransport{Reader: conn, Writer: conn}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tools, err := session.ListTools(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, tool := range tools.Tools {
|
||||
found = found || tool.Name == "bridge_checkin"
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("bridge_checkin not among %d tools", len(tools.Tools))
|
||||
}
|
||||
|
||||
res, err := session.CallTool(ctx, &mcp.CallToolParams{
|
||||
Name: "bridge_checkin",
|
||||
Arguments: map[string]any{"agent": "hermes", "mcphub_tools": []string{"espocrm_search"}},
|
||||
})
|
||||
if err != nil || res.IsError {
|
||||
t.Fatalf("bridge_checkin: %v %+v", err, res)
|
||||
}
|
||||
last, err := bridge.Last(d)
|
||||
if err != nil || last == nil || last.Agent != "hermes" || len(last.MCPHubTools) != 1 {
|
||||
t.Fatalf("last check-in = %+v, %v", last, err)
|
||||
}
|
||||
|
||||
session.Close()
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("ServeSocket: %v", err)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("ServeSocket didn't stop")
|
||||
}
|
||||
}
|
||||
+11
-10
@@ -184,6 +184,7 @@ type coreData struct {
|
||||
User string
|
||||
HubURL string
|
||||
MCPURL string
|
||||
MCPSocket string
|
||||
MCPToken string
|
||||
Last *bridge.Checkin
|
||||
Prompt string
|
||||
@@ -198,11 +199,12 @@ func (s *Server) corePage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
d := coreData{
|
||||
Core: s.Core,
|
||||
MCPURL: "http://127.0.0.1:" + publicPort(s.PublicURL) + "/mcp",
|
||||
MCPToken: s.MCPToken,
|
||||
Last: last,
|
||||
Prompt: bridge.CheckinPrompt,
|
||||
Core: s.Core,
|
||||
MCPURL: "http://127.0.0.1:" + publicPort(s.PublicURL) + "/mcp",
|
||||
MCPToken: s.MCPToken,
|
||||
MCPSocket: s.MCPSocket,
|
||||
Last: last,
|
||||
Prompt: bridge.CheckinPrompt,
|
||||
}
|
||||
hub := "https://mcphub.<your Core's domain>/sse?as_user=<your user name>"
|
||||
if s.Core != nil {
|
||||
@@ -213,14 +215,13 @@ func (s *Server) corePage(w http.ResponseWriter, r *http.Request) {
|
||||
d.HermesConfig = `mcp_servers:
|
||||
mcphub:
|
||||
url: "` + hub + `"
|
||||
transport: sse
|
||||
headers:
|
||||
Authorization: "Basic ${MCPHUB_AUTH}"
|
||||
supermarket:
|
||||
url: "` + d.MCPURL + `"
|
||||
headers:
|
||||
Authorization: "Bearer ${SUPERMARKET_MCP_TOKEN}"`
|
||||
d.HermesEnv = `MCPHUB_AUTH=<base64 of user:password>
|
||||
SUPERMARKET_MCP_TOKEN=<the token below>`
|
||||
command: socat
|
||||
args: ["STDIO", "UNIX-CONNECT:` + d.MCPSocket + `"]`
|
||||
d.HermesEnv = `MCPHUB_AUTH=<your own hub token from your Core: the part after "Basic ">`
|
||||
s.render(w, r, "core", page{Title: "Core & Hermes", Nav: "core", Data: d})
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ type Server struct {
|
||||
PublicURL string // e.g. http://localhost:8470, for OAuth redirect URIs
|
||||
Version string
|
||||
MCPToken string
|
||||
MCPSocket string // the MCP socket's path on the host, for Hermes' config
|
||||
MCP http.Handler
|
||||
Log *slog.Logger
|
||||
|
||||
|
||||
@@ -14,14 +14,14 @@
|
||||
|
||||
<section class="card">
|
||||
<h2>Hermes bridge</h2>
|
||||
<p>Hermes, CoreDesktop's agent, is the only thing that talks to your Core's MCP hub. It reaches Supermarket at <code>{{.MCPURL}}</code>.</p>
|
||||
<p>Hermes, CoreDesktop's agent, is the only thing that talks to your Core's MCP hub. It reaches Supermarket through the socket <code>{{.MCPSocket}}</code>; other agents, such as Claude Code, use <code>{{.MCPURL}}</code> with the MCP token.</p>
|
||||
{{with .Last}}
|
||||
<p class="ok">Hermes last checked in {{ago .At}}{{if .Note}}: “{{.Note}}”{{end}}.</p>
|
||||
{{if .MCPHubTools}}
|
||||
<details><summary>It can use {{len .MCPHubTools}} tools from the MCP hub</summary>
|
||||
<ul class="pills">{{range .MCPHubTools}}<li><span class="pill">{{.}}</span></li>{{end}}</ul>
|
||||
</details>
|
||||
{{else}}<p class="warn-text">It reported no tools from the MCP hub: check the hub's address and password.</p>{{end}}
|
||||
{{else}}<p class="warn-text">It reported no tools from the MCP hub: check the hub's address and your hub token.</p>{{end}}
|
||||
{{else}}
|
||||
<p>Hermes hasn't checked in yet.</p>
|
||||
{{end}}
|
||||
@@ -38,7 +38,7 @@
|
||||
<div class="copy"><pre id="hermes-config">{{.HermesConfig}}</pre><button type="button" class="secondary" data-copy="hermes-config">Copy</button></div>
|
||||
<p>and this to <code>~/.hermes/.env</code>, so no password is in <code>config.yaml</code>:</p>
|
||||
<pre>{{.HermesEnv}}</pre>
|
||||
<p>Hermes runs in CoreDesktop's sandbox, so also let it reach both servers: add <code>mcphub.{{if .Core}}{{.Core.Domain}}{{else}}<your Core's domain>{{end}}</code> and <code>127.0.0.1:8470</code> to <code>network.allowedDomains</code> in <code>~/.config/coredesktop/sandbox/hermes.json</code>. Then restart Hermes' gateway: <code>systemctl --user restart hermes-gateway</code>.</p>
|
||||
<p>Hermes runs in CoreDesktop's sandbox, so also let it reach the hub: add <code>mcphub.{{if .Core}}{{.Core.Domain}}{{else}}<your Core's domain>{{end}}</code> to <code>network.allowedDomains</code> in <code>~/.config/coredesktop/sandbox/hermes.json</code>. Then restart Hermes' gateway: <code>systemctl --user restart hermes-gateway</code>.</p>
|
||||
<details><summary>Show the MCP token</summary><pre>{{.MCPToken}}</pre><p class="help">Also in <code>mcp-token</code> in the data folder.</p></details>
|
||||
</details>
|
||||
{{end}}{{end}}
|
||||
|
||||
Reference in New Issue
Block a user