@@ -11,6 +11,7 @@ import (
1111
1212 "github.com/go-chi/httprate"
1313 "github.com/hyprmcp/mcp-gateway/config"
14+ "github.com/hyprmcp/mcp-gateway/htmlresponse"
1415 "github.com/hyprmcp/mcp-gateway/log"
1516 "github.com/lestrrat-go/httprc/v3"
1617 "github.com/lestrrat-go/httprc/v3/errsink"
@@ -84,10 +85,13 @@ func (mgr *Manager) Register(mux *http.ServeMux) error {
8485}
8586
8687func (mgr * Manager ) Handler (next http.Handler ) http.Handler {
88+ htmlHandler := htmlresponse .NewHandler (mgr .config )
8789 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
8890 rawToken :=
8991 strings .TrimSpace (strings .TrimPrefix (strings .TrimSpace (r .Header .Get ("Authorization" )), "Bearer" ))
9092
93+ shouldCallNext := true
94+
9195 token , err := jwt .ParseString (rawToken , jwt .WithKeySet (mgr .jwkSet ))
9296 if err != nil {
9397 metadataURL , _ := url .Parse (mgr .config .Host .String ())
@@ -98,9 +102,18 @@ func (mgr *Manager) Handler(next http.Handler) http.Handler {
98102 fmt .Sprintf (`Bearer resource_metadata="%s"` , metadataURL .String ()),
99103 )
100104 w .WriteHeader (http .StatusUnauthorized )
101- return
105+ shouldCallNext = false
106+ }
107+
108+ if strings .Contains (r .Header .Get ("Accept" ), "text/html" ) {
109+ if err := htmlHandler .Handle (w , r ); err != nil {
110+ log .Get (r .Context ()).Error (err , "failed to handle html response" )
111+ }
112+ shouldCallNext = false
102113 }
103114
104- next .ServeHTTP (w , r .WithContext (TokenContext (r .Context (), token , rawToken )))
115+ if shouldCallNext {
116+ next .ServeHTTP (w , r .WithContext (TokenContext (r .Context (), token , rawToken )))
117+ }
105118 })
106119}
0 commit comments