Skip to content

Commit 533e9c0

Browse files
authored
Fix nil checks in GetMcpContext (#35)
## Summary - guard against nil values when reading context variables - add tests for SetMcpContext and ResetMcpContext ## Testing - `go test ./...` *(fails: go.work requires go >= 1.24.3)*
1 parent 4c1d035 commit 533e9c0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pkg/mcp/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ func SetMcpContext(instance, cluster, organization string) {
2727
McpContextOrganization = &organization
2828
}
2929

30+
// GetMcpContext returns the currently configured StreamNative Cloud context.
31+
// If the context has not been fully configured, it returns empty strings.
3032
func GetMcpContext() (string, string, string) {
33+
if McpContextPulsarInstance == nil || McpContextPulsarCluster == nil || McpContextOrganization == nil {
34+
return "", "", ""
35+
}
36+
3137
return *McpContextPulsarInstance, *McpContextPulsarCluster, *McpContextOrganization
3238
}
3339

pkg/mcp/context_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package mcp
2+
3+
import "testing"
4+
5+
func TestGetMcpContextUnset(t *testing.T) {
6+
ResetMcpContext()
7+
instance, cluster, org := GetMcpContext()
8+
if instance != "" || cluster != "" || org != "" {
9+
t.Fatalf("expected empty context when unset, got %q %q %q", instance, cluster, org)
10+
}
11+
}
12+
13+
func TestGetMcpContextSet(t *testing.T) {
14+
ResetMcpContext()
15+
SetMcpContext("inst", "cluster", "org")
16+
instance, cluster, org := GetMcpContext()
17+
if instance != "inst" || cluster != "cluster" || org != "org" {
18+
t.Fatalf("expected context to be returned, got %q %q %q", instance, cluster, org)
19+
}
20+
}

0 commit comments

Comments
 (0)