feat: MCP view of BM answers (absolute links, no material body by default)

- jz.bm.mcp-links on the token twins and the management API: under MCP,
  url and *_url strings starting with a single / get the app base
- material answers under MCP leave the body out unless include_body,
  marking it with body_omitted; write answers never echo it
- jz.bm.manage keys a dispatched call by user
- bm_materials and bm_material student tools
Plain HTTP answers are unchanged.
This commit is contained in:
2026-10-28 14:37:10 +01:00
parent 464c74b80f
commit df42e30034
9 changed files with 301 additions and 46 deletions

View File

@@ -31,8 +31,8 @@ var (
const (
bucketCalendarIP = "throttle:jz.bm.calendar-ip"
bucketCalendarToken = "throttle:jz.bm.calendar-token"
// bucketManage throttles the management API per token, or per client IP
// for a request without one.
// bucketManage throttles the management API per user for an MCP tool
// call, else per token, or per client IP for a request without one.
bucketManage = "throttle:jz.bm.manage"
// bucketRealtime throttles the realtime token route per user (it runs
// after jwt.auth), or per client IP for a request without one. The
@@ -70,6 +70,13 @@ func (p *Plugin) Buckets() map[string]surf.Bucket {
Max: 120,
Decay: time.Minute,
Key: func(r *http.Request) string {
// An MCP tool call carries no bearer and comes from the
// endpoint's own address; the dispatch marker (set in-process
// only, never by an HTTP request) names its user. The bucket
// runs before user.api_token, so bouncer.User is not set yet.
if d, ok := bouncer.Dispatched(r.Context(), "api_token"); ok && d.Principal.ID != 0 {
return "jz.bm.manage.user|" + strconv.FormatUint(uint64(d.Principal.ID), 10)
}
if secret := usermiddleware.BearerToken(r); secret != "" {
return "jz.bm.manage.token|" + userclasses.HashAPIToken(secret)
}
@@ -142,15 +149,17 @@ func (p *Plugin) Routes(r pact.Router) error {
// and handlers behind user.api_token (golem15.user's MCP guard dispatches
// only to user.api_token routes), with a scope on each. The handlers read
// bouncer.User, so a token reaches only its own user's data. The bucket
// runs after the guard, so it keys by user. The calendar feed and the
// chat have no twin.
r.Group(TokenPrefix, surf.Use("user.api_token", bucketToken, "locale.from-principal"), func(g pact.Router) {
// runs after the guard, so it keys by user. jz.bm.mcp-links makes the
// links of an MCP answer absolute. The calendar feed and the chat have no
// twin.
r.Group(TokenPrefix, surf.Use("user.api_token", bucketToken, MiddlewareMCPLinks, "locale.from-principal"), func(g pact.Router) {
mountStudent(g, h, []string{"user.scope:read"}, []string{"user.scope:write"})
})
// The management API: what bm:seed does, plus materials,
// behind the application-wide user API token. Scope limits delegation;
// user.permission checks the owner's current group permissions.
r.Group(ManagePrefix, surf.Use(bucketManage, "user.api_token"), func(g pact.Router) {
// jz.bm.mcp-links makes the links of an MCP answer absolute.
r.Group(ManagePrefix, surf.Use(bucketManage, "user.api_token", MiddlewareMCPLinks), func(g pact.Router) {
g.Get("/me", h.TokenInfo, "user.scope:read")
g.Get("/materials", h.ManagedMaterials, "user.scope:read", "user.permission:"+controllers.PermMaterials)
g.Get("/materials/{id}", h.ManagedMaterial, "user.scope:read", "user.permission:"+controllers.PermMaterials)
@@ -290,6 +299,7 @@ func (p *Plugin) handlers() *api.Handlers {
h.PerPage = p.app.Config.Int("jz.bm.materials_per_page")
h.DashboardLimit = p.app.Config.Int("jz.bm.dashboard_limit")
h.PublicURL = p.app.Config.String("jz.bm.public_url")
h.AppURL = p.app.Config.String("app.url")
}
return h
}