mirror of
https://github.com/kubeshark/kubeshark.git
synced 2026-02-14 10:00:08 +00:00
* Add MCP (Model Context Protocol) server command Implement `kubeshark mcp` command that runs an MCP server over stdio, enabling AI assistants to query Kubeshark's network visibility data. Features: - MCP protocol implementation (JSON-RPC 2.0 over stdio) - Dynamic tool discovery from Hub's /api/mcp endpoint - Local cluster management tools (check_kubeshark_status, start_kubeshark, stop_kubeshark) - --url flag for direct connection to existing Kubeshark deployment - --kubeconfig flag for proxy mode with kubectl - --allow-destructive flag to enable start/stop operations (safe by default) - --list-tools flag to display available tools - --mcp-config flag to generate MCP client configuration - 5-minute cache TTL for Hub tools/prompts - Prompts for common analysis tasks * Address code review comments for MCP implementation - Add 30s timeout to HTTP client to prevent hanging requests - Add scanner.Err() check after stdin processing loop - Close HTTP response bodies to prevent resource leaks - Add goroutine to wait on started process to prevent zombies - Simplify polling loop by removing ineffective context check - Advertise check_kubeshark_status in URL mode (was callable but hidden) - Update documentation to clarify URL mode only disables start/stop * Fix lint errors in mcpRunner.go - Use type conversion instead of struct literals for hubMCPTool -> mcpTool and hubMCPPromptArg -> mcpPromptArg (S1016 gosimple) - Lowercase error string to follow Go conventions (ST1005 staticcheck) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"github.com/kubeshark/kubeshark/config"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var mcpURL string
|
|
var mcpKubeconfig string
|
|
var mcpListTools bool
|
|
var mcpConfig bool
|
|
var mcpAllowDestructive bool
|
|
|
|
var mcpCmd = &cobra.Command{
|
|
Use: "mcp",
|
|
Short: "Run MCP (Model Context Protocol) server for AI assistant integration",
|
|
Long: `Run an MCP server over stdio that exposes Kubeshark's L7 API visibility
|
|
to AI assistants like Claude Desktop.
|
|
|
|
TOOLS PROVIDED:
|
|
|
|
Cluster Management (work without Kubeshark running):
|
|
- check_kubeshark_status: Check if Kubeshark is running in the cluster
|
|
- start_kubeshark: Start Kubeshark to capture traffic
|
|
- stop_kubeshark: Stop Kubeshark and clean up resources
|
|
|
|
Traffic Analysis (require Kubeshark running):
|
|
- list_workloads: Discover pods, services, namespaces, and nodes with L7 traffic
|
|
- list_api_calls: Query L7 API transactions (HTTP, gRPC, etc.)
|
|
- get_api_call: Get detailed information about a specific API call
|
|
- get_api_stats: Get aggregated API statistics
|
|
|
|
CONFIGURATION:
|
|
|
|
To use with Claude Desktop, add to your claude_desktop_config.json
|
|
(typically at ~/Library/Application Support/Claude/claude_desktop_config.json):
|
|
|
|
{
|
|
"mcpServers": {
|
|
"kubeshark": {
|
|
"command": "/path/to/kubeshark",
|
|
"args": ["mcp", "--kubeconfig", "/Users/YOUR_USERNAME/.kube/config"]
|
|
}
|
|
}
|
|
}
|
|
|
|
DIRECT URL MODE:
|
|
|
|
If Kubeshark is already running and accessible via URL (e.g., exposed via ingress),
|
|
you can connect directly without needing kubectl/kubeconfig:
|
|
|
|
{
|
|
"mcpServers": {
|
|
"kubeshark": {
|
|
"command": "/path/to/kubeshark",
|
|
"args": ["mcp", "--url", "https://kubeshark.example.com"]
|
|
}
|
|
}
|
|
}
|
|
|
|
In URL mode, destructive tools (start/stop) are disabled since Kubeshark is
|
|
managed externally. The check_kubeshark_status tool remains available to confirm connectivity.
|
|
|
|
DESTRUCTIVE OPERATIONS:
|
|
|
|
By default, destructive operations (start_kubeshark, stop_kubeshark) are disabled
|
|
to prevent accidental cluster modifications. To enable them, use --allow-destructive:
|
|
|
|
{
|
|
"mcpServers": {
|
|
"kubeshark": {
|
|
"command": "/path/to/kubeshark",
|
|
"args": ["mcp", "--allow-destructive", "--kubeconfig", "/path/to/.kube/config"]
|
|
}
|
|
}
|
|
}
|
|
|
|
CUSTOM SETTINGS:
|
|
|
|
To use custom settings when starting Kubeshark, use the --set flag:
|
|
|
|
{
|
|
"mcpServers": {
|
|
"kubeshark": {
|
|
"command": "/path/to/kubeshark",
|
|
"args": ["mcp", "--set", "tap.docker.tag=v52.3"],
|
|
...
|
|
}
|
|
}
|
|
}
|
|
|
|
Multiple --set flags can be used for different settings.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
// Handle --mcp-config flag
|
|
if mcpConfig {
|
|
printMCPConfig(mcpURL, mcpKubeconfig)
|
|
return nil
|
|
}
|
|
|
|
// Set kubeconfig path if provided
|
|
if mcpKubeconfig != "" {
|
|
config.Config.Kube.ConfigPathStr = mcpKubeconfig
|
|
}
|
|
|
|
// Handle --list-tools flag
|
|
if mcpListTools {
|
|
listMCPTools(mcpURL)
|
|
return nil
|
|
}
|
|
|
|
setFlags, _ := cmd.Flags().GetStringSlice(config.SetCommandName)
|
|
runMCPWithConfig(setFlags, mcpURL, mcpAllowDestructive)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(mcpCmd)
|
|
|
|
mcpCmd.Flags().StringVar(&mcpURL, "url", "", "Direct URL to Kubeshark (e.g., https://kubeshark.example.com). When set, connects directly without kubectl/proxy and disables start/stop tools.")
|
|
mcpCmd.Flags().StringVar(&mcpKubeconfig, "kubeconfig", "", "Path to kubeconfig file (e.g., /Users/me/.kube/config)")
|
|
mcpCmd.Flags().BoolVar(&mcpListTools, "list-tools", false, "List available MCP tools and exit")
|
|
mcpCmd.Flags().BoolVar(&mcpConfig, "mcp-config", false, "Print MCP client configuration JSON and exit")
|
|
mcpCmd.Flags().BoolVar(&mcpAllowDestructive, "allow-destructive", false, "Enable destructive operations (start_kubeshark, stop_kubeshark). Without this flag, only read-only traffic analysis tools are available.")
|
|
}
|