Skip to content

Commit 0f44740

Browse files
authored
fix: move logic to enableAddingActors and enableDefaultActors to constructor (#90)
* fix: move logic to enableAddingActors and enableDefaultActors to constructor
1 parent a8caa10 commit 0f44740

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,4 @@ All notable changes to this project will be documented in this file.
205205
### 🐛 Bug Fixes
206206

207207
- Update express routes to correctly handle GET and HEAD requests, fix CI ([#5](https://github.com/apify/actors-mcp-server/pull/5)) ([ec6e9b0](https://github.com/apify/actors-mcp-server/commit/ec6e9b0a4657f673b3650a5906fe00e66411d7f1)) by [@jirispilka](https://github.com/jirispilka)
208-
- Correct publishing of npm module ([#6](https://github.com/apify/actors-mcp-server/pull/6)) ([4c953e9](https://github.com/apify/actors-mcp-server/commit/4c953e9fe0c735f1690c8356884dd78d8608979f)) by [@jirispilka](https://github.com/jirispilka)
208+
- Correct publishing of npm module ([#6](https://github.com/apify/actors-mcp-server/pull/6)) ([4c953e9](https://github.com/apify/actors-mcp-server/commit/4c953e9fe0c735f1690c8356884dd78d8608979f)) by [@jirispilka](https://github.com/jirispilka)

src/main.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ import log from '@apify/log';
1111
import { createExpressApp } from './actor/server.js';
1212
import { processInput } from './input.js';
1313
import { ActorsMcpServer } from './mcp/server.js';
14-
import {
15-
actorDefinitionTool,
16-
addTool,
17-
callActorGetDataset,
18-
getActorsAsTools,
19-
removeTool,
20-
searchTool,
21-
} from './tools/index.js';
14+
import { callActorGetDataset, getActorsAsTools } from './tools/index.js';
2215
import type { Input } from './types.js';
2316

2417
const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY';
@@ -33,25 +26,26 @@ if (!process.env.APIFY_TOKEN) {
3326
process.exit(1);
3427
}
3528

36-
const mcpServer = new ActorsMcpServer();
37-
3829
const input = processInput((await Actor.getInput<Partial<Input>>()) ?? ({} as Input));
3930
log.info(`Loaded input: ${JSON.stringify(input)} `);
4031

4132
if (STANDBY_MODE) {
33+
const mcpServer = new ActorsMcpServer({
34+
enableAddingActors: Boolean(input.enableAddingActors),
35+
enableDefaultActors: false,
36+
});
37+
4238
const app = createExpressApp(HOST, mcpServer);
4339
log.info('Actor is running in the STANDBY mode.');
44-
// Do not load default Actors here, for mastra.ai template we need to start without default Actors
45-
const tools = [searchTool, actorDefinitionTool];
46-
if (input.enableAddingActors) {
47-
tools.push(addTool, removeTool);
48-
}
40+
41+
// Load only Actors specified in the input
42+
// If you wish to start without any Actor, create a task and leave the input empty
4943
if (input.actors && input.actors.length > 0) {
5044
const { actors } = input;
5145
const actorsToLoad = Array.isArray(actors) ? actors : actors.split(',');
52-
tools.push(...await getActorsAsTools(actorsToLoad, process.env.APIFY_TOKEN as string));
46+
const tools = await getActorsAsTools(actorsToLoad, process.env.APIFY_TOKEN as string);
47+
mcpServer.updateTools(tools);
5348
}
54-
mcpServer.updateTools(tools);
5549
app.listen(PORT, () => {
5650
log.info(`The Actor web server is listening for user requests at ${HOST}`);
5751
});

src/mcp/server.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,37 @@ import {
1717
SERVER_NAME,
1818
SERVER_VERSION,
1919
} from '../const.js';
20-
import { actorDefinitionTool, callActorGetDataset, getActorsAsTools, searchTool } from '../tools/index.js';
20+
import {
21+
actorDefinitionTool,
22+
addTool,
23+
callActorGetDataset,
24+
getActorsAsTools,
25+
removeTool,
26+
searchTool,
27+
} from '../tools/index.js';
2128
import { actorNameToToolName } from '../tools/utils.js';
2229
import type { ActorMCPTool, ActorTool, HelperTool, ToolWrap } from '../types.js';
2330
import { createMCPClient } from './client.js';
2431
import { processParamsGetTools } from './utils.js';
2532

33+
type ActorsMcpServerOptions = {
34+
enableAddingActors?: boolean;
35+
enableDefaultActors?: boolean;
36+
};
37+
2638
/**
2739
* Create Apify MCP server
2840
*/
2941
export class ActorsMcpServer {
3042
public readonly server: Server;
3143
public readonly tools: Map<string, ToolWrap>;
44+
private readonly options: ActorsMcpServerOptions;
3245

33-
constructor() {
46+
constructor(options: ActorsMcpServerOptions = {}) {
47+
this.options = {
48+
enableAddingActors: options.enableAddingActors ?? false,
49+
enableDefaultActors: options.enableDefaultActors ?? true, // Default to true for backward compatibility
50+
};
3451
this.server = new Server(
3552
{
3653
name: SERVER_NAME,
@@ -49,10 +66,29 @@ export class ActorsMcpServer {
4966

5067
// Add default tools
5168
this.updateTools([searchTool, actorDefinitionTool]);
69+
70+
// Add tools to dynamically load Actors
71+
if (this.options.enableAddingActors) {
72+
this.loadToolsToAddActors();
73+
}
74+
75+
// Initialize automatically for backward compatibility
76+
this.initialize().catch((error) => {
77+
log.error('Failed to initialize server:', error);
78+
});
79+
}
80+
81+
/**
82+
* Initialize the server with default tools if enabled
83+
*/
84+
public async initialize(): Promise<void> {
85+
if (this.options.enableDefaultActors) {
86+
await this.loadDefaultTools(process.env.APIFY_TOKEN as string);
87+
}
5288
}
5389

5490
/**
55-
* Loads missing default tools.
91+
* Loads default tools if not already loaded.
5692
*/
5793
public async loadDefaultTools(apifyToken: string) {
5894
const missingDefaultTools = defaults.actors.filter((name) => !this.tools.has(actorNameToToolName(name)));
@@ -78,6 +114,13 @@ export class ActorsMcpServer {
78114
}
79115
}
80116

117+
/**
118+
* Add Actors to server dynamically
119+
*/
120+
public loadToolsToAddActors() {
121+
this.updateTools([addTool, removeTool]);
122+
}
123+
81124
/**
82125
* Upsert new tools.
83126
* @param tools - Array of tool wrappers.

src/stdio.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import log from '@apify/log';
1919

2020
import { defaults } from './const.js';
2121
import { ActorsMcpServer } from './mcp/server.js';
22-
import { addTool, getActorsAsTools, removeTool } from './tools/index.js';
22+
import { getActorsAsTools } from './tools/index.js';
2323

2424
// Configure logging, set to ERROR
2525
log.setLevel(log.LEVELS.ERROR);
@@ -36,7 +36,8 @@ const argv = parser(process.argv.slice(2), {
3636
'enable-adding-actors': false,
3737
},
3838
});
39-
const { actors = '', enableActorAutoLoading = false } = argv;
39+
const enableAddingActors = argv['enable-adding-actors'] || argv.enableActorAutoLoading || false;
40+
const { actors = '' } = argv;
4041
const actorList = actors ? actors.split(',').map((a: string) => a.trim()) : [];
4142

4243
// Validate environment
@@ -46,12 +47,8 @@ if (!process.env.APIFY_TOKEN) {
4647
}
4748

4849
async function main() {
49-
const mcpServer = new ActorsMcpServer();
50-
// Initialize tools
50+
const mcpServer = new ActorsMcpServer({ enableAddingActors, enableDefaultActors: false });
5151
const tools = await getActorsAsTools(actorList.length ? actorList : defaults.actors, process.env.APIFY_TOKEN as string);
52-
if (enableActorAutoLoading) {
53-
tools.push(addTool, removeTool);
54-
}
5552
mcpServer.updateTools(tools);
5653

5754
// Start server

0 commit comments

Comments
 (0)