1+
12/**
23 * Model Context Protocol (MCP) server for Apify Actors
34 */
@@ -14,12 +15,14 @@ import {
1415 ACTOR_OUTPUT_TRUNCATED_MESSAGE ,
1516 SERVER_NAME ,
1617 SERVER_VERSION ,
17- } from './const.js' ;
18- import { actorDefinitionTool , callActorGetDataset , getActorsAsTools , searchTool } from './tools/index.js' ;
19- import type { ActorTool , HelperTool , ToolWrap } from './types.js' ;
20- import { defaults } from './const.js' ;
21- import { actorNameToToolName } from './tools/utils.js' ;
22- import { processParamsGetTools } from './actor/utils.js' ;
18+ } from '../const.js' ;
19+ import { actorDefinitionTool , callActorGetDataset , getActorsAsTools , searchTool } from '../tools/index.js' ;
20+ import type { ActorMCPTool , ActorTool , HelperTool , ToolWrap } from '../types.js' ;
21+ import { defaults } from '../const.js' ;
22+ import { actorNameToToolName } from '../tools/utils.js' ;
23+ import { processParamsGetTools } from './utils.js' ;
24+ import { createMCPClient } from './client.js' ;
25+ import { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
2326
2427/**
2528 * Create Apify MCP server
@@ -51,9 +54,9 @@ export class ActorsMcpServer {
5154 /**
5255 * Loads missing default tools.
5356 */
54- public async loadDefaultTools ( ) {
57+ public async loadDefaultTools ( apifyToken : string ) {
5558 const missingDefaultTools = defaults . actors . filter ( name => ! this . tools . has ( actorNameToToolName ( name ) ) ) ;
56- const tools = await getActorsAsTools ( missingDefaultTools ) ;
59+ const tools = await getActorsAsTools ( missingDefaultTools , apifyToken ) ;
5760 if ( tools . length > 0 ) this . updateTools ( tools ) ;
5861 }
5962
@@ -62,8 +65,8 @@ export class ActorsMcpServer {
6265 *
6366 * Used primarily for SSE.
6467 */
65- public async loadToolsFromUrl ( url : string ) {
66- const tools = await processParamsGetTools ( url ) ;
68+ public async loadToolsFromUrl ( url : string , apifyToken : string ) {
69+ const tools = await processParamsGetTools ( url , apifyToken ) ;
6770 if ( tools . length > 0 ) this . updateTools ( tools ) ;
6871 }
6972
@@ -117,7 +120,10 @@ export class ActorsMcpServer {
117120 */
118121 this . server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
119122 const { name, arguments : args } = request . params ;
120- const apifyToken = request . params . apifyToken || process . env . APIFY_TOKEN ;
123+ const apifyToken = ( request . params . apifyToken || process . env . APIFY_TOKEN ) as string ;
124+
125+ // Remove apifyToken from request.params just in case
126+ delete request . params . apifyToken ;
121127
122128 // Validate token
123129 if ( ! apifyToken ) {
@@ -147,11 +153,29 @@ export class ActorsMcpServer {
147153 args,
148154 apifyMcpServer : this ,
149155 mcpServer : this . server ,
156+ apifyToken,
150157 } ) as object ;
151158
152159 return { ...res } ;
153160 }
154161
162+ if ( tool . type === 'actor-mcp' ) {
163+ const serverTool = tool . tool as ActorMCPTool ;
164+ let client : Client | undefined ;
165+ try {
166+ client = await createMCPClient ( serverTool . serverUrl , apifyToken ) ;
167+ const res = await client . callTool ( {
168+ name : name ,
169+ arguments : args ,
170+ } ) ;
171+
172+ return { ...res } ;
173+ } finally {
174+ if ( client ) await client . close ( ) ;
175+ }
176+
177+ }
178+
155179 // Handle actor tool
156180 if ( tool . type === 'actor' ) {
157181 const actorTool = tool . tool as ActorTool ;
0 commit comments