@mambalabsdev/mcp-gtm-suite 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/build/index.js +143 -34
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -24,32 +24,58 @@ function compact(obj) {
24
24
  function boolToString(v) {
25
25
  return v === undefined ? undefined : v ? "true" : "false";
26
26
  }
27
- // Shared caller. actorPath is the actor's immutable Apify actor ID (a stable
28
- // 17-char key that survives Store renames). The /v2/acts/{id} endpoint accepts
29
- // it directly. We use IDs rather than the mambalabs~slug path so a Store rename
30
- // never breaks these calls.
27
+ // How long the actor run itself is allowed to take, in seconds.
28
+ //
29
+ // MEASURED across the whole fleet, because this one caller fronts 21 actors
30
+ // and a per-actor number would be 21 constants that drift. Over every SUCCEEDED
31
+ // run in actor_runs on 2026-09-08, 5,088 of them carrying a duration: P95
32
+ // 38.5 s, P99 171.3 s, slowest ever 1,010.7 s.
33
+ //
34
+ // The suite was calling run-sync-get-dataset-items?timeout=300, so every tool
35
+ // it exposes was cut off at five minutes while the slowest run any of these
36
+ // actors has completed takes nearly seventeen. 1800 s is 1.8 times that slowest
37
+ // run and more than ten times the fleet P99, which is headroom for the slow
38
+ // tools without letting a hung run bill indefinitely.
39
+ //
40
+ // The standalone job discovery wrapper moved to start-and-poll first and the
41
+ // suite's copy of that tool did not, which is finding M-B4. This closes it for
42
+ // all 21.
43
+ const ACTOR_RUN_TIMEOUT_SECS = 1800;
44
+ // How long this wrapper waits for that run, in milliseconds. The actor's own
45
+ // timeout plus two minutes, so the run's own TIMED-OUT status is what the
46
+ // caller sees rather than the wrapper giving up first and reporting nothing.
47
+ const WRAPPER_WAIT_MS = (ACTOR_RUN_TIMEOUT_SECS + 120) * 1000;
48
+ const POLL_INTERVAL_MS = 3000;
49
+ const TERMINAL = new Set(["SUCCEEDED", "FAILED", "TIMED-OUT", "ABORTED", "ABORTING"]);
50
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
51
+ // Shared caller. actorPath is the actor's immutable Apify actor ID (a stable key
52
+ // that survives Store renames). The /v2/acts/{id} endpoint accepts it directly,
53
+ // so a Store rename never breaks these calls.
54
+ //
55
+ // START AND POLL, NOT RUN-SYNC. This wrapper used
56
+ // run-sync-get-dataset-items?timeout=300 and cut off runs the actor completes:
57
+ // runs that the actors complete. Raising that query parameter does not fix it,
58
+ // which is worth stating
59
+ // because it is the obvious fix and it is wrong. Apify's synchronous endpoints
60
+ // carry a platform ceiling of 300 seconds on the HTTP wait itself and answer
61
+ // 408 past it regardless of what `timeout` says. The only way for the wrapper
62
+ // to wait as long as the actor needs is to start the run, poll it to a terminal
63
+ // status, and then read the dataset.
64
+ //
65
+ // The token is read here rather than at module load, so the tool registers
66
+ // unconditionally and a server started without APIFY_TOKEN still advertises its
67
+ // capabilities instead of reporting none.
31
68
  async function runActor(actorPath, actorLabel, input) {
69
+ const APIFY_TOKEN = process.env.APIFY_TOKEN;
32
70
  if (!APIFY_TOKEN) {
33
71
  return { isError: true, content: [{ type: "text", text: "APIFY_TOKEN is not set. Create a token at https://console.apify.com/account/integrations and set it as the APIFY_TOKEN environment variable." }] };
34
72
  }
35
- const url = `https://api.apify.com/v2/acts/${actorPath}/run-sync-get-dataset-items?timeout=300`;
36
- let response;
37
- try {
38
- response = await fetch(url, {
39
- method: "POST",
40
- headers: {
41
- Authorization: `Bearer ${APIFY_TOKEN}`,
42
- "Content-Type": "application/json",
43
- "User-Agent": USER_AGENT,
44
- },
45
- body: JSON.stringify(input),
46
- });
47
- }
48
- catch (err) {
49
- const message = err instanceof Error ? err.message : String(err);
50
- return { isError: true, content: [{ type: "text", text: `Could not reach the Apify API: ${message}` }] };
51
- }
52
- if (!response.ok) {
73
+ const headers = {
74
+ Authorization: `Bearer ${APIFY_TOKEN}`,
75
+ "Content-Type": "application/json",
76
+ "User-Agent": USER_AGENT,
77
+ };
78
+ const httpError = async (response) => {
53
79
  let detail = "";
54
80
  try {
55
81
  const body = (await response.json());
@@ -59,24 +85,107 @@ async function runActor(actorPath, actorLabel, input) {
59
85
  catch {
60
86
  detail = "";
61
87
  }
62
- let message;
63
88
  switch (response.status) {
89
+ case 400:
90
+ return `The ${actorLabel} run was rejected as invalid input.${detail}`;
64
91
  case 401:
65
- message = "Invalid Apify token. Check your APIFY_TOKEN environment variable.";
66
- break;
92
+ return "Invalid Apify token. Check your APIFY_TOKEN environment variable.";
67
93
  case 402:
68
- message =
69
- "Insufficient Apify credits. Check your account balance at https://console.apify.com/billing";
70
- break;
71
- case 408:
72
- message = `The ${actorLabel} run timed out after 300 seconds. Try again, or run the actor on Apify directly for longer jobs.`;
73
- break;
94
+ return "Insufficient Apify credits. Check your account balance at https://console.apify.com/billing";
74
95
  default:
75
- message = `Apify request to ${actorLabel} failed with status ${response.status}.${detail}`;
96
+ return `Apify request to ${actorLabel} failed with status ${response.status}.${detail}`;
76
97
  }
77
- return { isError: true, content: [{ type: "text", text: message }] };
98
+ };
99
+ // 1. Start the run.
100
+ let started;
101
+ try {
102
+ started = await fetch(`https://api.apify.com/v2/acts/${actorPath}/runs?timeout=${ACTOR_RUN_TIMEOUT_SECS}`, { method: "POST", headers, body: JSON.stringify(input) });
103
+ }
104
+ catch (err) {
105
+ const message = err instanceof Error ? err.message : String(err);
106
+ return { isError: true, content: [{ type: "text", text: `Could not reach the Apify API: ${message}` }] };
107
+ }
108
+ if (!started.ok) {
109
+ return { isError: true, content: [{ type: "text", text: await httpError(started) }] };
110
+ }
111
+ let run;
112
+ try {
113
+ run = (await started.json()).data ?? {};
114
+ }
115
+ catch (err) {
116
+ const message = err instanceof Error ? err.message : String(err);
117
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run start returned a response that could not be parsed: ${message}` }] };
118
+ }
119
+ const runId = run.id;
120
+ if (!runId) {
121
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run start returned no run id, so there is nothing to wait for.` }] };
122
+ }
123
+ // 2. Poll to a terminal status.
124
+ const deadline = Date.now() + WRAPPER_WAIT_MS;
125
+ let status = run.status ?? "READY";
126
+ let datasetId = run.defaultDatasetId;
127
+ while (!TERMINAL.has(status)) {
128
+ if (Date.now() >= deadline) {
129
+ return {
130
+ isError: true,
131
+ content: [{ type: "text", text: `The ${actorLabel} run ${runId} was still ${status} after ${Math.round(WRAPPER_WAIT_MS / 1000)} seconds and this call stopped waiting. The run itself is still on Apify: read it at https://console.apify.com/actors/runs/${runId}` }],
132
+ };
133
+ }
134
+ await sleep(POLL_INTERVAL_MS);
135
+ let poll;
136
+ try {
137
+ poll = await fetch(`https://api.apify.com/v2/actor-runs/${runId}`, { headers });
138
+ }
139
+ catch (err) {
140
+ const message = err instanceof Error ? err.message : String(err);
141
+ return { isError: true, content: [{ type: "text", text: `Lost contact with the Apify API while waiting for ${actorLabel} run ${runId}: ${message}` }] };
142
+ }
143
+ if (!poll.ok) {
144
+ return { isError: true, content: [{ type: "text", text: await httpError(poll) }] };
145
+ }
146
+ const body = (await poll.json());
147
+ status = body.data?.status ?? status;
148
+ datasetId = body.data?.defaultDatasetId ?? datasetId;
149
+ }
150
+ // 3. A run that did not succeed is a failure the caller must see, never an
151
+ // empty success. Surfacing it here is what keeps a crashed run from reading
152
+ // as "no results found".
153
+ if (status !== "SUCCEEDED") {
154
+ return {
155
+ isError: true,
156
+ content: [{ type: "text", text: `The ${actorLabel} run did not succeed (run ID: ${runId}, status: ${status}).` }],
157
+ };
158
+ }
159
+ if (!datasetId) {
160
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run ${runId} succeeded but reported no dataset, so there is nothing to return.` }] };
161
+ }
162
+ // 4. Read the dataset.
163
+ let ds;
164
+ try {
165
+ ds = await fetch(`https://api.apify.com/v2/datasets/${datasetId}/items?format=json`, { headers });
166
+ }
167
+ catch (err) {
168
+ const message = err instanceof Error ? err.message : String(err);
169
+ return { isError: true, content: [{ type: "text", text: `Could not read the ${actorLabel} dataset: ${message}` }] };
170
+ }
171
+ if (!ds.ok) {
172
+ return { isError: true, content: [{ type: "text", text: await httpError(ds) }] };
173
+ }
174
+ let items;
175
+ try {
176
+ items = await ds.json();
177
+ }
178
+ catch (err) {
179
+ const message = err instanceof Error ? err.message : String(err);
180
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run returned a response that could not be parsed: ${message}` }] };
181
+ }
182
+ if (!Array.isArray(items)) {
183
+ const asObj = items;
184
+ const detail = asObj?.error?.message
185
+ ? `${asObj.error.message}`
186
+ : JSON.stringify(items);
187
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run did not return a dataset. ${detail}` }] };
78
188
  }
79
- const items = await response.json();
80
189
  return { content: [{ type: "text", text: JSON.stringify(items, null, 2) }] };
81
190
  }
82
191
  const server = new McpServer({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mambalabsdev/mcp-gtm-suite",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "MCP server for the full Mamba Labs GTM Suite. One server exposing eleven account-intelligence actors on Apify as tools: identity, firmographics, social presence, hiring, tech stack, job boards, funding, change feed, LinkedIn, signals and ICP scoring.",
5
5
  "type": "module",
6
6
  "bin": {