troxy-cli 1.13.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -67,6 +67,24 @@ npx troxy-cli <command>
67
67
 
68
68
  The CLI also ships an MCP server (`src/mcp-server.js`) that exposes Troxy as a tool for Claude and other MCP-compatible agents.
69
69
 
70
+ Checkpoints an agent can call:
71
+
72
+ | Tool | Call it before |
73
+ |------|----------------|
74
+ | `evaluate_payment` | paying, buying, transferring funds |
75
+ | `confirm_payment` | (after) reporting whether the charge succeeded |
76
+ | `evaluate_email` | sending an email or message |
77
+ | `evaluate_action` | deleting, dropping, force-pushing, revoking |
78
+ | `evaluate_login` | logging into or signing up for a site |
79
+ | `evaluate_model` | running a task on a model, and on any model or effort switch |
80
+ | `report_model_usage` | (after) reporting the real token total |
81
+ | `wait_for_payment_approval` | (after ESCALATE) waiting on the owner's decision |
82
+
83
+ The model checkpoint is the only one that is a required pair. `evaluate_model`
84
+ records what a run is expected to cost; `report_model_usage` records what it
85
+ actually cost. Without the second call Troxy holds only the agent's own
86
+ prediction, and the dashboard marks that agent's figures as unverified.
87
+
70
88
  ## Auth flow
71
89
 
72
90
  `troxy login` uses a device-code flow:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "troxy-cli",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "Control layer for AI agents: check payments, emails, logins and destructive actions against your policies",
5
5
  "homepage": "https://troxy.io",
6
6
  "bugs": {
package/src/api.js CHANGED
@@ -74,6 +74,11 @@ export const api = {
74
74
  evaluateEmail: (body, apiKey) => request('POST', '/evaluate/email', { apiKey, body }),
75
75
  evaluateLogin: (body, apiKey) => request('POST', '/evaluate/login', { apiKey, body }),
76
76
  evaluateAction: (body, apiKey) => request('POST', '/evaluate/action', { apiKey, body }),
77
+ // The model checkpoint is two calls: ask before the run, report after it.
78
+ // Only the pair produces usable data. The estimate alone is the agent's own
79
+ // guess, and Token Optimization says so on every agent that never reports.
80
+ evaluateModel: (body, apiKey) => request('POST', '/evaluate/model', { apiKey, body }),
81
+ reportModelUsage: (body, apiKey) => request('POST', '/evaluate/model/complete', { apiKey, body }),
77
82
  selfRevoke: (apiKey) => request('POST', '/mcp/self-revoke', { apiKey }),
78
83
  revokeAllOthers: (apiKey) => request('POST', '/mcp/revoke-all-others', { apiKey }),
79
84
  confirmPayment: (auditId, body, apiKey) => request('POST', `/payments/${auditId}/confirm`, { apiKey, body }),
package/src/mcp-server.js CHANGED
@@ -10,6 +10,8 @@ const confirmPayment = (auditId, body, apiKey) => api.confirmPayment(auditId, bo
10
10
  const evaluateEmail = (body, apiKey) => api.evaluateEmail(body, apiKey);
11
11
  const evaluateLogin = (body, apiKey) => api.evaluateLogin(body, apiKey);
12
12
  const evaluateAction = (body, apiKey) => api.evaluateAction(body, apiKey);
13
+ const evaluateModel = (body, apiKey) => api.evaluateModel(body, apiKey);
14
+ const reportModelUsage = (body, apiKey) => api.reportModelUsage(body, apiKey);
13
15
 
14
16
  export async function runMcp() {
15
17
  const config = loadConfig();
@@ -44,7 +46,12 @@ export async function runMcp() {
44
46
  'After every ALLOW or NOTIFY decision, attempt the payment, then call confirm_payment with the audit_id and the outcome (success/failed/cancelled). Always include a reason when the charge fails. ' +
45
47
  'You MUST also call the evaluate_email tool before sending any email on the user\'s behalf. Always include the full plaintext "body" argument, exactly as you intend to send it — Troxy policies inspect the body for secrets and confidential content, and cannot do so if it is omitted, summarized, or truncated. When the email goes to more than one person (a reply-all, a group, a bulk send), also pass every To/Cc/Bcc address in "recipients" so mass-email policies can apply. Never send an email without first receiving an ALLOW or NOTIFY decision. If BLOCK, do not send it. If ESCALATE, call wait_for_payment_approval with the returned approval_token (same wait mechanism payments use), then call evaluate_email again with the approval_token once approved. ' +
46
48
  'You MUST also call the evaluate_action tool before any destructive or hard-to-undo action on the user\'s behalf: deleting files or records, dropping or truncating a table, force-pushing or deleting a branch, destroying infrastructure, revoking access, or overwriting data. Give the verb, what it affects, how many items, and whether it can be undone. Never carry the action out without first receiving an ALLOW or NOTIFY decision. ' +
47
- 'You MUST also call the evaluate_login tool before logging into or signing up for any website on the user\'s behalf, giving the site\'s domain and whether it is a login or a signup. Never submit login credentials or a signup form without first receiving an ALLOW or NOTIFY decision. If BLOCK, do not proceed. If ESCALATE, call wait_for_payment_approval with the returned approval_token, then call evaluate_login again with the approval_token once approved.',
49
+ 'You MUST also call the evaluate_login tool before logging into or signing up for any website on the user\'s behalf, giving the site\'s domain and whether it is a login or a signup. Never submit login credentials or a signup form without first receiving an ALLOW or NOTIFY decision. If BLOCK, do not proceed. If ESCALATE, call wait_for_payment_approval with the returned approval_token, then call evaluate_login again with the approval_token once approved. ' +
50
+ // The model checkpoint is the only one that is a pair. The estimate on
51
+ // its own is the agent's own prediction, and the dashboard labels every
52
+ // agent that never reports back as unverified, so the instruction has
53
+ // to make the second call as non-optional as the first.
54
+ 'You MUST also call the evaluate_model tool when you begin a substantial task and whenever you switch model or effort level partway through it, giving the exact model id and how many tokens you expect to use. Do not run the task without first receiving an ALLOW or NOTIFY decision; if BLOCK, pick a cheaper or smaller model and call evaluate_model again with the new id. Then, when the task finishes, you MUST call report_model_usage with the audit_id from that decision and the real token total from your model API. Both halves are required: without the second call Troxy has only your prediction, and the account owner is shown that this agent\'s costs are unverified. Report the true number even when it is far above your estimate.',
48
55
  },
49
56
  { capabilities: { tools: {} } },
50
57
  );
@@ -255,6 +262,76 @@ export async function runMcp() {
255
262
  },
256
263
  },
257
264
  },
265
+ {
266
+ name: 'evaluate_model',
267
+ description:
268
+ 'Evaluate whether running a task on a given AI model should be allowed, blocked, or escalated ' +
269
+ 'based on your Troxy policies, and record what it is expected to cost. ' +
270
+ 'Call this when you start a task and whenever you switch model or effort level mid-task. ' +
271
+ 'Returns an audit_id. Keep it: you must pass it to report_model_usage when the task finishes.',
272
+ inputSchema: {
273
+ type: 'object',
274
+ required: ['model'],
275
+ properties: {
276
+ model: {
277
+ type: 'string',
278
+ description: 'The full model id you are about to use, e.g. "claude-sonnet-4.5", "gpt-4o-mini". Send the exact id, not a friendly name: Troxy prices per model, and generations of the same family differ (Claude 3 Opus costs three times Opus 4.x).',
279
+ },
280
+ estimated_tokens: {
281
+ type: 'number',
282
+ description: 'How many tokens you expect the task to use in total, input plus output. Troxy prices this itself, so an estimate in tokens is enough; do not convert it to dollars.',
283
+ },
284
+ effort: {
285
+ type: 'string',
286
+ enum: ['low', 'medium', 'high'],
287
+ description: 'How much work this task warrants. Report it honestly: "high effort on a simple task" is one of the things Troxy flags as waste.',
288
+ },
289
+ task: {
290
+ type: 'string',
291
+ description: 'One short line on what the model is being asked to do (optional). Shown to the account owner in the audit log.',
292
+ },
293
+ provider: {
294
+ type: 'string',
295
+ description: 'Model provider, e.g. "anthropic", "openai" (optional; inferred from the model id when omitted).',
296
+ },
297
+ agent: {
298
+ type: 'string',
299
+ description: 'Name of the agent running the task (optional).',
300
+ },
301
+ approval_token: {
302
+ type: 'string',
303
+ description: 'Approval token from a previous ESCALATE response. Include this to proceed after the user has approved.',
304
+ },
305
+ },
306
+ },
307
+ },
308
+ {
309
+ name: 'report_model_usage',
310
+ description:
311
+ 'Report how many tokens a task ACTUALLY used, once it has finished. ' +
312
+ 'Call this after every ALLOW or NOTIFY from evaluate_model, using the audit_id it returned. ' +
313
+ 'Without it Troxy only has your prediction, and the account owner is told, per agent, that its figures are unverified. ' +
314
+ 'Report the real number even when it is far above what you estimated: the gap is the point.',
315
+ inputSchema: {
316
+ type: 'object',
317
+ required: ['audit_id', 'actual_tokens'],
318
+ properties: {
319
+ audit_id: {
320
+ type: 'string',
321
+ description: 'The audit_id returned by evaluate_model for this task',
322
+ },
323
+ actual_tokens: {
324
+ type: 'number',
325
+ description: 'Total tokens the task really consumed, input plus output. Use the usage figures your model API returned, not an estimate.',
326
+ },
327
+ effort: {
328
+ type: 'string',
329
+ enum: ['low', 'medium', 'high'],
330
+ description: 'The effort level the task actually took, if it differed from what you declared up front (optional).',
331
+ },
332
+ },
333
+ },
334
+ },
258
335
  ],
259
336
  }));
260
337
 
@@ -395,6 +472,60 @@ export async function runMcp() {
395
472
  return { content: [{ type: 'text', text: actionText }], isError: decision === 'BLOCK' };
396
473
  }
397
474
 
475
+ if (toolName === 'evaluate_model') {
476
+ if (agentName && !args.agent) args.agent = agentName;
477
+ let result;
478
+ try {
479
+ result = await evaluateModel(args, apiKey);
480
+ } catch (err) {
481
+ return { content: [{ type: 'text', text: `Troxy error: ${err.message}` }], isError: true };
482
+ }
483
+ if (result.error) {
484
+ return { content: [{ type: 'text', text: `Troxy error: ${result.error}` }], isError: true };
485
+ }
486
+ const { decision, reason, audit_id, approval_token } = result;
487
+ const what = `${args.model}${args.effort ? ` at ${args.effort} effort` : ''}`;
488
+ // Every non-blocked branch repeats the audit_id and the instruction to
489
+ // report back. Half the value of this checkpoint is the second call, and
490
+ // an agent that is only told "approved" has no reason to make it.
491
+ const followUp = ` When the task finishes, call report_model_usage(audit_id="${audit_id}", actual_tokens=<real total>).`;
492
+ let modelText;
493
+ switch (decision) {
494
+ case 'ALLOW':
495
+ case 'NOTIFY':
496
+ modelText = `✓ Approved: ${what}.${reason ? ` ${reason}` : ''} You may run it.${followUp} (audit: ${audit_id})`;
497
+ break;
498
+ case 'BLOCK':
499
+ modelText = `✗ Blocked: ${what}.${reason ? ` ${reason}` : ''} Do not use this model for this task. Choose a cheaper or smaller model and call evaluate_model again with the new model id. (audit: ${audit_id})`;
500
+ break;
501
+ case 'ESCALATE':
502
+ modelText = `⏳ ${what} requires human approval; a request has been sent to the account owner.\n\nApproval token: ${approval_token}\n\nNow call wait_for_payment_approval(approval_token="${approval_token}") to automatically detect approval, then call evaluate_model again with the same arguments PLUS this approval_token. Do not run the task until it returns approved.`;
503
+ break;
504
+ default:
505
+ modelText = JSON.stringify(result);
506
+ }
507
+ return { content: [{ type: 'text', text: modelText }], isError: decision === 'BLOCK' };
508
+ }
509
+
510
+ if (toolName === 'report_model_usage') {
511
+ let result;
512
+ try {
513
+ result = await reportModelUsage(args, apiKey);
514
+ } catch (err) {
515
+ return { content: [{ type: 'text', text: `Troxy error: ${err.message}` }], isError: true };
516
+ }
517
+ if (result.error) {
518
+ return { content: [{ type: 'text', text: `Troxy error: ${result.error}` }], isError: true };
519
+ }
520
+ const cost = typeof result.actual_cost === 'number'
521
+ ? ` Cost: $${result.actual_cost.toFixed(4)}.`
522
+ : '';
523
+ return {
524
+ content: [{ type: 'text', text: `✓ Usage recorded: ${result.actual_tokens} tokens.${cost} (audit: ${args.audit_id})` }],
525
+ isError: false,
526
+ };
527
+ }
528
+
398
529
  if (toolName === 'evaluate_login') {
399
530
  if (agentName && !args.agent) args.agent = agentName;
400
531
  let result;
@@ -0,0 +1,100 @@
1
+ // The model checkpoint has to be reachable, and it has to be a pair.
2
+ //
3
+ // Until 2026-08-06 the API had /evaluate/model and /evaluate/model/complete and
4
+ // the MCP server exposed neither. Any agent connected over MCP, which is how
5
+ // most users connect, could not report model usage at all, so Token
6
+ // Optimization was permanently empty for them and nothing on the page said why.
7
+ // Verified on the live database that day: every model row on a real account was
8
+ // hand-made test data. Nothing had ever reported on its own.
9
+ //
10
+ // The subtler half is the report. An estimate on its own is the agent's own
11
+ // prediction of what a run would cost, which is exactly the number the
12
+ // dashboard refuses to present as fact. A tool list that offers the check
13
+ // without the report reintroduces that gap while looking complete.
14
+
15
+ import { test } from 'node:test';
16
+ import assert from 'node:assert';
17
+ import { readFileSync } from 'node:fs';
18
+ import { fileURLToPath } from 'node:url';
19
+ import { dirname, join } from 'node:path';
20
+
21
+ const src = readFileSync(
22
+ join(dirname(fileURLToPath(import.meta.url)), '..', 'mcp-server.js'),
23
+ 'utf8',
24
+ );
25
+ const apiSrc = readFileSync(
26
+ join(dirname(fileURLToPath(import.meta.url)), '..', 'api.js'),
27
+ 'utf8',
28
+ );
29
+
30
+ test('both halves of the model checkpoint are exposed as tools', () => {
31
+ for (const name of ['evaluate_model', 'report_model_usage']) {
32
+ assert.ok(
33
+ src.includes(`name: '${name}'`),
34
+ `${name} is not in the tool list, so an MCP agent cannot call it and ` +
35
+ `Token Optimization stays empty for every agent connected this way`,
36
+ );
37
+ assert.ok(
38
+ src.includes(`toolName === '${name}'`),
39
+ `${name} is advertised but has no handler, so calling it does nothing`,
40
+ );
41
+ }
42
+ });
43
+
44
+ test('both endpoints are wired in the api client', () => {
45
+ assert.ok(apiSrc.includes("'/evaluate/model'"), 'evaluate/model not wired');
46
+ assert.ok(
47
+ apiSrc.includes("'/evaluate/model/complete'"),
48
+ 'evaluate/model/complete not wired, so actuals can never be reported',
49
+ );
50
+ });
51
+
52
+ test('the server instructions tell the agent to do both', () => {
53
+ // Checkpoints are cooperative: an agent only calls in because it was told
54
+ // to. A tool nobody is instructed to use is a tool nobody uses.
55
+ const instructions = src.slice(0, src.indexOf('capabilities'));
56
+ assert.ok(
57
+ instructions.includes('evaluate_model'),
58
+ 'nothing in the server instructions tells the agent to check its model, ' +
59
+ 'so the tool exists and never gets called',
60
+ );
61
+ assert.ok(
62
+ instructions.includes('report_model_usage'),
63
+ 'the instructions ask for the estimate but never for the actual, which ' +
64
+ 'is the half that makes the number trustworthy',
65
+ );
66
+ });
67
+
68
+ test('an approved decision repeats the audit_id and asks for the report', () => {
69
+ // The agent has to carry the audit_id from one call to the other. If the
70
+ // ALLOW response does not hand it back along with the instruction, the
71
+ // second call depends on the agent having remembered a tool description.
72
+ const handler = src.slice(src.indexOf("toolName === 'evaluate_model'"));
73
+ const allowBranch = handler.slice(0, handler.indexOf("toolName === 'report_model_usage'"));
74
+ assert.ok(
75
+ allowBranch.includes('report_model_usage(audit_id='),
76
+ 'the ALLOW/NOTIFY response does not tell the agent how to report back ' +
77
+ 'with the audit_id it was just given',
78
+ );
79
+ });
80
+
81
+ test('a blocked model tells the agent what to do instead', () => {
82
+ const handler = src.slice(src.indexOf("toolName === 'evaluate_model'"));
83
+ const blocked = handler.slice(handler.indexOf("case 'BLOCK'"));
84
+ assert.ok(
85
+ /cheaper or smaller model/.test(blocked.slice(0, 400)),
86
+ 'BLOCK says no without saying what to do next, so the agent either ' +
87
+ 'stops or retries the same model',
88
+ );
89
+ });
90
+
91
+ test('the model id is asked for exactly, not as a friendly name', () => {
92
+ // Generations of one family do not share a price: Claude 3 Opus is $15/$75
93
+ // against $5/$25 for Opus 4.x. "claude opus" prices as the wrong one.
94
+ const schema = src.slice(src.indexOf("name: 'evaluate_model'"));
95
+ assert.ok(
96
+ /exact id, not a friendly name/.test(schema.slice(0, 2000)),
97
+ 'the model description does not insist on the exact id, so agents will ' +
98
+ 'send family names and be priced as the wrong generation',
99
+ );
100
+ });