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:
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user