Skip to content

Grant meter credits over MCP - #15434

Closed
strandhvilliam wants to merge 29 commits into
villiam/runtime-sdk-creditsfrom
villiam/mcp-credits
Closed

strandhvilliam wants to merge 29 commits into
villiam/runtime-sdk-creditsfrom
villiam/mcp-credits

Conversation

@strandhvilliam

@strandhvilliam strandhvilliam commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #15423.

Makes the credits endpoint easy to find over MCP. Agents find tools by searching their descriptions, so this rewrites the endpoint's description to match how people ask, "Grant free units (credits) on a customer's usage meter, for example to compensate for an outage or reward an action."

Also regenerates the spec, SDKs and client, and adds the tool to the MCP docs.

Open questions:

  • Does gram use the 2027-01 API version?
  • Do we need to add a tool description somewhere else in gram?

View guided diff Turn on auto-fix

strandhvilliam and others added 29 commits October 13, 2026 14:16
…ode gate

- The credit customer meter endpoint returns the meter.credited event; the
  balance updates asynchronously.
- external_id idempotency is checked after a unique index conflict instead
  of before the insert, so concurrent replays return the original event
  rather than a 409.
- Crediting no longer requires config as code.
…service

The credit endpoint no longer imports it, so the separate exceptions module isn't needed.
Exactly one of meter_id or external_meter_id is required. The external ID is
looked up in the customer's organization. Trim the credit endpoint tests to
the ones that cover distinct behavior.

Regenerate client and docs.
Adds an "Add credits" side panel that grants credits on a customer meter
through POST /v1/customer-meters/credits. It opens from the customer-meter
page with that meter preselected, and from the customer menu with no meter
selected. The panel previews the new balance and, for billed meters, how
much of the current overage the credits cover.

Credits always roll over and can't be removed. The entry points only show
for organizations with config as code enabled and members who can manage
customers.
Each meter row on the customer Usage tab gets an "Add credits" button
that opens the panel with that meter preselected. Column widths give the
meter name more room so the extra column fits on narrower screens.
- The panel checks the customers:manage permission itself and shows the
  restricted view, like the other customer modals; callers use useModal
  and InlineModal directly and only check the config as code flag.
- The amount is a number input with form rules instead of a custom parser.
- Reuse FormattedUnits and formatCurrency in the summary.
- Drop the intro copy, the unit suffix and the schema-name discriminators.
- Explain an external_id conflict as a possibly already added grant
  instead of showing the raw API message.
- Inline the panel header in both views, like the other customer modals.
- Keep the overage cost helper local to getCreditPreview.
- Build balance and subscription lookups by meter before mapping options.
Buttons follow the form instead of pinning to the drawer bottom, the
amount is a numeric text field validated as you type, the credits field
is focused when a meter is preselected, labels pluralize, and the entry
points are hidden without customers:manage.
…llback

On mobile, Add credits sits on the title row.
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
@vercel

vercel Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
orbit Ready Ready Preview Oct 14, 2026 10:08am UTC
polar-test Ready Ready Preview Oct 14, 2026 10:08am UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot 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.

2 issues found across 6 files

Confidence score: 3/5

  • The MCP-facing description in server/polar/customer_meter/endpoints.py doesn’t require confirmation of the customer, meter, units, and balance before granting irreversible credits. Add that instruction so agents don’t execute the write without verifying those details.
  • docs/integrate/mcp.mdx presents the tool as generally available, so users on older specs may not understand why it’s missing. Document that the MCP client spec must include version 2027-01.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/integrate/mcp.mdx">

<violation number="1" location="docs/integrate/mcp.mdx:161">
P2: This catalog presents the tool as generally available, but it only appears when the MCP client uses a spec that includes version 2027-01. Add that version requirement so users of older specs understand why they cannot find it.</violation>
</file>

<file name="server/polar/customer_meter/endpoints.py">

<violation number="1" location="server/polar/customer_meter/endpoints.py:186">
P2: The MCP-facing description does not require confirming the customer, meter, units, and balance before granting irreversible credits. Add that confirmation instruction so agents do not execute this write based only on a natural-language request.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread docs/integrate/mcp.mdx
| Get custom field | Get a custom field by ID. |
| List custom fields | List custom fields. |
| Update custom field | Update a custom field. |
| Credit customer meter | Grant free units (credits) on a customer's usage meter, for example to compensate for an outage or reward an action. |

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.

P2: This catalog presents the tool as generally available, but it only appears when the MCP client uses a spec that includes version 2027-01. Add that version requirement so users of older specs understand why they cannot find it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At docs/integrate/mcp.mdx, line 161:

<comment>This catalog presents the tool as generally available, but it only appears when the MCP client uses a spec that includes version 2027-01. Add that version requirement so users of older specs understand why they cannot find it.</comment>

<file context>
@@ -158,6 +158,7 @@ The runtime tools above provide access to the following Polar operations.
 | Get custom field | Get a custom field by ID. |
 | List custom fields | List custom fields. |
 | Update custom field | Update a custom field. |
+| Credit customer meter | Grant free units (credits) on a customer's usage meter, for example to compensate for an outage or reward an action. |
 | Get customer meter | Get a customer meter by ID. |
 | List customer meters | List customer meters. |
</file context>
Suggested change
| Credit customer meter | Grant free units (credits) on a customer's usage meter, for example to compensate for an outage or reward an action. |
| Credit customer meter | Grant free units (credits) on a customer's usage meter, for example to compensate for an outage or reward an action. Available only with an API spec that includes version 2027-01. |

"""
Manually grant credits on a customer meter.
Grant free units (credits) on a customer's usage meter, for example to
compensate for an outage or reward an action. Credits can't be reversed.

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.

P2: The MCP-facing description does not require confirming the customer, meter, units, and balance before granting irreversible credits. Add that confirmation instruction so agents do not execute this write based only on a natural-language request.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At server/polar/customer_meter/endpoints.py, line 186:

<comment>The MCP-facing description does not require confirming the customer, meter, units, and balance before granting irreversible credits. Add that confirmation instruction so agents do not execute this write based only on a natural-language request.</comment>

<file context>
@@ -182,7 +182,11 @@ async def credits(
     """
-    Manually grant credits on a customer meter.
+    Grant free units (credits) on a customer's usage meter, for example to
+    compensate for an outage or reward an action. Credits can't be reversed.
+
+    Retrying with the same `external_id` and details returns the original
</file context>

An error occurred while trying to automatically change base from villiam/runtime-sdk-credits to main October 15, 2026 11:51

This branch was successfully deployed

2 active deployments
Preview – polar-test — 1e8dd252 Deployed Oct 14, 2026 by vercel[bot]
Preview – orbit — 1e8dd252 Deployed Oct 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant