Skip to content

Commit 204b358

Browse files
committed
decouple processParamsAndUpdateTools
1 parent f8ad397 commit 204b358

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

src/input.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import type { Input } from './types.js';
55
* @param originalInput
66
* @returns input
77
*/
8-
export async function processInput(originalInput: Partial<Input>): Promise<Input> {
8+
export function processInput(originalInput: Partial<Input>): Input {
99
const input = originalInput as Input;
1010

1111
// actors can be a string or an array of strings
1212
if (input.actors && typeof input.actors === 'string') {
1313
input.actors = input.actors.split(',').map((format: string) => format.trim()) as string[];
1414
}
15+
// Convert string boolean to actual boolean
16+
if (input.enableActorAutoLoading && typeof input.enableActorAutoLoading === 'string') {
17+
input.enableActorAutoLoading = input.enableActorAutoLoading === 'true';
18+
}
1519
if (!input.enableActorAutoLoading) {
1620
input.enableActorAutoLoading = false;
1721
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (!process.env.APIFY_TOKEN) {
2626

2727
const mcpServer = new ApifyMcpServer();
2828

29-
const input = await processInput((await Actor.getInput<Partial<Input>>()) ?? ({} as Input));
29+
const input = processInput((await Actor.getInput<Partial<Input>>()) ?? ({} as Input));
3030
log.info(`Loaded input: ${JSON.stringify(input)} `);
3131

3232
if (isActorStandby()) {

src/mcp-server.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* Model Context Protocol (MCP) server for Apify Actors
33
*/
4-
import type { ParsedUrlQuery } from 'node:querystring';
5-
import { parse } from 'node:querystring';
64

75
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
86
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
@@ -20,10 +18,10 @@ import {
2018
SERVER_NAME,
2119
SERVER_VERSION,
2220
} from './const.js';
23-
import { processInput } from './input.js';
2421
import { log } from './logger.js';
2522
import { getActorAutoLoadingTools } from './tools/index.js';
26-
import type { Input, ActorTool, ToolWrap, InternalTool } from './types.js';
23+
import type { ActorTool, ToolWrap, InternalTool } from './types.js';
24+
import { parseInputParamsFromUrl } from './utils.js';
2725

2826
/**
2927
* Create Apify MCP server
@@ -84,24 +82,27 @@ export class ApifyMcpServer {
8482
});
8583
}
8684

85+
public enableActorAutoLoading() {
86+
this.updateTools(getActorAutoLoadingTools());
87+
log.debug('Enabled Actor auto-loading tools');
88+
}
89+
8790
/**
8891
* Process input parameters and update tools
8992
* If URL contains query parameter actors, add tools from actors, otherwise add tools from default actors
9093
* @param url
9194
*/
9295
public async processParamsAndUpdateTools(url: string) {
93-
const params = parse(url.split('?')[1] || '') as ParsedUrlQuery;
94-
delete params.token;
95-
log.debug(`Received input parameters: ${JSON.stringify(params)}`);
96-
const input = await processInput(params as unknown as Input);
96+
const input = parseInputParamsFromUrl(url);
9797
if (input.actors) {
9898
await this.addToolsFromActors(input.actors as string[]);
9999
}
100100
if (input.enableActorAutoLoading) {
101-
this.updateTools(getActorAutoLoadingTools());
101+
this.enableActorAutoLoading();
102102
}
103-
log.debug(`Server is running in STANDBY mode with the following Actors (tools): ${this.getToolNames()}.
104-
To use different Actors, provide them in query parameter "actors" or include them in the Actor Task input.`);
103+
104+
log.debug(`Server is running in STANDBY mode with Actors: ${this.getToolNames()}. `
105+
+ 'To use different Actors, provide them in "actors" query param or Actor Task input.');
105106
}
106107

107108
private setupToolHandlers(): void {

src/utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import { parse } from 'node:querystring';
2+
13
import { Actor } from 'apify';
24

3-
import type { ActorRunData } from './types';
5+
import { processInput } from './input.js';
6+
import type { ActorRunData, Input } from './types';
7+
8+
export function parseInputParamsFromUrl(url: string): Input {
9+
const query = url.split('?')[1] || '';
10+
const params = parse(query) as unknown as Input;
11+
return processInput(params);
12+
}
413

514
export function isActorStandby(): boolean {
615
return Actor.getEnv().metaOrigin === 'STANDBY';

tests/utils-test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { parseInputParamsFromUrl } from '../src/utils.js';
4+
5+
describe('parseInputParamsFromUrl', () => {
6+
it('should parse actors from URL query params', () => {
7+
const url = 'https://actors-mcp-server.apify.actor?token=123&actors=apify/web-scraper';
8+
const result = parseInputParamsFromUrl(url);
9+
expect(result.actors).toEqual(['apify/web-scraper']);
10+
});
11+
12+
it('should parse multiple actors from URL', () => {
13+
const url = 'https://actors-mcp-server.apify.actor?actors=apify/instagram-scraper,lukaskrivka/google-maps';
14+
const result = parseInputParamsFromUrl(url);
15+
expect(result.actors).toEqual(['apify/instagram-scraper', 'lukaskrivka/google-maps']);
16+
});
17+
18+
it('should handle URL without query params', () => {
19+
const url = 'https://actors-mcp-server.apify.actor';
20+
const result = parseInputParamsFromUrl(url);
21+
expect(result.actors).toBeUndefined();
22+
});
23+
24+
it('should parse enableActorAutoLoading flag', () => {
25+
const url = 'https://actors-mcp-server.apify.actor?enableActorAutoLoading=true';
26+
const result = parseInputParamsFromUrl(url);
27+
expect(result.enableActorAutoLoading).toBe(true);
28+
});
29+
30+
it('should handle actors as string parameter', () => {
31+
const url = 'https://actors-mcp-server.apify.actor?actors=apify/rag-web-browser';
32+
const result = parseInputParamsFromUrl(url);
33+
expect(result.actors).toEqual(['apify/rag-web-browser']);
34+
});
35+
});

0 commit comments

Comments
 (0)