22 * Model Context Protocol (MCP) server for Apify Actors
33 */
44
5+ import * as crypto from 'node:crypto' ;
6+
57import type { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
68import { Server } from '@modelcontextprotocol/sdk/server/index.js' ;
79import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' ;
@@ -764,7 +766,7 @@ Please verify the tool name and ensure the tool is properly registered.`;
764766 */
765767 private finalizeAndTrackTelemetry (
766768 telemetryData : ToolCallTelemetryProperties | null ,
767- userId : string | null ,
769+ userId : string ,
768770 startTime : number ,
769771 toolStatus : 'succeeded' | 'failed' | 'aborted' ,
770772 ) : void {
@@ -786,9 +788,9 @@ Please verify the tool name and ensure the tool is properly registered.`;
786788 */
787789 private async prepareTelemetryData (
788790 tool : HelperTool | ActorTool | ActorMcpTool , mcpSessionId : string | undefined , apifyToken : string ,
789- ) : Promise < { telemetryData : ToolCallTelemetryProperties | null ; userId : string | null } > {
791+ ) : Promise < { telemetryData : ToolCallTelemetryProperties | null ; userId : string } > {
790792 if ( this . options . telemetry ?. enabled !== true ) {
791- return { telemetryData : null , userId : null } ;
793+ return { telemetryData : null , userId : crypto . randomUUID ( ) } ;
792794 }
793795
794796 const toolFullName = tool . type === 'actor' ? tool . actorFullName : tool . name ;
@@ -810,11 +812,15 @@ Please verify the tool name and ensure the tool is properly registered.`;
810812 userId = await getUserIdFromTokenCached ( apifyToken , apifyClient ) ;
811813 log . debug ( 'Telemetry: fetched userId' , { userId } ) ;
812814 }
815+ if ( ! userId ) {
816+ userId = crypto . randomUUID ( ) ;
817+ log . debug ( 'Telemetry: using random userId' , { userId } ) ;
818+ }
813819
814820 const capabilities = this . options . initializeRequestData ?. params ?. capabilities ;
815821 const params = this . options . initializeRequestData ?. params as InitializeRequest [ 'params' ] ;
816822 const telemetryData : ToolCallTelemetryProperties = {
817- app_name : 'apify- mcp-server ' ,
823+ app : 'mcp' ,
818824 app_version : getPackageVersion ( ) || '' ,
819825 mcp_client_name : params ?. clientInfo ?. name || '' ,
820826 mcp_client_version : params ?. clientInfo ?. version || '' ,
0 commit comments