Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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 deletionscoderd/apidoc/docs.go
View file
Open in desktop

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

7 changes: 5 additions & 2 deletionscoderd/apidoc/swagger.json
View file
Open in desktop

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

7 changes: 5 additions & 2 deletionscoderd/coderd.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionscoderd/httpmw/experiments.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscoderd/oauth2.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionscodersdk/deployment.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsdocs/reference/api/schemas.md
View file
Open in desktop

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

2 changes: 1 addition & 1 deletiongo.mod
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -206,7 +206,7 @@ require (
golang.org/x/syncv0.14.0
golang.org/x/sysv0.33.0
golang.org/x/termv0.32.0
golang.org/x/textv0.25.0// indirect
golang.org/x/textv0.25.0
golang.org/x/toolsv0.33.0
golang.org/x/xerrorsv0.0.0-20240903120638-7835f813f4da
google.golang.org/apiv0.231.0
Expand Down
2 changes: 2 additions & 0 deletionssite/src/api/typesGenerated.ts
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp