|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/gptscript-ai/cmd" |
| 10 | + "github.com/obot-platform/mcp-oauth-proxy/pkg/proxy" |
| 11 | + "github.com/obot-platform/mcp-oauth-proxy/pkg/types" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + version = "dev" |
| 17 | + buildTime = "unknown" |
| 18 | +) |
| 19 | + |
| 20 | +// RootCmd represents the base command when called without any subcommands |
| 21 | +type RootCmd struct { |
| 22 | + // Database configuration |
| 23 | + DatabaseDSN string `name:"database-dsn" env:"DATABASE_DSN" usage:"Database connection string (PostgreSQL or SQLite file path). If empty, uses SQLite at data/oauth_proxy.db"` |
| 24 | + |
| 25 | + // OAuth Provider configuration |
| 26 | + OAuthClientID string `name:"oauth-client-id" env:"OAUTH_CLIENT_ID" usage:"OAuth client ID from your OAuth provider" required:"true"` |
| 27 | + OAuthClientSecret string `name:"oauth-client-secret" env:"OAUTH_CLIENT_SECRET" usage:"OAuth client secret from your OAuth provider" required:"true"` |
| 28 | + OAuthAuthorizeURL string `name:"oauth-authorize-url" env:"OAUTH_AUTHORIZE_URL" usage:"Authorization endpoint URL from your OAuth provider (e.g., https://accounts.google.com)" required:"true"` |
| 29 | + |
| 30 | + // Scopes and MCP configuration |
| 31 | + ScopesSupported string `name:"scopes-supported" env:"SCOPES_SUPPORTED" usage:"Comma-separated list of supported OAuth scopes (e.g., 'openid,profile,email')" required:"true"` |
| 32 | + MCPServerURL string `name:"mcp-server-url" env:"MCP_SERVER_URL" usage:"URL of the MCP server to proxy requests to" required:"true"` |
| 33 | + |
| 34 | + // Security configuration |
| 35 | + EncryptionKey string `name:"encryption-key" env:"ENCRYPTION_KEY" usage:"Base64-encoded 32-byte AES-256 key for encrypting sensitive data (optional)"` |
| 36 | + |
| 37 | + // Server configuration |
| 38 | + Port string `name:"port" env:"PORT" usage:"Port to run the server on" default:"8080"` |
| 39 | + Host string `name:"host" env:"HOST" usage:"Host to bind the server to" default:"localhost"` |
| 40 | + |
| 41 | + // Logging |
| 42 | + Verbose bool `name:"verbose,v" usage:"Enable verbose logging"` |
| 43 | + Version bool `name:"version" usage:"Show version information"` |
| 44 | + |
| 45 | + Mode string `name:"mode" env:"MODE" usage:"Mode to run the server in" default:"proxy"` |
| 46 | +} |
| 47 | + |
| 48 | +func (c *RootCmd) Run(cobraCmd *cobra.Command, args []string) error { |
| 49 | + if c.Version { |
| 50 | + fmt.Printf("MCP OAuth Proxy\n") |
| 51 | + fmt.Printf("Version: %s\n", version) |
| 52 | + fmt.Printf("Built: %s\n", buildTime) |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + // Configure logging |
| 57 | + if c.Verbose { |
| 58 | + log.SetFlags(log.LstdFlags | log.Lshortfile) |
| 59 | + log.Println("Verbose logging enabled") |
| 60 | + } |
| 61 | + |
| 62 | + // Convert CLI config to internal config format |
| 63 | + config := &types.Config{ |
| 64 | + DatabaseDSN: c.DatabaseDSN, |
| 65 | + OAuthClientID: c.OAuthClientID, |
| 66 | + OAuthClientSecret: c.OAuthClientSecret, |
| 67 | + OAuthAuthorizeURL: c.OAuthAuthorizeURL, |
| 68 | + ScopesSupported: c.ScopesSupported, |
| 69 | + MCPServerURL: c.MCPServerURL, |
| 70 | + EncryptionKey: c.EncryptionKey, |
| 71 | + Mode: c.Mode, |
| 72 | + } |
| 73 | + |
| 74 | + // Validate configuration |
| 75 | + if err := c.validateConfig(); err != nil { |
| 76 | + return fmt.Errorf("configuration validation failed: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Create OAuth proxy |
| 80 | + oauthProxy, err := proxy.NewOAuthProxy(config) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to create OAuth proxy: %w", err) |
| 83 | + } |
| 84 | + defer func() { |
| 85 | + if err := oauthProxy.Close(); err != nil { |
| 86 | + log.Printf("Error closing database: %v", err) |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + // Get HTTP handler |
| 91 | + handler := oauthProxy.GetHandler() |
| 92 | + |
| 93 | + // Start server |
| 94 | + address := fmt.Sprintf("%s:%s", c.Host, c.Port) |
| 95 | + log.Printf("Starting OAuth proxy server on %s", address) |
| 96 | + log.Printf("OAuth Provider: %s", c.OAuthAuthorizeURL) |
| 97 | + log.Printf("MCP Server: %s", c.MCPServerURL) |
| 98 | + log.Printf("Database: %s", c.getDatabaseType()) |
| 99 | + |
| 100 | + return http.ListenAndServe(address, handler) |
| 101 | +} |
| 102 | + |
| 103 | +func (c *RootCmd) validateConfig() error { |
| 104 | + if c.OAuthClientID == "" { |
| 105 | + return fmt.Errorf("oauth-client-id is required") |
| 106 | + } |
| 107 | + if c.OAuthClientSecret == "" { |
| 108 | + return fmt.Errorf("oauth-client-secret is required") |
| 109 | + } |
| 110 | + if c.OAuthAuthorizeURL == "" { |
| 111 | + return fmt.Errorf("oauth-authorize-url is required") |
| 112 | + } |
| 113 | + if c.ScopesSupported == "" { |
| 114 | + return fmt.Errorf("scopes-supported is required") |
| 115 | + } |
| 116 | + if c.MCPServerURL == "" { |
| 117 | + return fmt.Errorf("mcp-server-url is required") |
| 118 | + } |
| 119 | + if c.Mode == proxy.ModeProxy { |
| 120 | + if u, err := url.Parse(c.MCPServerURL); err != nil || u.Scheme != "http" && u.Scheme != "https" { |
| 121 | + return fmt.Errorf("invalid MCP server URL: %w", err) |
| 122 | + } else if u.Path != "" && u.Path != "/" || u.RawQuery != "" || u.Fragment != "" { |
| 123 | + return fmt.Errorf("MCP server URL must not contain a path, query, or fragment") |
| 124 | + } |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func (c *RootCmd) getDatabaseType() string { |
| 130 | + if c.DatabaseDSN == "" { |
| 131 | + return "SQLite (data/oauth_proxy.db)" |
| 132 | + } |
| 133 | + if len(c.DatabaseDSN) > 10 && (c.DatabaseDSN[:11] == "postgres://" || c.DatabaseDSN[:14] == "postgresql://") { |
| 134 | + return "PostgreSQL" |
| 135 | + } |
| 136 | + return fmt.Sprintf("SQLite (%s)", c.DatabaseDSN) |
| 137 | +} |
| 138 | + |
| 139 | +// Customizer interface implementation for additional command customization |
| 140 | +func (c *RootCmd) Customize(cobraCmd *cobra.Command) { |
| 141 | + cobraCmd.Use = "mcp-oauth-proxy" |
| 142 | + cobraCmd.Short = "OAuth 2.1 proxy server for MCP (Model Context Protocol)" |
| 143 | + cobraCmd.Long = `MCP OAuth Proxy is a comprehensive OAuth 2.1 proxy server that provides |
| 144 | +OAuth authorization server functionality with PostgreSQL/SQLite storage. |
| 145 | +
|
| 146 | +This proxy supports multiple OAuth providers (Google, Microsoft, GitHub) and |
| 147 | +proxies requests to MCP servers with user context headers. |
| 148 | +
|
| 149 | +Examples: |
| 150 | + # Start with environment variables |
| 151 | + export OAUTH_CLIENT_ID="your-google-client-id" |
| 152 | + export OAUTH_CLIENT_SECRET="your-secret" |
| 153 | + export OAUTH_AUTHORIZE_URL="https://accounts.google.com" |
| 154 | + export SCOPES_SUPPORTED="openid,profile,email" |
| 155 | + export MCP_SERVER_URL="http://localhost:3000" |
| 156 | + mcp-oauth-proxy |
| 157 | +
|
| 158 | + # Start with CLI flags |
| 159 | + mcp-oauth-proxy \ |
| 160 | + --oauth-client-id="your-google-client-id" \ |
| 161 | + --oauth-client-secret="your-secret" \ |
| 162 | + --oauth-authorize-url="https://accounts.google.com" \ |
| 163 | + --scopes-supported="openid,profile,email" \ |
| 164 | + --mcp-server-url="http://localhost:3000" |
| 165 | +
|
| 166 | + # Use PostgreSQL database |
| 167 | + mcp-oauth-proxy \ |
| 168 | + --database-dsn="postgres://user:pass@localhost:5432/oauth_db?sslmode=disable" \ |
| 169 | + --oauth-client-id="your-client-id" \ |
| 170 | + # ... other required flags |
| 171 | +
|
| 172 | +Configuration: |
| 173 | + Configuration values are loaded in this order (later values override earlier ones): |
| 174 | + 1. Default values |
| 175 | + 2. Environment variables |
| 176 | + 3. Command line flags |
| 177 | + |
| 178 | +Database Support: |
| 179 | + - PostgreSQL: Full ACID compliance, recommended for production |
| 180 | + - SQLite: Zero configuration, perfect for development and small deployments` |
| 181 | + |
| 182 | + cobraCmd.Version = version |
| 183 | +} |
| 184 | + |
| 185 | +// Execute is the main entry point for the CLI |
| 186 | +func Execute() error { |
| 187 | + rootCmd := &RootCmd{} |
| 188 | + cobraCmd := cmd.Command(rootCmd) |
| 189 | + return cobraCmd.Execute() |
| 190 | +} |
0 commit comments