Skip to content

Commit 7f545cf

Browse files
committed
refactor: Relocate getMCPServerID and other utilities to proxy.ts, clean up utils.ts, and update imports
1 parent 3f1c9f5 commit 7f545cf

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mcp/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import log from '@apify/log';
77
import { TimeoutError } from '../errors.js';
88
import { getHttpStatusCode } from '../utils/logging.js';
99
import { ACTORIZED_MCP_CONNECTION_TIMEOUT_MSEC } from './const.js';
10-
import { getMCPServerID } from './utils.js';
10+
import { getMCPServerID } from './proxy.js';
1111

1212
/**
1313
* Creates and connects a ModelContextProtocol client.

src/mcp/proxy.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
1+
import { createHash } from 'node:crypto';
2+
13
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
24

35
import { fixedAjvCompile } from '../tools/utils.js';
46
import type { ActorMcpTool, ToolEntry } from '../types.js';
57
import { ajv } from '../utils/ajv.js';
6-
import { getMCPServerID, getProxyMCPServerToolName } from './utils.js';
8+
import { MAX_TOOL_NAME_LENGTH, SERVER_ID_LENGTH } from './const.js';
9+
10+
/**
11+
* Generates a unique server ID based on the provided URL.
12+
*
13+
* URL is used instead of Actor ID because one Actor may expose multiple servers - legacy SSE / streamable HTTP.
14+
*
15+
* @param url The URL to generate the server ID from.
16+
* @returns A unique server ID.
17+
*/
18+
export function getMCPServerID(url: string): string {
19+
const serverHashDigest = createHash('sha256').update(url).digest('hex');
20+
21+
return serverHashDigest.slice(0, SERVER_ID_LENGTH);
22+
}
23+
24+
/**
25+
* Generates a unique tool name based on the provided URL and tool name.
26+
* @param url The URL to generate the tool name from.
27+
* @param toolName The tool name to generate the tool name from.
28+
* @returns A unique tool name.
29+
*/
30+
function getProxyMCPServerToolName(url: string, toolName: string): string {
31+
const prefix = getMCPServerID(url);
32+
33+
const fullName = `${prefix}-${toolName}`;
34+
return fullName.slice(0, MAX_TOOL_NAME_LENGTH);
35+
}
736

837
export async function getMCPServerTools(
938
actorID: string,

src/mcp/utils.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createHash } from 'node:crypto';
21
import { parse } from 'node:querystring';
32

43
import type { TaskStore } from '@modelcontextprotocol/sdk/experimental/tasks/interfaces.js';
@@ -7,34 +6,6 @@ import type { ApifyClient } from 'apify-client';
76
import { processInput } from '../input.js';
87
import type { ActorStore, Input, ServerMode } from '../types.js';
98
import { loadToolsFromInput } from '../utils/tools_loader.js';
10-
import { MAX_TOOL_NAME_LENGTH, SERVER_ID_LENGTH } from './const.js';
11-
12-
/**
13-
* Generates a unique server ID based on the provided URL.
14-
*
15-
* URL is used instead of Actor ID because one Actor may expose multiple servers - legacy SSE / streamable HTTP.
16-
*
17-
* @param url The URL to generate the server ID from.
18-
* @returns A unique server ID.
19-
*/
20-
export function getMCPServerID(url: string): string {
21-
const serverHashDigest = createHash('sha256').update(url).digest('hex');
22-
23-
return serverHashDigest.slice(0, SERVER_ID_LENGTH);
24-
}
25-
26-
/**
27-
* Generates a unique tool name based on the provided URL and tool name.
28-
* @param url The URL to generate the tool name from.
29-
* @param toolName The tool name to generate the tool name from.
30-
* @returns A unique tool name.
31-
*/
32-
export function getProxyMCPServerToolName(url: string, toolName: string): string {
33-
const prefix = getMCPServerID(url);
34-
35-
const fullName = `${prefix}-${toolName}`;
36-
return fullName.slice(0, MAX_TOOL_NAME_LENGTH);
37-
}
389

3910
/**
4011
* Process input parameters from URL and get tools

src/tools/core/actor_execution.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ export type CallActorGetDatasetResult = {
3131
* It requires the `APIFY_TOKEN` environment variable to be set.
3232
* If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
3333
*
34-
* @param {string} actorName - The name of the Actor to call.
35-
* @param {unknown} input - The input to pass to the actor.
36-
* @param {ApifyClient} apifyClient - The Apify client to use for authentication.
3734
* @returns {Promise<CallActorGetDatasetResult | null>} - A promise that resolves to an object containing the actor run and dataset items.
3835
* @throws {Error} - Throws an error if the `APIFY_TOKEN` is not set
36+
* @param options
3937
*/
4038
export async function callActorGetDataset(options: {
4139
actorName: string;

0 commit comments

Comments
 (0)