-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathactors.ts
More file actions
66 lines (57 loc) · 2.41 KB
/
actors.ts
File metadata and controls
66 lines (57 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { ActorDefinition } from 'apify-client';
import { ApifyClient } from '../apify_client.js';
import { MCP_STREAMABLE_ENDPOINT } from '../const.js';
import type { ActorDefinitionPruned } from '../types.js';
import { parseCommaSeparatedList } from '../utils/generic.js';
/**
* Returns the MCP server path for the given Actor ID.
* Prioritizes the streamable transport path if available.
* The `webServerMcpPath` is a string containing MCP endpoint or endpoints separated by commas.
*/
export function getActorMCPServerPath(actorDefinition: ActorDefinition | ActorDefinitionPruned): string | null {
if ('webServerMcpPath' in actorDefinition && typeof actorDefinition.webServerMcpPath === 'string') {
const webServerMcpPath = actorDefinition.webServerMcpPath.trim();
const paths = parseCommaSeparatedList(webServerMcpPath);
// If there is only one path, return it directly
if (paths.length === 1) {
return paths[0];
}
// If there are multiple paths, prioritize the streamable transport path
// otherwise return the first one.
const streamablePath = paths.find((path) => path === MCP_STREAMABLE_ENDPOINT);
if (streamablePath) {
return streamablePath;
}
// Otherwise, return the first path
return paths[0];
}
return null;
}
/**
* Returns the MCP server URL for the given Actor ID.
*/
export async function getActorMCPServerURL(realActorId: string, mcpServerPath: string): Promise<string> {
// TODO: get from API instead
const standbyBaseUrl = process.env.HOSTNAME === 'mcp-securitybyobscurity.apify.com'
? 'securitybyobscurity.apify.actor' : 'apify.actor';
const standbyUrl = await getActorStandbyURL(realActorId, standbyBaseUrl);
return `${standbyUrl}${mcpServerPath}`;
}
/**
* Gets Actor ID from the Actor object.
*/
export async function getRealActorID(actorIdOrName: string, apifyToken: string): Promise<string> {
const apifyClient = new ApifyClient({ token: apifyToken });
const actor = apifyClient.actor(actorIdOrName);
const info = await actor.get();
if (!info) {
throw new Error(`Actor ${actorIdOrName} not found`);
}
return info.id;
}
/**
* Returns standby URL for given Actor ID.
*/
export async function getActorStandbyURL(realActorId: string, standbyBaseUrl = 'apify.actor'): Promise<string> {
return `https://${realActorId}.${standbyBaseUrl}`;
}