Skip to content

Open source alternative to Semrush and Ahrefs

Link to the billing page when the MCP reports no credits (EVE-447) (#975)

Carter Smith234f3a788367
diff --git a/src/server/mcp/instrumentation.test.ts b/src/server/mcp/instrumentation.test.ts
index b91817f9..3464f8bf 100644
--- a/src/server/mcp/instrumentation.test.ts
+++ b/src/server/mcp/instrumentation.test.ts
@@ -98,13 +98,13 @@ describe("instrumentMcpToolHandler", () => {
expect(mocks.recordExternalMcpToolCall).not.toHaveBeenCalled();
});
- it("explains an out-of-credits call to the agent instead of the bare code", async () => {
+ it("explains an out-of-credits call to the agent with a billing link instead of the bare code", async () => {
const wrapped = instrumentMcpToolHandler("demo", outputSchema, async () => {
throw new AppError("INSUFFICIENT_CREDITS");
});
await expect(wrapped({}, toolContext)).rejects.toThrow(
- "doesn't have enough credits for this request",
+ "The organization owner can add credits at https://app.openseo.so/billing",
);
});
diff --git a/src/server/mcp/instrumentation.ts b/src/server/mcp/instrumentation.ts
index fb6dfa8e..6994acea 100644
--- a/src/server/mcp/instrumentation.ts
+++ b/src/server/mcp/instrumentation.ts
@@ -8,6 +8,8 @@ import { shouldCaptureAppErrorCode } from "@/shared/error-codes";
import { type ToolContext } from "@/server/mcp/context";
import { incrementSelfHostMcpToolCallCount } from "@/server/lib/self-host-telemetry";
import { toolErrorMessage } from "@/server/mcp/tool-error-message";
+import { buildDashboardUrl } from "@/server/mcp/urls";
+import { BILLING_ROUTE } from "@/shared/billing";
type ToolHandler<TArgs> = (
args: TArgs,
@@ -196,17 +198,21 @@ export function instrumentMcpToolHandler<TArgs>(
),
);
}
- throw agentFacingError(error, appError);
+ throw agentFacingError(error, appError, context.auth.baseUrl);
}
};
}
// The agent sees only the error message, and a bare INSUFFICIENT_CREDITS code
// gives it nothing to tell the user, so it tends to retry. Explain it instead,
-// without promoting upgrades or purchases (the server instructions forbid
-// that). A provider outage gets the same note as per-item tool errors; every
-// other error passes through unchanged.
-function agentFacingError(error: unknown, appError: AppError | null): unknown {
+// with a plain billing link and no upgrade or purchase pitch (directory
+// policies ban promotional content). A provider outage gets the same note as
+// per-item tool errors; every other error passes through unchanged.
+function agentFacingError(
+ error: unknown,
+ appError: AppError | null,
+ baseUrl: string,
+): unknown {
if (appError?.code === "UPSTREAM_UNAVAILABLE")
return new AppError("UPSTREAM_UNAVAILABLE", toolErrorMessage(appError));
if (appError?.code !== "INSUFFICIENT_CREDITS") return error;
@@ -214,6 +220,6 @@ function agentFacingError(error: unknown, appError: AppError | null): unknown {
appError.message === appError.code ? "" : `${appError.message} `;
return new AppError(
"INSUFFICIENT_CREDITS",
- `${detail}This OpenSEO organization doesn't have enough credits for this request. Retrying won't help until the organization has more credits.`,
+ `${detail}This OpenSEO organization doesn't have enough credits for this request. Retrying won't help until the organization has more credits. The organization owner can add credits at ${buildDashboardUrl(baseUrl, BILLING_ROUTE)}`,
);
}
diff --git a/src/server/mcp/server.ts b/src/server/mcp/server.ts
index fc373618..a4d94b04 100644
--- a/src/server/mcp/server.ts
+++ b/src/server/mcp/server.ts
@@ -193,7 +193,7 @@ export function createOpenSeoMcpServer(authProps: McpProps) {
// Without the pre-declaration, registerTool defaults it to true.
capabilities: { tools: { listChanged: false } },
instructions:
- "OpenSEO research tools use credits. Proceed with normal focused research, but ask the user for confirmation before planned batches over 2,000 credits. OpenSEO cannot purchase credits or charge cards. Explain unsupported purchase requests without promoting subscriptions, upgrades or credit purchases, or directing users to checkout.",
+ "OpenSEO research tools use credits. Proceed with normal focused research, but ask the user for confirmation before planned batches over 2,000 credits. OpenSEO cannot purchase credits or charge cards. Explain unsupported purchase requests without promoting subscriptions, upgrades or credit purchases. When a tool reports that the organization is out of credits, you may share the billing link from that error as a plain link.",
},
);