@mambalabsdev/mcp-email-waterfall-orchestrator 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/build/index.js +127 -32
  3. package/package.json +5 -4
package/README.md CHANGED
@@ -59,7 +59,7 @@ More identifiers means a better chance of a hit. Keyed on a LinkedIn URL the top
59
59
  }
60
60
  ```
61
61
 
62
- Get a token at [console.apify.com/account/integrations](https://console.apify.com/account/integrations). Read-only. Consumes Apify credits per contact submitted and per address found, on top of whatever your own provider keys spend.
62
+ Get a token at [console.apify.com/account/integrations](https://console.apify.com/account/integrations). Each call spends from your own provider accounts and consumes Apify credits per contact submitted and per address found, on top of whatever your own provider keys spend.
63
63
 
64
64
  ## Also available
65
65
 
package/build/index.js CHANGED
@@ -21,7 +21,6 @@ const here = dirname(fileURLToPath(import.meta.url));
21
21
  const pkg = JSON.parse(readFileSync(join(here, "..", "package.json"), "utf8"));
22
22
  // Distinctive UA so Apify run meta.userAgent marks MCP-originated runs.
23
23
  const USER_AGENT = `mambalabs-mcp ${pkg.name}@${pkg.version}`;
24
- const APIFY_TOKEN = process.env.APIFY_TOKEN;
25
24
  // Drop undefined values so optional inputs are not sent to the actor.
26
25
  function compact(obj) {
27
26
  const out = {};
@@ -31,31 +30,42 @@ function compact(obj) {
31
30
  }
32
31
  return out;
33
32
  }
33
+ // How long the actor run itself is allowed to take, in seconds. One value for
34
+ // every Mamba Labs wrapper, set 2026-10-05: start and poll exists so a long run
35
+ // survives, and a shorter limit would end the long runs it was built for. Past
36
+ // this limit the run ends TIMED-OUT and the caller is told so, with the run id.
37
+ const ACTOR_RUN_TIMEOUT_SECS = 1800;
38
+ // How long this wrapper waits for that run, in milliseconds. The actor's own
39
+ // timeout plus two minutes, so the run's own TIMED-OUT status is what the
40
+ // caller sees rather than the wrapper giving up first and reporting nothing.
41
+ const WRAPPER_WAIT_MS = (ACTOR_RUN_TIMEOUT_SECS + 120) * 1000;
42
+ const POLL_INTERVAL_MS = 3000;
43
+ const TERMINAL = new Set(["SUCCEEDED", "FAILED", "TIMED-OUT", "ABORTED", "ABORTING"]);
44
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
34
45
  // Shared caller. actorPath is the actor's immutable Apify actor ID (a stable key
35
46
  // that survives Store renames). The /v2/acts/{id} endpoint accepts it directly,
36
47
  // so a Store rename never breaks these calls.
48
+ //
49
+ // START AND POLL, NOT RUN-SYNC. Apify's synchronous endpoints carry a platform
50
+ // ceiling of 300 seconds on the HTTP wait itself and answer 408 past it whatever
51
+ // the timeout parameter says, so a long run reads as a timeout even though the
52
+ // actor goes on to finish. Starting the run, polling it to a terminal status and
53
+ // then reading the dataset is the only way to wait as long as the actor needs.
54
+ //
55
+ // The token is read here rather than at module load, so the tool registers
56
+ // unconditionally and a server started without APIFY_TOKEN still advertises its
57
+ // capabilities instead of reporting none.
37
58
  async function runActor(actorPath, actorLabel, input) {
59
+ const APIFY_TOKEN = process.env.APIFY_TOKEN;
38
60
  if (!APIFY_TOKEN) {
39
61
  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." }] };
40
62
  }
41
- const url = `https://api.apify.com/v2/acts/${actorPath}/run-sync-get-dataset-items?timeout=300`;
42
- let response;
43
- try {
44
- response = await fetch(url, {
45
- method: "POST",
46
- headers: {
47
- Authorization: `Bearer ${APIFY_TOKEN}`,
48
- "Content-Type": "application/json",
49
- "User-Agent": USER_AGENT,
50
- },
51
- body: JSON.stringify(input),
52
- });
53
- }
54
- catch (err) {
55
- const message = err instanceof Error ? err.message : String(err);
56
- return { isError: true, content: [{ type: "text", text: `Could not reach the Apify API: ${message}` }] };
57
- }
58
- if (!response.ok) {
63
+ const headers = {
64
+ Authorization: `Bearer ${APIFY_TOKEN}`,
65
+ "Content-Type": "application/json",
66
+ "User-Agent": USER_AGENT,
67
+ };
68
+ const httpError = async (response) => {
59
69
  let detail = "";
60
70
  try {
61
71
  const body = (await response.json());
@@ -65,24 +75,107 @@ async function runActor(actorPath, actorLabel, input) {
65
75
  catch {
66
76
  detail = "";
67
77
  }
68
- let message;
69
78
  switch (response.status) {
79
+ case 400:
80
+ return `The ${actorLabel} run was rejected as invalid input.${detail}`;
70
81
  case 401:
71
- message = "Invalid Apify token. Check your APIFY_TOKEN environment variable.";
72
- break;
82
+ return "Invalid Apify token. Check your APIFY_TOKEN environment variable.";
73
83
  case 402:
74
- message =
75
- "Insufficient Apify credits. Check your account balance at https://console.apify.com/billing";
76
- break;
77
- case 408:
78
- message = `The ${actorLabel} run timed out after 300 seconds. Try a smaller batch, or run the actor on Apify directly for longer jobs.`;
79
- break;
84
+ return "Insufficient Apify credits. Check your account balance at https://console.apify.com/billing";
80
85
  default:
81
- message = `Apify request to ${actorLabel} failed with status ${response.status}.${detail}`;
86
+ return `Apify request to ${actorLabel} failed with status ${response.status}.${detail}`;
87
+ }
88
+ };
89
+ // 1. Start the run.
90
+ let started;
91
+ try {
92
+ started = await fetch(`https://api.apify.com/v2/acts/${actorPath}/runs?timeout=${ACTOR_RUN_TIMEOUT_SECS}`, { method: "POST", headers, body: JSON.stringify(input) });
93
+ }
94
+ catch (err) {
95
+ const message = err instanceof Error ? err.message : String(err);
96
+ return { isError: true, content: [{ type: "text", text: `Could not reach the Apify API: ${message}` }] };
97
+ }
98
+ if (!started.ok) {
99
+ return { isError: true, content: [{ type: "text", text: await httpError(started) }] };
100
+ }
101
+ let run;
102
+ try {
103
+ run = (await started.json()).data ?? {};
104
+ }
105
+ catch (err) {
106
+ const message = err instanceof Error ? err.message : String(err);
107
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run start returned a response that could not be parsed: ${message}` }] };
108
+ }
109
+ const runId = run.id;
110
+ if (!runId) {
111
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run start returned no run id, so there is nothing to wait for.` }] };
112
+ }
113
+ // 2. Poll to a terminal status.
114
+ const deadline = Date.now() + WRAPPER_WAIT_MS;
115
+ let status = run.status ?? "READY";
116
+ let datasetId = run.defaultDatasetId;
117
+ while (!TERMINAL.has(status)) {
118
+ if (Date.now() >= deadline) {
119
+ return {
120
+ isError: true,
121
+ 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}` }],
122
+ };
123
+ }
124
+ await sleep(POLL_INTERVAL_MS);
125
+ let poll;
126
+ try {
127
+ poll = await fetch(`https://api.apify.com/v2/actor-runs/${runId}`, { headers });
128
+ }
129
+ catch (err) {
130
+ const message = err instanceof Error ? err.message : String(err);
131
+ return { isError: true, content: [{ type: "text", text: `Lost contact with the Apify API while waiting for ${actorLabel} run ${runId}: ${message}` }] };
132
+ }
133
+ if (!poll.ok) {
134
+ return { isError: true, content: [{ type: "text", text: await httpError(poll) }] };
82
135
  }
83
- return { isError: true, content: [{ type: "text", text: message }] };
136
+ const body = (await poll.json());
137
+ status = body.data?.status ?? status;
138
+ datasetId = body.data?.defaultDatasetId ?? datasetId;
139
+ }
140
+ // 3. A run that did not succeed is a failure the caller must see, never an
141
+ // empty success. Surfacing it here is what keeps a crashed run from reading
142
+ // as "no results found".
143
+ if (status !== "SUCCEEDED") {
144
+ return {
145
+ isError: true,
146
+ content: [{ type: "text", text: `The ${actorLabel} run did not succeed (run ID: ${runId}, status: ${status}).` }],
147
+ };
148
+ }
149
+ if (!datasetId) {
150
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run ${runId} succeeded but reported no dataset, so there is nothing to return.` }] };
151
+ }
152
+ // 4. Read the dataset.
153
+ let ds;
154
+ try {
155
+ ds = await fetch(`https://api.apify.com/v2/datasets/${datasetId}/items?format=json`, { headers });
156
+ }
157
+ catch (err) {
158
+ const message = err instanceof Error ? err.message : String(err);
159
+ return { isError: true, content: [{ type: "text", text: `Could not read the ${actorLabel} dataset: ${message}` }] };
160
+ }
161
+ if (!ds.ok) {
162
+ return { isError: true, content: [{ type: "text", text: await httpError(ds) }] };
163
+ }
164
+ let items;
165
+ try {
166
+ items = await ds.json();
167
+ }
168
+ catch (err) {
169
+ const message = err instanceof Error ? err.message : String(err);
170
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run returned a response that could not be parsed: ${message}` }] };
171
+ }
172
+ if (!Array.isArray(items)) {
173
+ const asObj = items;
174
+ const detail = asObj?.error?.message
175
+ ? `${asObj.error.message}`
176
+ : JSON.stringify(items);
177
+ return { isError: true, content: [{ type: "text", text: `The ${actorLabel} run did not return a dataset. ${detail}` }] };
84
178
  }
85
- const items = await response.json();
86
179
  return { content: [{ type: "text", text: JSON.stringify(items, null, 2) }] };
87
180
  }
88
181
  const server = new McpServer({
@@ -106,7 +199,9 @@ server.registerTool("find_work_email", {
106
199
  description: "Find a work email address for each contact by running a chain of finder providers on YOUR OWN provider API keys, verify each result, and return real per-provider spend attribution instead of an opaque credit count. This is bring your own key: Mamba Labs supplies no provider keys, never sees your credits, and the run spends from your accounts, so set maxProviderUnits. Supply contacts as an array of objects, each carrying any of full_name, first_name, last_name, company_domain, company_name, linkedin_url and email. More identifiers means a better chance of a hit. Supply your keys in providerKeys by provider name. Every key is validated for free before any paid call, and a provider with no usable key is dropped from the chain and reported rather than failing the run; with no keys at all the run still completes, explains what was unavailable and charges nothing. Verification runs after each finder that returns an address, not once at the end. Every contact gets a row including misses, so you can see what was attempted, and a final summary row carries the run totals and spend. Returns flat Clay-ready JSON. Requires an APIFY_TOKEN and consumes Apify credits per contact submitted and per address found, on top of whatever your own provider keys spend.",
107
200
  annotations: {
108
201
  title: "Find Work Email",
109
- readOnlyHint: true,
202
+ // Not read only: the run calls paid finder and verifier endpoints on the
203
+ // caller's own provider accounts and spends their credits.
204
+ readOnlyHint: false,
110
205
  destructiveHint: false,
111
206
  idempotentHint: false,
112
207
  openWorldHint: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mambalabsdev/mcp-email-waterfall-orchestrator",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "MCP server for the Mamba Labs Work Email Waterfall actor on Apify. Give it contacts and your own provider API keys and it finds a work email for each, verifies it, and returns real per-provider spend attribution as flat Clay-ready JSON.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -14,6 +14,7 @@
14
14
  ],
15
15
  "scripts": {
16
16
  "build": "tsc && chmod +x build/index.js",
17
+ "test": "npm run build && node --test test/*.test.js",
17
18
  "prepublishOnly": "npm run build"
18
19
  },
19
20
  "keywords": [
@@ -40,11 +41,11 @@
40
41
  },
41
42
  "dependencies": {
42
43
  "@modelcontextprotocol/sdk": "^1.12.0",
43
- "zod": "^3.23.8"
44
+ "zod": "3.25.76"
44
45
  },
45
46
  "devDependencies": {
46
- "@types/node": "^20.14.0",
47
- "typescript": "^5.6.0"
47
+ "@types/node": "^26.4.1",
48
+ "typescript": "5.9.3"
48
49
  },
49
50
  "mcpName": "com.mambabuilt/mcp-email-waterfall-orchestrator"
50
51
  }