Conversation
k8s_get_resources had no way to bound its result size, so a call with all_namespaces=true returned every row on the cluster into the model's context and could exhaust the context window. The limit is applied per output format, because slicing lines out of a structured document would return an invalid one: tabular output is cut by row while preserving its header, json is cut at its items array, and yaml is left untouched since it has no equally safe cut point. A truncated result carries a notice naming the totals, so a shortened listing is not mistaken for a complete one. limit=0 returns everything. Closes kagent-dev#82 Signed-off-by: Lucas Anjos <[email protected]>
anjosluc
force-pushed
the
feature/k8s-get-resources-limit
branch
from
September 16, 2026 20:33
4597f61 to
51cc7aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #82
What this changes
Adds an optional
limitparameter tok8s_get_resourcesthat caps how many resources a call returns.limit=0returns everything.Before this, the tool had no way to bound its result size. An agent calling it with
all_namespaces=trueon a large cluster gets every row of every namespace in one tool result, which goes straight into the model's context. In our production estate that is the largest single source of context growth — turns issuing a few cluster-wide calls reached several million input tokens and exhausted the context window, which ends the turn with an error rather than a large-but-usable answer. Prompt guidance reduced how often it happened but could not prevent it, because the tool still permitted the unbounded call.How it works
Truncation is applied per output format, because slicing lines out of a structured document would return an invalid one:
outputwide,name,custom-columns…, unsetNAMEheaderjsonitemsarray, so the document stays parseableyaml,jsonpath,go-templateA single object rather than a list (for example
-o jsonwith aresource_name) is returned untouched, and unparseable output is passed through rather than guessed at.A truncated result carries a notice naming the totals:
That notice is as important as the cap. A silently shortened listing reads as complete, so the model would draw conclusions from partial data — worse than the original problem.
Decision I would like your input on
The default is a finite cap (200) rather than unlimited, which changes behaviour for existing callers.
I chose a finite default because it is the only version that protects an agent that does not know to pass the parameter, and that is the case causing real failures for us. The truncation notice keeps it honest, and
limit=0restores the old behaviour exactly.The trade-off is real though, and if you would rather this be strictly opt-in I am happy to flip the default to
0— it is a one-line change plus a test. Please say which you prefer.Tests
Added seven cases to
TestHandleKubectlGetEnhancedinpkg/k8s/k8s_test.go, covering truncation, a listing under the limit,limit=0, the default limit, JSONitemstruncation (asserting the result still parses), YAML passthrough, and a single JSON object.I verified the new tests actually bite by reverting
pkg/k8s/k8s.gotomainand confirming they fail, then restoring it and confirming they pass.make buildpasses. I did not runmake e2e, which needs a cluster — CI covers it on Kind.Documentation
The tool's own MCP description documents the new parameter. I did not touch
README.md: its Kubernetes section lists tool names only, no parameters, so there was nothing parameter-level to update. Happy to add something if you would like it documented there.