Skip to content

feat(mcp): implement Phase 3 (smart filtering, memory guards, stdio transport, and 80%+ test suite) - #1969

Open
devGPP23 wants to merge 7 commits into
kmesh-net:mainfrom
devGPP23:feat/mcp-server-phase3
Open

devGPP23 wants to merge 7 commits into
kmesh-net:mainfrom
devGPP23:feat/mcp-server-phase3

Conversation

@devGPP23

@devGPP23 devGPP23 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

This PR implements Phase 3 of the Kmesh MCP Server proposal
It builds upon Phase 1 & 2 by introducing token-efficient context summarization, robust input validation, dual transport modes (stdio and sse), and a comprehensive unit testing suite achieving 80.2% statement coverage.

Work Done in this PR
###1. Smart Context Summarization & 16 MiB Memory Guard (mcp/summarize.go):

    • Replaced naive byte truncation with a structured JSON summarizer to keep massive xDS dumps and eBPF maps within LLM token limits without breaking JSON syntax.
    • Appends compact resource counts and identifiers by default (summary=true).
    • Implemented case-insensitive drill-down filtering (filter="<name/uid>") to inspect individual listeners, clusters, routes, or map entries in full.
    • Enforced a strict 16 MiB buffer guard to prevent memory exhaustion on large-scale clusters.

2. Dual Transport Protocol Support (mcp/server.go, ctl/mcp/mcp.go):

  • Added --transport flag to kmeshctl mcp serve with support for:
    • stdio (default): Enables seamless, zero-config plug-and-play with Claude Desktop and Cursor IDE via standard I/O.
    • sse: Enables HTTP Server-Sent Events streaming transport for web and network-accessible agents.
  • Added context cancellation and graceful shutdown handling.

3. Parameter Validation (mcp/validate.go):

  • Added strict sanitization and validation for Kubernetes names (podName, namespace) and operating mode parameters.

4. Comprehensive Unit Test Suite (mcp/tools_test.go, mcp/summarize_test.go):

  • Built fully isolated unit tests for all 10 tools using Kubernetes fake.Clientset and daemon mock servers.
  • Achieved 80.2% statement test coverage across the mcp package:
    • get_version, get_authz_status, get_daemon_health, get_logger_levels
    • config_dump and get_bpf_maps (summary and filter paths)
    • list_daemon_pods, get_mesh_namespaces, list_waypoints, get_waypoint_status

5. Documentation (docs/ctl/*.md):

  • Regenerated Cobra CLI reference documentation reflecting the new --transport flag and options.

ITERATIONS AFTER LIVE DEMO - in newer commits

While testing tool chaining with Claude Desktop we ran into some real failures, and this update fixes them properly:

  • config_dump without a mode used to always 404 - it hit an endpoint the daemon doesn't serve. It now auto-detects the mode via /version, and if that fails it says so clearly instead of silently guessing kernel-native (which could quietly return the wrong data).
    fixes mcp: config_dump tool returns 404 when the optional mode argument is omitted #1979
  • Parallel tool calls are now truly parallel. Port-forwards used to grab a local port before binding it, so two calls at the same time could collide. We now let the OS assign the port at bind time now no more races, no retries.
  • Errors are actually useful now. A bare EOF used to leave the AI guessing; now it explains the likely cause (e.g. "the daemon may not be running inside the pod") and includes the pod's phase/ready status. Timeouts are split too i.e 10s to connect, 30s to read big dumps.
  • Smaller stuff: filter matches field values only (so filter=name no longer matches every entry), truncation is UTF-8 safe, mcp/models.go (unused structs) is deleted, and each request uses a fresh connection.

Copilot AI balanced review requested due to automatic review settings October 19, 2026 17:27
@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign yaozengzeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 19, 2026 18:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.74%. Comparing base (c48f7f0) to head (596585a).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
pkg/kube/portforwarder.go 0.00% 36 Missing ⚠️

❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Files with missing lines Coverage Δ
pkg/kube/client.go 0.00% <ø> (ø)
pkg/kube/portforwarder.go 0.00% <0.00%> (ø)

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

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

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

…and test suite

Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
@devGPP23
devGPP23 force-pushed the feat/mcp-server-phase3 branch from 4f0b2ec to e4f8c59 Compare October 20, 2026 11:12
@devGPP23
devGPP23 marked this pull request as ready for review October 29, 2026 07:59
Copilot AI balanced review requested due to automatic review settings October 29, 2026 08:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

…g, and test updates

Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 17:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…errors

- Use OS-assigned ephemeral local ports (port 0 + GetPorts) so concurrent tool calls can never collide on the same port; removes the pre-allocation race in getAvailablePort
- Wrap the client-go forwarder (capturingForwarder) to resolve the bound port after readiness
- Split fetch timeouts: 10s forward-start, 30s daemon read for large dumps
- Fresh HTTP transport per call (no keep-alive reuse across port-forwards)
- Map EOF/refused/timeout failures to actionable hints including pod phase/ready status

Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 19:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 19:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 19:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…_namespaces

The daemon enrolls namespaces regardless of the label value casing (strings.EqualFold in pkg/utils/enroll.go), but the tool selected on the exact value 'Kmesh' so namespaces labelled 'kmesh' were silently missing. Select on the label key and filter client-side with EqualFold to match the daemon's enrollment behavior.

Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Signed-off-by: devGP7 <gauravpatil232005@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 12:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: config_dump tool returns 404 when the optional mode argument is omitted

3 participants