feat(19.7-02): pier MCP module tracer with in-process dispatch and sessions
- modules/pier: Host contract, mcp.* config, catalog compiled from pact.HasMCP (prefixing, route binding, guard resolution, schema reflection, annotations), per-request identity through the published verifier and resolvers, built-in backend guard provider, in-process dispatch with the bouncer marker, per-identity SDK server with the per-request re-filter, stateful session router with rehydration over a memory store, audit lines - modules/surf/pier_on.go: the summercms_mcp hook adapting the router to pier.Host; the untagged hello binary carries no SDK - go.mod: go-sdk v1.8.0 and jsonschema-go v0.4.3 (decision note committed before this change) - TestPierTracerSmoke: SDK client lists and calls acme_echo end to end through surf's compiled guard chain; README and root table row
This commit is contained in:
275
modules/pier/pier.go
Normal file
275
modules/pier/pier.go
Normal file
@@ -0,0 +1,275 @@
|
||||
// Package pier is the opt-in MCP endpoint of a SummerCMS application: the
|
||||
// place where outside agents dock to reach the application's plugins.
|
||||
// Plugins declare tools and prompts through pact.HasMCP, bound to their own
|
||||
// token-guarded routes; pier lists them per caller and runs every call
|
||||
// in-process through the route's compiled middleware chain as the caller's
|
||||
// pre-resolved principal. It is linked only in binaries built with the
|
||||
// summercms_mcp tag and mounted only with mcp.enabled true.
|
||||
package pier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
"github.com/modelcontextprotocol/go-sdk/auth"
|
||||
)
|
||||
|
||||
// frameworkModule is the module path whose version names the server.
|
||||
const frameworkModule = "git.golem15.com/golem15/summercms"
|
||||
|
||||
// maxRequestBody bounds one MCP request body, as the SDK's default does.
|
||||
const maxRequestBody = 4 << 20
|
||||
|
||||
// wellKnownPRM is the RFC 9728 well-known path.
|
||||
const wellKnownPRM = "/.well-known/oauth-protected-resource"
|
||||
|
||||
// Pier is the mounted MCP endpoint: its configuration, the compiled
|
||||
// catalog, the identity resolver, the dispatcher, the session store and the
|
||||
// stateful session router.
|
||||
type Pier struct {
|
||||
cfg Config
|
||||
app *backpack.App
|
||||
host Host
|
||||
catalog *catalog
|
||||
identities *identityResolver
|
||||
dispatch *dispatcher
|
||||
store Store
|
||||
sessions *sessionRouter
|
||||
handler http.Handler
|
||||
prmURL string
|
||||
appName string
|
||||
version string
|
||||
instance string
|
||||
}
|
||||
|
||||
// Mount reads the mcp.* keys and, when mcp.enabled is true, compiles every
|
||||
// plugin's MCP declaration against the host's route table, registers the
|
||||
// built-in backend guard provider, builds the session store and mounts
|
||||
// POST, GET and DELETE at mcp.path through the host's streaming route kind
|
||||
// behind the inline throttle. Nothing is mounted when the flag is off. A
|
||||
// bad binding, a duplicate name, an unknown guard or a route already
|
||||
// registered at mcp.path fails with an error naming the plugin, so boot
|
||||
// (and route:list) fail. Nothing dials Redis here.
|
||||
func Mount(host Host, app *backpack.App, plugins []party.Plugin) error {
|
||||
if host == nil || app == nil {
|
||||
return errors.New("pier: a host and an application are required")
|
||||
}
|
||||
cfg, err := LoadConfig(app.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !cfg.Enabled {
|
||||
return nil
|
||||
}
|
||||
reg, err := guardRegistry(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := registerBackendGuard(app, reg); err != nil {
|
||||
return err
|
||||
}
|
||||
cat, err := compileCatalog(host, reg, plugins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
frontend, err := frontendGuardName(reg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := &Pier{
|
||||
cfg: cfg,
|
||||
app: app,
|
||||
host: host,
|
||||
catalog: cat,
|
||||
store: newMemoryStore(),
|
||||
appName: appName(app),
|
||||
version: frameworkVersion(),
|
||||
instance: rand.Text(),
|
||||
}
|
||||
p.identities = newIdentityResolver(app, cfg, frontend)
|
||||
p.dispatch = &dispatcher{host: host, app: app, cfg: cfg}
|
||||
p.sessions = newSessionRouter(p)
|
||||
p.prmURL = p.metadataURL()
|
||||
p.handler = p.originCheck(p.bodyLimit(p.bearer(p.sessions)))
|
||||
|
||||
methods := []string{http.MethodPost, http.MethodGet, http.MethodDelete}
|
||||
for _, m := range methods {
|
||||
if owner, ok := host.Owner(m, cfg.Path); ok {
|
||||
return fmt.Errorf("pier: route %s %s (plugin %q) collides with the MCP endpoint %s mounts", m, cfg.Path, owner, PathKey)
|
||||
}
|
||||
}
|
||||
for _, m := range methods {
|
||||
if err := host.MountStream(m, cfg.Path, p.handler, "throttle:"+cfg.Throttle); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handler returns the endpoint's handler (the Origin check, the body limit,
|
||||
// the bearer verification and the session router), for tests and hosts
|
||||
// that serve it outside the route table.
|
||||
func (p *Pier) Handler() http.Handler {
|
||||
return p.handler
|
||||
}
|
||||
|
||||
// Config returns the loaded configuration.
|
||||
func (p *Pier) Config() Config {
|
||||
return p.cfg
|
||||
}
|
||||
|
||||
// ToolNames lists the exposed tool names of the compiled catalog, sorted.
|
||||
func (p *Pier) ToolNames() []string {
|
||||
return toolNames(p.catalog.tools)
|
||||
}
|
||||
|
||||
// metadataURL is the protected resource metadata URL named in the 401
|
||||
// challenge: <app.url>/.well-known/oauth-protected-resource<mcp.path>, or
|
||||
// the resource's origin when app.url is unset.
|
||||
func (p *Pier) metadataURL() string {
|
||||
base := p.cfg.AppURL
|
||||
if base == "" {
|
||||
if u, err := url.Parse(p.cfg.Resource); err == nil && u.Scheme != "" && u.Host != "" {
|
||||
base = u.Scheme + "://" + u.Host
|
||||
}
|
||||
}
|
||||
return base + wellKnownPRM + p.cfg.Path
|
||||
}
|
||||
|
||||
// originCheck refuses a browser request whose Origin is not allowed (403
|
||||
// before any body is read); a request without Origin passes.
|
||||
func (p *Pier) originCheck(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if origin := strings.TrimSpace(r.Header.Get("Origin")); origin != "" {
|
||||
allowed := false
|
||||
lower := strings.ToLower(origin)
|
||||
for _, o := range p.cfg.AllowedOrigins {
|
||||
if o == lower {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
http.Error(w, "Forbidden: origin not allowed", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// bodyLimit caps the request body at maxRequestBody.
|
||||
func (p *Pier) bodyLimit(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body != nil {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBody)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// bearer verifies the bearer of every request afresh and stores the
|
||||
// resolved Identity on the request for the session router and the SDK
|
||||
// handlers; a failure is a 401 carrying WWW-Authenticate with the
|
||||
// protected resource metadata URL and the read scope.
|
||||
func (p *Pier) bearer(next http.Handler) http.Handler {
|
||||
verify := func(ctx context.Context, token string, r *http.Request) (*auth.TokenInfo, error) {
|
||||
id, err := p.identities.resolve(ctx, token)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", auth.ErrInvalidToken, err)
|
||||
}
|
||||
return &auth.TokenInfo{
|
||||
Scopes: id.Scopes,
|
||||
Expiration: id.ExpiresAt,
|
||||
UserID: id.Binding,
|
||||
Extra: map[string]any{extraIdentity: id, extraCall: callInfoFrom(r)},
|
||||
}, nil
|
||||
}
|
||||
h := auth.RequireBearerToken(verify, &auth.RequireBearerTokenOptions{
|
||||
ResourceMetadataURL: p.prmURL,
|
||||
AllowMissingExpiration: true,
|
||||
})(next)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(&challengeWriter{ResponseWriter: w}, r)
|
||||
})
|
||||
}
|
||||
|
||||
// challengeWriter completes the 401 challenge with scope="read", which the
|
||||
// SDK middleware would otherwise enforce on every request.
|
||||
type challengeWriter struct {
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func (c *challengeWriter) WriteHeader(status int) {
|
||||
if status == http.StatusUnauthorized {
|
||||
h := c.Header()
|
||||
if v := h.Get("WWW-Authenticate"); v != "" && !strings.Contains(v, "scope=") {
|
||||
h.Set("WWW-Authenticate", v+`, scope="read"`)
|
||||
}
|
||||
}
|
||||
c.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (c *challengeWriter) Flush() {
|
||||
if f, ok := c.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *challengeWriter) Unwrap() http.ResponseWriter { return c.ResponseWriter }
|
||||
|
||||
// identityFromRequest reads the Identity the bearer middleware stored.
|
||||
func identityFromRequest(r *http.Request) (Identity, bool) {
|
||||
ti := auth.TokenInfoFromContext(r.Context())
|
||||
if ti == nil || ti.Extra == nil {
|
||||
return Identity{}, false
|
||||
}
|
||||
id, ok := ti.Extra[extraIdentity].(Identity)
|
||||
return id, ok && id.Binding != ""
|
||||
}
|
||||
|
||||
// storeUnavailable answers 503 for a session store failure, logged once per
|
||||
// call without the store's address.
|
||||
func (p *Pier) storeUnavailable(w http.ResponseWriter, err error) {
|
||||
logger().Error("pier: session store unavailable", "error", err)
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "session_store_unavailable"})
|
||||
}
|
||||
|
||||
// appName is app.name, or "summercms" when unset.
|
||||
func appName(app *backpack.App) string {
|
||||
if app != nil && app.Config != nil {
|
||||
if name := strings.TrimSpace(app.Config.String("app.name")); name != "" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return "summercms"
|
||||
}
|
||||
|
||||
// frameworkVersion is the framework module's version in the binary's build
|
||||
// info, or "devel" for a replace or a checkout build.
|
||||
func frameworkVersion() string {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return "devel"
|
||||
}
|
||||
if info.Main.Path == frameworkModule && info.Main.Version != "" && info.Main.Version != "(devel)" {
|
||||
return info.Main.Version
|
||||
}
|
||||
for _, dep := range info.Deps {
|
||||
if dep.Path == frameworkModule {
|
||||
if dep.Replace != nil || dep.Version == "" || dep.Version == "(devel)" {
|
||||
return "devel"
|
||||
}
|
||||
return dep.Version
|
||||
}
|
||||
}
|
||||
return "devel"
|
||||
}
|
||||
Reference in New Issue
Block a user