@apify/actors-mcp-server 0.1.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.
@@ -0,0 +1,227 @@
1
+ /* eslint-disable no-console */
2
+ /**
3
+ * Create a simple chat client that connects to the Model Context Protocol server using the stdio transport.
4
+ * Based on the user input, the client sends a query to the MCP server, retrieves results and processes them.
5
+ *
6
+ * You can expect the following output:
7
+ *
8
+ * MCP Client Started!
9
+ * Type your queries or 'quit|q|exit' to exit.
10
+ * You: Find to articles about AI agent and return URLs
11
+ * [internal] Received response from Claude: [{"type":"text","text":"I'll search for information about AI agents
12
+ * and provide you with a summary."},{"type":"tool_use","id":"tool_01He9TkzQfh2979bbeuxWVqM","name":"search",
13
+ * "input":{"query":"what are AI agents definition capabilities applications","maxResults":2}}]
14
+ * [internal] Calling tool: {"name":"search","arguments":{"query":"what are AI agents definition ...
15
+ * I can help analyze the provided content about AI agents.
16
+ * This appears to be crawled content from AWS and IBM websites explaining what AI agents are.
17
+ * Let me summarize the key points:
18
+ */
19
+
20
+ import { execSync } from 'child_process';
21
+ import path from 'path';
22
+ import * as readline from 'readline';
23
+ import { fileURLToPath } from 'url';
24
+
25
+ import { Anthropic } from '@anthropic-ai/sdk';
26
+ import type { Message, ToolUseBlock, MessageParam } from '@anthropic-ai/sdk/resources/messages';
27
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
28
+ import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
29
+ import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
30
+ import dotenv from 'dotenv';
31
+
32
+ const filename = fileURLToPath(import.meta.url);
33
+ const dirname = path.dirname(filename);
34
+
35
+ dotenv.config({ path: path.resolve(dirname, '../../.env') });
36
+
37
+ const REQUEST_TIMEOUT = 120_000; // 2 minutes
38
+ const MAX_TOKENS = 2048; // Maximum tokens for Claude response
39
+
40
+ // const CLAUDE_MODEL = 'claude-3-5-sonnet-20241022'; // the most intelligent model
41
+ // const CLAUDE_MODEL = 'claude-3-5-haiku-20241022'; // a fastest model
42
+ const CLAUDE_MODEL = 'claude-3-haiku-20240307'; // a fastest and most compact model for near-instant responsiveness
43
+ const DEBUG = true;
44
+ const DEBUG_SERVER_PATH = path.resolve(dirname, '../../dist/index.js');
45
+
46
+ const NODE_PATH = execSync('which node').toString().trim();
47
+
48
+ dotenv.config(); // Load environment variables from .env
49
+
50
+ export type Tool = {
51
+ name: string;
52
+ description: string | undefined;
53
+ input_schema: unknown;
54
+ }
55
+
56
+ class MCPClient {
57
+ private anthropic: Anthropic;
58
+ private client = new Client(
59
+ {
60
+ name: 'example-client',
61
+ version: '0.1.0',
62
+ },
63
+ {
64
+ capabilities: {}, // Optional capabilities
65
+ },
66
+ );
67
+
68
+ private tools: Tool[] = [];
69
+
70
+ constructor() {
71
+ this.anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
72
+ }
73
+
74
+ /**
75
+ * Start the server using node and provided server script path.
76
+ * Connect to the server using stdio transport and list available tools.
77
+ */
78
+ async connectToServer(serverArgs: string[]) {
79
+ const transport = new StdioClientTransport({
80
+ command: NODE_PATH,
81
+ args: serverArgs,
82
+ env: { APIFY_TOKEN: process.env.APIFY_TOKEN || '' },
83
+ });
84
+
85
+ await this.client.connect(transport);
86
+ const response = await this.client.listTools();
87
+
88
+ this.tools = response.tools.map((x) => ({
89
+ name: x.name,
90
+ description: x.description,
91
+ input_schema: x.inputSchema,
92
+ }));
93
+ console.log('Connected to server with tools:', this.tools.map((x) => x.name));
94
+ }
95
+
96
+ /**
97
+ * Process LLM response and check whether it contains any tool calls.
98
+ * If a tool call is found, call the tool and return the response and save the results to messages with type: user.
99
+ * If the tools response is too large, truncate it to the limit.
100
+ */
101
+ async processMsg(response: Message, messages: MessageParam[]): Promise<MessageParam[]> {
102
+ for (const content of response.content) {
103
+ if (content.type === 'text') {
104
+ messages.push({ role: 'assistant', content: content.text });
105
+ } else if (content.type === 'tool_use') {
106
+ await this.handleToolCall(content, messages);
107
+ }
108
+ }
109
+ return messages;
110
+ }
111
+
112
+ /**
113
+ * Call the tool and return the response.
114
+ */
115
+ private async handleToolCall(content: ToolUseBlock, messages: MessageParam[], toolCallCount = 0): Promise<MessageParam[]> {
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ const params = { name: content.name, arguments: content.input as any };
118
+ console.log(`[internal] Calling tool (count: ${toolCallCount}): ${JSON.stringify(params)}`);
119
+ let results;
120
+ try {
121
+ results = await this.client.callTool(params, CallToolResultSchema, { timeout: REQUEST_TIMEOUT });
122
+ if (results.content instanceof Array && results.content.length !== 0) {
123
+ const text = results.content.map((x) => x.text);
124
+ messages.push({ role: 'user', content: `Tool result: ${text.join('\n\n')}` });
125
+ } else {
126
+ messages.push({ role: 'user', content: `No results retrieved from ${params.name}` });
127
+ }
128
+ } catch (error) {
129
+ messages.push({ role: 'user', content: `Error calling tool: ${params.name}, error: ${error}` });
130
+ }
131
+ // Get next response from Claude
132
+ const nextResponse: Message = await this.anthropic.messages.create({
133
+ model: CLAUDE_MODEL,
134
+ max_tokens: MAX_TOKENS,
135
+ messages,
136
+ tools: this.tools as any[], // eslint-disable-line @typescript-eslint/no-explicit-any
137
+ });
138
+
139
+ for (const c of nextResponse.content) {
140
+ if (c.type === 'text') {
141
+ messages.push({ role: 'assistant', content: c.text });
142
+ } else if (c.type === 'tool_use' && toolCallCount < 3) {
143
+ return await this.handleToolCall(c, messages, toolCallCount + 1);
144
+ }
145
+ }
146
+
147
+ return messages;
148
+ }
149
+
150
+ /**
151
+ * Process user query by sending it to the server and returning the response.
152
+ * Also, process any tool calls.
153
+ */
154
+ async processQuery(query: string, messages: MessageParam[]): Promise<MessageParam[]> {
155
+ messages.push({ role: 'user', content: query });
156
+ const response: Message = await this.anthropic.messages.create({
157
+ model: CLAUDE_MODEL,
158
+ max_tokens: MAX_TOKENS,
159
+ messages,
160
+ tools: this.tools as any[], // eslint-disable-line @typescript-eslint/no-explicit-any
161
+ });
162
+ console.log('[internal] Received response from Claude:', JSON.stringify(response.content));
163
+ return await this.processMsg(response, messages);
164
+ }
165
+
166
+ /**
167
+ * Create a chat loop that reads user input from the console and sends it to the server for processing.
168
+ */
169
+ async chatLoop() {
170
+ const rl = readline.createInterface({
171
+ input: process.stdin,
172
+ output: process.stdout,
173
+ prompt: 'You: ',
174
+ });
175
+
176
+ console.log("MCP Client Started!\nType your queries or 'quit|q|exit' to exit.");
177
+ rl.prompt();
178
+
179
+ let lastPrintMessage = 0;
180
+ const messages: MessageParam[] = [];
181
+ rl.on('line', async (input) => {
182
+ const v = input.trim().toLowerCase();
183
+ if (v === 'quit' || v === 'q' || v === 'exit') {
184
+ rl.close();
185
+ return;
186
+ }
187
+ try {
188
+ await this.processQuery(input, messages);
189
+ for (let i = lastPrintMessage + 1; i < messages.length; i++) {
190
+ if (messages[i].role === 'assistant') {
191
+ console.log('CLAUDE:', messages[i].content);
192
+ } else if (messages[i].role === 'user') {
193
+ console.log('USER:', messages[i].content.slice(0, 500), '...');
194
+ } else {
195
+ console.log('CLAUDE[thinking]:', messages[i].content);
196
+ }
197
+ }
198
+ lastPrintMessage += messages.length;
199
+ } catch (error) {
200
+ console.error('Error processing query:', error);
201
+ }
202
+ rl.prompt();
203
+ });
204
+ }
205
+ }
206
+
207
+ async function main() {
208
+ const client = new MCPClient();
209
+
210
+ if (process.argv.length < 3) {
211
+ if (DEBUG) {
212
+ process.argv.push(DEBUG_SERVER_PATH);
213
+ } else {
214
+ console.error('Usage: node <path_to_server_script>');
215
+ process.exit(1);
216
+ }
217
+ }
218
+
219
+ try {
220
+ await client.connectToServer(process.argv.slice(2));
221
+ await client.chatLoop();
222
+ } catch (error) {
223
+ console.error('Error:', error);
224
+ }
225
+ }
226
+
227
+ main().catch(console.error);
@@ -0,0 +1,48 @@
1
+ """
2
+ Test Apify MCP Server using SSE client
3
+
4
+ It is using python client as the typescript one does not support custom headers when connecting to the SSE server.
5
+
6
+ Install python dependencies (assumes you have python installed):
7
+ > pip install requests python-dotenv mcp
8
+ """
9
+
10
+ import asyncio
11
+ import os
12
+ from pathlib import Path
13
+
14
+ import requests
15
+ from dotenv import load_dotenv
16
+ from mcp.client.session import ClientSession
17
+ from mcp.client.sse import sse_client
18
+
19
+ load_dotenv(Path(__file__).resolve().parent.parent.parent / ".env")
20
+
21
+ MCP_SERVER_URL = "https://actors-mcp-server.apify.actor"
22
+
23
+ HEADERS = {"Authorization": f"Bearer {os.getenv('APIFY_TOKEN')}"}
24
+
25
+ async def run() -> None:
26
+
27
+ print("Start MCP Server with Actors")
28
+ r = requests.get(MCP_SERVER_URL, headers=HEADERS)
29
+ print("MCP Server Response:", r.json(), end="\n\n")
30
+
31
+ async with sse_client(url=f"{MCP_SERVER_URL}/sse", timeout=60, headers=HEADERS) as (read, write):
32
+ async with ClientSession(read, write) as session:
33
+ await session.initialize()
34
+
35
+ tools = await session.list_tools()
36
+ print("Available Tools:", tools, end="\n\n")
37
+
38
+ if hasattr(tools, "tools") and not tools.tools:
39
+ print("No tools available!")
40
+ return
41
+
42
+ result = await session.call_tool("apify/rag-web-browser", { "query": "example.com", "maxResults": 3 })
43
+ print("Tools Call Result:")
44
+
45
+ for content in result.content:
46
+ print(content)
47
+
48
+ asyncio.run(run())
package/src/index.ts ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * This script initializes and starts the Apify MCP server using the Stdio transport.
3
+ *
4
+ * Usage:
5
+ * node <script_name> --actors=<actor1,actor2,...>
6
+ *
7
+ * Command-line arguments:
8
+ * --actors - A comma-separated list of actor full names to add to the server.
9
+ *
10
+ * Example:
11
+ * node index.js --actors=apify/google-search-scraper,apify/instagram-scraper
12
+ */
13
+
14
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
15
+ import minimist from 'minimist';
16
+
17
+ import { ApifyMcpServer } from './server.js';
18
+
19
+ const argv = minimist(process.argv.slice(2));
20
+ const argActors = argv.actors?.split(',').map((actor: string) => actor.trim()) || [];
21
+
22
+ async function main() {
23
+ const server = new ApifyMcpServer();
24
+ await (argActors.length !== 0
25
+ ? server.addToolsFromActors(argActors)
26
+ : server.addToolsFromDefaultActors());
27
+ const transport = new StdioServerTransport();
28
+ await server.connect(transport);
29
+ }
30
+
31
+ main().catch((error) => {
32
+ console.error('Server error:', error); // eslint-disable-line no-console
33
+ process.exit(1);
34
+ });
package/src/input.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Input } from './types.js';
2
+
3
+ /**
4
+ * Process input parameters, split actors string into an array
5
+ * @param originalInput
6
+ * @returns input
7
+ */
8
+ export async function processInput(originalInput: Partial<Input>): Promise<Input> {
9
+ const input = originalInput as Input;
10
+
11
+ // actors can be a string or an array of strings
12
+ if (input.actors && typeof input.actors === 'string') {
13
+ input.actors = input.actors.split(',').map((format: string) => format.trim()) as string[];
14
+ }
15
+ return input;
16
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { log } from 'apify';
2
+
3
+ log.setLevel(log.LEVELS.DEBUG);
4
+
5
+ export { log };
package/src/main.ts ADDED
@@ -0,0 +1,106 @@
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 ADDED
@@ -0,0 +1,153 @@
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 ADDED
@@ -0,0 +1,20 @@
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
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["./src/**/*"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
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
+ }