@@ -7,11 +7,9 @@ import { parse } from 'node:querystring';
77import { Server } from '@modelcontextprotocol/sdk/server/index.js' ;
88import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' ;
99import { CallToolRequestSchema , ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js' ;
10- import { Actor , type ApifyClientOptions } from 'apify' ;
1110import type { ActorCallOptions } from 'apify-client' ;
12- import { ApifyClient } from 'apify-client' ;
13- import type { AxiosRequestConfig } from 'axios' ;
1411
12+ import { callActorGetDataset } from './actors/call.js' ;
1513import {
1614 getActorsAsTools ,
1715} from './actors/tools.js' ;
@@ -21,7 +19,6 @@ import {
2119 defaults ,
2220 SERVER_NAME ,
2321 SERVER_VERSION ,
24- USER_AGENT_ORIGIN ,
2522} from './const.js' ;
2623import { processInput } from './input.js' ;
2724import { log } from './logger.js' ;
@@ -52,59 +49,6 @@ export class ApifyMcpServer {
5249 this . setupToolHandlers ( ) ;
5350 }
5451
55- /**
56- * Adds a User-Agent header to the request config.
57- * @param config
58- * @private
59- */
60- private addUserAgent ( config : AxiosRequestConfig ) : AxiosRequestConfig {
61- const updatedConfig = { ...config } ;
62- updatedConfig . headers = updatedConfig . headers ?? { } ;
63- updatedConfig . headers [ 'User-Agent' ] = `${ updatedConfig . headers [ 'User-Agent' ] ?? '' } ; ${ USER_AGENT_ORIGIN } ` ;
64- return updatedConfig ;
65- }
66-
67- /**
68- * Calls an Apify actor and retrieves the dataset items.
69- *
70- * It requires the `APIFY_TOKEN` environment variable to be set.
71- * If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
72- *
73- * @param {string } actorName - The name of the actor to call.
74- * @param {ActorCallOptions } callOptions - The options to pass to the actor.
75- * @param {unknown } input - The input to pass to the actor.
76- * @returns {Promise<object[]> } - A promise that resolves to an array of dataset items.
77- * @throws {Error } - Throws an error if the `APIFY_TOKEN` is not set
78- */
79- public async callActorGetDataset (
80- actorName : string ,
81- input : unknown ,
82- apifyToken : string ,
83- callOptions : ActorCallOptions | undefined = undefined ,
84- ) : Promise < object [ ] > {
85- const name = actorName ;
86- try {
87- log . info ( `Calling Actor ${ name } with input: ${ JSON . stringify ( input ) } ` ) ;
88-
89- const options : ApifyClientOptions = { requestInterceptors : [ this . addUserAgent ] } ;
90- const client = new ApifyClient ( { ...options , token : apifyToken } ) ;
91- const actorClient = client . actor ( name ) ;
92-
93- const results = await actorClient . call ( input , callOptions ) ;
94- const dataset = await client . dataset ( results . defaultDatasetId ) . listItems ( ) ;
95- log . info ( `Actor ${ name } finished with ${ dataset . items . length } items` ) ;
96-
97- if ( process . env . APIFY_IS_AT_HOME ) {
98- await Actor . pushData ( dataset . items ) ;
99- log . info ( `Pushed ${ dataset . items . length } items to the dataset` ) ;
100- }
101- return dataset . items ;
102- } catch ( error ) {
103- log . error ( `Error calling actor: ${ error } . Actor: ${ name } , input: ${ JSON . stringify ( input ) } ` ) ;
104- throw new Error ( `Error calling Actor: ${ error } ` ) ;
105- }
106- }
107-
10852 public async addToolsFromActors ( actors : string [ ] ) {
10953 const tools = await getActorsAsTools ( actors ) ;
11054 this . updateTools ( tools ) ;
@@ -207,7 +151,7 @@ export class ApifyMcpServer {
207151 if ( tool . type === 'actor' ) {
208152 const actorTool = tool . tool as ActorTool ;
209153
210- const items = await this . callActorGetDataset ( actorTool . actorFullName , args , apifyToken as string , {
154+ const items = await callActorGetDataset ( actorTool . actorFullName , args , apifyToken as string , {
211155 memory : actorTool . memoryMbytes ,
212156 } as ActorCallOptions ) ;
213157 const content = items . map ( ( item ) => {
0 commit comments