Skip to content

Commit f14025d

Browse files
committed
get standby url from actor id, check if is mcp server based on actor name for now
1 parent 0bf2e0c commit f14025d

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/const.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ export const defaults = {
3737
enableActorAutoLoading: false,
3838
maxMemoryMbytes: 4096,
3939
};
40+
41+
export const APIFY_USERNAME = 'apify';

src/mcp/actors.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
export async function isActorMCPServer(actorID: string): Promise<boolean> {
2+
import { getActorStandbyURL } from "./utils.js";
3+
4+
export async function isActorMCPServer(actorID: string, _apifyToken: string): Promise<boolean> {
35
// TODO: implement the logic
4-
return actorID === 'apify/actors-mcp-server';
6+
return actorID.toLowerCase().includes('mcp-') || actorID.toLowerCase().includes('-mcp');
57
}
68

7-
export async function getActorsMCPServerURL(actorID: string): Promise<string> {
8-
// TODO: implement the logic
9-
if (actorID === 'apify/actors-mcp-server') {
10-
return 'https://actors-mcp-server.apify.actor/sse';
11-
}
12-
throw new Error(`Actor ${actorID} is not an MCP server`);
9+
export async function getActorsMCPServerURL(actorID: string, _apifyToken: string): Promise<string> {
10+
// TODO: get from API instead
11+
const standbyUrl = getActorStandbyURL(actorID)
12+
return `${standbyUrl}/sse`;
1313
}

src/mcp/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ToolWrap } from '../types.js';
88

99
import { addTool, getActorsAsTools, removeTool } from '../tools/index.js';
1010
import { Input } from "../types.js";
11+
import { APIFY_USERNAME } from "../const.js";
1112

1213
/**
1314
* Generates a unique server ID based on the provided URL.
@@ -60,3 +61,22 @@ export function parseInputParamsFromUrl(url: string): Input {
6061
const params = parse(query) as unknown as Input;
6162
return processInput(params);
6263
}
64+
65+
/**
66+
* Returns standby URL for given Actor ID.
67+
*
68+
* @param actorID
69+
* @param standbyBaseUrl
70+
* @returns
71+
*/
72+
export function getActorStandbyURL(actorID: string, standbyBaseUrl = '.apify.actor'): string {
73+
const actorOwner = actorID.split('/')[0];
74+
const actorName = actorID.split('/')[1];
75+
if (!actorOwner || !actorName) {
76+
throw new Error(`Invalid actor ID: ${actorID}`);
77+
}
78+
79+
const prefix = actorOwner === APIFY_USERNAME ? '' : `${actorOwner}--`;
80+
81+
return `https://${prefix}${actorName}${standbyBaseUrl}`;
82+
}

src/tools/actor.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function getMCPServersAsTools(
126126
): Promise<ToolWrap[]> {
127127
const actorsMCPServerTools: ToolWrap[] = [];
128128
for (const actorID of actors) {
129-
const serverUrl = await getActorsMCPServerURL(actorID);
129+
const serverUrl = await getActorsMCPServerURL(actorID, apifyToken);
130130

131131
let client: Client | undefined;
132132
try {
@@ -148,22 +148,22 @@ export async function getActorsAsTools(
148148
console.log('Fetching actors as tools...');
149149
console.log(actors)
150150
// Actorized MCP servers
151-
const actorsMCPServer: string[] = [];
151+
const actorsMCPServers: string[] = [];
152152
for (const actorID of actors) {
153-
if (await isActorMCPServer(actorID)) {
154-
actorsMCPServer.push(actorID);
153+
if (await isActorMCPServer(actorID, apifyToken)) {
154+
actorsMCPServers.push(actorID);
155155
}
156156
}
157157
// Normal Actors as a tool
158-
const toolActors = actors.filter((actorID) => !actorsMCPServer.includes(actorID));
159-
console.log('actorsMCPserver', actorsMCPServer);
158+
const toolActors = actors.filter((actorID) => !actorsMCPServers.includes(actorID));
159+
console.log('actorsMCPserver', actorsMCPServers);
160160
console.log('toolActors', toolActors);
161161

162162
// Normal Actors as a tool
163163
const normalTools = await getNormalActorsAsTools(toolActors, apifyToken);
164164

165165
// Tools from Actorized MCP servers
166-
const mcpServerTools = await getMCPServersAsTools(actorsMCPServer, apifyToken);
166+
const mcpServerTools = await getMCPServersAsTools(actorsMCPServers, apifyToken);
167167

168168
return [...normalTools, ...mcpServerTools];
169169
}

0 commit comments

Comments
 (0)