@apify/actors-mcp-server 0.1.22-beta.13 → 0.1.22-beta.15
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 +62 -64
- package/dist/actors.d.ts +74 -0
- package/dist/actors.d.ts.map +1 -0
- package/dist/{tools/utils.js → actors.js} +241 -112
- package/dist/actors.js.map +1 -0
- package/dist/const.d.ts +18 -14
- package/dist/const.d.ts.map +1 -1
- package/dist/const.js +22 -22
- package/dist/const.js.map +1 -1
- package/dist/examples/clientSse.d.ts +1 -10
- package/dist/examples/clientSse.d.ts.map +1 -1
- package/dist/examples/clientSse.js +4 -7
- package/dist/examples/clientSse.js.map +1 -1
- package/dist/examples/clientStdio.js +2 -2
- package/dist/examples/clientStdio.js.map +1 -1
- package/dist/examples/clientStdioChat.js +1 -1
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +51 -6
- package/dist/index.js.map +1 -1
- package/dist/input.d.ts +1 -1
- package/dist/input.d.ts.map +1 -1
- package/dist/input.js +8 -6
- package/dist/input.js.map +1 -1
- package/dist/logger.d.ts +3 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +4 -0
- package/dist/logger.js.map +1 -0
- package/dist/main.d.ts +0 -4
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +122 -27
- package/dist/main.js.map +1 -1
- package/dist/server.d.ts +43 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +178 -0
- package/dist/server.js.map +1 -0
- package/dist/tools.d.ts +46 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +128 -0
- package/dist/tools.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +10 -93
- package/dist/types.d.ts.map +1 -1
- package/package.json +16 -18
- package/dist/actor/const.d.ts +0 -10
- package/dist/actor/const.d.ts.map +0 -1
- package/dist/actor/const.js +0 -11
- package/dist/actor/const.js.map +0 -1
- package/dist/actor/server.d.ts +0 -4
- package/dist/actor/server.d.ts.map +0 -1
- package/dist/actor/server.js +0 -82
- package/dist/actor/server.js.map +0 -1
- package/dist/actor/types.d.ts +0 -23
- package/dist/actor/types.d.ts.map +0 -1
- package/dist/actor/types.js +0 -2
- package/dist/actor/types.js.map +0 -1
- package/dist/actor/utils.d.ts +0 -3
- package/dist/actor/utils.d.ts.map +0 -1
- package/dist/actor/utils.js +0 -26
- package/dist/actor/utils.js.map +0 -1
- package/dist/apify-client.d.ts +0 -6
- package/dist/apify-client.d.ts.map +0 -1
- package/dist/apify-client.js +0 -23
- package/dist/apify-client.js.map +0 -1
- package/dist/mcp/actors.d.ts +0 -20
- package/dist/mcp/actors.d.ts.map +0 -1
- package/dist/mcp/actors.js +0 -66
- package/dist/mcp/actors.js.map +0 -1
- package/dist/mcp/client.d.ts +0 -6
- package/dist/mcp/client.d.ts.map +0 -1
- package/dist/mcp/client.js +0 -33
- package/dist/mcp/client.js.map +0 -1
- package/dist/mcp/const.d.ts +0 -3
- package/dist/mcp/const.d.ts.map +0 -1
- package/dist/mcp/const.js +0 -3
- package/dist/mcp/const.js.map +0 -1
- package/dist/mcp/proxy.d.ts +0 -4
- package/dist/mcp/proxy.d.ts.map +0 -1
- package/dist/mcp/proxy.js +0 -29
- package/dist/mcp/proxy.js.map +0 -1
- package/dist/mcp/server.d.ts +0 -41
- package/dist/mcp/server.d.ts.map +0 -1
- package/dist/mcp/server.js +0 -177
- package/dist/mcp/server.js.map +0 -1
- package/dist/mcp/utils.d.ts +0 -26
- package/dist/mcp/utils.d.ts.map +0 -1
- package/dist/mcp/utils.js +0 -52
- package/dist/mcp/utils.js.map +0 -1
- package/dist/stdio.d.ts +0 -15
- package/dist/stdio.d.ts.map +0 -1
- package/dist/stdio.js +0 -46
- package/dist/stdio.js.map +0 -1
- package/dist/tools/actor.d.ts +0 -40
- package/dist/tools/actor.d.ts.map +0 -1
- package/dist/tools/actor.js +0 -137
- package/dist/tools/actor.js.map +0 -1
- package/dist/tools/build.d.ts +0 -12
- package/dist/tools/build.d.ts.map +0 -1
- package/dist/tools/build.js +0 -120
- package/dist/tools/build.js.map +0 -1
- package/dist/tools/helpers.d.ts +0 -19
- package/dist/tools/helpers.d.ts.map +0 -1
- package/dist/tools/helpers.js +0 -62
- package/dist/tools/helpers.js.map +0 -1
- package/dist/tools/index.d.ts +0 -6
- package/dist/tools/index.d.ts.map +0 -1
- package/dist/tools/index.js +0 -8
- package/dist/tools/index.js.map +0 -1
- package/dist/tools/store_collection.d.ts +0 -21
- package/dist/tools/store_collection.d.ts.map +0 -1
- package/dist/tools/store_collection.js +0 -80
- package/dist/tools/store_collection.js.map +0 -1
- package/dist/tools/utils.d.ts +0 -77
- package/dist/tools/utils.d.ts.map +0 -1
- package/dist/tools/utils.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientStdio.js","sourceRoot":"","sources":["../../src/examples/clientStdio.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAC1E,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"clientStdio.js","sourceRoot":"","sources":["../../src/examples/clientStdio.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,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AC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACnC,CAAC;AACL,CAAC;AAED,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IAClB,OAAO,CAAC,KAAK,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;IACzC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
|
@@ -34,7 +34,7 @@ const MAX_TOKENS = 2048; // Maximum tokens for Claude response
|
|
|
34
34
|
// const CLAUDE_MODEL = 'claude-3-5-haiku-20241022'; // a fastest model
|
|
35
35
|
const CLAUDE_MODEL = 'claude-3-haiku-20240307'; // a fastest and most compact model for near-instant responsiveness
|
|
36
36
|
const DEBUG = true;
|
|
37
|
-
const DEBUG_SERVER_PATH = path.resolve(dirname, '../../dist/
|
|
37
|
+
const DEBUG_SERVER_PATH = path.resolve(dirname, '../../dist/index.js');
|
|
38
38
|
const NODE_PATH = execSync('which node').toString().trim();
|
|
39
39
|
dotenv.config(); // Load environment variables from .env
|
|
40
40
|
class MCPClient {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* This script initializes and starts the Apify MCP server using the Stdio transport.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* node <script_name> --actors=<actor1,actor2,...>
|
|
7
|
+
*
|
|
8
|
+
* Command-line arguments:
|
|
9
|
+
* --actors - A comma-separated list of actor full names to add to the server.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
* node index.js --actors=apify/google-search-scraper,apify/instagram-scraper
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
3
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;GAWG"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* This script initializes and starts the Apify MCP server using the Stdio transport.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* node <script_name> --actors=<actor1,actor2,...>
|
|
7
|
+
*
|
|
8
|
+
* Command-line arguments:
|
|
9
|
+
* --actors - A comma-separated list of actor full names to add to the server.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
* node index.js --actors=apify/google-search-scraper,apify/instagram-scraper
|
|
13
|
+
*/
|
|
14
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
15
|
+
import parser from 'yargs-parser';
|
|
16
|
+
import { log } from './logger.js';
|
|
17
|
+
import { ApifyMcpServer } from './server.js';
|
|
18
|
+
import { getActorDiscoveryTools, getActorAutoLoadingTools } from './tools.js';
|
|
19
|
+
log.setLevel(log.LEVELS.ERROR);
|
|
20
|
+
const argv = parser(process.argv.slice(2), {
|
|
21
|
+
boolean: [
|
|
22
|
+
'enable-adding-actors',
|
|
23
|
+
'enableActorAutoLoading', // deprecated
|
|
24
|
+
],
|
|
25
|
+
string: ['actors'],
|
|
26
|
+
default: {
|
|
27
|
+
'enable-adding-actors': false,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const argEnableAddingActors = argv['enable-adding-actors'] || argv.enableActorAutoLoading || false;
|
|
31
|
+
const argActors = argv.actors?.split(',').map((actor) => actor.trim()) || [];
|
|
32
|
+
if (!process.env.APIFY_TOKEN) {
|
|
33
|
+
log.error('APIFY_TOKEN is required but not set in the environment variables.');
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
async function main() {
|
|
37
|
+
const server = new ApifyMcpServer();
|
|
38
|
+
await (argActors.length !== 0
|
|
39
|
+
? server.addToolsFromActors(argActors)
|
|
40
|
+
: server.addToolsFromDefaultActors());
|
|
41
|
+
server.updateTools(getActorDiscoveryTools());
|
|
42
|
+
if (argEnableAddingActors) {
|
|
43
|
+
server.updateTools(getActorAutoLoadingTools());
|
|
44
|
+
}
|
|
45
|
+
const transport = new StdioServerTransport();
|
|
46
|
+
await server.connect(transport);
|
|
47
|
+
}
|
|
48
|
+
main().catch((error) => {
|
|
49
|
+
console.error('Server error:', error); // eslint-disable-line no-console
|
|
50
|
+
process.exit(1);
|
|
51
|
+
});
|
|
7
52
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAE9E,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAE/B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IACvC,OAAO,EAAE;QACL,sBAAsB;QACtB,wB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,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
package/dist/input.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ import type { Input } from './types.js';
|
|
|
4
4
|
* @param originalInput
|
|
5
5
|
* @returns input
|
|
6
6
|
*/
|
|
7
|
-
export declare function processInput(originalInput: Partial<Input>): Input
|
|
7
|
+
export declare function processInput(originalInput: Partial<Input>): Promise<Input>;
|
|
8
8
|
//# sourceMappingURL=input.d.ts.map
|
package/dist/input.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,aAAa,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAmBhF"}
|
package/dist/input.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
+
import { log } from './logger.js';
|
|
1
2
|
/**
|
|
2
3
|
* Process input parameters, split actors string into an array
|
|
3
4
|
* @param originalInput
|
|
4
5
|
* @returns input
|
|
5
6
|
*/
|
|
6
|
-
export function processInput(originalInput) {
|
|
7
|
+
export async function processInput(originalInput) {
|
|
7
8
|
const input = originalInput;
|
|
8
9
|
// actors can be a string or an array of strings
|
|
9
10
|
if (input.actors && typeof input.actors === 'string') {
|
|
10
11
|
input.actors = input.actors.split(',').map((format) => format.trim());
|
|
11
12
|
}
|
|
12
|
-
//
|
|
13
|
-
if (input.enableActorAutoLoading &&
|
|
14
|
-
|
|
13
|
+
// enableAddingActors is deprecated, use enableActorAutoLoading instead
|
|
14
|
+
if (input.enableActorAutoLoading !== undefined && input.enableAddingActors === undefined) {
|
|
15
|
+
log.warning('enableActorAutoLoading is deprecated, use enableAddingActors instead');
|
|
16
|
+
input.enableAddingActors = input.enableActorAutoLoading;
|
|
15
17
|
}
|
|
16
|
-
if (!input.
|
|
17
|
-
input.
|
|
18
|
+
if (!input.enableAddingActors) {
|
|
19
|
+
input.enableAddingActors = false;
|
|
18
20
|
}
|
|
19
21
|
return input;
|
|
20
22
|
}
|
package/dist/input.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../src/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAGlC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,aAA6B;IAC5D,MAAM,KAAK,GAAG,aAAsB,CAAC;IAErC,gDAAgD;IAChD,IAAI,KAAK,CAAC,MAAM,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;QACnD,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,MAAc,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAa,CAAC;IAC9F,CAAC;IAED,uEAAuE;IACvE,IAAI,KAAK,CAAC,sBAAsB,KAAK,SAAS,IAAI,KAAK,CAAC,kBAAkB,KAAK,SAAS,EAAE,CAAC;QACvF,GAAG,CAAC,OAAO,CAAC,sEAAsE,CAAC,CAAC;QACpF,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC,sBAAsB,CAAC;IAC5D,CAAC;IAED,IAAI,CAAC,KAAK,CAAC,kBAAkB,EAAE,CAAC;QAC5B,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;IACrC,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAI5B,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAE5B,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAE/B,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
package/dist/main.d.ts
CHANGED
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":""}
|
package/dist/main.js
CHANGED
|
@@ -1,37 +1,134 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* This file needs to be named `main.ts` to be recognized by the Apify platform.
|
|
4
|
-
*/
|
|
1
|
+
import { parse } from 'querystring';
|
|
2
|
+
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
5
3
|
import { Actor } from 'apify';
|
|
6
|
-
import
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import { HEADER_READINESS_PROBE, Routes } from './const.js';
|
|
7
6
|
import { processInput } from './input.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { defaults } from './const.js';
|
|
12
|
-
const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY';
|
|
7
|
+
import { log } from './logger.js';
|
|
8
|
+
import { ApifyMcpServer } from './server.js';
|
|
9
|
+
import { getActorDiscoveryTools, getActorAutoLoadingTools } from './tools.js';
|
|
13
10
|
await Actor.init();
|
|
11
|
+
const STANDBY_MODE = Actor.getEnv().metaOrigin === 'STANDBY';
|
|
14
12
|
const HOST = Actor.isAtHome() ? process.env.ACTOR_STANDBY_URL : 'http://localhost';
|
|
15
|
-
const PORT = Actor.isAtHome() ?
|
|
13
|
+
const PORT = Actor.isAtHome() ? process.env.ACTOR_STANDBY_PORT : 3001;
|
|
16
14
|
if (!process.env.APIFY_TOKEN) {
|
|
17
15
|
log.error('APIFY_TOKEN is required but not set in the environment variables.');
|
|
18
16
|
process.exit(1);
|
|
19
17
|
}
|
|
20
|
-
const
|
|
21
|
-
const
|
|
18
|
+
const app = express();
|
|
19
|
+
const mcpServer = new ApifyMcpServer();
|
|
20
|
+
let transport;
|
|
21
|
+
const HELP_MESSAGE = `Connect to the server with GET request to ${HOST}/sse?token=YOUR-APIFY-TOKEN`
|
|
22
|
+
+ ` and then send POST requests to ${HOST}/message?token=YOUR-APIFY-TOKEN`;
|
|
23
|
+
const actorRun = Actor.isAtHome() ? {
|
|
24
|
+
id: process.env.ACTOR_RUN_ID,
|
|
25
|
+
actId: process.env.ACTOR_ID,
|
|
26
|
+
userId: process.env.APIFY_USER_ID,
|
|
27
|
+
startedAt: process.env.ACTOR_STARTED_AT,
|
|
28
|
+
finishedAt: null,
|
|
29
|
+
status: 'RUNNING',
|
|
30
|
+
meta: {
|
|
31
|
+
origin: process.env.APIFY_META_ORIGIN,
|
|
32
|
+
},
|
|
33
|
+
options: {
|
|
34
|
+
build: process.env.ACTOR_BUILD_NUMBER,
|
|
35
|
+
memoryMbytes: process.env.ACTOR_MEMORY_MBYTES,
|
|
36
|
+
},
|
|
37
|
+
buildId: process.env.ACTOR_BUILD_ID,
|
|
38
|
+
defaultKeyValueStoreId: process.env.ACTOR_DEFAULT_KEY_VALUE_STORE_ID,
|
|
39
|
+
defaultDatasetId: process.env.ACTOR_DEFAULT_DATASET_ID,
|
|
40
|
+
defaultRequestQueueId: process.env.ACTOR_DEFAULT_REQUEST_QUEUE_ID,
|
|
41
|
+
buildNumber: process.env.ACTOR_BUILD_NUMBER,
|
|
42
|
+
containerUrl: process.env.ACTOR_WEB_SERVER_URL,
|
|
43
|
+
standbyUrl: process.env.ACTOR_STANDBY_URL,
|
|
44
|
+
} : {};
|
|
45
|
+
/**
|
|
46
|
+
* Process input parameters and update tools.
|
|
47
|
+
* If URL contains query parameter `actors`, add Actors as tools.
|
|
48
|
+
* Otherwise, add default Actors but only if there are no tools added yet.
|
|
49
|
+
* @param url
|
|
50
|
+
*/
|
|
51
|
+
async function processParamsAndUpdateTools(url) {
|
|
52
|
+
const params = parse(url.split('?')[1] || '');
|
|
53
|
+
delete params.token;
|
|
54
|
+
log.debug(`Received input parameters: ${JSON.stringify(params)}`);
|
|
55
|
+
const input = await processInput(params);
|
|
56
|
+
if (input.actors) {
|
|
57
|
+
await mcpServer.addToolsFromActors(input.actors);
|
|
58
|
+
}
|
|
59
|
+
else if (mcpServer.getToolNames().length === 0) {
|
|
60
|
+
await mcpServer.addToolsFromDefaultActors();
|
|
61
|
+
}
|
|
62
|
+
if (input.enableAddingActors) {
|
|
63
|
+
mcpServer.updateTools(getActorAutoLoadingTools());
|
|
64
|
+
}
|
|
65
|
+
log.debug(`Server is running in STANDBY mode with the following Actors (tools): ${mcpServer.getToolNames()}.
|
|
66
|
+
To use different Actors, provide them in query parameter "actors" or include them in the Actor Task input.`);
|
|
67
|
+
}
|
|
68
|
+
app.route(Routes.ROOT)
|
|
69
|
+
.get(async (req, res) => {
|
|
70
|
+
if (req.headers && req.get(HEADER_READINESS_PROBE) !== undefined) {
|
|
71
|
+
log.debug('Received readiness probe');
|
|
72
|
+
res.status(200).json({ message: 'Server is ready' }).end();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
log.info(`Received GET message at: ${Routes.ROOT}`);
|
|
77
|
+
await processParamsAndUpdateTools(req.url);
|
|
78
|
+
res.status(200).json({ message: `Actor is using Model Context Protocol. ${HELP_MESSAGE}`, data: actorRun }).end();
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
log.error(`Error in GET ${Routes.ROOT} ${error}`);
|
|
82
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
.head((_req, res) => {
|
|
86
|
+
res.status(200).end();
|
|
87
|
+
});
|
|
88
|
+
app.route(Routes.SSE)
|
|
89
|
+
.get(async (req, res) => {
|
|
90
|
+
try {
|
|
91
|
+
log.info(`Received GET message at: ${Routes.SSE}`);
|
|
92
|
+
await processParamsAndUpdateTools(req.url);
|
|
93
|
+
transport = new SSEServerTransport(Routes.MESSAGE, res);
|
|
94
|
+
await mcpServer.connect(transport);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
log.error(`Error in GET ${Routes.SSE}: ${error}`);
|
|
98
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
app.route(Routes.MESSAGE)
|
|
102
|
+
.post(async (req, res) => {
|
|
103
|
+
try {
|
|
104
|
+
log.info(`Received POST message at: ${Routes.MESSAGE}`);
|
|
105
|
+
if (transport) {
|
|
106
|
+
await transport.handlePostMessage(req, res);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
res.status(400).json({
|
|
110
|
+
message: 'Server is not connected to the client. '
|
|
111
|
+
+ 'Connect to the server with GET request to /sse endpoint',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
log.error(`Error in POST ${Routes.MESSAGE}: ${error}`);
|
|
117
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
// Catch-all for undefined routes
|
|
121
|
+
app.use((req, res) => {
|
|
122
|
+
res.status(404).json({ message: `There is nothing at route ${req.method} ${req.originalUrl}. ${HELP_MESSAGE}` }).end();
|
|
123
|
+
});
|
|
124
|
+
const input = await processInput((await Actor.getInput()) ?? {});
|
|
22
125
|
log.info(`Loaded input: ${JSON.stringify(input)} `);
|
|
23
126
|
if (STANDBY_MODE) {
|
|
24
|
-
const app = createExpressApp(HOST, mcpServer);
|
|
25
127
|
log.info('Actor is running in the STANDBY mode.');
|
|
26
|
-
|
|
27
|
-
if (input.
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
const actors = input.actors ?? defaults.actors;
|
|
31
|
-
const actorsToLoad = Array.isArray(actors) ? actors : actors.split(',');
|
|
32
|
-
const actorTools = await getActorsAsTools(actorsToLoad, process.env.APIFY_TOKEN);
|
|
33
|
-
tools.push(...actorTools);
|
|
34
|
-
mcpServer.updateTools(tools);
|
|
128
|
+
mcpServer.updateTools(getActorDiscoveryTools());
|
|
129
|
+
if (input.enableAddingActors) {
|
|
130
|
+
mcpServer.updateTools(getActorAutoLoadingTools());
|
|
131
|
+
}
|
|
35
132
|
app.listen(PORT, () => {
|
|
36
133
|
log.info(`The Actor web server is listening for user requests at ${HOST}`);
|
|
37
134
|
});
|
|
@@ -39,12 +136,10 @@ if (STANDBY_MODE) {
|
|
|
39
136
|
else {
|
|
40
137
|
log.info('Actor is not designed to run in the NORMAL model (use this mode only for debugging purposes)');
|
|
41
138
|
if (input && !input.debugActor && !input.debugActorInput) {
|
|
42
|
-
await Actor.fail('If you need to debug a specific
|
|
139
|
+
await Actor.fail('If you need to debug a specific actor, please provide the debugActor and debugActorInput fields in the input');
|
|
43
140
|
}
|
|
44
141
|
const options = { memory: input.maxActorMemoryBytes };
|
|
45
|
-
|
|
46
|
-
await Actor.pushData(items);
|
|
47
|
-
log.info(`Pushed ${items.length} items to the dataset`);
|
|
142
|
+
await mcpServer.callActorGetDataset(input.debugActor, input.debugActorInput, options);
|
|
48
143
|
await Actor.exit();
|
|
49
144
|
}
|
|
50
145
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AACA
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAG9B,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAG9E,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;AAEnB,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,UAAU,KAAK,SAAS,CAAC;AAC7D,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,kBAAkB,CAAC;AACnF,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC;AAEtE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;IAC3B,GAAG,CAAC,KAAK,CAAC,mEAAmE,CAAC,CAAC;IAC/E,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC;AAED,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AAEtB,MAAM,SAA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AAC;AAEpD,IAAI,YAAY,EAAE,CAAC;IACf,GAAG,CAAC,IAAI,CAAC,uCAAuC,CAAC,CAAC;IAClD,SAAS,CAAC,WAAW,CAAC,sBAAsB,EAAE,CAAC,CAAC;IAChD,IAAI,KAAK,CAAC,kBAAkB,EAAE,CAAC;QAC3B,SAAS,CAAC,WAAW,CAAC,wBAAwB,EAAE,CAAC,CAAC;IACtD,CAAC;IACD,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;QAClB,GAAG,CAAC,IAAI,CAAC,0DAA0D,IAAI,EAAE,CAAC,CAAC;IAC/E,CAAC,CAAC,CAAC;AACP,CAAC;KAAM,CAAC;IACJ,GAAG,CAAC,IAAI,CAAC,8FAA8F,CAAC,CAAC;IAEzG,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,UAAU,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;QACvD,MAAM,KAAK,CAAC,IAAI,CAAC,8GAA8G,CAAC,CAAC;IACrI,CAAC;IACD,MAAM,OAAO,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,mBAAmB,EAAsB,CAAC;IAC1E,MAAM,SAAS,CAAC,mBAAmB,CAAC,KAAK,CAAC,UAAW,EAAE,KAAK,CAAC,eAAgB,EAAE,OAAO,CAAC,CAAC;IACxF,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;AACvB,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
3
|
+
import type { ActorCallOptions } from 'apify-client';
|
|
4
|
+
import type { Tool } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Create Apify MCP server
|
|
7
|
+
*/
|
|
8
|
+
export declare class ApifyMcpServer {
|
|
9
|
+
private server;
|
|
10
|
+
private tools;
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Adds a User-Agent header to the request config.
|
|
14
|
+
* @param config
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
private addUserAgent;
|
|
18
|
+
/**
|
|
19
|
+
* Calls an Apify actor and retrieves the dataset items.
|
|
20
|
+
*
|
|
21
|
+
* It requires the `APIFY_TOKEN` environment variable to be set.
|
|
22
|
+
* If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} actorName - The name of the actor to call.
|
|
25
|
+
* @param {ActorCallOptions} callOptions - The options to pass to the actor.
|
|
26
|
+
* @param {unknown} input - The input to pass to the actor.
|
|
27
|
+
* @returns {Promise<object[]>} - A promise that resolves to an array of dataset items.
|
|
28
|
+
* @throws {Error} - Throws an error if the `APIFY_TOKEN` is not set
|
|
29
|
+
*/
|
|
30
|
+
callActorGetDataset(actorName: string, input: unknown, callOptions?: ActorCallOptions | undefined): Promise<object[]>;
|
|
31
|
+
addToolsFromActors(actors: string[]): Promise<Tool[]>;
|
|
32
|
+
addToolsFromDefaultActors(): Promise<void>;
|
|
33
|
+
updateTools(tools: Tool[]): void;
|
|
34
|
+
/**
|
|
35
|
+
* Returns an array of tool names.
|
|
36
|
+
* @returns {string[]} - An array of tool names.
|
|
37
|
+
*/
|
|
38
|
+
getToolNames(): string[];
|
|
39
|
+
private setupErrorHandling;
|
|
40
|
+
private setupToolHandlers;
|
|
41
|
+
connect(transport: Transport): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAI/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AA2BrD,OAAO,KAAK,EAAqB,IAAI,EAAE,MAAM,YAAY,CAAC;AAE1D;;GAEG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAoB;;IAmBjC;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAOpB;;;;;;;;;;;OAWG;IACU,mBAAmB,CAC5B,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,EACd,WAAW,GAAE,gBAAgB,GAAG,SAAqB,GACtD,OAAO,CAAC,MAAM,EAAE,CAAC;IAwBP,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE;IAMnC,yBAAyB;IAI/B,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI;IAOvC;;;OAGG;IACI,YAAY,IAAI,MAAM,EAAE;IAI/B,OAAO,CAAC,kBAAkB;IAU1B,OAAO,CAAC,iBAAiB;IA2EnB,OAAO,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAGrD"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
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 { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { Actor } from 'apify';
|
|
8
|
+
import { ApifyClient } from 'apify-client';
|
|
9
|
+
import { filterSchemaProperties, getActorDefinition, getActorsAsTools, shortenProperties, } from './actors.js';
|
|
10
|
+
import { ACTOR_OUTPUT_MAX_CHARS_PER_ITEM, ACTOR_OUTPUT_TRUNCATED_MESSAGE, defaults, InternalTools, SERVER_NAME, SERVER_VERSION, USER_AGENT_ORIGIN, } from './const.js';
|
|
11
|
+
import { log } from './logger.js';
|
|
12
|
+
import { RemoveActorToolArgsSchema, AddActorToToolsArgsSchema, DiscoverActorsArgsSchema, searchActorsByKeywords, GetActorDefinition, } from './tools.js';
|
|
13
|
+
/**
|
|
14
|
+
* Create Apify MCP server
|
|
15
|
+
*/
|
|
16
|
+
export class ApifyMcpServer {
|
|
17
|
+
server;
|
|
18
|
+
tools;
|
|
19
|
+
constructor() {
|
|
20
|
+
this.server = new Server({
|
|
21
|
+
name: SERVER_NAME,
|
|
22
|
+
version: SERVER_VERSION,
|
|
23
|
+
}, {
|
|
24
|
+
capabilities: {
|
|
25
|
+
tools: { listChanged: true },
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
this.tools = new Map();
|
|
29
|
+
this.setupErrorHandling();
|
|
30
|
+
this.setupToolHandlers();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adds a User-Agent header to the request config.
|
|
34
|
+
* @param config
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
addUserAgent(config) {
|
|
38
|
+
const updatedConfig = { ...config };
|
|
39
|
+
updatedConfig.headers = updatedConfig.headers ?? {};
|
|
40
|
+
updatedConfig.headers['User-Agent'] = `${updatedConfig.headers['User-Agent'] ?? ''}; ${USER_AGENT_ORIGIN}`;
|
|
41
|
+
return updatedConfig;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Calls an Apify actor and retrieves the dataset items.
|
|
45
|
+
*
|
|
46
|
+
* It requires the `APIFY_TOKEN` environment variable to be set.
|
|
47
|
+
* If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} actorName - The name of the actor to call.
|
|
50
|
+
* @param {ActorCallOptions} callOptions - The options to pass to the actor.
|
|
51
|
+
* @param {unknown} input - The input to pass to the actor.
|
|
52
|
+
* @returns {Promise<object[]>} - A promise that resolves to an array of dataset items.
|
|
53
|
+
* @throws {Error} - Throws an error if the `APIFY_TOKEN` is not set
|
|
54
|
+
*/
|
|
55
|
+
async callActorGetDataset(actorName, input, callOptions = undefined) {
|
|
56
|
+
const name = actorName;
|
|
57
|
+
try {
|
|
58
|
+
log.info(`Calling actor ${name} with input: ${JSON.stringify(input)}`);
|
|
59
|
+
const options = { requestInterceptors: [this.addUserAgent] };
|
|
60
|
+
const client = new ApifyClient({ ...options, token: process.env.APIFY_TOKEN });
|
|
61
|
+
const actorClient = client.actor(name);
|
|
62
|
+
const results = await actorClient.call(input, callOptions);
|
|
63
|
+
const dataset = await client.dataset(results.defaultDatasetId).listItems();
|
|
64
|
+
log.info(`Actor ${name} finished with ${dataset.items.length} items`);
|
|
65
|
+
if (process.env.APIFY_IS_AT_HOME) {
|
|
66
|
+
await Actor.pushData(dataset.items);
|
|
67
|
+
log.info(`Pushed ${dataset.items.length} items to the dataset`);
|
|
68
|
+
}
|
|
69
|
+
return dataset.items;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
log.error(`Error calling actor: ${error}. Actor: ${name}, input: ${JSON.stringify(input)}`);
|
|
73
|
+
throw new Error(`Error calling actor: ${error}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async addToolsFromActors(actors) {
|
|
77
|
+
const tools = await getActorsAsTools(actors);
|
|
78
|
+
this.updateTools(tools);
|
|
79
|
+
return tools;
|
|
80
|
+
}
|
|
81
|
+
async addToolsFromDefaultActors() {
|
|
82
|
+
await this.addToolsFromActors(defaults.actors);
|
|
83
|
+
}
|
|
84
|
+
updateTools(tools) {
|
|
85
|
+
for (const tool of tools) {
|
|
86
|
+
this.tools.set(tool.name, tool);
|
|
87
|
+
log.info(`Added/Updated tool: ${tool.actorFullName} (tool: ${tool.name})`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns an array of tool names.
|
|
92
|
+
* @returns {string[]} - An array of tool names.
|
|
93
|
+
*/
|
|
94
|
+
getToolNames() {
|
|
95
|
+
return Array.from(this.tools.keys());
|
|
96
|
+
}
|
|
97
|
+
setupErrorHandling() {
|
|
98
|
+
this.server.onerror = (error) => {
|
|
99
|
+
console.error('[MCP Error]', error); // eslint-disable-line no-console
|
|
100
|
+
};
|
|
101
|
+
process.on('SIGINT', async () => {
|
|
102
|
+
await this.server.close();
|
|
103
|
+
process.exit(0);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
setupToolHandlers() {
|
|
107
|
+
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
108
|
+
return { tools: Array.from(this.tools.values()) };
|
|
109
|
+
});
|
|
110
|
+
/**
|
|
111
|
+
* Handles the request to call a tool.
|
|
112
|
+
* @param {object} request - The request object containing tool name and arguments.
|
|
113
|
+
* @throws {Error} - Throws an error if the tool is unknown or arguments are invalid.
|
|
114
|
+
*/
|
|
115
|
+
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
116
|
+
const { name, arguments: args } = request.params;
|
|
117
|
+
const tool = Array.from(this.tools.values()).find((t) => t.name === name || t.actorFullName === name);
|
|
118
|
+
if (!tool) {
|
|
119
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
120
|
+
}
|
|
121
|
+
if (!args) {
|
|
122
|
+
throw new Error(`Missing arguments for tool: ${name}`);
|
|
123
|
+
}
|
|
124
|
+
log.info(`Validate arguments for tool: ${tool.name} with arguments: ${JSON.stringify(args)}`);
|
|
125
|
+
if (!tool.ajvValidate(args)) {
|
|
126
|
+
throw new Error(`Invalid arguments for tool ${tool.name}: args: ${JSON.stringify(args)} error: ${JSON.stringify(tool?.ajvValidate.errors)}`);
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
switch (name) {
|
|
130
|
+
case InternalTools.ADD_ACTOR_TO_TOOLS: {
|
|
131
|
+
const parsed = AddActorToToolsArgsSchema.parse(args);
|
|
132
|
+
const toolsAdded = await this.addToolsFromActors([parsed.actorName]);
|
|
133
|
+
await this.server.notification({ method: 'notifications/tools/list_changed' });
|
|
134
|
+
return { content: [{ type: 'text', text: `Actor added: ${toolsAdded.map((t) => `${t.actorFullName} (tool name: ${t.name})`).join(', ')}` }] };
|
|
135
|
+
}
|
|
136
|
+
case InternalTools.REMOVE_ACTOR_FROM_TOOLS: {
|
|
137
|
+
const parsed = RemoveActorToolArgsSchema.parse(args);
|
|
138
|
+
this.tools.delete(parsed.toolName);
|
|
139
|
+
await this.server.notification({ method: 'notifications/tools/list_changed' });
|
|
140
|
+
return { content: [{ type: 'text', text: `Tool ${parsed.toolName} was removed` }] };
|
|
141
|
+
}
|
|
142
|
+
case InternalTools.DISCOVER_ACTORS: {
|
|
143
|
+
const parsed = DiscoverActorsArgsSchema.parse(args);
|
|
144
|
+
const actors = await searchActorsByKeywords(parsed.search, parsed.limit, parsed.offset);
|
|
145
|
+
return { content: actors?.map((item) => ({ type: 'text', text: JSON.stringify(item) })) };
|
|
146
|
+
}
|
|
147
|
+
case InternalTools.GET_ACTOR_DETAILS: {
|
|
148
|
+
const parsed = GetActorDefinition.parse(args);
|
|
149
|
+
const v = await getActorDefinition(parsed.actorName, parsed.limit);
|
|
150
|
+
if (v && v.input && 'properties' in v.input && v.input) {
|
|
151
|
+
const properties = filterSchemaProperties(v.input.properties);
|
|
152
|
+
v.input.properties = shortenProperties(properties);
|
|
153
|
+
}
|
|
154
|
+
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
|
|
155
|
+
}
|
|
156
|
+
default: {
|
|
157
|
+
const items = await this.callActorGetDataset(tool.actorFullName, args, { memory: tool.memoryMbytes });
|
|
158
|
+
const content = items.map((item) => {
|
|
159
|
+
const text = JSON.stringify(item).slice(0, ACTOR_OUTPUT_MAX_CHARS_PER_ITEM);
|
|
160
|
+
return text.length === ACTOR_OUTPUT_MAX_CHARS_PER_ITEM
|
|
161
|
+
? { type: 'text', text: `${text} ... ${ACTOR_OUTPUT_TRUNCATED_MESSAGE}` }
|
|
162
|
+
: { type: 'text', text };
|
|
163
|
+
});
|
|
164
|
+
return { content };
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
log.error(`Error calling tool: ${error}`);
|
|
170
|
+
throw new Error(`Error calling tool: ${error}`);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async connect(transport) {
|
|
175
|
+
await this.server.connect(transport);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=server.js.map
|