@@ -4,9 +4,11 @@ package main
44import (
55 "context"
66 "flag"
7+ "fmt"
78 "log"
89 "os"
910 "os/signal"
11+ "strconv"
1012 "syscall"
1113 "time"
1214
@@ -17,7 +19,7 @@ import (
1719func main () {
1820 // Parse command line flags
1921 kubeconfig := flag .String ("kubeconfig" , "" , "Path to kubeconfig file. If not provided, in-cluster config will be used" )
20- addr := flag .String ("addr" , ":8080" , "Address to listen on" )
22+ addr := flag .String ("addr" , getDefaultAddress () , "Address to listen on" )
2123 serveResources := flag .Bool ("serve-resources" , false ,
2224 "Whether to serve cluster resources as MCP resources. Setting to false reduces context size for LLMs with large clusters" )
2325 readWrite := flag .Bool ("read-write" , false ,
@@ -138,3 +140,29 @@ func main() {
138140 // Ensure we exit the program
139141 os .Exit (0 )
140142}
143+
144+ // getDefaultAddress returns the address to listen on based on MCP_PORT environment variable.
145+ // If the environment variable is not set, returns ":8080".
146+ // If set, validates that the port is valid and returns ":<port>".
147+ func getDefaultAddress () string {
148+ defaultPort := ":8080"
149+
150+ portEnv := os .Getenv ("MCP_PORT" )
151+ if portEnv == "" {
152+ return defaultPort
153+ }
154+
155+ port , err := strconv .Atoi (portEnv )
156+ if err != nil {
157+ log .Printf ("Invalid port number in MCP_PORT environment variable: %v, using default port 8080" , err )
158+ return defaultPort
159+ }
160+
161+ // Check if port is within valid range
162+ if port < 1 || port > 65535 {
163+ log .Printf ("Port %d out of valid range (1-65535), using default port" , port )
164+ return defaultPort
165+ }
166+
167+ return fmt .Sprintf (":%d" , port )
168+ }
0 commit comments