MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / buildHTTPRequest

Function buildHTTPRequest

internal/mcp/dispatch_http.go:671–752  ·  view source on GitHub ↗

buildHTTPRequest constructs the in-process request, attaching the user via the exported server.WithCurrentUser helper so the handler chain treats the call as authenticated. Pulled out so tests can inspect / decorate it cheaply.

(ctx context.Context, method, urlPath string, body []byte, user *models.User)

Source from the content-addressed store, hash-verified

669// chain treats the call as authenticated. Pulled out so tests can
670// inspect / decorate it cheaply.
671func buildHTTPRequest(ctx context.Context, method, urlPath string, body []byte, user *models.User) (*http.Request, error) {
672 // Strip any inherited chi.RouteCtxKey from the inbound context
673 // before synthesizing the new request. Without this, every
674 // production /mcp tool call 404s on the synthesized /api/v1/...
675 // request because chi's Mux.ServeHTTP short-circuits when it
676 // detects an existing RouteCtxKey:
677 //
678 // // chi/v5/mux.go:71-75
679 // rctx, _ := r.Context().Value(RouteCtxKey).(*Context)
680 // if rctx != nil {
681 // mx.handler.ServeHTTP(w, r) // bypass fresh routing
682 // return
683 // }
684 //
685 // chi assumes "if there's already a route context, I'm being
686 // invoked as a sub-router from a parent — don't reset state."
687 // That's correct for chi's own Sub() / Mount() patterns, but
688 // here we're synthesizing a brand-new request that needs to
689 // route from scratch against the ROOT mux. The stale RouteCtxKey
690 // from the inbound /mcp request causes chi to skip its
691 // rctx.Reset() + RoutePath = "/api/v1/..." setup; the route
692 // table lookup runs against contaminated routing state and
693 // falls through to chi's default NotFound handler — whose body
694 // is the literal "404 page not found\n" the production user
695 // reported on every dispatcher call.
696 //
697 // In tests this never fired because Dispatch was always called
698 // with context.Background() (no RouteCtxKey to inherit). In
699 // production every call enters via /mcp's chi-routed handler,
700 // so the contamination is universal.
701 //
702 // Setting the value to a typed nil shadows the parent's value:
703 // chi's `.(*Context)` type assertion on a context.Value of nil
704 // returns (nil, false), the `rctx != nil` check fails, and
705 // chi takes the fresh-routing branch as intended.
706 //
707 // Critically we DON'T strip pad's own context values
708 // (WithCurrentUser, WithAPITokenAuth, TokenScopes,
709 // TokenAllowedWorkspaces) — those are added below / preserved
710 // from the inbound request and are exactly what the synthesized
711 // request needs to authenticate as the same user. We only strip
712 // the chi-specific routing key.
713 ctx = context.WithValue(ctx, chi.RouteCtxKey, (*chi.Context)(nil))
714
715 var bodyReader io.Reader
716 if len(body) > 0 {
717 bodyReader = bytes.NewReader(body)
718 }
719 req, err := http.NewRequestWithContext(ctx, method, urlPath, bodyReader)
720 if err != nil {
721 return nil, err
722 }
723 if len(body) > 0 {
724 req.Header.Set("Content-Type", "application/json")
725 }
726 // Loopback-equivalent — handlers gating on RemoteAddr (e.g. the
727 // localhost-bootstrap path) treat this as in-process. The auth
728 // chain sees us as already-authenticated via WithCurrentUser, so

Calls 3

WithCurrentUserFunction · 0.92
WithAPITokenAuthFunction · 0.92
SetMethod · 0.45