@@ -30,7 +30,7 @@ import {
3030import { prompts } from '../prompts/index.js' ;
3131import { callActorGetDataset , defaultTools , getActorsAsTools , toolCategories } from '../tools/index.js' ;
3232import { decodeDotPropertyNames } from '../tools/utils.js' ;
33- import type { ActorMcpTool , ActorTool , HelperTool , ToolEntry } from '../types.js' ;
33+ import type { ActorMcpTool , ActorTool , AuthToken , HelperTool , ToolEntry } from '../types.js' ;
3434import { createProgressTracker } from '../utils/progress.js' ;
3535import { getToolPublicFieldOnly } from '../utils/tools.js' ;
3636import { connectMCPClient } from './client.js' ;
@@ -160,7 +160,7 @@ export class ActorsMcpServer {
160160 * @param toolNames - Array of tool names to ensure are loaded
161161 * @param apifyToken - Apify API token for authentication
162162 */
163- public async loadToolsByName ( toolNames : string [ ] , apifyToken : string ) {
163+ public async loadToolsByName ( toolNames : string [ ] , authToken : AuthToken ) {
164164 const loadedTools = this . listAllToolNames ( ) ;
165165 const actorsToLoad : string [ ] = [ ] ;
166166 const toolsToLoad : ToolEntry [ ] = [ ] ;
@@ -185,7 +185,7 @@ export class ActorsMcpServer {
185185 }
186186
187187 if ( actorsToLoad . length > 0 ) {
188- await this . loadActorsAsTools ( actorsToLoad , apifyToken ) ;
188+ await this . loadActorsAsTools ( actorsToLoad , authToken ) ;
189189 }
190190 }
191191
@@ -196,8 +196,8 @@ export class ActorsMcpServer {
196196 * @param apifyToken - Apify API token for authentication
197197 * @returns Promise<ToolEntry[]> - Array of loaded tool entries
198198 */
199- public async loadActorsAsTools ( actorIdsOrNames : string [ ] , apifyToken : string ) : Promise < ToolEntry [ ] > {
200- const actorTools = await getActorsAsTools ( actorIdsOrNames , apifyToken ) ;
199+ public async loadActorsAsTools ( actorIdsOrNames : string [ ] , authToken : AuthToken ) : Promise < ToolEntry [ ] > {
200+ const actorTools = await getActorsAsTools ( actorIdsOrNames , authToken ) ;
201201 if ( actorTools . length > 0 ) {
202202 this . upsertTools ( actorTools , true ) ;
203203 }
@@ -211,8 +211,8 @@ export class ActorsMcpServer {
211211 *
212212 * Used primarily for SSE.
213213 */
214- public async loadToolsFromUrl ( url : string , apifyToken : string ) {
215- const tools = await processParamsGetTools ( url , apifyToken ) ;
214+ public async loadToolsFromUrl ( url : string , authToken : AuthToken ) {
215+ const tools = await processParamsGetTools ( url , authToken ) ;
216216 if ( tools . length > 0 ) {
217217 log . debug ( 'Loading tools from query parameters' ) ;
218218 this . upsertTools ( tools , false ) ;
@@ -381,17 +381,28 @@ export class ActorsMcpServer {
381381 // eslint-disable-next-line prefer-const
382382 let { name, arguments : args , _meta : meta } = request . params ;
383383 const { progressToken } = meta || { } ;
384- const apifyToken = ( request . params . apifyToken || process . env . APIFY_TOKEN ) as string ;
384+ // Extract auth token with fallback to APIFY_TOKEN environment variable
385+ let authToken : AuthToken | undefined = request . params . authToken as AuthToken ;
386+
387+ // Fallback to APIFY_TOKEN environment variable for local development
388+ if ( ! authToken && process . env . APIFY_TOKEN ) {
389+ authToken = {
390+ value : process . env . APIFY_TOKEN ,
391+ type : 'apify' , // Environment variable is always an Apify token
392+ } ;
393+ }
385394 const userRentedActorIds = request . params . userRentedActorIds as string [ ] | undefined ;
386395
387- // Remove apifyToken from request.params just in case
388- delete request . params . apifyToken ;
396+ // Remove authToken from request.params only if it was provided in params
397+ if ( request . params . authToken ) {
398+ delete request . params . authToken ;
399+ }
389400 // Remove other custom params passed from apify-mcp-server
390401 delete request . params . userRentedActorIds ;
391402
392- // Validate token
393- if ( ! apifyToken ) {
394- const msg = 'APIFY_TOKEN is required. It must be set in the environment variables or passed as a parameter in the body .' ;
403+ // Validate auth token
404+ if ( ! authToken || ! authToken . value ) {
405+ const msg = 'Valid authentication token required. It must be provided either in the authToken parameter or APIFY_TOKEN environment variable .' ;
395406 log . error ( msg ) ;
396407 await this . server . sendLoggingMessage ( { level : 'error' , data : msg } ) ;
397408 throw new McpError (
@@ -462,7 +473,7 @@ export class ActorsMcpServer {
462473 extra,
463474 apifyMcpServer : this ,
464475 mcpServer : this . server ,
465- apifyToken ,
476+ authToken ,
466477 userRentedActorIds,
467478 progressTracker,
468479 } ) as object ;
@@ -478,7 +489,7 @@ export class ActorsMcpServer {
478489 const serverTool = tool . tool as ActorMcpTool ;
479490 let client : Client | undefined ;
480491 try {
481- client = await connectMCPClient ( serverTool . serverUrl , apifyToken ) ;
492+ client = await connectMCPClient ( serverTool . serverUrl , authToken . value ) ;
482493
483494 // Only set up notification handlers if progressToken is provided by the client
484495 if ( progressToken ) {
@@ -527,7 +538,7 @@ export class ActorsMcpServer {
527538 const { runId, datasetId, items } = await callActorGetDataset (
528539 actorTool . actorFullName ,
529540 args ,
530- apifyToken as string ,
541+ authToken ,
531542 callOptions ,
532543 progressTracker ,
533544 ) ;
0 commit comments