Skip to content

Commit cb2e8e0

Browse files
committed
add support for filtering if user rented Actor Ids list if passed from apify-mcp-server
1 parent 415829b commit cb2e8e0

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/mcp/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,12 @@ export class ActorsMcpServer {
348348
// eslint-disable-next-line prefer-const
349349
let { name, arguments: args } = request.params;
350350
const apifyToken = (request.params.apifyToken || process.env.APIFY_TOKEN) as string;
351+
const userRentedActorIds = request.params.userRentedActorIds as string[] | undefined;
351352

352353
// Remove apifyToken from request.params just in case
353354
delete request.params.apifyToken;
355+
// Remove other custom params passed from apify-mcp-server
356+
delete request.params.userRentedActorIds;
354357

355358
// Validate token
356359
if (!apifyToken) {
@@ -415,6 +418,7 @@ export class ActorsMcpServer {
415418
apifyMcpServer: this,
416419
mcpServer: this.server,
417420
apifyToken,
421+
userRentedActorIds,
418422
}) as object;
419423

420424
return { ...res };

src/tools/store_collection.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,26 @@ export const searchActorsArgsSchema = z.object({
6969
});
7070

7171
/**
72-
* Filters out actors with the 'FLAT_PRICE_PER_MONTH' pricing model (rental actors)..
72+
* Filters out actors with the 'FLAT_PRICE_PER_MONTH' pricing model (rental actors),
73+
* unless the actor's ID is present in the user's rented actor IDs list.
7374
*
74-
* Returns new array of Actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model.
75+
* This is necessary because the Store list API does not support filtering by multiple pricing models at once.
7576
*
7677
* @param actors - Array of ActorStorePruned objects to filter.
77-
* @returns Array of actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model.
78+
* @param userRentedActorIds - Array of actor IDs that the user has rented.
79+
* @returns Array of actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model,
80+
* except for actors that the user has rented (whose IDs are in userRentedActorIds).
7881
*/
7982
function filterRentalActors(
8083
actors: ActorStorePruned[],
84+
userRentedActorIds: string[],
8185
): ActorStorePruned[] {
8286
// Store list API does not support filtering by two pricing models at once,
8387
// so we filter the results manually after fetching them.
84-
return actors.filter((actor) => (actor.currentPricingInfo.pricingModel as ActorPricingModel) !== 'FLAT_PRICE_PER_MONTH');
88+
return actors.filter((actor) => (
89+
actor.currentPricingInfo.pricingModel as ActorPricingModel) !== 'FLAT_PRICE_PER_MONTH'
90+
|| userRentedActorIds.includes(actor.id),
91+
);
8592
}
8693

8794
/**
@@ -102,15 +109,15 @@ export const searchActors: ToolEntry = {
102109
inputSchema: zodToJsonSchema(searchActorsArgsSchema),
103110
ajvValidate: ajv.compile(zodToJsonSchema(searchActorsArgsSchema)),
104111
call: async (toolArgs) => {
105-
const { args, apifyToken } = toolArgs;
112+
const { args, apifyToken, userRentedActorIds } = toolArgs;
106113
const parsed = searchActorsArgsSchema.parse(args);
107114
let actors = await searchActorsByKeywords(
108115
parsed.search,
109116
apifyToken,
110117
parsed.limit + ACTOR_SEARCH_ABOVE_LIMIT,
111118
parsed.offset,
112119
);
113-
actors = filterRentalActors(actors || []).slice(0, parsed.limit);
120+
actors = filterRentalActors(actors || [], userRentedActorIds || []).slice(0, parsed.limit);
114121

115122
return { content: actors?.map((item) => ({ type: 'text', text: JSON.stringify(item) })) };
116123
},

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export type InternalToolArgs = {
8787
mcpServer: Server;
8888
/** Apify API token */
8989
apifyToken: string;
90+
/** List of Actor IDs that the user has rented */
91+
userRentedActorIds?: string[];
9092
}
9193

9294
/**

0 commit comments

Comments
 (0)