|
| 1 | +import type { ActorDefinition } from 'apify-client'; |
| 2 | + |
| 3 | +import { ApifyClient, getApifyAPIBaseUrl } from '../apify-client.js'; |
| 4 | + |
| 5 | +export async function isActorMCPServer(actorID: string, apifyToken: string): Promise<boolean> { |
| 6 | + const mcpPath = await getActorsMCPServerPath(actorID, apifyToken); |
| 7 | + return (mcpPath?.length || 0) > 0; |
| 8 | +} |
| 9 | + |
| 10 | +export async function getActorsMCPServerPath(actorID: string, apifyToken: string): Promise<string | undefined> { |
| 11 | + const actorDefinition = await getActorDefinition(actorID, apifyToken); |
| 12 | + |
| 13 | + if ('webServerMcpPath' in actorDefinition && typeof actorDefinition.webServerMcpPath === 'string') { |
| 14 | + return actorDefinition.webServerMcpPath; |
| 15 | + } |
| 16 | + |
| 17 | + return undefined; |
| 18 | +} |
| 19 | + |
| 20 | +export async function getActorsMCPServerURL(actorID: string, apifyToken: string): Promise<string> { |
| 21 | + // TODO: get from API instead |
| 22 | + const standbyBaseUrl = process.env.HOSTNAME === 'mcp-securitybyobscurity.apify.com' |
| 23 | + ? 'securitybyobscurity.apify.actor' : 'apify.actor'; |
| 24 | + const standbyUrl = await getActorStandbyURL(actorID, apifyToken, standbyBaseUrl); |
| 25 | + const mcpPath = await getActorsMCPServerPath(actorID, apifyToken); |
| 26 | + return `${standbyUrl}${mcpPath}`; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | +* Gets Actor ID from the Actor object. |
| 31 | +* |
| 32 | +* @param actorID |
| 33 | +* @param apifyToken |
| 34 | +*/ |
| 35 | +export async function getRealActorID(actorID: string, apifyToken: string): Promise<string> { |
| 36 | + const apifyClient = new ApifyClient({ token: apifyToken }); |
| 37 | + |
| 38 | + const actor = apifyClient.actor(actorID); |
| 39 | + const info = await actor.get(); |
| 40 | + if (!info) { |
| 41 | + throw new Error(`Actor ${actorID} not found`); |
| 42 | + } |
| 43 | + return info.id; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | +* Returns standby URL for given Actor ID. |
| 48 | +* |
| 49 | +* @param actorID |
| 50 | +* @param standbyBaseUrl |
| 51 | +* @param apifyToken |
| 52 | +* @returns |
| 53 | +*/ |
| 54 | +export async function getActorStandbyURL(actorID: string, apifyToken: string, standbyBaseUrl = 'apify.actor'): Promise<string> { |
| 55 | + const actorRealID = await getRealActorID(actorID, apifyToken); |
| 56 | + return `https://${actorRealID}.${standbyBaseUrl}`; |
| 57 | +} |
| 58 | + |
| 59 | +export async function getActorDefinition(actorID: string, apifyToken: string): Promise<ActorDefinition> { |
| 60 | + const apifyClient = new ApifyClient({ token: apifyToken }); |
| 61 | + const actor = apifyClient.actor(actorID); |
| 62 | + const info = await actor.get(); |
| 63 | + if (!info) { |
| 64 | + throw new Error(`Actor ${actorID} not found`); |
| 65 | + } |
| 66 | + |
| 67 | + const actorObjID = info.id; |
| 68 | + const res = await fetch(`${getApifyAPIBaseUrl()}/v2/acts/${actorObjID}/builds/default`, { |
| 69 | + headers: { |
| 70 | + // This is done so tests can pass with public Actors without token |
| 71 | + ...(apifyToken ? { Authorization: `Bearer ${apifyToken}` } : {}), |
| 72 | + }, |
| 73 | + }); |
| 74 | + if (!res.ok) { |
| 75 | + throw new Error(`Failed to fetch default build for actor ${actorID}: ${res.statusText}`); |
| 76 | + } |
| 77 | + const json = await res.json() as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 78 | + const buildInfo = json.data; |
| 79 | + if (!buildInfo) { |
| 80 | + throw new Error(`Default build for Actor ${actorID} not found`); |
| 81 | + } |
| 82 | + const { actorDefinition } = buildInfo; |
| 83 | + if (!actorDefinition) { |
| 84 | + throw new Error(`Actor default build ${actorID} does not have Actor definition`); |
| 85 | + } |
| 86 | + |
| 87 | + return actorDefinition; |
| 88 | +} |
0 commit comments