@apify/actors-mcp-server 0.1.1-beta.0 → 0.1.1-beta.1
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/LICENSE.md +201 -0
- package/README.md +2 -0
- package/package.json +4 -2
- package/src/actorDefinition.ts +87 -0
- package/src/const.ts +20 -0
- package/src/examples/clientSse.ts +100 -0
- package/src/examples/clientStdio.ts +85 -0
- package/src/examples/clientStdioChat.ts +227 -0
- package/src/examples/client_sse.py +48 -0
- package/src/index.ts +34 -0
- package/src/input.ts +16 -0
- package/src/logger.ts +5 -0
- package/src/main.ts +115 -0
- package/src/server.ts +153 -0
- package/src/types.ts +20 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 Apify Technologies s.r.o.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Apify Model Context Protocol (MCP) Server
|
|
2
2
|
|
|
3
|
+
[](https://apify.com/apify/actors-mcp-server)
|
|
4
|
+
|
|
3
5
|
Implementation of an MCP server for all [Apify Actors](https://apify.com/store).
|
|
4
6
|
This server enables interaction with one or more Apify Actors that can be defined in the MCP Server configuration.
|
|
5
7
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apify/actors-mcp-server",
|
|
3
|
-
"version": "0.1.1-beta.
|
|
3
|
+
"version": "0.1.1-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Model Context Protocol Server for Apify Actors",
|
|
6
6
|
"engines": {
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
"actors-mcp-server": "dist/index.js"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
|
-
"dist"
|
|
14
|
+
"dist/**",
|
|
15
|
+
"src/**",
|
|
16
|
+
"LICENSE"
|
|
15
17
|
],
|
|
16
18
|
"repository": {
|
|
17
19
|
"type": "git",
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import { ApifyClient } from 'apify-client';
|
|
3
|
+
|
|
4
|
+
import { log } from './logger.js';
|
|
5
|
+
import type { ActorDefinitionWithDesc, Tool } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get actor input schema by actor name.
|
|
9
|
+
* First, fetch the actor details to get the default build tag and buildId.
|
|
10
|
+
* Then, fetch the build details and return actorName, description, and input schema.
|
|
11
|
+
* @param {string} actorFullName - The full name of the actor.
|
|
12
|
+
* @returns {Promise<ActorDefinitionWithDesc | null>} - The actor definition with description or null if not found.
|
|
13
|
+
*/
|
|
14
|
+
async function fetchActorDefinition(actorFullName: string): Promise<ActorDefinitionWithDesc | null> {
|
|
15
|
+
if (!process.env.APIFY_TOKEN) {
|
|
16
|
+
log.error('APIFY_TOKEN is required but not set. Please set it as an environment variable');
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
|
|
20
|
+
const actorClient = client.actor(actorFullName);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
// Fetch actor details
|
|
24
|
+
const actor = await actorClient.get();
|
|
25
|
+
if (!actor) {
|
|
26
|
+
log.error(`Failed to fetch input schema for actor: ${actorFullName}. Actor not found.`);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// fnesveda: The default build is not necessarily tagged, you can specify any build number as default build.
|
|
31
|
+
// There will be a new API endpoint to fetch a default build.
|
|
32
|
+
// For now, we'll use the tagged build, it will work for 90% of Actors. Later, we can update this.
|
|
33
|
+
const tag = actor.defaultRunOptions?.build || '';
|
|
34
|
+
const buildId = actor.taggedBuilds?.[tag]?.buildId || '';
|
|
35
|
+
|
|
36
|
+
if (!buildId) {
|
|
37
|
+
log.error(`Failed to fetch input schema for actor: ${actorFullName}. Build ID not found.`);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
// Fetch build details and return the input schema
|
|
41
|
+
const buildDetails = await client.build(buildId).get();
|
|
42
|
+
if (buildDetails?.actorDefinition) {
|
|
43
|
+
const actorDefinitions = buildDetails?.actorDefinition as ActorDefinitionWithDesc;
|
|
44
|
+
actorDefinitions.description = actor.description || '';
|
|
45
|
+
actorDefinitions.name = actorFullName;
|
|
46
|
+
return actorDefinitions;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
log.error(`Failed to fetch input schema for actor: ${actorFullName} with error ${error}.`);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Fetches actor input schemas by actor full names and creates MCP tools.
|
|
57
|
+
*
|
|
58
|
+
* This function retrieves the input schemas for the specified actors and compiles them into MCP tools.
|
|
59
|
+
* It uses the AJV library to validate the input schemas.
|
|
60
|
+
*
|
|
61
|
+
* Tool name can't contain /, so it is replaced with _
|
|
62
|
+
*
|
|
63
|
+
* @param {string[]} actors - An array of actor full names.
|
|
64
|
+
* @returns {Promise<Tool[]>} - A promise that resolves to an array of MCP tools.
|
|
65
|
+
*/
|
|
66
|
+
export async function getActorsAsTools(actors: string[]): Promise<Tool[]> {
|
|
67
|
+
// Fetch input schemas in parallel
|
|
68
|
+
const ajv = new Ajv({ coerceTypes: 'array', strict: false });
|
|
69
|
+
const results = await Promise.all(actors.map(fetchActorDefinition));
|
|
70
|
+
const tools = [];
|
|
71
|
+
for (const result of results) {
|
|
72
|
+
if (result) {
|
|
73
|
+
try {
|
|
74
|
+
tools.push({
|
|
75
|
+
name: result.name.replace('/', '_'),
|
|
76
|
+
actorName: result.name,
|
|
77
|
+
description: result.description,
|
|
78
|
+
inputSchema: result.input || {},
|
|
79
|
+
ajvValidate: ajv.compile(result.input || {}),
|
|
80
|
+
});
|
|
81
|
+
} catch (validationError) {
|
|
82
|
+
log.error(`Failed to compile AJV schema for actor: ${result.name}. Error: ${validationError}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return tools;
|
|
87
|
+
}
|
package/src/const.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const SERVER_NAME = 'apify-mcp-server';
|
|
2
|
+
export const SERVER_VERSION = '0.1.0';
|
|
3
|
+
|
|
4
|
+
export const defaults = {
|
|
5
|
+
actors: [
|
|
6
|
+
'apify/instagram-scraper',
|
|
7
|
+
'apify/rag-web-browser',
|
|
8
|
+
'lukaskrivka/google-maps-with-contact-details',
|
|
9
|
+
],
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const ACTOR_OUTPUT_MAX_CHARS_PER_ITEM = 2_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
|
+
|
|
16
|
+
export enum Routes {
|
|
17
|
+
ROOT = '/',
|
|
18
|
+
SSE = '/sse',
|
|
19
|
+
MESSAGE = '/message',
|
|
20
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
/**
|
|
3
|
+
* Connect to the MCP server using SSE transport and call a tool.
|
|
4
|
+
* The Actors MCP Server will load default Actors.
|
|
5
|
+
*
|
|
6
|
+
* !!! This example needs to be fixed as it does not work !!!
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
|
|
12
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
13
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
14
|
+
import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
15
|
+
import dotenv from 'dotenv';
|
|
16
|
+
import { EventSource } from 'eventsource';
|
|
17
|
+
|
|
18
|
+
// Resolve dirname equivalent in ES module
|
|
19
|
+
const filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const dirname = path.dirname(filename);
|
|
21
|
+
|
|
22
|
+
dotenv.config({ path: path.resolve(dirname, '../../.env') });
|
|
23
|
+
|
|
24
|
+
const SERVER_URL = 'https://actors-mcp-server/sse';
|
|
25
|
+
// We need to change forward slash / to underscore _ in the tool name as Anthropic does not allow forward slashes in the tool name
|
|
26
|
+
const SELECTED_TOOL = 'apify_rag-web-browser';
|
|
27
|
+
|
|
28
|
+
if (!process.env.APIFY_TOKEN) {
|
|
29
|
+
console.error('APIFY_TOKEN is required but not set in the environment variables.');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof globalThis.EventSource === 'undefined') {
|
|
34
|
+
globalThis.EventSource = EventSource as unknown as typeof globalThis.EventSource;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function main(): Promise<void> {
|
|
38
|
+
const transport = new SSEClientTransport(
|
|
39
|
+
new URL(SERVER_URL),
|
|
40
|
+
{
|
|
41
|
+
requestInit: {
|
|
42
|
+
headers: {
|
|
43
|
+
authorization: `Bearer ${process.env.APIFY_TOKEN}`,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
eventSourceInit: {
|
|
47
|
+
// The EventSource package augments EventSourceInit with a "fetch" parameter.
|
|
48
|
+
// You can use this to set additional headers on the outgoing request.
|
|
49
|
+
// Based on this example: https://github.com/modelcontextprotocol/typescript-sdk/issues/118
|
|
50
|
+
async fetch(input: Request | URL | string, init?: RequestInit) {
|
|
51
|
+
const headers = new Headers(init?.headers || {});
|
|
52
|
+
headers.set('authorization', `Bearer ${process.env.APIFY_TOKEN}`);
|
|
53
|
+
return fetch(input, { ...init, headers });
|
|
54
|
+
},
|
|
55
|
+
// We have to cast to "any" to use it, since it's non-standard
|
|
56
|
+
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
const client = new Client(
|
|
60
|
+
{ name: 'example-client', version: '1.0.0' },
|
|
61
|
+
{ capabilities: {} },
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
// Connect to the MCP server
|
|
66
|
+
await client.connect(transport);
|
|
67
|
+
|
|
68
|
+
// List available tools
|
|
69
|
+
const tools = await client.listTools();
|
|
70
|
+
console.log('Available tools:', tools);
|
|
71
|
+
|
|
72
|
+
if (tools.tools.length === 0) {
|
|
73
|
+
console.log('No tools available');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const selectedTool = tools.tools.find((tool) => tool.name === SELECTED_TOOL);
|
|
78
|
+
if (!selectedTool) {
|
|
79
|
+
console.error(`The specified tool: ${selectedTool} is not available. Exiting.`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Call a tool
|
|
84
|
+
console.log('Calling actor ...');
|
|
85
|
+
const result = await client.callTool(
|
|
86
|
+
{ name: SELECTED_TOOL, arguments: { query: 'web browser for Anthropic' } },
|
|
87
|
+
CallToolResultSchema,
|
|
88
|
+
);
|
|
89
|
+
console.log('Tool result:', JSON.stringify(result, null, 2));
|
|
90
|
+
} catch (error: unknown) {
|
|
91
|
+
if (error instanceof Error) {
|
|
92
|
+
console.error('Error:', error.message);
|
|
93
|
+
console.error(error.stack);
|
|
94
|
+
} else {
|
|
95
|
+
console.error('An unknown error occurred:', error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
await main();
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
/**
|
|
3
|
+
* Connect to the MCP server using stdio transport and call a tool.
|
|
4
|
+
* This script uses a selected tool without LLM involvement.
|
|
5
|
+
* You need to provide the path to the MCP server and `APIFY_TOKEN` in the `.env` file.
|
|
6
|
+
* You can choose actors to run in the server, for example: `apify/rag-web-browser`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { execSync } from 'child_process';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import { fileURLToPath } from 'url';
|
|
12
|
+
|
|
13
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
14
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
15
|
+
import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
16
|
+
import dotenv from 'dotenv';
|
|
17
|
+
|
|
18
|
+
// Resolve dirname equivalent in ES module
|
|
19
|
+
const filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const dirname = path.dirname(filename);
|
|
21
|
+
|
|
22
|
+
dotenv.config({ path: path.resolve(dirname, '../../.env') });
|
|
23
|
+
const SERVER_PATH = path.resolve(dirname, '../../dist/index.js');
|
|
24
|
+
const NODE_PATH = execSync(process.platform === 'win32' ? 'where node' : 'which node').toString().trim();
|
|
25
|
+
|
|
26
|
+
const TOOLS = 'apify/rag-web-browser,lukaskrivka/google-maps-with-contact-details';
|
|
27
|
+
const SELECTED_TOOL = 'apify_rag-web-browser'; // We need to change / to _ in the tool name
|
|
28
|
+
|
|
29
|
+
if (!process.env.APIFY_TOKEN) {
|
|
30
|
+
console.error('APIFY_TOKEN is required but not set in the environment variables.');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Create server parameters for stdio connection
|
|
35
|
+
const transport = new StdioClientTransport({
|
|
36
|
+
command: NODE_PATH,
|
|
37
|
+
args: [SERVER_PATH, '--actors', TOOLS],
|
|
38
|
+
env: { APIFY_TOKEN: process.env.APIFY_TOKEN || '' },
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Create a new client instance
|
|
42
|
+
const client = new Client(
|
|
43
|
+
{ name: 'example-client', version: '0.1.0' },
|
|
44
|
+
{ capabilities: {} },
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Main function to run the example client
|
|
48
|
+
async function run() {
|
|
49
|
+
try {
|
|
50
|
+
// Connect to the MCP server
|
|
51
|
+
await client.connect(transport);
|
|
52
|
+
|
|
53
|
+
// List available tools
|
|
54
|
+
const tools = await client.listTools();
|
|
55
|
+
console.log('Available tools:', tools);
|
|
56
|
+
|
|
57
|
+
if (tools.tools.length === 0) {
|
|
58
|
+
console.log('No tools available');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Example: Call the first available tool
|
|
63
|
+
const selectedTool = tools.tools.find((tool) => tool.name === SELECTED_TOOL);
|
|
64
|
+
|
|
65
|
+
if (!selectedTool) {
|
|
66
|
+
console.error(`The specified tool: ${selectedTool} is not available. Exiting.`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Call a tool
|
|
71
|
+
console.log('Calling actor ...');
|
|
72
|
+
const result = await client.callTool(
|
|
73
|
+
{ name: SELECTED_TOOL, arguments: { query: 'web browser for Anthropic' } },
|
|
74
|
+
CallToolResultSchema,
|
|
75
|
+
);
|
|
76
|
+
console.log('Tool result:', JSON.stringify(result));
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error('Error:', error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
run().catch((error) => {
|
|
83
|
+
console.error('Unhandled error:', error);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
});
|
|
@@ -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
package/src/main.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
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.route(Routes.ROOT)
|
|
48
|
+
.get(async (req: Request, res: Response) => {
|
|
49
|
+
try {
|
|
50
|
+
log.info(`Received GET message at: ${req.url}`);
|
|
51
|
+
await processParamsAndUpdateTools(req.url);
|
|
52
|
+
res.status(200).json({ message: `Actor is using Model Context Protocol. ${HELP_MESSAGE}` }).end();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
log.error(`Error in GET ${Routes.ROOT} ${error}`);
|
|
55
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
.head((_req: Request, res: Response) => {
|
|
59
|
+
res.status(200).end();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
app.route(Routes.SSE)
|
|
63
|
+
.get(async (req: Request, res: Response) => {
|
|
64
|
+
try {
|
|
65
|
+
log.info(`Received GET message at: ${req.url}`);
|
|
66
|
+
await processParamsAndUpdateTools(req.url);
|
|
67
|
+
transport = new SSEServerTransport(Routes.MESSAGE, res);
|
|
68
|
+
await mcpServer.connect(transport);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
log.error(`Error in GET ${Routes.SSE}: ${error}`);
|
|
71
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
app.route(Routes.MESSAGE)
|
|
76
|
+
.post(async (req: Request, res: Response) => {
|
|
77
|
+
try {
|
|
78
|
+
log.info(`Received POST message at: ${req.url}`);
|
|
79
|
+
if (transport) {
|
|
80
|
+
await transport.handlePostMessage(req, res);
|
|
81
|
+
} else {
|
|
82
|
+
res.status(400).json({
|
|
83
|
+
message: 'Server is not connected to the client. '
|
|
84
|
+
+ 'Connect to the server with GET request to /sse endpoint',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
log.error(`Error in POST ${Routes.MESSAGE}: ${error}`);
|
|
89
|
+
res.status(500).json({ message: 'Internal Server Error' }).end();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Catch-all for undefined routes
|
|
94
|
+
app.use((req: Request, res: Response) => {
|
|
95
|
+
res.status(404).json({ message: `There is nothing at route ${req.method} ${req.originalUrl}. ${HELP_MESSAGE}` }).end();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const input = await processInput((await Actor.getInput<Partial<Input>>()) ?? ({} as Input));
|
|
99
|
+
log.info(`Loaded input: ${JSON.stringify(input)} `);
|
|
100
|
+
|
|
101
|
+
if (STANDBY_MODE) {
|
|
102
|
+
log.info('Actor is running in the STANDBY mode.');
|
|
103
|
+
await mcpServer.addToolsFromDefaultActors();
|
|
104
|
+
app.listen(PORT, () => {
|
|
105
|
+
log.info(`The Actor web server is listening for user requests at ${HOST}`);
|
|
106
|
+
});
|
|
107
|
+
} else {
|
|
108
|
+
log.info('Actor is not designed to run in the NORMAL model (use this mode only for debugging purposes)');
|
|
109
|
+
|
|
110
|
+
if (input && !input.debugActor && !input.debugActorInput) {
|
|
111
|
+
await Actor.fail('If you need to debug a specific actor, please provide the debugActor and debugActorInput fields in the input');
|
|
112
|
+
}
|
|
113
|
+
await mcpServer.callActorGetDataset(input.debugActor!, input.debugActorInput!);
|
|
114
|
+
await Actor.exit();
|
|
115
|
+
}
|
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
|
+
}
|