UNPKG

@apify/actors-mcp-server

Version:

Model Context Protocol Server for Apify

58 lines 2.66 kB
/** * Serves as an Actor MCP SSE server entry point. * This file needs to be named `main.ts` to be recognized by the Apify platform. */ var _a; import { Actor } from 'apify'; import log from '@apify/log'; import { createExpressApp } from './actor/server.js'; import { processInput } from './input.js'; import { callActorGetDataset } from './tools/index.js'; const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY'; await Actor.init(); const HOST = Actor.isAtHome() ? process.env.ACTOR_STANDBY_URL : 'http://localhost'; const PORT = Actor.isAtHome() ? Number(process.env.ACTOR_STANDBY_PORT) : 3001; if (!process.env.APIFY_TOKEN) { log.error('APIFY_TOKEN is required but not set in the environment variables.'); process.exit(1); } const input = processInput((_a = (await Actor.getInput())) !== null && _a !== void 0 ? _a : {}); log.info('Loaded input', { input: JSON.stringify(input) }); if (STANDBY_MODE) { let actorsToLoad = []; // TODO: in standby mode the input loading does not actually work, // we should remove this since we are using the URL query parameters to load Actors // Load only Actors specified in the input // If you wish to start without any Actor, create a task and leave the input empty if (input.actors && input.actors.length > 0) { const { actors } = input; actorsToLoad = Array.isArray(actors) ? actors : actors.split(','); } // Include Actors to load in the MCP server options for backwards compatibility const app = createExpressApp(HOST, { enableAddingActors: Boolean(input.enableAddingActors), enableDefaultActors: false, actors: actorsToLoad, }); log.info('Actor is running in the STANDBY mode.'); app.listen(PORT, () => { log.info('Actor web server listening', { host: HOST, port: PORT }); }); } else { log.info('Actor is not designed to run in the NORMAL model (use this mode only for debugging purposes)'); if (input && !input.debugActor && !input.debugActorInput) { await Actor.fail('If you need to debug a specific Actor, please provide the debugActor and debugActorInput fields in the input'); } const options = { memory: input.maxActorMemoryBytes }; const { items } = await callActorGetDataset(input.debugActor, input.debugActorInput, process.env.APIFY_TOKEN, options); await Actor.pushData(items); log.info('Pushed items to dataset', { itemCount: items.count }); await Actor.exit(); } // So Ctrl+C works locally process.on('SIGINT', async () => { log.info('Received SIGINT, shutting down gracefully...'); await Actor.exit(); }); //# sourceMappingURL=main.js.map