4
4
*/
5
5
import { Actor , log } from 'apify' ;
6
6
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
+
7
21
/**
8
22
* Charges the user for a message request based on the method type.
9
23
* Supported method types are mapped to specific billing events.
@@ -12,27 +26,27 @@ import { Actor, log } from 'apify';
12
26
* @param request - The request object containing the method string and optional params.
13
27
* @returns Promise<void>
14
28
*/
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 ;
17
32
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 ;
31
36
}
32
37
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
+ }
38
52
}
0 commit comments