Skip to content

feat: add MCP HTTP server experiment and improve experiment middleware #18712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: thomask33/07-01-feat_oauth2_replace_dev_flag_with_oauth2_experiment_for_controlled_rollout
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func New(options *Options) *API {
// logging into Coder with an external OAuth2 provider.
r.Route("/oauth2", func(r chi.Router) {
r.Use(
api.oAuth2ProviderMiddleware,
httpmw.RequireExperimentWithDevBypass(api.Experiments, codersdk.ExperimentOAuth2),
)
r.Route("/authorize", func(r chi.Router) {
r.Use(
Expand Down Expand Up @@ -973,6 +973,9 @@ func New(options *Options) *API {
r.Get("/prompts", api.aiTasksPrompts)
})
r.Route("/mcp", func(r chi.Router) {
r.Use(
httpmw.RequireExperimentWithDevBypass(api.Experiments, codersdk.ExperimentOAuth2, codersdk.ExperimentMCPServerHTTP),
)
// MCP HTTP transport endpoint with mandatory authentication
r.Mount("/http", api.mcpHTTPHandler())
})
Expand Down Expand Up @@ -1473,7 +1476,7 @@ func New(options *Options) *API {
r.Route("/oauth2-provider", func(r chi.Router) {
r.Use(
apiKeyMiddleware,
api.oAuth2ProviderMiddleware,
httpmw.RequireExperimentWithDevBypass(api.Experiments, codersdk.ExperimentOAuth2),
)
r.Route("/apps", func(r chi.Router) {
r.Get("/", api.oAuth2ProviderApps)
Expand Down
50 changes: 44 additions & 6 deletions coderd/httpmw/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,59 @@ 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"
)

func RequireExperiment(experiments codersdk.Experiments, experiment codersdk.Experiment) func(next http.Handler) http.Handler {
// 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) {
if !experiments.Enabled(experiment) {
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{
Message: fmt.Sprintf("Experiment '%s' is required but not enabled", experiment),
})
return
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)
})
}
}
14 changes: 0 additions & 14 deletions coderd/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/sqlc-dev/pqtype"

"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
Expand All @@ -37,19 +36,6 @@ const (
displaySecretLength = 6 // Length of visible part in UI (last 6 characters)
)

func (api *API) oAuth2ProviderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if !api.Experiments.Enabled(codersdk.ExperimentOAuth2) && !buildinfo.IsDev() {
httpapi.Write(r.Context(), rw, http.StatusForbidden, codersdk.Response{
Message: "OAuth2 provider functionality requires enabling the 'oauth2' experiment.",
})
return
}

next.ServeHTTP(rw, r)
})
}

// @Summary Get OAuth2 applications.
// @ID get-oauth2-applications
// @Security CoderSessionToken
Expand Down
37 changes: 30 additions & 7 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/google/uuid"
"golang.org/x/mod/semver"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/xerrors"

"github.com/coreos/go-oidc/v3/oidc"
Expand Down Expand Up @@ -3342,8 +3344,33 @@ const (
ExperimentWorkspaceUsage Experiment = "workspace-usage" // Enables the new workspace usage tracking.
ExperimentWebPush Experiment = "web-push" // Enables web push notifications through the browser.
ExperimentOAuth2 Experiment = "oauth2" // Enables OAuth2 provider functionality.
ExperimentMCPServerHTTP Experiment = "mcp-server-http" // Enables the MCP HTTP server functionality.
)

func (e Experiment) DisplayName() string {
switch e {
case ExperimentExample:
return "Example Experiment"
case ExperimentAutoFillParameters:
return "Auto-fill Template Parameters"
case ExperimentNotifications:
return "SMTP and Webhook Notifications"
case ExperimentWorkspaceUsage:
return "Workspace Usage Tracking"
case ExperimentWebPush:
return "Browser Push Notifications"
case ExperimentOAuth2:
return "OAuth2 Provider Functionality"
case ExperimentMCPServerHTTP:
return "MCP HTTP Server Functionality"
default:
// Split on hyphen and convert to title case
// e.g. "web-push" -> "Web Push", "mcp-server-http" -> "Mcp Server Http"
caser := cases.Title(language.English)
return caser.String(strings.ReplaceAll(string(e), "-", " "))
}
}

// ExperimentsKnown should include all experiments defined above.
var ExperimentsKnown = Experiments{
ExperimentExample,
Expand All @@ -3352,6 +3379,7 @@ var ExperimentsKnown = Experiments{
ExperimentWorkspaceUsage,
ExperimentWebPush,
ExperimentOAuth2,
ExperimentMCPServerHTTP,
}

// ExperimentsSafe should include all experiments that are safe for
Expand All @@ -3369,14 +3397,9 @@ var ExperimentsSafe = Experiments{}
// @typescript-ignore Experiments
type Experiments []Experiment

// Returns a list of experiments that are enabled for the deployment.
// Enabled returns a list of experiments that are enabled for the deployment.
func (e Experiments) Enabled(ex Experiment) bool {
for _, v := range e {
if v == ex {
return true
}
}
return false
return slices.Contains(e, ex)
}

func (c *Client) Experiments(ctx context.Context) (Experiments, error) {
Expand Down
1 change: 1 addition & 0 deletions docs/reference/api/schemas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ require (
golang.org/x/sync v0.14.0
golang.org/x/sys v0.33.0
golang.org/x/term v0.32.0
golang.org/x/text v0.25.0 // indirect
golang.org/x/text v0.25.0
golang.org/x/tools v0.33.0
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
google.golang.org/api v0.231.0
Expand Down
2 changes: 2 additions & 0 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading