Files
coder/coderd/httpmw/experiments.go
Thomas Kosiewski 7fbb3ced5b feat: add MCP HTTP server experiment and improve experiment middleware (#18712)
# Add MCP HTTP Server Experiment

This PR adds a new experiment flag `mcp-server-http` to enable the MCP HTTP server functionality. The changes include:

1. Added a new experiment constant `ExperimentMCPServerHTTP` with the value "mcp-server-http"
2. Added display name and documentation for the new experiment
3. Improved the experiment middleware to:
   - Support requiring multiple experiments
   - Provide better error messages with experiment display names
   - Add a development mode bypass option
4. Applied the new experiment requirement to the MCP HTTP endpoint
5. Replaced the custom OAuth2 middleware with the standard experiment middleware

The PR also improves the `Enabled()` method on the `Experiments` type by using `slices.Contains()` for better readability.
2025-07-03 20:09:18 +02:00

62 lines
2.1 KiB
Go

package httpmw
import (
"fmt"
"net/http"
"strings"
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
)
// RequireExperiment returns middleware that checks if all required experiments are enabled.
// If any experiment is disabled, it returns a 403 Forbidden response with details about the missing experiments.
func RequireExperiment(experiments codersdk.Experiments, requiredExperiments ...codersdk.Experiment) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, experiment := range requiredExperiments {
if !experiments.Enabled(experiment) {
var experimentNames []string
for _, exp := range requiredExperiments {
experimentNames = append(experimentNames, string(exp))
}
// Print a message that includes the experiment names
// even if some experiments are already enabled.
var message string
if len(requiredExperiments) == 1 {
message = fmt.Sprintf("%s functionality requires enabling the '%s' experiment.",
requiredExperiments[0].DisplayName(), requiredExperiments[0])
} else {
message = fmt.Sprintf("This functionality requires enabling the following experiments: %s",
strings.Join(experimentNames, ", "))
}
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{
Message: message,
})
return
}
}
next.ServeHTTP(w, r)
})
}
}
// RequireExperimentWithDevBypass checks if ALL the given experiments are enabled,
// but bypasses the check in development mode (buildinfo.IsDev()).
func RequireExperimentWithDevBypass(experiments codersdk.Experiments, requiredExperiments ...codersdk.Experiment) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if buildinfo.IsDev() {
next.ServeHTTP(w, r)
return
}
RequireExperiment(experiments, requiredExperiments...)(next).ServeHTTP(w, r)
})
}
}