MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / ProtectedResourceMetadataHandler

Function ProtectedResourceMetadataHandler

auth/auth.go:154–180  ·  view source on GitHub ↗

ProtectedResourceMetadataHandler returns an http.Handler that serves OAuth 2.0 protected resource metadata (RFC 9728) with CORS support. This handler allows cross-origin requests from any origin (Access-Control-Allow-Origin: *) because OAuth metadata is public information intended for client discov

(metadata *oauthex.ProtectedResourceMetadata)

Source from the content-addressed store, hash-verified

152// For more sophisticated CORS policies or to restrict origins, wrap this handler with a
153// CORS middleware like github.com/rs/cors or github.com/jub0bs/cors.
154func ProtectedResourceMetadataHandler(metadata *oauthex.ProtectedResourceMetadata) http.Handler {
155 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156 // Set CORS headers for cross-origin client discovery.
157 // OAuth metadata is public information, so allowing any origin is safe.
158 w.Header().Set("Access-Control-Allow-Origin", "*")
159 w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
160 w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
161
162 // Handle CORS preflight requests
163 if r.Method == http.MethodOptions {
164 w.WriteHeader(http.StatusNoContent)
165 return
166 }
167
168 // Only GET allowed for metadata retrieval
169 if r.Method != http.MethodGet {
170 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
171 return
172 }
173
174 w.Header().Set("Content-Type", "application/json")
175 if err := json.NewEncoder(w).Encode(metadata); err != nil {
176 http.Error(w, "Failed to encode metadata", http.StatusInternalServerError)
177 return
178 }
179 })
180}

Callers 7

mainFunction · 0.92
mainFunction · 0.92
TestAuthorizeFunction · 0.85

Calls 1

ErrorMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…