// Package mcpserver exposes the app's data and actions to agents (Hermes, // Claude Code) over MCP at /mcp, behind a bearer token. package mcpserver import ( "context" "crypto/subtle" "encoding/json" "net/http" "strings" "github.com/modelcontextprotocol/go-sdk/mcp" "supermarket/internal/bridge" "supermarket/internal/db" "supermarket/internal/inventory" "supermarket/internal/profile" ) // Handler returns the /mcp handler. Requests without "Authorization: Bearer // " 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) h := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server }, &mcp.StreamableHTTPOptions{Stateless: true, JSONResponse: true}) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { got, ok := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ") if !ok || token == "" || subtle.ConstantTimeCompare([]byte(got), []byte(token)) != 1 { w.Header().Set("WWW-Authenticate", `Bearer realm="supermarket"`) http.Error(w, "unauthorized", http.StatusUnauthorized) return } h.ServeHTTP(w, r) }) } func jsonResult(v any) (*mcp.CallToolResult, error) { raw, err := json.MarshalIndent(v, "", " ") if err != nil { return nil, err } return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: string(raw)}}}, nil } type noArgs struct{} type inventoryArgs struct { Relationship string `json:"relationship,omitempty" jsonschema:"only entries with this relationship: partner, competitor or watch; empty for all"` } type checkinArgs struct { Agent string `json:"agent" jsonschema:"who is checking in, e.g. hermes"` MCPHubTools []string `json:"mcphub_tools" jsonschema:"names of the tools the agent has from the Core's mcphub MCP server"` Note string `json:"note,omitempty" jsonschema:"a short note on anything that failed"` } func addTools(s *mcp.Server, d *db.DB) { mcp.AddTool(s, &mcp.Tool{ Name: "profile_get", Description: "The company profile: what the company sells, its products, target customers, voice, websites, social accounts and keywords.", }, func(ctx context.Context, _ *mcp.CallToolRequest, _ noArgs) (*mcp.CallToolResult, any, error) { p, err := profile.Load(d) if err != nil { return nil, nil, err } res, err := jsonResult(p) return res, nil, err }) mcp.AddTool(s, &mcp.Tool{ Name: "inventory_list", Description: "The inventory: partners (whose news we amplify), competitors (whom we position against) and projects we watch, with their accounts, keywords and talking points.", }, func(ctx context.Context, _ *mcp.CallToolRequest, args inventoryArgs) (*mcp.CallToolResult, any, error) { entries, err := inventory.List(d, strings.ToLower(strings.TrimSpace(args.Relationship))) if err != nil { return nil, nil, err } if entries == nil { entries = []inventory.Entry{} } res, err := jsonResult(entries) return res, nil, err }) mcp.AddTool(s, &mcp.Tool{ Name: "bridge_checkin", Description: "Tell Supermarket that the agent can reach it and the Core's mcphub MCP server. Call it with the names of the mcphub tools you have.", }, func(ctx context.Context, _ *mcp.CallToolRequest, args checkinArgs) (*mcp.CallToolResult, any, error) { agent := strings.TrimSpace(args.Agent) if agent == "" { agent = "unknown" } if err := bridge.Record(d, bridge.Checkin{Agent: agent, MCPHubTools: args.MCPHubTools, Note: strings.TrimSpace(args.Note)}); err != nil { return nil, nil, err } return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{ Text: "Checked in. Supermarket has recorded the mcphub tools you can use.", }}}, nil, nil }) }