@coldiq/mcp 0.3.18 → 0.3.19
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-session-usage.d.ts","sourceRoot":"","sources":["../../src/tools/get-session-usage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"get-session-usage.d.ts","sourceRoot":"","sources":["../../src/tools/get-session-usage.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,mBAAmB,sBAAsB,CAAA;AAEtD,eAAO,MAAM,0BAA0B,QAOY,CAAA;AAEnD,eAAO,MAAM,qBAAqB,IAAK,CAAA;AAMvC,wBAAsB,sBAAsB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;;;GA8B3E"}
|
|
@@ -1,14 +1,45 @@
|
|
|
1
1
|
import { getSessionUsage } from '../session-usage.js';
|
|
2
|
+
import { callApi } from '../client.js';
|
|
2
3
|
export const getSessionUsageName = 'get_session_usage';
|
|
3
|
-
export const getSessionUsageDescription = 'Report the ColdIQ credits spent so far in
|
|
4
|
-
'
|
|
5
|
-
'
|
|
6
|
-
'
|
|
7
|
-
'
|
|
8
|
-
'
|
|
4
|
+
export const getSessionUsageDescription = 'Report the ColdIQ credits spent so far in this MCP process: total credits charged, billed-call count, ' +
|
|
5
|
+
'and a per-endpoint breakdown (highest spend first), plus the live authoritative balance for reconciliation. ' +
|
|
6
|
+
'The session tally is BEST-EFFORT — it sums what each call reported at request time and therefore: (a) cannot ' +
|
|
7
|
+
'see out-of-band async settlement (webhook refunds/charges that land after the call returns), (b) excludes ' +
|
|
8
|
+
'transport failures that never reached the server, and (c) persists for the life of the MCP process, so a ' +
|
|
9
|
+
'reused process can include earlier conversations. Treat session_spend as indicative; use authoritative_balance ' +
|
|
10
|
+
'(or get_credit_balance) as the source of truth.';
|
|
9
11
|
export const getSessionUsageSchema = {};
|
|
12
|
+
const SESSION_USAGE_NOTE = 'session_spend is a best-effort, header-derived tally for this MCP process; it excludes out-of-band async ' +
|
|
13
|
+
'settlement and resets only when the process restarts. Reconcile against authoritative_balance (live).';
|
|
10
14
|
export async function getSessionUsageHandler(_input) {
|
|
11
15
|
const usage = getSessionUsage();
|
|
12
|
-
|
|
16
|
+
// Reconcile against the authoritative balance. /me/credits is not billed, so this
|
|
17
|
+
// call never pollutes the tally. Best-effort: a failure just omits the field.
|
|
18
|
+
let authoritativeBalance;
|
|
19
|
+
try {
|
|
20
|
+
const res = await callApi('GET', '/me/credits');
|
|
21
|
+
if (res.ok && res.data && typeof res.data === 'object') {
|
|
22
|
+
const bal = res.data.data?.balance;
|
|
23
|
+
if (typeof bal === 'number')
|
|
24
|
+
authoritativeBalance = bal;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// ignore — reconciliation is best-effort
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
content: [
|
|
32
|
+
{
|
|
33
|
+
type: 'text',
|
|
34
|
+
text: JSON.stringify({
|
|
35
|
+
data: {
|
|
36
|
+
...usage,
|
|
37
|
+
...(authoritativeBalance !== undefined ? { authoritative_balance: authoritativeBalance } : {}),
|
|
38
|
+
note: SESSION_USAGE_NOTE,
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
13
44
|
}
|
|
14
45
|
//# sourceMappingURL=get-session-usage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-session-usage.js","sourceRoot":"","sources":["../../src/tools/get-session-usage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"get-session-usage.js","sourceRoot":"","sources":["../../src/tools/get-session-usage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,O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;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,51 @@
|
|
|
1
1
|
import { getSessionUsage } from '../session-usage.js'
|
|
2
|
+
import { callApi } from '../client.js'
|
|
2
3
|
|
|
3
4
|
export const getSessionUsageName = 'get_session_usage'
|
|
4
5
|
|
|
5
6
|
export const getSessionUsageDescription =
|
|
6
|
-
'Report the ColdIQ credits spent so far in
|
|
7
|
-
'
|
|
8
|
-
'
|
|
9
|
-
'
|
|
10
|
-
'
|
|
11
|
-
'
|
|
7
|
+
'Report the ColdIQ credits spent so far in this MCP process: total credits charged, billed-call count, ' +
|
|
8
|
+
'and a per-endpoint breakdown (highest spend first), plus the live authoritative balance for reconciliation. ' +
|
|
9
|
+
'The session tally is BEST-EFFORT — it sums what each call reported at request time and therefore: (a) cannot ' +
|
|
10
|
+
'see out-of-band async settlement (webhook refunds/charges that land after the call returns), (b) excludes ' +
|
|
11
|
+
'transport failures that never reached the server, and (c) persists for the life of the MCP process, so a ' +
|
|
12
|
+
'reused process can include earlier conversations. Treat session_spend as indicative; use authoritative_balance ' +
|
|
13
|
+
'(or get_credit_balance) as the source of truth.'
|
|
12
14
|
|
|
13
15
|
export const getSessionUsageSchema = {}
|
|
14
16
|
|
|
17
|
+
const SESSION_USAGE_NOTE =
|
|
18
|
+
'session_spend is a best-effort, header-derived tally for this MCP process; it excludes out-of-band async ' +
|
|
19
|
+
'settlement and resets only when the process restarts. Reconcile against authoritative_balance (live).'
|
|
20
|
+
|
|
15
21
|
export async function getSessionUsageHandler(_input: Record<string, unknown>) {
|
|
16
22
|
const usage = getSessionUsage()
|
|
17
|
-
|
|
23
|
+
|
|
24
|
+
// Reconcile against the authoritative balance. /me/credits is not billed, so this
|
|
25
|
+
// call never pollutes the tally. Best-effort: a failure just omits the field.
|
|
26
|
+
let authoritativeBalance: number | undefined
|
|
27
|
+
try {
|
|
28
|
+
const res = await callApi('GET', '/me/credits')
|
|
29
|
+
if (res.ok && res.data && typeof res.data === 'object') {
|
|
30
|
+
const bal = (res.data as { data?: { balance?: unknown } }).data?.balance
|
|
31
|
+
if (typeof bal === 'number') authoritativeBalance = bal
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
// ignore — reconciliation is best-effort
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
content: [
|
|
39
|
+
{
|
|
40
|
+
type: 'text' as const,
|
|
41
|
+
text: JSON.stringify({
|
|
42
|
+
data: {
|
|
43
|
+
...usage,
|
|
44
|
+
...(authoritativeBalance !== undefined ? { authoritative_balance: authoritativeBalance } : {}),
|
|
45
|
+
note: SESSION_USAGE_NOTE,
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
}
|
|
18
51
|
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
resetSessionUsage,
|
|
7
7
|
} from '../src/session-usage.js'
|
|
8
8
|
import { initClient, callApi } from '../src/client.js'
|
|
9
|
+
import { getSessionUsageHandler } from '../src/tools/get-session-usage.js'
|
|
9
10
|
|
|
10
11
|
describe('session-usage accumulator', () => {
|
|
11
12
|
beforeEach(() => {
|
|
@@ -128,3 +129,34 @@ describe('callApi records credit charges into session usage', () => {
|
|
|
128
129
|
expect(getSessionUsage().billedCalls).toBe(0)
|
|
129
130
|
})
|
|
130
131
|
})
|
|
132
|
+
|
|
133
|
+
// --- B2: get_session_usage reconciles against the authoritative balance ------
|
|
134
|
+
describe('get_session_usage handler reconciliation (B2)', () => {
|
|
135
|
+
const originalFetch = globalThis.fetch
|
|
136
|
+
beforeEach(() => {
|
|
137
|
+
initClient('http://test-api.local', 'test-key')
|
|
138
|
+
resetSessionUsage(0)
|
|
139
|
+
})
|
|
140
|
+
afterEach(() => { globalThis.fetch = originalFetch })
|
|
141
|
+
|
|
142
|
+
it('attaches the live authoritative_balance and a best-effort note', async () => {
|
|
143
|
+
recordCharge('/email/find', 2, 500) // header-derived tally (may drift from real balance)
|
|
144
|
+
globalThis.fetch = vi.fn(async () =>
|
|
145
|
+
new Response(JSON.stringify({ data: { balance: 480.5, usedThisMonth: 20 } }), { status: 200 }),
|
|
146
|
+
) as typeof fetch
|
|
147
|
+
|
|
148
|
+
const res = await getSessionUsageHandler({})
|
|
149
|
+
const payload = JSON.parse(res.content[0].text).data
|
|
150
|
+
expect(payload.totalCreditsCharged).toBe(2)
|
|
151
|
+
expect(payload.authoritative_balance).toBe(480.5) // reconciliation source of truth
|
|
152
|
+
expect(payload.note).toMatch(/best-effort/i)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('still returns the tally + note when the balance lookup fails (best-effort)', async () => {
|
|
156
|
+
globalThis.fetch = vi.fn(async () => new Response('nope', { status: 502 })) as typeof fetch
|
|
157
|
+
const res = await getSessionUsageHandler({})
|
|
158
|
+
const payload = JSON.parse(res.content[0].text).data
|
|
159
|
+
expect(payload.authoritative_balance).toBeUndefined()
|
|
160
|
+
expect(payload.note).toBeTruthy()
|
|
161
|
+
})
|
|
162
|
+
})
|