Skip to content

Commit e12cf8e

Browse files
committed
Truncate tool output and limit tool response.
Also limit number of Actors used to avoid rate limiting.
1 parent c8b2da4 commit e12cf8e

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,9 @@ Any [Apify Actor](https://apify.com/store) can be used as a tool.
4343
By default, the server is pre-configured with the Actors specified below, but it can be overridden by providing a list of Actor names in the `actors` query parameter.
4444

4545
```text
46-
'apidojo/tweet-scraper',
47-
'apify/facebook-posts-scraper',
48-
'apify/google-search-scraper',
4946
'apify/instagram-scraper',
5047
'apify/rag-web-browser',
51-
'clockworks/free-tiktok-scraper',
5248
'lukaskrivka/google-maps-with-contact-details',
53-
'voyager/booking-scraper'
5449
```
5550
The MCP server loads the Actor input schema and creates MCP tools corresponding to the Actors.
5651
See this example of input schema for the [RAG Web Browser](https://apify.com/apify/rag-web-browser/input-schema).

src/const.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ export const SERVER_VERSION = '0.1.0';
33

44
export const defaults = {
55
actors: [
6-
'apidojo/tweet-scraper',
7-
'apify/facebook-posts-scraper',
8-
'apify/google-search-scraper',
96
'apify/instagram-scraper',
107
'apify/rag-web-browser',
11-
'clockworks/free-tiktok-scraper',
12-
'compass/crawler-google-places',
138
'lukaskrivka/google-maps-with-contact-details',
14-
'voyager/booking-scraper',
159
],
1610
};
1711

12+
export const ACTOR_OUTPUT_MAX_CHARS_PER_ITEM = 1_000;
13+
export const ACTOR_OUTPUT_TRUNCATED_MESSAGE = `Output was truncated because it will not fit into context.` +
14+
`There is no reason to call this tool again!`;
15+
1816
export enum Routes {
1917
ROOT = '/',
2018
SSE = '/sse',

src/examples/clientStdioChat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class MCPClient {
121121
results = await this.client.callTool(params, CallToolResultSchema, { timeout: REQUEST_TIMEOUT });
122122
if (results.content instanceof Array && results.content.length !== 0) {
123123
const text = results.content.map((x) => x.text);
124-
messages.push({ role: 'user', content: text.join('\n\n') });
124+
messages.push({ role: 'user', content: `Tool result: ${text.join('\n\n')}` });
125125
} else {
126126
messages.push({ role: 'user', content: `No results retrieved from ${params.name}` });
127127
}

src/server.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import { Actor } from 'apify';
99
import { ApifyClient } from 'apify-client';
1010

1111
import { getActorsAsTools } from './actorDefinition.js';
12-
import { defaults, SERVER_NAME, SERVER_VERSION } from './const.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';
1319
import { log } from './logger.js';
1420
import type { Tool } from './types';
1521

@@ -123,14 +129,20 @@ export class ApifyMcpServer {
123129

124130
try {
125131
const items = await this.callActorGetDataset(tool.actorName, args);
126-
return { content: items.map((item) => ({ type: 'text', text: JSON.stringify(item) })) };
132+
const content = items.map(item => {
133+
let text = JSON.stringify(item).slice(0, ACTOR_OUTPUT_MAX_CHARS_PER_ITEM);
134+
return text.length === ACTOR_OUTPUT_MAX_CHARS_PER_ITEM
135+
? { type: 'text', text: `${text} ... ${ACTOR_OUTPUT_TRUNCATED_MESSAGE}` }
136+
: { type: 'text', text };
137+
});
138+
return { content: content };
139+
127140
} catch (error) {
128141
log.error(`Error calling tool: ${error}`);
129142
throw new Error(`Error calling tool: ${error}`);
130143
}
131144
});
132145
}
133-
134146
async connect(transport: Transport): Promise<void> {
135147
await this.server.connect(transport);
136148
}

0 commit comments

Comments
 (0)