Skip to content

Conversation

@clamoriniere
Copy link
Collaborator

@clamoriniere clamoriniere commented Jan 5, 2026

Summary

This PR adds Model Context Protocol (MCP) server support to the kubectl-datadog plugin, enabling AI assistants like Claude to query and interact with DatadogAgent resources and installed Datadog components in Kubernetes clusters.

Key Features

MCP Server Implementation

  • New kubectl datadog mcp command that runs an MCP server
  • Supports both stdio and SSE transport modes
  • Two operational modes:
    • Local mode: Direct queries against the Kubernetes API
    • Proxy mode: Forwards requests to cluster-agent's built-in MCP server

Local Tools (5 tools with auto-selection support)

  • list_datadog_agents - List all DatadogAgent resources
  • get_datadog_agent_status - Get runtime status of agents
  • describe_datadog_agent_features - Show enabled monitoring features
  • describe_datadog_agent_components - Show component configurations
  • get_cluster_agent_leader - Identify the current cluster-agent leader pod

Auto-Selection

  • All tools support automatic DatadogAgent selection when name is not provided
  • Discovers and selects the first available agent in the namespace
  • Simplifies usage in single-agent environments

Proxy Mode

  • Automatic discovery of cluster-agent pods with MCP server enabled
  • Port-forwarding with connection pooling and lifecycle management
  • Transparent request forwarding to cluster-agent MCP servers
  • Extends tool availability with additional cluster-agent-specific tools

Testing

  • Comprehensive unit tests for all components
  • Test coverage: 29.7% overall (new code well-tested)
  • 11 tests for tools.go with table-driven patterns
  • Tests for discovery, port-forwarding, proxy, and client components

Documentation

  • Complete documentation in docs/kubectl-datadog-mcp.md
  • Usage examples and architecture diagrams
  • Configuration guide for .mcp.json

Test Plan

  • Build and run the plugin: make kubectl-datadog
  • Test local mode: npx @modelcontextprotocol/inspector ./bin/kubectl-datadog mcp
  • Verify auto-selection with single DatadogAgent
  • Test proxy mode with cluster-agent npx @modelcontextprotocol/inspector ./bin/kubectl-datadog mcp --proxy-cluster-agent
  • Run test suite: go test ./cmd/kubectl-datadog/mcp/...

🤖 Generated with Claude Code

@clamoriniere clamoriniere requested review from a team as code owners January 5, 2026 21:52
@clamoriniere clamoriniere added this to the v1.23.0 milestone Jan 5, 2026
@clamoriniere clamoriniere added component/plugin enhancement New feature or request labels Jan 5, 2026
@codecov-commenter
Copy link

codecov-commenter commented Jan 5, 2026

Codecov Report

❌ Patch coverage is 33.53383% with 442 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.21%. Comparing base (305e9c2) to head (83acbff).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
cmd/kubectl-datadog/mcp/tools.go 26.54% 145 Missing and 10 partials ⚠️
cmd/kubectl-datadog/mcp/proxy.go 11.20% 98 Missing and 5 partials ⚠️
cmd/kubectl-datadog/mcp/portforward.go 25.71% 52 Missing ⚠️
cmd/kubectl-datadog/mcp/client.go 50.00% 36 Missing and 1 partial ⚠️
cmd/kubectl-datadog/mcp/server.go 0.00% 33 Missing ⚠️
cmd/kubectl-datadog/mcp/discovery.go 61.42% 22 Missing and 5 partials ⚠️
cmd/kubectl-datadog/mcp/mcp.go 64.40% 20 Missing and 1 partial ⚠️
cmd/kubectl-datadog/clusteragent/leader/leader.go 0.00% 11 Missing ⚠️
...adog/autoscaling/cluster/install/guess/aws-auth.go 0.00% 1 Missing ⚠️
...toscaling/cluster/install/guess/nodesproperties.go 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2456      +/-   ##
==========================================
- Coverage   37.29%   37.21%   -0.09%     
==========================================
  Files         290      298       +8     
  Lines       24698    25356     +658     
==========================================
+ Hits         9212     9435     +223     
- Misses      14773    15186     +413     
- Partials      713      735      +22     
Flag Coverage Δ
unittests 37.21% <33.53%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ctl-datadog/autoscaling/cluster/install/install.go 12.90% <ø> (ø)
cmd/kubectl-datadog/mcp/types_proxy.go 100.00% <100.00%> (ø)
pkg/plugin/common/options.go 0.00% <ø> (ø)
...adog/autoscaling/cluster/install/guess/aws-auth.go 0.00% <0.00%> (ø)
...toscaling/cluster/install/guess/nodesproperties.go 0.00% <0.00%> (ø)
cmd/kubectl-datadog/datadog/datadog.go 0.00% <0.00%> (ø)
cmd/kubectl-datadog/clusteragent/leader/leader.go 0.00% <0.00%> (ø)
cmd/kubectl-datadog/mcp/mcp.go 64.40% <64.40%> (ø)
cmd/kubectl-datadog/mcp/discovery.go 61.42% <61.42%> (ø)
cmd/kubectl-datadog/mcp/server.go 0.00% <0.00%> (ø)
... and 4 more

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 305e9c2...83acbff. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@estherk15
Copy link
Contributor

Created an editorial review card for documentation

@estherk15 estherk15 added documentation Improvements or additions to documentation editorial review Waiting on a more in-depth review labels Jan 5, 2026
clamoriniere and others added 2 commits January 6, 2026 21:53
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]>
@clamoriniere clamoriniere force-pushed the clamoriniere/plugin-mcp branch from 9ae8905 to 83acbff Compare January 6, 2026 20:57
@L3n41c
Copy link
Member

L3n41c commented Jan 14, 2026

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a 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".

Comment on lines +156 to +160
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)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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
Copy link
Contributor

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).

Comment on lines +158 to +159
// Warn if multiple DatadogAgents exist
return dda.Name, fmt.Errorf("multiple DatadogAgents found, auto-selecting %s (use --proxy-dda-name to specify)", dda.Name)
Copy link
Contributor

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 = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: const

Copy link
Contributor

@joepeeples joepeeples left a 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
Copy link
Contributor

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:

Suggested change
- **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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

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).

Suggested change
After updating the configuration, restart Claude Desktop for the changes to take effect.

Comment on lines +121 to +124
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`

Comment on lines +182 to +184
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.
Copy link
Contributor

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.

Suggested change
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.

Comment on lines +361 to +364
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +379 to +382
### 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

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)
Copy link
Contributor

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?

Suggested change
- [kubectl-datadog Plugin Documentation](kubectl-datadog.md)
- [kubectl-datadog Plugin Documentation](kubectl-plugin.md)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/plugin documentation Improvements or additions to documentation editorial review Waiting on a more in-depth review enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants