@apify/actors-mcp-server 0.1.0 → 0.1.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +6 -2
- package/.actor/Dockerfile +0 -57
- package/.actor/actor.json +0 -9
- package/.actor/input_schema.json +0 -35
- package/.dockerignore +0 -19
- package/.editorconfig +0 -9
- package/.env.example +0 -3
- package/.github/scripts/before-beta-release.cjs +0 -32
- package/.github/workflows/check.yaml +0 -31
- package/.github/workflows/pre_release.yaml +0 -103
- package/.github/workflows/pre_release_ci.yaml +0 -101
- package/.github/workflows/release.yaml +0 -119
- package/CHANGELOG.md +0 -19
- package/eslint.config.mjs +0 -16
- package/src/actorDefinition.ts +0 -87
- package/src/const.ts +0 -20
- package/src/examples/clientSse.ts +0 -100
- package/src/examples/clientStdio.ts +0 -85
- package/src/examples/clientStdioChat.ts +0 -227
- package/src/examples/client_sse.py +0 -48
- package/src/index.ts +0 -34
- package/src/input.ts +0 -16
- package/src/logger.ts +0 -5
- package/src/main.ts +0 -106
- package/src/server.ts +0 -153
- package/src/types.ts +0 -20
- package/tsconfig.eslint.json +0 -4
- package/tsconfig.json +0 -16
package/src/main.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import type { ParsedUrlQuery } from 'querystring';
|
|
2
|
-
import { parse } from 'querystring';
|
|
3
|
-
|
|
4
|
-
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
5
|
-
import { Actor } from 'apify';
|
|
6
|
-
import type { Request, Response } from 'express';
|
|
7
|
-
import express from 'express';
|
|
8
|
-
|
|
9
|
-
import { Routes } from './const.js';
|
|
10
|
-
import { processInput } from './input.js';
|
|
11
|
-
import { log } from './logger.js';
|
|
12
|
-
import { ApifyMcpServer } from './server.js';
|
|
13
|
-
import type { Input } from './types.js';
|
|
14
|
-
|
|
15
|
-
await Actor.init();
|
|
16
|
-
|
|
17
|
-
const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY';
|
|
18
|
-
const HOST = Actor.isAtHome() ? process.env.ACTOR_STANDBY_URL : 'http://localhost';
|
|
19
|
-
const PORT = Actor.isAtHome() ? process.env.ACTOR_STANDBY_PORT : 3001;
|
|
20
|
-
|
|
21
|
-
const app = express();
|
|
22
|
-
|
|
23
|
-
const mcpServer = new ApifyMcpServer();
|
|
24
|
-
let transport: SSEServerTransport;
|
|
25
|
-
|
|
26
|
-
const HELP_MESSAGE = `Connect to the server with GET request to ${HOST}/sse?token=YOUR-APIFY-TOKEN`
|
|
27
|
-
+ ` and then send POST requests to ${HOST}/message?token=YOUR-APIFY-TOKEN.`;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Process input parameters and update tools
|
|
31
|
-
* If URL contains query parameter actors, add tools from actors, otherwise add tools from default actors
|
|
32
|
-
* @param url
|
|
33
|
-
*/
|
|
34
|
-
async function processParamsAndUpdateTools(url: string) {
|
|
35
|
-
const params = parse(url.split('?')[1] || '') as ParsedUrlQuery;
|
|
36
|
-
delete params.token;
|
|
37
|
-
log.debug(`Received input parameters: ${JSON.stringify(params)}`);
|
|
38
|
-
const input = await processInput(params as Input);
|
|
39
|
-
if (input.actors) {
|
|
40
|
-
await mcpServer.addToolsFromActors(input.actors as string[]);
|
|
41
|
-
} else {
|
|
42
|
-
log.debug(`Server is running in STANDBY mode with the following Actors (tools): ${mcpServer.getToolNames()}.
|
|
43
|
-
To use different Actors, provide them in query parameter "actors" or include them in the Actor Task input.`);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
app.get(Routes.ROOT, async (req: Request, res: Response) => {
|
|
48
|
-
try {
|
|
49
|
-
log.info(`Received GET message at: ${req.url}`);
|
|
50
|
-
await processParamsAndUpdateTools(req.url);
|
|
51
|
-
res.status(200).json({ message: `Actor is using Model Context Protocol. ${HELP_MESSAGE}` }).end();
|
|
52
|
-
} catch (error) {
|
|
53
|
-
log.error(`Error in GET ${Routes.ROOT} ${error}`);
|
|
54
|
-
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
app.head(Routes.ROOT, (_req: Request, res: Response) => {
|
|
59
|
-
res.status(200).end();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
app.get(Routes.SSE, async (req: Request, res: Response) => {
|
|
63
|
-
try {
|
|
64
|
-
log.info(`Received GET message at: ${req.url}`);
|
|
65
|
-
await processParamsAndUpdateTools(req.url);
|
|
66
|
-
transport = new SSEServerTransport(Routes.MESSAGE, res);
|
|
67
|
-
await mcpServer.connect(transport);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
log.error(`Error in GET ${Routes.SSE}: ${error}`);
|
|
70
|
-
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
app.post(Routes.MESSAGE, async (req: Request, res: Response) => {
|
|
75
|
-
try {
|
|
76
|
-
log.info(`Received POST message at: ${req.url}`);
|
|
77
|
-
await transport.handlePostMessage(req, res);
|
|
78
|
-
} catch (error) {
|
|
79
|
-
log.error(`Error in POST ${Routes.MESSAGE}: ${error}`);
|
|
80
|
-
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
// Catch-all for undefined routes
|
|
85
|
-
app.use((req: Request, res: Response) => {
|
|
86
|
-
res.status(404).json({ message: `There is nothing at route ${req.method} ${req.originalUrl}. ${HELP_MESSAGE}` }).end();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const input = await processInput((await Actor.getInput<Partial<Input>>()) ?? ({} as Input));
|
|
90
|
-
log.info(`Loaded input: ${JSON.stringify(input)} `);
|
|
91
|
-
|
|
92
|
-
if (STANDBY_MODE) {
|
|
93
|
-
log.info('Actor is running in the STANDBY mode.');
|
|
94
|
-
await mcpServer.addToolsFromDefaultActors();
|
|
95
|
-
app.listen(PORT, () => {
|
|
96
|
-
log.info(`The Actor web server is listening for user requests at ${HOST}.`);
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
log.info('Actor is not designed to run in the NORMAL model (use this mode only for debugging purposes)');
|
|
100
|
-
|
|
101
|
-
if (input && !input.debugActor && !input.debugActorInput) {
|
|
102
|
-
await Actor.fail('If you need to debug a specific actor, please provide the debugActor and debugActorInput fields in the input.');
|
|
103
|
-
}
|
|
104
|
-
await mcpServer.callActorGetDataset(input.debugActor!, input.debugActorInput!);
|
|
105
|
-
await Actor.exit();
|
|
106
|
-
}
|
package/src/server.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Model Context Protocol (MCP) server for Apify Actors
|
|
4
|
-
*/
|
|
5
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
|
-
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
7
|
-
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
-
import { Actor } from 'apify';
|
|
9
|
-
import { ApifyClient } from 'apify-client';
|
|
10
|
-
|
|
11
|
-
import { getActorsAsTools } from './actorDefinition.js';
|
|
12
|
-
import {
|
|
13
|
-
ACTOR_OUTPUT_MAX_CHARS_PER_ITEM,
|
|
14
|
-
ACTOR_OUTPUT_TRUNCATED_MESSAGE,
|
|
15
|
-
defaults,
|
|
16
|
-
SERVER_NAME,
|
|
17
|
-
SERVER_VERSION,
|
|
18
|
-
} from './const.js';
|
|
19
|
-
import { log } from './logger.js';
|
|
20
|
-
import type { Tool } from './types';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create Apify MCP server
|
|
24
|
-
*/
|
|
25
|
-
export class ApifyMcpServer {
|
|
26
|
-
private server: Server;
|
|
27
|
-
private tools: Map<string, Tool>;
|
|
28
|
-
|
|
29
|
-
constructor() {
|
|
30
|
-
this.server = new Server(
|
|
31
|
-
{
|
|
32
|
-
name: SERVER_NAME,
|
|
33
|
-
version: SERVER_VERSION,
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
capabilities: {
|
|
37
|
-
tools: {},
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
);
|
|
41
|
-
this.tools = new Map();
|
|
42
|
-
this.setupErrorHandling();
|
|
43
|
-
this.setupToolHandlers();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Calls an Apify actor and retrieves the dataset items.
|
|
48
|
-
*
|
|
49
|
-
* It requires the `APIFY_TOKEN` environment variable to be set.
|
|
50
|
-
* If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
|
|
51
|
-
*
|
|
52
|
-
* @param {string} actorName - The name of the actor to call.
|
|
53
|
-
* @param {unknown} input - The input to pass to the actor.
|
|
54
|
-
* @returns {Promise<object[]>} - A promise that resolves to an array of dataset items.
|
|
55
|
-
* @throws {Error} - Throws an error if the `APIFY_TOKEN` is not set
|
|
56
|
-
*/
|
|
57
|
-
public async callActorGetDataset(actorName: string, input: unknown): Promise<object[]> {
|
|
58
|
-
if (!process.env.APIFY_TOKEN) {
|
|
59
|
-
throw new Error('APIFY_TOKEN is required but not set. Please set it as an environment variable');
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
log.info(`Calling actor ${actorName} with input: ${JSON.stringify(input)}`);
|
|
63
|
-
const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
|
|
64
|
-
const actorClient = client.actor(actorName);
|
|
65
|
-
|
|
66
|
-
const results = await actorClient.call(input);
|
|
67
|
-
const dataset = await client.dataset(results.defaultDatasetId).listItems();
|
|
68
|
-
log.info(`Actor ${actorName} finished with ${dataset.items.length} items`);
|
|
69
|
-
|
|
70
|
-
if (process.env.APIFY_IS_AT_HOME) {
|
|
71
|
-
await Actor.pushData(dataset.items);
|
|
72
|
-
log.info(`Pushed ${dataset.items.length} items to the dataset`);
|
|
73
|
-
}
|
|
74
|
-
return dataset.items;
|
|
75
|
-
} catch (error) {
|
|
76
|
-
log.error(`Error calling actor: ${error}. Actor: ${actorName}, input: ${JSON.stringify(input)}`);
|
|
77
|
-
throw new Error(`Error calling actor: ${error}`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public async addToolsFromActors(actors: string[]) {
|
|
82
|
-
const tools = await getActorsAsTools(actors);
|
|
83
|
-
this.updateTools(tools);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public async addToolsFromDefaultActors() {
|
|
87
|
-
await this.addToolsFromActors(defaults.actors);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public updateTools(tools: Tool[]): void {
|
|
91
|
-
for (const tool of tools) {
|
|
92
|
-
this.tools.set(tool.name, tool);
|
|
93
|
-
log.info(`Added/Updated tool: ${tool.name}`);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public getToolNames(): string[] {
|
|
98
|
-
return Array.from(this.tools.keys());
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private setupErrorHandling(): void {
|
|
102
|
-
this.server.onerror = (error) => {
|
|
103
|
-
console.error('[MCP Error]', error); // eslint-disable-line no-console
|
|
104
|
-
};
|
|
105
|
-
process.on('SIGINT', async () => {
|
|
106
|
-
await this.server.close();
|
|
107
|
-
process.exit(0);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private setupToolHandlers(): void {
|
|
112
|
-
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
113
|
-
return { tools: Array.from(this.tools.values()) };
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Handles the request to call a tool.
|
|
118
|
-
* @param {object} request - The request object containing tool name and arguments.
|
|
119
|
-
* @throws {Error} - Throws an error if the tool is unknown or arguments are invalid.
|
|
120
|
-
*/
|
|
121
|
-
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
122
|
-
const { name, arguments: args } = request.params;
|
|
123
|
-
|
|
124
|
-
// Anthropic can't handle '/' in tool names. The replace is only necessary when calling the tool from stdio clients.
|
|
125
|
-
const tool = this.tools.get(name) || this.tools.get(name.replace('/', '_'));
|
|
126
|
-
if (!tool) {
|
|
127
|
-
throw new Error(`Unknown tool: ${name}`);
|
|
128
|
-
}
|
|
129
|
-
log.info(`Validate arguments for tool: ${tool.name} with arguments: ${JSON.stringify(args)}`);
|
|
130
|
-
if (!tool.ajvValidate(args)) {
|
|
131
|
-
throw new Error(`Invalid arguments for tool ${tool.name}: args: ${JSON.stringify(args)} error: ${JSON.stringify(tool?.ajvValidate.errors)}`);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
const items = await this.callActorGetDataset(tool.actorName, args);
|
|
136
|
-
const content = items.map((item) => {
|
|
137
|
-
const text = JSON.stringify(item).slice(0, ACTOR_OUTPUT_MAX_CHARS_PER_ITEM);
|
|
138
|
-
return text.length === ACTOR_OUTPUT_MAX_CHARS_PER_ITEM
|
|
139
|
-
? { type: 'text', text: `${text} ... ${ACTOR_OUTPUT_TRUNCATED_MESSAGE}` }
|
|
140
|
-
: { type: 'text', text };
|
|
141
|
-
});
|
|
142
|
-
return { content };
|
|
143
|
-
} catch (error) {
|
|
144
|
-
log.error(`Error calling tool: ${error}`);
|
|
145
|
-
throw new Error(`Error calling tool: ${error}`);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async connect(transport: Transport): Promise<void> {
|
|
151
|
-
await this.server.connect(transport);
|
|
152
|
-
}
|
|
153
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ValidateFunction } from 'ajv';
|
|
2
|
-
import type { ActorDefinition } from 'apify-client';
|
|
3
|
-
|
|
4
|
-
export type Input = {
|
|
5
|
-
actors: string[] | string;
|
|
6
|
-
debugActor?: string;
|
|
7
|
-
debugActorInput?: unknown;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export interface ActorDefinitionWithDesc extends ActorDefinition {
|
|
11
|
-
description: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface Tool {
|
|
15
|
-
name: string;
|
|
16
|
-
actorName: string;
|
|
17
|
-
description: string;
|
|
18
|
-
inputSchema: object;
|
|
19
|
-
ajvValidate: ValidateFunction;
|
|
20
|
-
}
|
package/tsconfig.eslint.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@apify/tsconfig",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"outDir": "dist",
|
|
7
|
-
"moduleResolution": "node",
|
|
8
|
-
"noUnusedLocals": false,
|
|
9
|
-
"lib": ["ES2022"],
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"typeRoots": ["./types", "./node_modules/@types"],
|
|
12
|
-
"strict": true
|
|
13
|
-
},
|
|
14
|
-
"include": ["./src/**/*"],
|
|
15
|
-
"exclude": ["node_modules"]
|
|
16
|
-
}
|