Skip to content

Commit 009ad57

Browse files
authored
Merge pull request #47 from StacklokLabs/46-mcp-port
Allow MCP_PORT to override default address
2 parents 6e8fcb1 + 79b3d75 commit 009ad57

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ To run the server with a specific kubeconfig:
9696
KUBECONFIG=/path/to/kubeconfig task run-with-kubeconfig
9797
```
9898

99+
To run the server on a specific port:
100+
```bash
101+
MCP_PORT=9091 task run
102+
```
103+
99104
### MCP Tools
100105

101106
The MKP server provides the following MCP tools:

cmd/server/main.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ package main
44
import (
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 (
1719
func 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

Comments
 (0)