feat: optional per-invocation MCP tool call cap#79
Open
dmartinol wants to merge 1 commit intoRHEcosystemAppEng:mainfrom
Open
feat: optional per-invocation MCP tool call cap#79dmartinol wants to merge 1 commit intoRHEcosystemAppEng:mainfrom
dmartinol wants to merge 1 commit intoRHEcosystemAppEng:mainfrom
Conversation
Introduces the `MAX_TOOL_CALLS_PER_INVOCATION` environment variable to limit the number of MCP tool executions per agent run. This feature is enforced in-process and is not shared across replicas. Updates to documentation and configuration files reflect this new capability, along with implementation in the `UsageTrackingPlugin` to manage tool call limits during execution. Signed-off-by: Daniele Martinoli <dmartino@redhat.com>
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.
Summary
Adds
MAX_TOOL_CALLS_PER_INVOCATION(default0= disabled) so operators can cap how many MCP tools may start in one ADK agent run. When the limit is exceeded,UsageTrackingPlugin.before_tool_callbackshort-circuits (ADK error dict); the tool is not executed andafter_tool_callbackdoes not run, so blocked calls are not persisted as usagetool_calls.Motivation
Implementation
Settings.max_tool_calls_per_invocationwith envMAX_TOOL_CALLS_PER_INVOCATION.UsageTrackingPlugin(invocation_idfromToolContext), guarded byasyncio.Lock.after_run_callbackdrops counters for the finished invocation to avoid unbounded memory growth.attempted tool=for clarity.Limitations (documented)
Counters are in-memory per process — not shared across Cloud Run instances, Podman replicas, or multi-worker processes.
docs/metering.mddescribes mitigations and a future Redis/DB direction for shared caps.Testing
make lintandmake testpass.tests/test_usage_plugin.py: disabled limit (0), block after N starts,after_run_callbackresets budget.Documentation and deploy wiring
docs/metering.md(per-invocation tool budget, diagram, comparison to rate limits, multi-instance caveat).docs/rate-limiting.md(three-layer table).docs/configuration.md(usage table; also lists existing metering env vars in the table)..env.example,CLAUDE.md,deploy/cloudrun/service.yaml,deploy/podman/lightspeed-agent-configmap.yaml.Made with Cursor