Skip to content

feat(config): --read-only mode flag exposes only read-only annotated tools #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ uvx kubernetes-mcp-server@latest --help
| `--sse-port` | Starts the MCP server in Server-Sent Event (SSE) mode and listens on the specified port. |
| `--log-level` | Sets the logging level (values [from 0-9](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md)). Similar to [kubectl logging levels](https://kubernetes.io/docs/reference/kubectl/quick-reference/#kubectl-output-verbosity-and-debugging). |
| `--kubeconfig` | Path to the Kubernetes configuration file. If not provided, it will try to resolve the configuration (in-cluster, default location, etc.). |
| `--read-only` | If set, the MCP server will run in read-only mode, meaning it will not allow any write operations (create, update, delete) on the Kubernetes cluster. This is useful for debugging or inspecting the cluster without making changes. |

## 🛠️ Tools <a id="tools"></a>

Expand Down
6 changes: 5 additions & 1 deletion pkg/kubernetes-mcp-server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ Kubernetes Model Context Protocol (MCP) server
fmt.Printf("Invalid profile name: %s, valid names are: %s\n", viper.GetString("profile"), strings.Join(mcp.ProfileNames, ", "))
os.Exit(1)
}
klog.V(1).Infof("Starting kubernetes-mcp-server with profile: %s", profile.GetName())
klog.V(1).Info("Starting kubernetes-mcp-server")
klog.V(1).Infof(" - Profile: %s", profile.GetName())
klog.V(1).Infof(" - Read-only mode: %t", viper.GetBool("read-only"))
if viper.GetBool("version") {
fmt.Println(version.Version)
return
}
mcpServer, err := mcp.NewSever(mcp.Configuration{
Profile: profile,
ReadOnly: viper.GetBool("read-only"),
Kubeconfig: viper.GetString("kubeconfig"),
})
if err != nil {
Expand Down Expand Up @@ -123,5 +126,6 @@ func init() {
rootCmd.Flags().StringP("sse-base-url", "", "", "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
rootCmd.Flags().StringP("kubeconfig", "", "", "Path to the kubeconfig file to use for authentication")
rootCmd.Flags().String("profile", "full", "MCP profile to use (one of: "+strings.Join(mcp.ProfileNames, ", ")+")")
rootCmd.Flags().Bool("read-only", false, "If true, only tools annotated with readOnlyHint=true are exposed")
_ = viper.BindPFlags(rootCmd.Flags())
}
10 changes: 9 additions & 1 deletion pkg/kubernetes-mcp-server/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ func TestVersion(t *testing.T) {
func TestDefaultProfile(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, "Starting kubernetes-mcp-server with profile: full") {
if !strings.Contains(out, "- Profile: full") {
t.Fatalf("Expected profile 'full', got %s %v", out, err)
}
}

func TestDefaultReadOnly(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, " - Read-only mode: false") {
t.Fatalf("Expected read-only mode false, got %s %v", out, err)
}
}
3 changes: 2 additions & 1 deletion pkg/mcp/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestMain(m *testing.M) {

type mcpContext struct {
profile Profile
readOnly bool
before func(*mcpContext)
after func(*mcpContext)
ctx context.Context
Expand All @@ -116,7 +117,7 @@ func (c *mcpContext) beforeEach(t *testing.T) {
if c.before != nil {
c.before(c)
}
if c.mcpServer, err = NewSever(Configuration{Profile: c.profile}); err != nil {
if c.mcpServer, err = NewSever(Configuration{Profile: c.profile, ReadOnly: c.readOnly}); err != nil {
t.Fatal(err)
return
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/mcp/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

type Configuration struct {
Profile Profile
Profile Profile
// When true, expose only tools annotated with readOnlyHint=true
ReadOnly bool
Kubeconfig string
}

Expand Down Expand Up @@ -43,7 +45,14 @@ func (s *Server) reloadKubernetesClient() error {
return err
}
s.k = k
s.server.SetTools(s.configuration.Profile.GetTools(s)...)
applicableTools := make([]server.ServerTool, 0)
for _, tool := range s.configuration.Profile.GetTools(s) {
if s.configuration.ReadOnly && (tool.Tool.Annotations.ReadOnlyHint == nil || !*tool.Tool.Annotations.ReadOnlyHint) {
continue
}
applicableTools = append(applicableTools, tool)
}
s.server.SetTools(applicableTools...)
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ func TestWatchKubeConfig(t *testing.T) {
})
})
}

func TestReadOnly(t *testing.T) {
readOnlyServer := func(c *mcpContext) { c.readOnly = true }
testCaseWithContext(t, &mcpContext{before: readOnlyServer}, func(c *mcpContext) {
tools, err := c.mcpClient.ListTools(c.ctx, mcp.ListToolsRequest{})
t.Run("ListTools returns tools", func(t *testing.T) {
if err != nil {
t.Fatalf("call ListTools failed %v", err)
}
})
t.Run("ListTools returns only read-only tools", func(t *testing.T) {
for _, tool := range tools.Tools {
if tool.Annotations.ReadOnlyHint == nil || !*tool.Annotations.ReadOnlyHint {
t.Errorf("Tool %s is not read-only but should be", tool.Name)
}
}
})
})
}
Loading