Skip to content

Commit b665a2c

Browse files
committed
remove double updateTools main.ts call, extend ApifyClient with MCP user agent request interceptor
1 parent af3eb35 commit b665a2c

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ if (STANDBY_MODE) {
3535
const app = createExpressApp(HOST, mcpServer);
3636
log.info('Actor is running in the STANDBY mode.');
3737
const tools = [searchTool, actorDefinitionTool];
38-
mcpServer.updateTools([searchTool, actorDefinitionTool]);
3938
if (input.enableActorAutoLoading) {
4039
tools.push(addTool, removeTool);
4140
}

src/tools/actor.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Ajv } from 'ajv';
2-
import type { ApifyClientOptions } from 'apify';
32
import type { ActorCallOptions } from 'apify-client';
4-
import { ApifyClient } from 'apify-client';
5-
import type { AxiosRequestConfig } from 'axios';
63

74
import log from '@apify/log';
85

96
import type { ToolWrap } from '../types.js';
107
import { getActorDefinition } from './build.js';
11-
import { ACTOR_ADDITIONAL_INSTRUCTIONS, ACTOR_MAX_MEMORY_MBYTES, USER_AGENT_ORIGIN } from '../const.js';
8+
import { ACTOR_ADDITIONAL_INSTRUCTIONS, ACTOR_MAX_MEMORY_MBYTES } from '../const.js';
9+
import { ApifyClient } from './mcp-apify-client.js';
1210
import {
1311
actorNameToToolName,
1412
addEnumsToDescriptionsWithExamples,
@@ -18,18 +16,6 @@ import {
1816
shortenProperties,
1917
} from './utils.js';
2018

21-
/**
22-
* Adds a User-Agent header to the request config.
23-
* @param config
24-
* @private
25-
*/
26-
export function addUserAgent(config: AxiosRequestConfig): AxiosRequestConfig {
27-
const updatedConfig = { ...config };
28-
updatedConfig.headers = updatedConfig.headers ?? {};
29-
updatedConfig.headers['User-Agent'] = `${updatedConfig.headers['User-Agent'] ?? ''}; ${USER_AGENT_ORIGIN}`;
30-
return updatedConfig;
31-
}
32-
3319
/**
3420
* Calls an Apify actor and retrieves the dataset items.
3521
*
@@ -54,8 +40,7 @@ export async function callActorGetDataset(
5440
try {
5541
log.info(`Calling Actor ${name} with input: ${JSON.stringify(input)}`);
5642

57-
const options: ApifyClientOptions = { requestInterceptors: [addUserAgent] };
58-
const client = new ApifyClient({ ...options, token: apifyToken });
43+
const client = new ApifyClient({ token: apifyToken });
5944
const actorClient = client.actor(name);
6045

6146
const results = await actorClient.call(input, callOptions);

src/tools/mcp-apify-client.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { ApifyClientOptions } from 'apify';
2+
import { ApifyClient as _ApifyClient } from 'apify-client';
3+
import type { AxiosRequestConfig } from 'axios';
4+
5+
import { USER_AGENT_ORIGIN } from '../const.js';
6+
7+
/**
8+
* Adds a User-Agent header to the request config.
9+
* @param config
10+
* @private
11+
*/
12+
function addUserAgent(config: AxiosRequestConfig): AxiosRequestConfig {
13+
const updatedConfig = { ...config };
14+
updatedConfig.headers = updatedConfig.headers ?? {};
15+
updatedConfig.headers['User-Agent'] = `${updatedConfig.headers['User-Agent'] ?? ''}; ${USER_AGENT_ORIGIN}`;
16+
return updatedConfig;
17+
}
18+
19+
export class ApifyClient extends _ApifyClient {
20+
constructor(options: ApifyClientOptions) {
21+
super({
22+
...options,
23+
requestInterceptors: [addUserAgent],
24+
});
25+
}
26+
}

0 commit comments

Comments
 (0)