-
Notifications
You must be signed in to change notification settings - Fork 130
Add MCP server support to kubectl-datadog plugin #2456
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
base: main
Are you sure you want to change the base?
Conversation
|
Created an editorial review card for documentation |
This commit enhances the kubectl-datadog MCP server with several improvements: **Auto-selection for MCP tools:** - Made `name` parameter optional in GetAgentStatusArgs, DescribeAgentFeaturesArgs, DescribeAgentComponentsArgs, and GetClusterAgentLeaderArgs - Added `selectDatadogAgent` helper method to eliminate code duplication - Auto-selects first DatadogAgent when name not provided (convenient for single-agent clusters) - Updated tool descriptions to reflect auto-selection capability **Added get_cluster_agent_leader tool:** - Implements cluster-agent leader discovery functionality - Returns leader pod name and election method (Lease or ConfigMap) - Supports auto-selection like other tools **Test improvements:** - Created comprehensive test suite for tools.go (0% → 100% coverage for helper functions) - Added 11 tests covering schema generation, auto-selection, and tool registration - Refactored tests to eliminate duplication using helper functions - Reduced test code from 377 to 230 lines (39% reduction) - Package coverage improved from 23.7% to 29.7% **Documentation updates:** - Updated kubectl-datadog-mcp.md with auto-selection examples - Added documentation for get_cluster_agent_leader tool - Updated architecture diagram with all 5 local tools **Code quality:** - Added explicit channel lifecycle documentation in PortForwarder - All changes pass linting with 0 issues 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
9ae8905 to
83acbff
Compare
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 83acbff2fb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if d.hasClusterAgentEnabled(dda) { | ||
| if len(ddaList.Items) > 1 { | ||
| // Warn if multiple DatadogAgents exist | ||
| return dda.Name, fmt.Errorf("multiple DatadogAgents found, auto-selecting %s (use --proxy-dda-name to specify)", dda.Name) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid failing auto-selection when multiple DatadogAgents exist
When more than one DatadogAgent exists, this returns a non-nil error alongside the selected name. Both SelectDatadogAgent callers (ProxyManager.Initialize and selectDatadogAgent) treat any error as fatal and abort, so auto-selection fails entirely in namespaces with multiple DatadogAgents—even though the code says it is auto-selecting one. In practice this means the MCP proxy (and any tool relying on auto-selection) stops working as soon as a second DatadogAgent is present, even if only one has cluster-agent enabled. If you intend to warn, return the name with nil here and surface the warning elsewhere.
Useful? React with 👍 / 👎.
| // Connect establishes a connection to the cluster-agent MCP server | ||
| func (c *ClusterAgentMCPClient) Connect(ctx context.Context) error { | ||
| // Build full endpoint URL | ||
| fullURL := c.baseURL + c.endpoint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: If baseURL and endpoint are only ever used together it makes sense to have one config value instead of two (this url value could also be parsed into parts if needed).
| // Warn if multiple DatadogAgents exist | ||
| return dda.Name, fmt.Errorf("multiple DatadogAgents found, auto-selecting %s (use --proxy-dda-name to specify)", dda.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller likely ignores return value in case of error but the intent seems to be to pick the first value.
| "github.com/DataDog/datadog-operator/pkg/plugin/common" | ||
| ) | ||
|
|
||
| var mcpExample = ` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: const
joepeeples
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added several non-blocking style and format edits. Ping me directly if GitHub requires another approval after applying any changes, thanks!
| - **Namespace-aware**: Respects your kubeconfig context and namespace settings | ||
| - **Auto-selection**: Most tools automatically select a DatadogAgent when name is omitted (convenient for single-agent clusters) | ||
| - **Structured output**: Returns JSON data optimized for AI assistant consumption | ||
| - **Local tools**: 5 specialized tools for querying DatadogAgent resources |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd omit numbering, in case we add/remove tools in the future and would have to remember to update this line:
| - **Local tools**: 5 specialized tools for querying DatadogAgent resources | |
| - **Local tools**: Specialized tools for querying DatadogAgent resources |
|
|
||
| ### Starting the MCP Server | ||
|
|
||
| To start the MCP server, simply run: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| To start the MCP server, simply run: | |
| To start the MCP server: |
| } | ||
| ``` | ||
|
|
||
| After updating the configuration, restart Claude Desktop for the changes to take effect. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a little buried under all the code samples, but it's a required step. I'd move it above the examples (see previous comment).
| After updating the configuration, restart Claude Desktop for the changes to take effect. |
| To use the MCP server with Claude Desktop, add the following to your Claude Desktop configuration file: | ||
|
|
||
| **macOS/Linux**: `~/Library/Application Support/Claude/claude_desktop_config.json` | ||
| **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| To use the MCP server with Claude Desktop, add the following to your Claude Desktop configuration file: | |
| **macOS/Linux**: `~/Library/Application Support/Claude/claude_desktop_config.json` | |
| **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` | |
| To use the MCP server with Claude Desktop, add the following to your Claude Desktop configuration file. After updating the configuration, restart Claude Desktop for the changes to take effect | |
| - **macOS/Linux**: `~/Library/Application Support/Claude/claude_desktop_config.json` | |
| - **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` |
| The MCP server exposes 5 local tools that query DatadogAgent resources from the Kubernetes API. | ||
|
|
||
| **Auto-selection**: Tools that query a specific DatadogAgent (tools 2-5) support auto-selection. When the `name` parameter is omitted, the tool automatically selects the first DatadogAgent found in the namespace. This is convenient for clusters with a single DatadogAgent. If multiple agents exist, the first one is chosen and a warning is included in the error message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, numbering can become a future maintenance issue. It's probably OK not to specify which tools query a specific DatadogAgent since we also list the parameters for each tool.
| The MCP server exposes 5 local tools that query DatadogAgent resources from the Kubernetes API. | |
| **Auto-selection**: Tools that query a specific DatadogAgent (tools 2-5) support auto-selection. When the `name` parameter is omitted, the tool automatically selects the first DatadogAgent found in the namespace. This is convenient for clusters with a single DatadogAgent. If multiple agents exist, the first one is chosen and a warning is included in the error message. | |
| The MCP server exposes the following local tools that query DatadogAgent resources from the Kubernetes API. | |
| **Auto-selection**: Tools that query a specific DatadogAgent `name` support auto-selection. When the `name` parameter is omitted, the tool automatically selects the first DatadogAgent found in the namespace. This is convenient for clusters with a single DatadogAgent. If multiple agents exist, the first one is chosen and a warning is included in the error message. |
| 1. Verify your `claude_desktop_config.json` syntax is valid JSON | ||
| 2. Ensure the `kubectl` command is in your PATH | ||
| 3. Restart Claude Desktop after configuration changes | ||
| 4. Check Claude Desktop logs for error messages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 1. Verify your `claude_desktop_config.json` syntax is valid JSON | |
| 2. Ensure the `kubectl` command is in your PATH | |
| 3. Restart Claude Desktop after configuration changes | |
| 4. Check Claude Desktop logs for error messages | |
| 1. Verify your AI assistant configuration file syntax is valid JSON | |
| 2. Ensure the `kubectl` command is in your PATH | |
| 3. Restart AI assistant after configuration changes | |
| 4. Check AI assistant logs for error messages |
| ### Claude doesn't seem to see my DatadogAgents | ||
|
|
||
| 1. Verify agents exist: `kubectl get datadogagents -A` | ||
| 2. Check the namespace Claude is querying (it uses your kubeconfig context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ### Claude doesn't seem to see my DatadogAgents | |
| 1. Verify agents exist: `kubectl get datadogagents -A` | |
| 2. Check the namespace Claude is querying (it uses your kubeconfig context) | |
| ### AI assistant doesn't seem to see my DatadogAgents | |
| 1. Verify agents exist: `kubectl get datadogagents -A` | |
| 2. Check the namespace the AI assistant is querying (it uses your kubeconfig context) |
|
|
||
| ### "Warning: Failed to register cluster-agent proxy tools" | ||
|
|
||
| This warning appears when the cluster-agent proxy cannot be initialized. This is not a critical error - local tools will continue to work. Common causes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| This warning appears when the cluster-agent proxy cannot be initialized. This is not a critical error - local tools will continue to work. Common causes: | |
| This warning appears when the cluster-agent proxy cannot be initialized. This is not a critical error; local tools will continue to work. Common causes: |
|
|
||
| All tool responses return structured JSON data. The MCP SDK automatically serializes Go types to JSON schemas that AI assistants can understand. | ||
|
|
||
| ### Cluster-Agent Proxy Architecture |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd omit this diagram and its explanation below unless it's crucial for customers. Most users probably don't need architecture-level knowledge and just want to install and use the tool.
|
|
||
| - [Model Context Protocol Specification](https://github.com/modelcontextprotocol) | ||
| - [DatadogAgent CRD Documentation](configuration.v2alpha1.md) | ||
| - [kubectl-datadog Plugin Documentation](kubectl-datadog.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broken link; should this be kubectl-plugin.md?
| - [kubectl-datadog Plugin Documentation](kubectl-datadog.md) | |
| - [kubectl-datadog Plugin Documentation](kubectl-plugin.md) |
Summary
This PR adds Model Context Protocol (MCP) server support to the
kubectl-datadogplugin, enabling AI assistants like Claude to query and interact with DatadogAgent resources and installed Datadog components in Kubernetes clusters.Key Features
MCP Server Implementation
kubectl datadog mcpcommand that runs an MCP serverLocal Tools (5 tools with auto-selection support)
list_datadog_agents- List all DatadogAgent resourcesget_datadog_agent_status- Get runtime status of agentsdescribe_datadog_agent_features- Show enabled monitoring featuresdescribe_datadog_agent_components- Show component configurationsget_cluster_agent_leader- Identify the current cluster-agent leader podAuto-Selection
Proxy Mode
Testing
Documentation
docs/kubectl-datadog-mcp.md.mcp.jsonTest Plan
make kubectl-datadognpx @modelcontextprotocol/inspector ./bin/kubectl-datadog mcpnpx @modelcontextprotocol/inspector ./bin/kubectl-datadog mcp --proxy-cluster-agentgo test ./cmd/kubectl-datadog/mcp/...🤖 Generated with Claude Code