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

Function buildHTTPRequest

internal/mcp/dispatch_http.go:706–787  ·  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

704// chain treats the call as authenticated. Pulled out so tests can
705// inspect / decorate it cheaply.
706func buildHTTPRequest(ctx context.Context, method, urlPath string, body []byte, user *models.User) (*http.Request, error) {
707 // Strip any inherited chi.RouteCtxKey from the inbound context
708 // before synthesizing the new request. Without this, every
709 // production /mcp tool call 404s on the synthesized /api/v1/...
710 // request because chi's Mux.ServeHTTP short-circuits when it
711 // detects an existing RouteCtxKey:
712 //
713 // // chi/v5/mux.go:71-75
714 // rctx, _ := r.Context().Value(RouteCtxKey).(*Context)
715 // if rctx != nil {
716 // mx.handler.ServeHTTP(w, r) // bypass fresh routing
717 // return
718 // }
719 //
720 // chi assumes "if there's already a route context, I'm being
721 // invoked as a sub-router from a parent — don't reset state."
722 // That's correct for chi's own Sub() / Mount() patterns, but
723 // here we're synthesizing a brand-new request that needs to
724 // route from scratch against the ROOT mux. The stale RouteCtxKey
725 // from the inbound /mcp request causes chi to skip its
726 // rctx.Reset() + RoutePath = "/api/v1/..." setup; the route
727 // table lookup runs against contaminated routing state and
728 // falls through to chi's default NotFound handler — whose body
729 // is the literal "404 page not found\n" the production user
730 // reported on every dispatcher call.
731 //
732 // In tests this never fired because Dispatch was always called
733 // with context.Background() (no RouteCtxKey to inherit). In
734 // production every call enters via /mcp's chi-routed handler,
735 // so the contamination is universal.
736 //
737 // Setting the value to a typed nil shadows the parent's value:
738 // chi's `.(*Context)` type assertion on a context.Value of nil
739 // returns (nil, false), the `rctx != nil` check fails, and
740 // chi takes the fresh-routing branch as intended.
741 //
742 // Critically we DON'T strip pad's own context values
743 // (WithCurrentUser, WithAPITokenAuth, TokenScopes,
744 // TokenAllowedWorkspaces) — those are added below / preserved
745 // from the inbound request and are exactly what the synthesized
746 // request needs to authenticate as the same user. We only strip
747 // the chi-specific routing key.
748 ctx = context.WithValue(ctx, chi.RouteCtxKey, (*chi.Context)(nil))
749
750 var bodyReader io.Reader
751 if len(body) > 0 {
752 bodyReader = bytes.NewReader(body)
753 }
754 req, err := http.NewRequestWithContext(ctx, method, urlPath, bodyReader)
755 if err != nil {
756 return nil, err
757 }
758 if len(body) > 0 {
759 req.Header.Set("Content-Type", "application/json")
760 }
761 // Loopback-equivalent — handlers gating on RemoteAddr (e.g. the
762 // localhost-bootstrap path) treat this as in-process. The auth
763 // chain sees us as already-authenticated via WithCurrentUser, so

Calls 3

WithCurrentUserFunction · 0.92
WithAPITokenAuthFunction · 0.92
SetMethod · 0.45