@@ -57,6 +57,7 @@ import type {
5757 ActorMcpTool ,
5858 ActorsMcpServerOptions ,
5959 ActorTool ,
60+ ApifyRequestParams ,
6061 HelperTool ,
6162 TelemetryEnv ,
6263 ToolCallTelemetryProperties ,
@@ -525,8 +526,9 @@ export class ActorsMcpServer {
525526 // List tasks
526527 this . server . setRequestHandler ( ListTasksRequestSchema , async ( request ) => {
527528 // mcpSessionId is injected at transport layer for session isolation in task stores
528- const params = ( request . params || { } ) as { cursor ?: string ; mcpSessionId ?: string } ;
529- const { cursor, mcpSessionId } = params ;
529+ const params = ( request . params || { } ) as ApifyRequestParams & { cursor ?: string } ;
530+ const { cursor } = params ;
531+ const mcpSessionId = params . _meta ?. mcpSessionId ;
530532 log . debug ( '[ListTasksRequestSchema] Listing tasks' , { mcpSessionId } ) ;
531533 const result = await this . taskStore . listTasks ( cursor , mcpSessionId ) ;
532534 return { tasks : result . tasks , nextCursor : result . nextCursor } ;
@@ -535,8 +537,9 @@ export class ActorsMcpServer {
535537 // Get task status
536538 this . server . setRequestHandler ( GetTaskRequestSchema , async ( request ) => {
537539 // mcpSessionId is injected at transport layer for session isolation in task stores
538- const params = ( request . params || { } ) as { taskId : string ; mcpSessionId ?: string } ;
539- const { taskId, mcpSessionId } = params ;
540+ const params = ( request . params || { } ) as ApifyRequestParams & { taskId : string } ;
541+ const { taskId } = params ;
542+ const mcpSessionId = params . _meta ?. mcpSessionId ;
540543 log . debug ( '[GetTaskRequestSchema] Getting task status' , { taskId, mcpSessionId } ) ;
541544 const task = await this . taskStore . getTask ( taskId , mcpSessionId ) ;
542545 if ( task ) return task ;
@@ -549,8 +552,9 @@ export class ActorsMcpServer {
549552 // Get task result payload
550553 this . server . setRequestHandler ( GetTaskPayloadRequestSchema , async ( request ) => {
551554 // mcpSessionId is injected at transport layer for session isolation in task stores
552- const params = ( request . params || { } ) as { taskId : string ; mcpSessionId ?: string } ;
553- const { taskId, mcpSessionId } = params ;
555+ const params = ( request . params || { } ) as ApifyRequestParams & { taskId : string } ;
556+ const { taskId } = params ;
557+ const mcpSessionId = params . _meta ?. mcpSessionId ;
554558 log . debug ( '[GetTaskPayloadRequestSchema] Getting task result' , { taskId, mcpSessionId } ) ;
555559 const task = await this . taskStore . getTask ( taskId , mcpSessionId ) ;
556560 if ( ! task ) {
@@ -573,8 +577,9 @@ export class ActorsMcpServer {
573577 // Cancel task
574578 this . server . setRequestHandler ( CancelTaskRequestSchema , async ( request ) => {
575579 // mcpSessionId is injected at transport layer for session isolation in task stores
576- const params = ( request . params || { } ) as { taskId : string ; mcpSessionId ?: string } ;
577- const { taskId, mcpSessionId } = params ;
580+ const params = ( request . params || { } ) as ApifyRequestParams & { taskId : string } ;
581+ const { taskId } = params ;
582+ const mcpSessionId = params . _meta ?. mcpSessionId ;
578583 log . debug ( '[CancelTaskRequestSchema] Cancelling task' , { taskId, mcpSessionId } ) ;
579584
580585 const task = await this . taskStore . getTask ( taskId , mcpSessionId ) ;
@@ -622,22 +627,19 @@ export class ActorsMcpServer {
622627 * @throws {McpError } - based on the McpServer class code from the typescript MCP SDK
623628 */
624629 this . server . setRequestHandler ( CallToolRequestSchema , async ( request , extra ) => {
630+ const params = request . params as ApifyRequestParams & { name : string ; arguments ?: Record < string , unknown > } ;
625631 // eslint-disable-next-line prefer-const
626- let { name, arguments : args , _meta : meta } = request . params ;
627- const { progressToken } = meta || { } ;
628- const apifyToken = ( request . params . apifyToken || this . options . token || process . env . APIFY_TOKEN ) as string ;
629- const userRentedActorIds = request . params . userRentedActorIds as string [ ] | undefined ;
632+ let { name, arguments : args , _meta : meta } = params ;
633+ const progressToken = meta ?. progressToken ;
634+ const metaApifyToken = meta ?. apifyToken ;
635+ const apifyToken = ( metaApifyToken || this . options . token || process . env . APIFY_TOKEN ) as string ;
636+ const userRentedActorIds = meta ?. userRentedActorIds ;
630637 // mcpSessionId was injected upstream it is important and required for long running tasks as the store uses it and there is not other way to pass it
631- const mcpSessionId = typeof request . params . mcpSessionId === 'string' ? request . params . mcpSessionId : undefined ;
638+ const mcpSessionId = meta ?. mcpSessionId ;
632639 if ( ! mcpSessionId ) {
633640 log . error ( 'MCP Session ID is missing in tool call request. This should never happen.' ) ;
634- // TEMP: do not throw for now as it causes issues with stdio transport
635- // throw new Error('MCP Session ID is required for tool calls');
641+ throw new Error ( 'MCP Session ID is required for tool calls' ) ;
636642 }
637- // Remove apifyToken from request.params just in case
638- delete request . params . apifyToken ;
639- // Remove other custom params passed from apify-mcp-server
640- delete request . params . userRentedActorIds ;
641643
642644 // Validate token
643645 if ( ! apifyToken && ! this . options . skyfireMode && ! this . options . allowUnauthMode ) {
@@ -690,7 +692,7 @@ Please provide the required arguments for this tool. Check the tool's input sche
690692 }
691693 // Decode dot property names in arguments before validation,
692694 // since validation expects the original, non-encoded property names.
693- args = decodeDotPropertyNames ( args ) ;
695+ args = decodeDotPropertyNames ( args as Record < string , unknown > ) as Record < string , unknown > ;
694696 log . debug ( 'Validate arguments for tool' , { toolName : tool . name , input : args } ) ;
695697 if ( ! tool . ajvValidate ( args ) ) {
696698 const errors = tool ?. ajvValidate . errors || [ ] ;
0 commit comments