Skip to content

Commit 69c4327

Browse files
committed
get mcp path from definition
1 parent 364c0c4 commit 69c4327

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/mcp/actors.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11

2-
import { getActorStandbyURL } from "./utils.js";
2+
import { ActorDefinition } from "apify-client";
3+
import { getActorDefinition, getActorStandbyURL } from "./utils.js";
34

4-
export async function isActorMCPServer(actorID: string, _apifyToken: string): Promise<boolean> {
5-
// TODO: implement the logic
6-
return actorID.toLowerCase().includes('mcp-') || actorID.toLowerCase().includes('-mcp');
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+
return (actorDefinition as any).webServerMcpPath;
713
}
814

915
export async function getActorsMCPServerURL(actorID: string, _apifyToken: string): Promise<string> {
1016
// TODO: get from API instead
1117
const standbyBaseUrl = process.env.HOSTNAME === 'mcp-securitybyobscurity.apify.com' ?
1218
'securitybyobscurity.apify.actor' : 'apify.actor';
1319
const standbyUrl = getActorStandbyURL(actorID, standbyBaseUrl);
14-
return `${standbyUrl}/sse`;
20+
const mcpPath = await getActorsMCPServerPath(actorID, _apifyToken);
21+
return `${standbyUrl}/${mcpPath}`;
1522
}

src/mcp/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type { ToolWrap } from '../types.js';
99
import { addTool, getActorsAsTools, removeTool } from '../tools/index.js';
1010
import { Input } from "../types.js";
1111
import { APIFY_USERNAME } from "../const.js";
12+
import { ActorDefinition } from "apify-client";
13+
import { ApifyClient } from "../apify-client.js";
1214

1315
/**
1416
* Generates a unique server ID based on the provided URL.
@@ -81,3 +83,28 @@ export function getActorStandbyURL(actorID: string, standbyBaseUrl = 'apify.acto
8183

8284
return `https://${prefix}${actorName}.${standbyBaseUrl}`;
8385
}
86+
87+
export async function getActorDefinition(actorID: string, apifyToken: string): Promise<ActorDefinition> {
88+
const apifyClient = new ApifyClient({ token: apifyToken
89+
})
90+
const actor = apifyClient.actor(actorID);
91+
const info = await actor.get();
92+
if (!info) {
93+
throw new Error(`Actor ${actorID} not found`);
94+
}
95+
const latestBuildID = info.taggedBuilds?.['latest']?.buildId;
96+
if (!latestBuildID) {
97+
throw new Error(`Actor ${actorID} does not have a latest build`);
98+
}
99+
const build = apifyClient.build(latestBuildID);
100+
const buildInfo = await build.get();
101+
if (!buildInfo) {
102+
throw new Error(`Build ${latestBuildID} not found`);
103+
}
104+
const actorDefinition = buildInfo.actorDefinition;
105+
if (!actorDefinition) {
106+
throw new Error(`Build ${latestBuildID} does not have an actor definition`);
107+
}
108+
109+
return actorDefinition;
110+
}

0 commit comments

Comments
 (0)