Skip to content

Commit 80441b8

Browse files
committed
Revert "cache Actor definition and real Actor ID API calls"
This reverts commit aadbfa3.
1 parent 0a707f4 commit 80441b8

File tree

2 files changed

+3
-48
lines changed

2 files changed

+3
-48
lines changed

src/mcp/actors.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { log } from 'apify';
21
import type { ActorDefinition } from 'apify-client';
32

43
import { ApifyClient, getApifyAPIBaseUrl } from '../apify-client.js';
@@ -9,7 +8,7 @@ export async function isActorMCPServer(actorID: string, apifyToken: string): Pro
98
}
109

1110
export async function getActorsMCPServerPath(actorID: string, apifyToken: string): Promise<string | undefined> {
12-
const actorDefinition = await getFullActorDefinition(actorID, apifyToken);
11+
const actorDefinition = await getActorDefinition(actorID, apifyToken);
1312

1413
if ('webServerMcpPath' in actorDefinition && typeof actorDefinition.webServerMcpPath === 'string') {
1514
return actorDefinition.webServerMcpPath;
@@ -27,32 +26,20 @@ export async function getActorsMCPServerURL(actorID: string, apifyToken: string)
2726
return `${standbyUrl}${mcpPath}`;
2827
}
2928

30-
// ID does not change, so no TTL
31-
export const actorIDCache: Record<string, string> = {};
3229
/**
3330
* Gets Actor ID from the Actor object.
3431
*
3532
* @param actorID
3633
* @param apifyToken
3734
*/
3835
export async function getRealActorID(actorID: string, apifyToken: string): Promise<string> {
39-
if (actorIDCache[actorID]) {
40-
log.debug(`Actor ${actorID} ID cache hit`);
41-
return actorIDCache[actorID];
42-
}
43-
log.debug(`Actor ${actorID} ID cache miss`);
44-
4536
const apifyClient = new ApifyClient({ token: apifyToken });
4637

4738
const actor = apifyClient.actor(actorID);
4839
const info = await actor.get();
4940
if (!info) {
5041
throw new Error(`Actor ${actorID} not found`);
5142
}
52-
53-
if (!actorIDCache[actorID]) {
54-
actorIDCache[actorID] = info.id;
55-
}
5643
return info.id;
5744
}
5845

@@ -69,29 +56,7 @@ export async function getActorStandbyURL(actorID: string, apifyToken: string, st
6956
return `https://${actorRealID}.${standbyBaseUrl}`;
7057
}
7158

72-
export const actorDefinitionCache: Record<string, {
73-
timestamp: number;
74-
definition: ActorDefinition;
75-
}> = {};
76-
export const ACTOR_DEFINITION_CACHE_TTL_MS = 1000 * 60 * 60; // 1 hour
77-
/**
78-
* Gets full Actor definition from the Apify API.
79-
*/
80-
export async function getFullActorDefinition(actorID: string, apifyToken: string): Promise<ActorDefinition> {
81-
const cacheInTTL = Date.now() - (actorDefinitionCache[actorID]?.timestamp || 0) < ACTOR_DEFINITION_CACHE_TTL_MS;
82-
// Hit the cache
83-
if (actorDefinitionCache[actorID]
84-
&& cacheInTTL) {
85-
log.debug(`Actor ${actorID} definition cache hit`);
86-
return actorDefinitionCache[actorID].definition;
87-
}
88-
// Refresh the cache after TTL expired
89-
if (actorDefinitionCache[actorID] && !cacheInTTL) {
90-
log.debug(`Actor ${actorID} definition cache TTL expired, re-fetching`);
91-
} else {
92-
log.debug(`Actor ${actorID} definition cache miss`);
93-
}
94-
59+
export async function getActorDefinition(actorID: string, apifyToken: string): Promise<ActorDefinition> {
9560
const apifyClient = new ApifyClient({ token: apifyToken });
9661
const actor = apifyClient.actor(actorID);
9762
const info = await actor.get();
@@ -119,14 +84,5 @@ export async function getFullActorDefinition(actorID: string, apifyToken: string
11984
throw new Error(`Actor default build ${actorID} does not have Actor definition`);
12085
}
12186

122-
// If the Actor is public, we cache the definition
123-
// This code branch is executed only on cache miss, so we know the cache entry is empty
124-
if (info.isPublic) {
125-
actorDefinitionCache[actorID] = {
126-
timestamp: Date.now(),
127-
definition: actorDefinition,
128-
};
129-
}
130-
13187
return actorDefinition;
13288
}

src/tools/actor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ async function getMCPServersAsTools(
125125
): Promise<ToolWrap[]> {
126126
const actorsMCPServerTools: ToolWrap[] = [];
127127
for (const actorID of actors) {
128-
// getFullActorDefinition uses cache, so we can call it twice (this is the second time)
129128
const serverUrl = await getActorsMCPServerURL(actorID, apifyToken);
130129
log.info(`ActorID: ${actorID} MCP server URL: ${serverUrl}`);
131130

@@ -151,7 +150,7 @@ export async function getActorsAsTools(
151150
// Actorized MCP servers
152151
const actorsMCPServers: string[] = [];
153152
for (const actorID of actors) {
154-
// getFullActorDefinition uses cache, so we can call it twice (second time in the getMCPServerTools)
153+
// TODO: rework, we are fetching actor definition from API twice - in the getMCPServerTools
155154
if (await isActorMCPServer(actorID, apifyToken)) {
156155
actorsMCPServers.push(actorID);
157156
}

0 commit comments

Comments
 (0)