Skip to content

Commit 0ccc995

Browse files
committed
improve
1 parent 03d772c commit 0ccc995

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
This file provides essential functions for constructing HTTP and MCP servers, effectively serving as a library.
33
Acts as a library entrypoint.
44
*/
5+
6+
export { createExpressApp } from './server.js';
7+
export { ApifyMcpServer } from './mcp-server.js';

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { ActorCallOptions } from 'apify-client';
88
import { processInput } from './input.js';
99
import { log } from './logger.js';
1010
import { ApifyMcpServer } from './mcp-server.js';
11-
import { createServerApp } from './server.js';
11+
import { createExpressApp } from './server.js';
1212
import { getActorDiscoveryTools, getActorAutoLoadingTools } from './tools.js';
1313
import type { Input } from './types.js';
1414
import { isActorStandby } from './utils.js';
@@ -52,7 +52,7 @@ const input = await processInput((await Actor.getInput<Partial<Input>>()) ?? ({}
5252
log.info(`Loaded input: ${JSON.stringify(input)} `);
5353

5454
if (isActorStandby()) {
55-
const app = createServerApp(HOST, mcpServer, actorRunData);
55+
const app = createExpressApp(HOST, mcpServer, actorRunData);
5656
log.info('Actor is running in the STANDBY mode.');
5757
await mcpServer.addToolsFromDefaultActors();
5858
mcpServer.updateTools(getActorDiscoveryTools());

src/mcp-server.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ export class ApifyMcpServer {
149149
});
150150
}
151151

152+
/**
153+
* Process input parameters and update tools
154+
* If URL contains query parameter actors, add tools from actors, otherwise add tools from default actors
155+
* @param url
156+
*/
157+
public async processParamsAndUpdateTools(url: string) {
158+
const params = parse(url.split('?')[1] || '') as ParsedUrlQuery;
159+
delete params.token;
160+
log.debug(`Received input parameters: ${JSON.stringify(params)}`);
161+
const input = await processInput(params as unknown as Input);
162+
if (input.actors) {
163+
await this.addToolsFromActors(input.actors as string[]);
164+
}
165+
if (input.enableActorAutoLoading) {
166+
this.updateTools(getActorAutoLoadingTools());
167+
}
168+
log.debug(`Server is running in STANDBY mode with the following Actors (tools): ${this.getToolNames()}.
169+
To use different Actors, provide them in query parameter "actors" or include them in the Actor Task input.`);
170+
}
171+
152172
private setupToolHandlers(): void {
153173
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
154174
return { tools: Array.from(this.tools.values()) };
@@ -228,23 +248,3 @@ export class ApifyMcpServer {
228248
await this.server.connect(transport);
229249
}
230250
}
231-
232-
/**
233-
* Process input parameters and update tools
234-
* If URL contains query parameter actors, add tools from actors, otherwise add tools from default actors
235-
* @param url
236-
*/
237-
export async function processParamsAndUpdateTools(url: string, mcpServer: ApifyMcpServer) {
238-
const params = parse(url.split('?')[1] || '') as ParsedUrlQuery;
239-
delete params.token;
240-
log.debug(`Received input parameters: ${JSON.stringify(params)}`);
241-
const input = await processInput(params as unknown as Input);
242-
if (input.actors) {
243-
await mcpServer.addToolsFromActors(input.actors as string[]);
244-
}
245-
if (input.enableActorAutoLoading) {
246-
mcpServer.updateTools(getActorAutoLoadingTools());
247-
}
248-
log.debug(`Server is running in STANDBY mode with the following Actors (tools): ${mcpServer.getToolNames()}.
249-
To use different Actors, provide them in query parameter "actors" or include them in the Actor Task input.`);
250-
}

src/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import express from 'express';
44

55
import { HEADER_READINESS_PROBE, Routes } from './const.js';
66
import { log } from './logger.js';
7-
import { type ApifyMcpServer, processParamsAndUpdateTools } from './mcp-server.js';
7+
import { type ApifyMcpServer } from './mcp-server.js';
88

9-
export function createServerApp(host: string,
9+
export function createExpressApp(host: string,
1010
mcpServer: ApifyMcpServer,
1111
additionalData?: object): express.Express {
1212
const HELP_MESSAGE = `Connect to the server with GET request to ${host}/sse?token=YOUR-APIFY-TOKEN`
@@ -25,7 +25,7 @@ export function createServerApp(host: string,
2525
}
2626
try {
2727
log.info(`Received GET message at: ${Routes.ROOT}`);
28-
await processParamsAndUpdateTools(req.url, mcpServer);
28+
await mcpServer.processParamsAndUpdateTools(req.url);
2929
res.setHeader('Content-Type', 'text/event-stream');
3030
res.setHeader('Cache-Control', 'no-cache');
3131
res.setHeader('Connection', 'keep-alive');
@@ -43,7 +43,7 @@ export function createServerApp(host: string,
4343
.get(async (req: Request, res: Response) => {
4444
try {
4545
log.info(`Received GET message at: ${Routes.SSE}`);
46-
await processParamsAndUpdateTools(req.url, mcpServer);
46+
await mcpServer.processParamsAndUpdateTools(req.url);
4747
transport = new SSEServerTransport(Routes.MESSAGE, res);
4848
await mcpServer.connect(transport);
4949
} catch (error) {

0 commit comments

Comments
 (0)