Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.5 — 2026-04-28

- **`@leadbay/mcp` 0.2.5** + **`@leadbay/leadclaw` 0.2.5**: new `leadbay_import_leads` composite write tool ([product#3537](https://github.com/leadbay/product/issues/3537)). Imports a list of company domains and returns Leadbay leadIds for the ones the crawler already knows, chainable into `leadbay_bulk_qualify_leads` and `leadbay_research_lead`. Writes user state (creates a CRM-imports row visible in the web UI). Gated behind `LEADBAY_MCP_WRITE=1` (MCP) and `exposeWrite: true` (OpenClaw). See package CHANGELOGs for full surface, error codes, and limitations.

## 0.1.0 — 2026-04-20

Initial release.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ openclaw plugins install @leadbay/openclaw-leadclaw
| `leadbay_qualify_lead` | Trigger AI qualification on a lead (~60s async) |
| `leadbay_enrich_contacts` | Order email/phone enrichment for a contact (~60s async) |
| `leadbay_add_note` | Add a note to a lead (visible to your team) |
| `leadbay_import_leads` | Map a list of company domains to Leadbay `leadId`s, chainable into `leadbay_bulk_qualify_leads`. Wraps the CSV-import wizard; **mutates user state** (creates a CRM-imports row). Suitable for occasional automation, not high-cadence. Admin-only. |

## How it works

Expand Down
104 changes: 101 additions & 3 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function httpsRequest(
method: string,
url: string,
headers: Record<string, string>,
body?: string
body?: string | Buffer
): Promise<HttpResult> {
return new Promise((resolve, reject) => {
const start = Date.now();
const parsed = new URL(url);
const reqHeaders: Record<string, string | number> = { ...headers };
if (body) {
if (body !== undefined) {
reqHeaders["Content-Length"] = Buffer.byteLength(body);
}
const req = https.request(
Expand All @@ -65,7 +65,7 @@ function httpsRequest(
}
);
req.on("error", reject);
if (body) req.write(body);
if (body !== undefined) req.write(body);
req.end();
});
}
Expand Down Expand Up @@ -464,6 +464,59 @@ export class LeadbayClient {
}
}

// Like request(), but the caller supplies the Content-Type and the already-
// serialized body (string for text payloads such as CSV; Buffer for binary
// uploads). Auth, semaphore, error mapping, _lastMeta, and mock-mode all
// mirror request() exactly. Used by leadbay_import_leads to upload CSVs to
// the wizard at POST /1.5/imports.
async requestRawBinary<T>(
method: string,
path: string,
contentType: string,
body: string | Buffer
): Promise<T> {
if (process.env.LEADBAY_MOCK === "1") {
return this.mockRequestBinary<T>(method, path, contentType, body);
}
if (!this.token) {
throw this.makeError(
"NOT_AUTHENTICATED",
"Not logged in to Leadbay",
"Set LEADBAY_TOKEN in your MCP client config, or run: npx -y @leadbay/mcp install --email <you> --region <us|fr>",
path
);
}
await this.acquireSemaphore();
try {
const url = `${this._baseUrl}/1.5${path}`;
const headers: Record<string, string> = {
Authorization: `Bearer ${this.token}`,
"Content-Type": contentType,
};

const res = await httpsRequest(method, url, headers, body);

this._lastMeta = {
region: this._region,
endpoint: `${method} ${path}`,
latency_ms: res.latency_ms,
retry_after: parseRetryAfter(res.headers["retry-after"]),
};

if (res.status === 204) {
return null as T;
}

if (res.status < 200 || res.status >= 300) {
throw this.mapErrorResponse(res.status, res.body, path, res.headers);
}

return JSON.parse(res.body) as T;
} finally {
this.releaseSemaphore();
}
}

private mockRequest<T>(method: string, path: string, body?: unknown): T {
const fullPath = `/1.5${path}`;
this._lastMeta = {
Expand Down Expand Up @@ -493,6 +546,51 @@ export class LeadbayClient {
} as unknown as T;
}

private mockRequestBinary<T>(
method: string,
path: string,
contentType: string,
body: string | Buffer
): T {
const fullPath = `/1.5${path}`;
this._lastMeta = {
region: this._region,
endpoint: `${method} ${path}`,
latency_ms: 0,
retry_after: null,
};
if (method === "GET") {
// Binary GETs aren't a thing in Leadbay's API today; fall through to
// standard fixture lookup so the same mocks apply.
const fixture = findMockFixture("GET", fullPath);
if (!fixture) {
throw this.makeError(
"MOCK_NOT_FOUND",
`LEADBAY_MOCK=1: no fixture for GET ${path}`,
`Add a fixture to LEADBAY_MOCK_DIR (default: ./.context/leadbay-live-shapes/) — run a probe script to generate one.`,
path
);
}
if (fixture.status === 204) return null as T;
return fixture.body as T;
}
const journalBody = {
_binary: true,
length: Buffer.byteLength(body),
content_type: contentType,
};
_mockJournal.push({
method,
path: fullPath,
body: journalBody,
ts: Date.now(),
});
return {
mocked: true,
would_call: { method, path: fullPath, body: journalBody },
} as unknown as T;
}

private mapErrorResponse(
status: number,
rawBody: string,
Expand Down
Loading
Loading