Skip to content

Commit 498db94

Browse files
authored
Support HTTP bearer authentication for mcp proxy and wrap (#4368)
1 parent 68c0215 commit 498db94

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

internal/command/mcp/proxy.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func NewProxy() *cobra.Command {
4141
Name: "url",
4242
Description: "URL of the MCP wrapper server",
4343
},
44+
flag.String{
45+
Name: "bearer-token",
46+
Description: "Bearer token to authenticate with",
47+
},
4448
flag.String{
4549
Name: "user",
4650
Description: "User to authenticate with",
@@ -305,7 +309,9 @@ func getFromServer(ctx context.Context, url string) error {
305309
req.Header.Set("Accept", "application/json")
306310

307311
// Set basic authentication if user is provided
308-
if flag.GetString(ctx, "user") != "" {
312+
if flag.GetString(ctx, "bearer-token") != "" {
313+
req.Header.Set("Authorization", "Bearer "+flag.GetString(ctx, "bearer-token"))
314+
} else if flag.GetString(ctx, "user") != "" {
309315
req.SetBasicAuth(flag.GetString(ctx, "user"), flag.GetString(ctx, "password"))
310316
}
311317

internal/command/mcp/wrap.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
type Server struct {
3131
port int
3232
mcp string
33+
token string
3334
user string
3435
password string
3536
private bool
@@ -63,6 +64,10 @@ func NewWrap() *cobra.Command {
6364
Description: "Path to the stdio MCP program to be wrapped.",
6465
Shorthand: "m",
6566
},
67+
flag.String{
68+
Name: "bearer-token",
69+
Description: "Bearer token to authenticate with. Defaults to the value of the FLY_MCP_BEARER_TOKEN environment variable.",
70+
},
6671
flag.String{
6772
Name: "user",
6873
Description: "User to authenticate with. Defaults to the value of the FLY_MCP_USER environment variable.",
@@ -81,10 +86,15 @@ func NewWrap() *cobra.Command {
8186
}
8287

8388
func runWrap(ctx context.Context) error {
89+
token, _ := os.LookupEnv("FLY_MCP_BEARER_TOKEN")
8490
user, _ := os.LookupEnv("FLY_MCP_USER")
8591
password, _ := os.LookupEnv("FLY_MCP_PASSWORD")
8692
_, private := os.LookupEnv("FLY_MCP_PRIVATE")
8793

94+
if token == "" {
95+
token = flag.GetString(ctx, "bearer-token")
96+
}
97+
8898
if user == "" {
8999
user = flag.GetString(ctx, "user")
90100
}
@@ -96,6 +106,7 @@ func runWrap(ctx context.Context) error {
96106
// Create server
97107
server := &Server{
98108
port: flag.GetInt(ctx, "port"),
109+
token: token,
99110
user: user,
100111
password: password,
101112
private: flag.GetBool(ctx, "private") || private,
@@ -247,7 +258,14 @@ func (s *Server) HandleHTTPRequest(w http.ResponseWriter, r *http.Request) {
247258
}
248259
}
249260

250-
if s.user != "" {
261+
if s.token != "" {
262+
// Check for bearer token
263+
bearerToken := r.Header.Get("Authorization")
264+
if bearerToken == "" || !strings.HasPrefix(bearerToken, "Bearer ") || strings.TrimSpace(bearerToken[7:]) != s.token {
265+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
266+
return
267+
}
268+
} else if s.user != "" {
251269
// Check for basic authentication
252270
user, password, ok := r.BasicAuth()
253271
if !ok || user != s.user || password != s.password {

0 commit comments

Comments
 (0)