Python: feat(middleware): add agentpay-mcp payment governance middleware example#5048
Open
up2itnow0822 wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: feat(middleware): add agentpay-mcp payment governance middleware example#5048up2itnow0822 wants to merge 2 commits intomicrosoft:mainfrom
up2itnow0822 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…tpay-mcp Adds a new middleware sample demonstrating spend governance for AI agents making paid tool calls via x402 protocol. Features: - Per-session budget caps - Per-call spend limits - Category-based spending policies (data, compute, services) - Integration pattern with agentpay-mcp for production use This fills the payment governance gap in the middleware examples. AI gateways handle inference costs — this middleware handles everything else the agent spends. Reference: NVIDIA/NeMo-Agent-Toolkit-Examples PR microsoft#17 (merged)
Author
@microsoft-github-policy-service agree |
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 a new middleware sample (
payment_governance_middleware.py) demonstrating how to enforce spend governance on AI agent tool calls using agentpay-mcp.Problem
Agent Framework ships a rich middleware system with examples for logging, security, usage tracking, and shared state — but no example for payment governance. As agents gain the ability to make paid API calls (via x402 or other payment protocols), operators need middleware that enforces budget limits before money leaves the wallet.
AI gateways (LiteLLM, Portkey) govern inference costs. Nothing in the current samples governs outbound agent spending.
Solution
A function-based middleware that enforces three gates before every paid tool call:
The sample follows the existing middleware patterns (same imports, same structure as
usage_tracking_middleware.py) and runs standalone withuv run.Why agentpay-mcp
Changes
python/samples/02-agents/middleware/payment_governance_middleware.py— new samplepython/samples/02-agents/middleware/README.md— added table entry