@@ -30,6 +30,7 @@ import (
3030type 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
8388func 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