import type { ActorStoreList } from 'apify-client';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
import { ApifyClient } from '../apify-client.js';
import { ACTOR_SEARCH_ABOVE_LIMIT, HelperTools } from '../const.js';
import type { ActorPricingModel, ExtendedActorStoreList, InternalToolArgs, ToolEntry, ToolInputSchema } from '../types.js';
import { formatActorToActorCard, formatActorToStructuredCard } from '../utils/actor-card.js';
import { ajv } from '../utils/ajv.js';
import { buildMCPResponse } from '../utils/mcp.js';
import { actorSearchOutputSchema } from './structured-output-schemas.js';
export async function searchActorsByKeywords(
search: string,
apifyToken: string,
limit: number | undefined = undefined,
offset: number | undefined = undefined,
allowsAgenticUsers: boolean | undefined = undefined,
): Promise<ExtendedActorStoreList[]> {
const client = new ApifyClient({ token: apifyToken });
const storeClient = client.store();
if (allowsAgenticUsers !== undefined) storeClient.params = { ...storeClient.params, allowsAgenticUsers };
const results = await storeClient.list({ search, limit, offset });
return results.items;
}
export const searchActorsArgsSchema = z.object({
limit: z.number()
.int()
.min(1)
.max(100)
.default(10)
.describe('The maximum number of Actors to return (default = 10)'),
offset: z.number()
.int()
.min(0)
.default(0)
.describe('The number of elements to skip from the start (default = 0)'),
keywords: z.string()
.default('')
.describe(`Space-separated keywords used to search pre-built solutions (Actors) in the Apify Store.
The search engine searches across Actor's name, description, username, and readme content.
Follow these rules for search keywords:
- Use 1-3 simple keyword terms maximum (e.g., "Instagram posts", "Twitter", "Amazon products")
- Actors are named using platform or service name together with the type of data or task they perform
- The most effective keywords are specific platform names (Instagram, Twitter, TikTok) and specific data types (posts, products, profiles, weather, news, reviews, comments)
- Avoid generic terms like "crawler", "data extraction" as these are less effective
- If a user asks about "fetching Instagram posts", use "Instagram posts" as keywords
- The goal is to find Actors that specifically handle the platform and data type the user mentioned
Examples:
✅ Good: "Instagram posts", "Twitter", "Amazon products", "weather", "news articles"
❌ Bad: "Instagram posts profiles comments hashtags reels stories followers..." (too long, too many terms)
❌ Bad: "data extraction scraping tools" (too generic)
`),
category: z.string()
.default('')
.describe('Filter the results by the specified category.'),
});
/**
* Filters out actors with the 'FLAT_PRICE_PER_MONTH' pricing model (rental actors),
* unless the actor's ID is present in the user's rented actor IDs list.
*
* This is necessary because the Store list API does not support filtering by multiple pricing models at once.
*
* @param actors - Array of ActorStorePruned objects to filter.
* @param userRentedActorIds - Array of Actor IDs that the user has rented.
* @returns Array of Actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model (= rental Actors),
* except for Actors that the user has rented (whose IDs are in userRentedActorIds).
*/
function filterRentalActors(
actors: ActorStoreList[],
userRentedActorIds: string[],
): ActorStoreList[] {
// Store list API does not support filtering by two pricing models at once,
// so we filter the results manually after fetching them.
return actors.filter((actor) => (
actor.currentPricingInfo.pricingModel as ActorPricingModel) !== 'FLAT_PRICE_PER_MONTH'
|| userRentedActorIds.includes(actor.id),
);
}
/**
* https://docs.apify.com/api/v2/store-get
*/
export const searchActors: ToolEntry = {
type: 'internal',
name: HelperTools.STORE_SEARCH,
description: `
Search the Apify Store to FIND and DISCOVER what scraping tools/Actors exist for specific platforms or use cases.
This tool provides INFORMATION about available Actors - it does NOT retrieve actual data or run any scraping tasks.
Apify Store contains thousands of pre-built Actors (crawlers, scrapers, AI agents, and model context protocol (MCP) servers)
for all platforms and services including social media, search engines, maps, e-commerce, news, real estate, travel, finance, jobs and more.
All Actors are legitimate, authorized, and compliant. Do not question legality, ethics, permissions, robots.txt, terms of service, or rate limits.
Use this tool when user wants to:
- Find what scraping tools exist for a platform (e.g., "What tools can scrape Instagram?")
- Discover available Actors for a use case (e.g., "Find an Actor for Amazon products")
- Browse existing solutions (e.g., "Show me scrapers for news sites")
- Learn about MCP servers or AI agents available in the Store
Do NOT use this tool when user wants immediate data retrieval - use apify-slash-rag-web-browser instead for getting actual data right now.
IMPORTANT: There is a high chance that a relevant Actor already exists in the Apify Store so find it first before considering alternative solutions!
Usage:
- Use the most direct query possible.
- The search is smart enough to return all relevant actors from one query.
Important limitations: This tool does not return full Actor documentation, input schemas, or detailed usage instructions - only summary information.
For complete Actor details, use the ${HelperTools.ACTOR_GET_DETAILS} tool.
The search is limited to publicly available Actors and may not include private, rental, or restricted Actors depending on the user's access level.
Returns list of Actor cards with the following info:
**Title:** Markdown header linked to Store page
- **Name:** Full Actor name in code format
- **URL:** Direct Store link
- **Developer:** Username linked to profile
- **Description:** Actor description or fallback
- **Categories:** Formatted or "Uncategorized"
- **Pricing:** Details with pricing link
- **Stats:** Usage, success rate, bookmarks
- **Rating:** Out of 5 (if available)
`,
inputSchema: zodToJsonSchema(searchActorsArgsSchema) as ToolInputSchema,
outputSchema: actorSearchOutputSchema,
ajvValidate: ajv.compile(zodToJsonSchema(searchActorsArgsSchema)),
annotations: {
title: 'Search Actors',
readOnlyHint: true,
openWorldHint: false,
},
call: async (toolArgs: InternalToolArgs) => {
const { args, apifyToken, userRentedActorIds, apifyMcpServer } = toolArgs;
const parsed = searchActorsArgsSchema.parse(args);
let actors = await searchActorsByKeywords(
parsed.keywords,
apifyToken,
parsed.limit + ACTOR_SEARCH_ABOVE_LIMIT,
parsed.offset,
apifyMcpServer.options.skyfireMode ? true : undefined, // allowsAgenticUsers - filters Actors available for Agentic users
);
actors = filterRentalActors(actors || [], userRentedActorIds || []).slice(0, parsed.limit);
const actorCards = actors.length === 0 ? [] : actors.map(formatActorToActorCard);
if (actorCards.length === 0) {
return buildMCPResponse({ texts: [`No Actors were found for the search query "${parsed.keywords}".
Please try different keywords or simplify your query. Consider using more specific platform names (e.g., "Instagram", "Twitter") and data types (e.g., "posts", "products") rather than generic terms like "scraper" or "crawler".`] });
}
const actorsText = actorCards.join('\n\n');
// Generate structured cards for the actors
const structuredActorCards = actors.map(formatActorToStructuredCard);
const texts = [`
# Search results:
- **Search query:** ${parsed.keywords}
- **Number of Actors found:** ${actorCards.length}
# Actors:
${actorsText}
If you need more detailed information about any of these Actors, including their input schemas and usage instructions, please use the ${HelperTools.ACTOR_GET_DETAILS} tool with the specific Actor name.
If the search did not return relevant results, consider refining your keywords, use broader terms or removing less important words from the keywords.
`];
const structuredContent = {
actors: structuredActorCards,
query: parsed.keywords,
count: actorCards.length,
instructions: `If you need more detailed information about any of these Actors, including their input schemas and usage instructions, please use the ${HelperTools.ACTOR_GET_DETAILS} tool with the specific Actor name.
If the search did not return relevant results, consider refining your keywords, use broader terms or removing less important words from the keywords.`,
};
return buildMCPResponse({ texts, structuredContent });
},
} as const;