50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
// Package mcp is the MCP edge for the cart: a Model Context Protocol server
|
|
// exposing the cart grain as tools so an agent can inspect and mutate carts
|
|
// (list items, change quantities, remove items, clear carts, apply vouchers,
|
|
// manage users, etc.).
|
|
//
|
|
// Transport (JSON-RPC 2.0 over streamable HTTP), dispatch, and the schema/result
|
|
// helpers live in platform/mcp; this package only defines the cart-specific
|
|
// tools and wires them to the grain pool. Mounted by cmd/cart under /mcp.
|
|
package mcp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.k6n.net/mats/go-cart-actor/pkg/actor"
|
|
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
|
pmcp "git.k6n.net/mats/platform/mcp"
|
|
"net/http"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
const (
|
|
serverName = "cart"
|
|
serverVersion = "0.1.0"
|
|
)
|
|
|
|
// Applier is the minimal grain-pool interface the cart MCP needs: read a
|
|
// cart's current state (Get) and apply a mutation (Apply). The
|
|
// SimpleGrainPool[cart.CartGrain] in cmd/cart satisfies it directly.
|
|
type Applier interface {
|
|
Get(ctx context.Context, id uint64) (*cart.CartGrain, error)
|
|
Apply(ctx context.Context, id uint64, mutation ...proto.Message) (*actor.MutationResult[cart.CartGrain], error)
|
|
}
|
|
|
|
// Server is the MCP edge over the cart grain pool.
|
|
type Server struct {
|
|
applier Applier
|
|
mcp *pmcp.Server
|
|
}
|
|
|
|
// New builds an MCP server exposing the cart grain as tools.
|
|
func New(applier Applier) *Server {
|
|
s := &Server{applier: applier}
|
|
s.mcp = pmcp.NewServer(pmcp.Info{Name: serverName, Version: serverVersion}, s.buildTools()...)
|
|
return s
|
|
}
|
|
|
|
// Handler returns the streamable-HTTP handler to mount (e.g. at /mcp).
|
|
func (s *Server) Handler() http.Handler { return s.mcp.Handler() }
|