11
22import { ActorDefinition } from "apify-client" ;
3- import { getActorDefinition , getActorStandbyURL } from "./utils.js" ;
3+ import { ApifyClient } from "../apify-client.js" ;
4+
45
56export async function isActorMCPServer ( actorID : string , apifyToken : string ) : Promise < boolean > {
67 const mcpPath = await getActorsMCPServerPath ( actorID , apifyToken ) ;
@@ -12,11 +13,64 @@ export async function getActorsMCPServerPath(actorID: string, apifyToken: string
1213 return ( actorDefinition as any ) . webServerMcpPath ;
1314}
1415
15- export async function getActorsMCPServerURL ( actorID : string , _apifyToken : string ) : Promise < string > {
16+ export async function getActorsMCPServerURL ( actorID : string , apifyToken : string ) : Promise < string > {
1617 // TODO: get from API instead
1718 const standbyBaseUrl = process . env . HOSTNAME === 'mcp-securitybyobscurity.apify.com' ?
1819 'securitybyobscurity.apify.actor' : 'apify.actor' ;
19- const standbyUrl = getActorStandbyURL ( actorID , standbyBaseUrl ) ;
20- const mcpPath = await getActorsMCPServerPath ( actorID , _apifyToken ) ;
20+ const standbyUrl = await getActorStandbyURL ( actorID , apifyToken , standbyBaseUrl ) ;
21+ const mcpPath = await getActorsMCPServerPath ( actorID , apifyToken ) ;
2122 return `${ standbyUrl } ${ mcpPath } ` ;
2223}
24+
25+ /**
26+ * Gets Actor ID from the Actor object.
27+ *
28+ * @param actorID
29+ */
30+ export async function getRealActorID ( actorID : string , apifyToken : string ) : Promise < string > {
31+ const apifyClient = new ApifyClient ( { token : apifyToken } ) ;
32+
33+ const actor = apifyClient . actor ( actorID ) ;
34+ const info = await actor . get ( ) ;
35+ if ( ! info ) {
36+ throw new Error ( `Actor ${ actorID } not found` ) ;
37+ }
38+ return info . id ;
39+ }
40+
41+ /**
42+ * Returns standby URL for given Actor ID.
43+ *
44+ * @param actorID
45+ * @param standbyBaseUrl
46+ * @returns
47+ */
48+ export async function getActorStandbyURL ( actorID : string , apifyToken : string , standbyBaseUrl = 'apify.actor' ) : Promise < string > {
49+ const actorRealID = await getRealActorID ( actorID , apifyToken ) ;
50+ return `https://${ actorRealID } .${ standbyBaseUrl } ` ;
51+ }
52+
53+ export async function getActorDefinition ( actorID : string , apifyToken : string ) : Promise < ActorDefinition > {
54+ const apifyClient = new ApifyClient ( { token : apifyToken
55+ } )
56+ const actor = apifyClient . actor ( actorID ) ;
57+ const info = await actor . get ( ) ;
58+ if ( ! info ) {
59+ throw new Error ( `Actor ${ actorID } not found` ) ;
60+ }
61+ const latestBuildID = info . taggedBuilds ?. [ 'latest' ] ?. buildId ;
62+ if ( ! latestBuildID ) {
63+ throw new Error ( `Actor ${ actorID } does not have a latest build` ) ;
64+ }
65+ const build = apifyClient . build ( latestBuildID ) ;
66+ const buildInfo = await build . get ( ) ;
67+ if ( ! buildInfo ) {
68+ throw new Error ( `Build ${ latestBuildID } not found` ) ;
69+ }
70+ const actorDefinition = buildInfo . actorDefinition ;
71+ if ( ! actorDefinition ) {
72+ throw new Error ( `Build ${ latestBuildID } does not have an actor definition` ) ;
73+ }
74+
75+ return actorDefinition ;
76+ }
0 commit comments