Skip to content

Commit 28a02c9

Browse files
authored
fix(microsoft-learn-mcp-server): fixed charging (#59)
1 parent 4ec3698 commit 28a02c9

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

microsoft-learn-mcp-server/src/billing.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
*/
55
import { Actor, log } from 'apify';
66

7+
const CHARGEABLE_EVENT_NAMES = [
8+
'microsoft_learn_search',
9+
'microsoft_learn_fetch',
10+
] as const;
11+
12+
type ChargeableRequest = {
13+
method: string;
14+
} | {
15+
method: string;
16+
params?: {
17+
name?: string;
18+
};
19+
}
20+
721
/**
822
* Charges the user for a message request based on the method type.
923
* Supported method types are mapped to specific billing events.
@@ -12,27 +26,27 @@ import { Actor, log } from 'apify';
1226
* @param request - The request object containing the method string and optional params.
1327
* @returns Promise<void>
1428
*/
15-
export async function chargeMessageRequest(request: { method: string; params?: any }): Promise<void> {
16-
const { method, params } = request;
29+
export async function chargeMessageRequest(request: ChargeableRequest): Promise<void> {
30+
const method = request.method ?? null;
31+
const name = 'params' in request ? request.params?.name : null;
1732

18-
// Charge for specific Microsoft Learn MCP Server tools
19-
if (method === 'tools/call' && params?.name) {
20-
const toolName = params.name;
21-
if (toolName === 'microsoft_learn_search') {
22-
await Actor.charge({ eventName: 'microsoft-learn-search' });
23-
log.info(`Charged for Microsoft Learn search: ${toolName}`);
24-
return;
25-
}
26-
if (toolName === 'microsoft_learn_fetch') {
27-
await Actor.charge({ eventName: 'microsoft-learn-fetch' });
28-
log.info(`Charged for Microsoft Learn fetch: ${toolName}`);
29-
return;
30-
}
33+
if (!method) {
34+
log.warning(`Not charging for unknown method`);
35+
return;
3136
}
3237

33-
// See https://modelcontextprotocol.io/specification/2025-06-18/server for more details
34-
// on the method names and protocol messages
35-
// Charge for list requests (e.g., tools/list, resources/list, etc.)
36-
// Do not charge for other methods
37-
log.info(`Not charging for method: ${method}`);
38+
if (name) {
39+
const knownEventName = CHARGEABLE_EVENT_NAMES.find(
40+
(eventName) => name.includes(eventName)
41+
);
42+
43+
if (knownEventName) {
44+
await Actor.charge({ eventName: knownEventName });
45+
log.info(`Charged for ${knownEventName} request: ${method}`);
46+
} else {
47+
log.info(`Not charging for unknown event ${name}.`);
48+
}
49+
} else {
50+
log.info(`Not charging for method: ${method}`);
51+
}
3852
}

0 commit comments

Comments
 (0)