mcp in cart
Build and Publish / BuildAndDeployArm64 (push) Failing after 6s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
2026-06-17 21:43:38 +02:00
parent ad410d217c
commit 74dc252279
9 changed files with 1054 additions and 13 deletions
+44
View File
@@ -0,0 +1,44 @@
package mcp
import "encoding/json"
// JSON-RPC 2.0 envelope types (the wire format MCP rides on). Notifications are
// requests with no id and must not receive a response.
type rpcRequest struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
func (r *rpcRequest) isNotification() bool { return len(r.ID) == 0 }
type rpcResponse struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Result any `json:"result,omitempty"`
Error *rpcError `json:"error,omitempty"`
}
type rpcError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
const (
codeParseError = -32700
codeInvalidRequest = -32600
codeMethodNotFound = -32601
codeInvalidParams = -32602
codeInternalError = -32603
)
func result(id json.RawMessage, v any) *rpcResponse {
return &rpcResponse{JSONRPC: "2.0", ID: id, Result: v}
}
func errorResponse(id json.RawMessage, code int, msg string) *rpcResponse {
return &rpcResponse{JSONRPC: "2.0", ID: id, Error: &rpcError{Code: code, Message: msg}}
}
+154
View File
@@ -0,0 +1,154 @@
// Package mcp is the MCP edge for the cart's promotion engine: a Model Context
// Protocol server exposing the promotion Store and evaluator as tools so an
// agent can list, create, update, status-toggle and delete the promotions the
// cart actually applies (e.g. Volymrabatt), plus preview the discount a cart
// total would receive.
//
// Transport is streamable HTTP (JSON-RPC 2.0 over POST). It is mounted by
// cmd/cart under /mcp on the same HTTP server as the cart API. Tools mutate the
// shared promotions.Store and persist to data/promotions.json; the cart's
// mutation processor reads a fresh Store snapshot on every recompute, so edits
// take effect on the next cart change without a restart.
package mcp
import (
"encoding/json"
"io"
"net/http"
"git.k6n.net/mats/go-cart-actor/pkg/promotions"
)
const (
serverName = "cart-promotions"
serverVersion = "0.1.0"
protocolVersion = "2025-06-18"
)
// Server is the MCP edge over the shared promotion Store and evaluator.
type Server struct {
store *promotions.Store
eval *promotions.PromotionService
tools []tool
}
// New builds an MCP server exposing the promotion store as tools.
func New(store *promotions.Store, eval *promotions.PromotionService) *Server {
if eval == nil {
eval = promotions.NewPromotionService(nil)
}
s := &Server{store: store, eval: eval}
s.tools = s.buildTools()
return s
}
// Handler returns the streamable-HTTP handler to mount (e.g. at /mcp).
func (s *Server) Handler() http.Handler {
return http.HandlerFunc(s.serveHTTP)
}
func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(io.LimitReader(r.Body, 4<<20))
if err != nil {
writeRPC(w, errorResponse(nil, codeParseError, "read error"))
return
}
if isBatch(body) {
var reqs []rpcRequest
if err := json.Unmarshal(body, &reqs); err != nil {
writeRPC(w, errorResponse(nil, codeParseError, "invalid JSON"))
return
}
var out []*rpcResponse
for i := range reqs {
if resp := s.dispatch(&reqs[i]); resp != nil {
out = append(out, resp)
}
}
if len(out) == 0 {
w.WriteHeader(http.StatusAccepted)
return
}
writeRPC(w, out)
return
}
var req rpcRequest
if err := json.Unmarshal(body, &req); err != nil {
writeRPC(w, errorResponse(nil, codeParseError, "invalid JSON"))
return
}
resp := s.dispatch(&req)
if resp == nil {
w.WriteHeader(http.StatusAccepted)
return
}
writeRPC(w, resp)
}
func (s *Server) dispatch(req *rpcRequest) *rpcResponse {
if req.JSONRPC != "2.0" {
if req.isNotification() {
return nil
}
return errorResponse(req.ID, codeInvalidRequest, "jsonrpc must be \"2.0\"")
}
switch req.Method {
case "initialize":
return result(req.ID, s.initialize(req.Params))
case "ping":
return result(req.ID, struct{}{})
case "tools/list":
return result(req.ID, map[string]any{"tools": s.tools})
case "tools/call":
return s.callTool(req)
case "notifications/initialized", "notifications/cancelled":
return nil
default:
if req.isNotification() {
return nil
}
return errorResponse(req.ID, codeMethodNotFound, "unknown method: "+req.Method)
}
}
func (s *Server) initialize(params json.RawMessage) map[string]any {
pv := protocolVersion
if len(params) > 0 {
var p struct {
ProtocolVersion string `json:"protocolVersion"`
}
if json.Unmarshal(params, &p) == nil && p.ProtocolVersion != "" {
pv = p.ProtocolVersion
}
}
return map[string]any{
"protocolVersion": pv,
"capabilities": map[string]any{"tools": map[string]any{}},
"serverInfo": map[string]any{"name": serverName, "version": serverVersion},
}
}
func isBatch(body []byte) bool {
for _, b := range body {
switch b {
case ' ', '\t', '\r', '\n':
continue
case '[':
return true
default:
return false
}
}
return false
}
func writeRPC(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(v)
}
+130
View File
@@ -0,0 +1,130 @@
package mcp
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"git.k6n.net/mats/go-cart-actor/pkg/promotions"
)
// call issues a tools/call against the handler and returns the decoded text
// payload (the JSON the tool returned), failing on a tool/transport error.
func call(t *testing.T, h http.Handler, name string, args map[string]any) map[string]any {
t.Helper()
body, _ := json.Marshal(map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": map[string]any{"name": name, "arguments": args},
})
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/mcp", bytes.NewReader(body))
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("%s: status %d", name, rec.Code)
}
var resp struct {
Result struct {
IsError bool `json:"isError"`
Content []struct {
Text string `json:"text"`
} `json:"content"`
} `json:"result"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("%s: decode response: %v (%s)", name, err, rec.Body.String())
}
if resp.Error != nil {
t.Fatalf("%s: rpc error: %s", name, resp.Error.Message)
}
if resp.Result.IsError {
t.Fatalf("%s: tool error: %s", name, resp.Result.Content[0].Text)
}
var out map[string]any
if len(resp.Result.Content) > 0 {
_ = json.Unmarshal([]byte(resp.Result.Content[0].Text), &out)
}
return out
}
func TestPromotionMCPRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "promotions.json")
store, err := promotions.NewStore(path)
if err != nil {
t.Fatal(err)
}
h := New(store, nil).Handler()
rule := map[string]any{
"id": "volymrabatt",
"name": "Volymrabatt",
"status": "active",
"priority": 100,
"conditions": []any{
map[string]any{"id": "min", "type": "cart_total", "operator": ">=", "value": 2000000},
},
"actions": []any{
map[string]any{
"id": "tiers", "type": "tiered_discount", "value": 0,
"config": map[string]any{
"tiers": []any{
map[string]any{"minTotal": 2000000, "maxTotal": 4000000, "percent": 5},
map[string]any{"minTotal": 4000000, "maxTotal": 6000000, "percent": 9},
map[string]any{"minTotal": 6000000, "maxTotal": 9000000, "percent": 14},
},
},
},
},
}
// Upsert (create).
got := call(t, h, "upsert_promotion", map[string]any{"promotion": rule})
if got["replaced"] != false {
t.Errorf("first upsert should create, got replaced=%v", got["replaced"])
}
// It persisted to disk and a fresh store sees it.
reloaded, err := promotions.NewStore(path)
if err != nil {
t.Fatal(err)
}
if len(reloaded.List()) != 1 {
t.Fatalf("expected 1 persisted promotion, got %d", len(reloaded.List()))
}
// List.
listed := call(t, h, "list_promotions", map[string]any{"status": "active"})
if c, _ := listed["count"].(float64); c != 1 {
t.Errorf("list count = %v, want 1", listed["count"])
}
// Preview the 9% mid tier at 50 000 kr.
preview := call(t, h, "preview_cart_discount", map[string]any{"cartTotalIncVat": 5000000})
if d, _ := preview["discountIncVat"].(float64); int64(d) != 450000 {
t.Errorf("discount = %v, want 450000", preview["discountIncVat"])
}
if n, _ := preview["netIncVat"].(float64); int64(n) != 4550000 {
t.Errorf("net = %v, want 4550000", preview["netIncVat"])
}
// Toggle off → preview yields no discount.
call(t, h, "set_promotion_status", map[string]any{"id": "volymrabatt", "status": "inactive"})
off := call(t, h, "preview_cart_discount", map[string]any{"cartTotalIncVat": 5000000})
if d, _ := off["discountIncVat"].(float64); d != 0 {
t.Errorf("inactive promo should give 0 discount, got %v", off["discountIncVat"])
}
// Delete.
call(t, h, "delete_promotion", map[string]any{"id": "volymrabatt"})
final := call(t, h, "list_promotions", nil)
if c, _ := final["count"].(float64); c != 0 {
t.Errorf("after delete count = %v, want 0", final["count"])
}
}
+289
View File
@@ -0,0 +1,289 @@
package mcp
import (
"encoding/json"
"fmt"
"time"
"git.k6n.net/mats/go-cart-actor/pkg/cart"
"git.k6n.net/mats/go-cart-actor/pkg/promotions"
)
// tool is one MCP tool: a name, a description, a JSON-Schema for its arguments,
// and the handler that runs it.
type tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
invoke func(args json.RawMessage) (any, error) `json:"-"`
}
func (s *Server) callTool(req *rpcRequest) *rpcResponse {
var p struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
if err := json.Unmarshal(req.Params, &p); err != nil {
return errorResponse(req.ID, codeInvalidParams, "invalid params")
}
var t *tool
for i := range s.tools {
if s.tools[i].Name == p.Name {
t = &s.tools[i]
break
}
}
if t == nil {
return errorResponse(req.ID, codeInvalidParams, "unknown tool: "+p.Name)
}
out, err := t.invoke(p.Arguments)
if err != nil {
return result(req.ID, toolError(err))
}
return result(req.ID, toolText(out))
}
func (s *Server) buildTools() []tool {
return []tool{
{
Name: "list_promotions",
Description: "List all promotion rules the cart engine evaluates, optionally filtered by status (active|inactive|scheduled|expired).",
InputSchema: object(props{
"status": str("optional status filter: active|inactive|scheduled|expired"),
}, nil),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
Status string `json:"status"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
rules := s.store.List()
if a.Status != "" {
filtered := rules[:0:0]
for _, r := range rules {
if string(r.Status) == a.Status {
filtered = append(filtered, r)
}
}
rules = filtered
}
return map[string]any{"promotions": rules, "count": len(rules)}, nil
},
},
{
Name: "get_promotion",
Description: "Fetch a single promotion rule by id.",
InputSchema: object(props{"id": str("the promotion id")}, []string{"id"}),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
ID string `json:"id"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
r, ok := s.store.Get(a.ID)
if !ok {
return nil, fmt.Errorf("promotion %q not found", a.ID)
}
return r, nil
},
},
{
Name: "upsert_promotion",
Description: "Create a promotion or replace the existing one with the same id. " +
"`promotion` is a full promotion-rule object (id, name, status, priority, conditions, actions). " +
"For a tiered volume discount (Volymrabatt) use a cart_total condition plus a tiered_discount action " +
"whose config.tiers each have minTotal/maxTotal (öre incl. VAT) and percent.",
InputSchema: object(props{
"promotion": obj("the full promotion rule object"),
}, []string{"promotion"}),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
Promotion json.RawMessage `json:"promotion"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
var rule promotions.PromotionRule
if err := json.Unmarshal(a.Promotion, &rule); err != nil {
return nil, fmt.Errorf("invalid promotion: %w", err)
}
if rule.ID == "" {
return nil, fmt.Errorf("promotion.id is required")
}
if rule.Status == "" {
rule.Status = promotions.StatusActive
}
replaced, err := s.store.Upsert(rule)
if err != nil {
return nil, err
}
return map[string]any{"id": rule.ID, "replaced": replaced}, nil
},
},
{
Name: "set_promotion_status",
Description: "Set a promotion's status (active|inactive|scheduled|expired) to toggle it on or off.",
InputSchema: object(props{
"id": str("the promotion id"),
"status": str("new status: active|inactive|scheduled|expired"),
}, []string{"id", "status"}),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
ID string `json:"id"`
Status string `json:"status"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
if !validStatus(a.Status) {
return nil, fmt.Errorf("invalid status %q", a.Status)
}
found, err := s.store.SetStatus(a.ID, promotions.PromotionStatus(a.Status))
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("promotion %q not found", a.ID)
}
return map[string]any{"id": a.ID, "status": a.Status}, nil
},
},
{
Name: "delete_promotion",
Description: "Delete a promotion rule by id.",
InputSchema: object(props{"id": str("the promotion id")}, []string{"id"}),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
ID string `json:"id"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
found, err := s.store.Delete(a.ID)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("promotion %q not found", a.ID)
}
return map[string]any{"id": a.ID, "deleted": true}, nil
},
},
{
Name: "preview_cart_discount",
Description: "Evaluate the current active promotions against a hypothetical cart total and report the " +
"matched discount. cartTotalIncVat is in öre incl. VAT (e.g. 5 000 000 = 50 000 kr). Useful for " +
"verifying Volymrabatt tiers without touching a real cart.",
InputSchema: object(props{
"cartTotalIncVat": integer("cart total incl. VAT, in öre"),
"itemQuantity": integer("optional total item quantity"),
"customerSegment": str("optional customer segment"),
}, []string{"cartTotalIncVat"}),
invoke: func(args json.RawMessage) (any, error) {
var a struct {
CartTotalIncVat int64 `json:"cartTotalIncVat"`
ItemQuantity int `json:"itemQuantity"`
CustomerSegment string `json:"customerSegment"`
}
if err := decode(args, &a); err != nil {
return nil, err
}
g := syntheticCart(a.CartTotalIncVat, a.ItemQuantity)
opts := []promotions.ContextOption{promotions.WithNow(time.Now())}
if a.CustomerSegment != "" {
opts = append(opts, promotions.WithCustomerSegment(a.CustomerSegment))
}
ctx := promotions.NewContextFromCart(g, opts...)
_, actions := s.eval.EvaluateAll(s.store.Snapshot(), ctx)
promotions.ApplyActions(g, actions)
return map[string]any{
"subtotalIncVat": a.CartTotalIncVat,
"discountIncVat": g.TotalDiscount.IncVat,
"netIncVat": g.TotalPrice.IncVat,
"matchedActions": actions,
}, nil
},
},
}
}
// syntheticCart builds a one-line cart whose gross total is incVat öre (25% VAT
// assumed) so the promotion engine can be evaluated against an arbitrary total.
func syntheticCart(incVat int64, qty int) *cart.CartGrain {
if qty <= 0 {
qty = 1
}
g := cart.NewCartGrain(0, time.Now())
g.Items = []*cart.CartItem{
{Id: 1, Sku: "PREVIEW", Quantity: uint16(qty), Price: *cart.NewPriceFromIncVat(incVat, 25)},
}
// Quantity > 1 would multiply the row total; keep the gross equal to incVat by
// pricing a single unit at the full total.
g.Items[0].Quantity = 1
g.UpdateTotals()
g.Items[0].Quantity = uint16(qty)
return g
}
func validStatus(s string) bool {
switch promotions.PromotionStatus(s) {
case promotions.StatusActive, promotions.StatusInactive, promotions.StatusScheduled, promotions.StatusExpired:
return true
}
return false
}
// ---- result + schema helpers (mirrors the graph-cms MCP edge) -------------
func toolText(v any) map[string]any {
b, err := json.Marshal(v)
if err != nil {
return toolError(err)
}
return map[string]any{
"content": []map[string]any{{"type": "text", "text": string(b)}},
}
}
func toolError(err error) map[string]any {
return map[string]any{
"isError": true,
"content": []map[string]any{{"type": "text", "text": err.Error()}},
}
}
// decode unmarshals tool arguments, tolerating empty/absent arguments.
func decode(args json.RawMessage, v any) error {
if len(args) == 0 || string(args) == "null" {
return nil
}
if err := json.Unmarshal(args, v); err != nil {
return fmt.Errorf("invalid arguments: %w", err)
}
return nil
}
type props map[string]json.RawMessage
func object(p props, required []string) json.RawMessage {
m := map[string]any{
"type": "object",
"properties": p,
}
if len(required) > 0 {
m["required"] = required
}
b, _ := json.Marshal(m)
return b
}
func str(desc string) json.RawMessage { return scalar("string", desc) }
func obj(desc string) json.RawMessage { return scalar("object", desc) }
func integer(d string) json.RawMessage { return scalar("integer", d) }
func scalar(typ, desc string) json.RawMessage {
b, _ := json.Marshal(map[string]any{"type": typ, "description": desc})
return b
}