@pipeworx/mcp-businesswire 0.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.
package/src/index.ts ADDED
@@ -0,0 +1,1200 @@
1
+ interface McpToolDefinition {
2
+ name: string;
3
+ description: string;
4
+ /** Human-facing one-liner (fleet #1967). Optional; consumers fall back to
5
+ * description. Kept in step with shared/src/types.ts — scripts/lib/
6
+ * check-inlined-types.mjs reports drift at publish time. */
7
+ summary?: string;
8
+ inputSchema: {
9
+ type: 'object';
10
+ properties: Record<string, unknown>;
11
+ required?: string[];
12
+ anyOf?: Array<{ required: string[] }>;
13
+ oneOf?: Array<{ required: string[] }>;
14
+ allOf?: Array<{ required: string[] }>;
15
+ };
16
+ outputSchema?: Record<string, unknown>;
17
+ }
18
+
19
+ interface McpToolExport {
20
+ tools: McpToolDefinition[];
21
+ callTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
22
+ meter?: { credits: number };
23
+ cost?: Record<string, unknown>;
24
+ provider?: string;
25
+ }
26
+
27
+ /**
28
+ * RSS / Atom item parsing, shared across every pack and worker that reads a
29
+ * feed.
30
+ *
31
+ * Ported from `workers/press-intel/src/feed.ts`, which was itself ported from
32
+ * the working parser in `mcps/us-news-feeds/src/index.ts` — the
33
+ * regex-over-XML shape is deliberate (no DOM in Workers, and every feed in
34
+ * the wild is malformed in some small way that a strict parser would
35
+ * reject). press-intel's version is the hardened one: it was built against
36
+ * 85 live feeds and adds two things the pack-local copies were missing.
37
+ * Moved here 2026-09-21 (fleet #2285) so the press-release wire packs reuse
38
+ * it instead of re-copy-pasting a third parser — a capability proven against
39
+ * 85 feeds belongs in one place, not one worker.
40
+ *
41
+ * 1. `<media:credit role="author">`. ZDNET carries a byline on 20 of 20 items
42
+ * in that tag and nothing else. The original probe scored ZDNET
43
+ * `byline_pct: 0` and filed it as needing an article-page fetch — the feed
44
+ * was fine, the parser was looking in three places out of five.
45
+ *
46
+ * 2. `newestItemAt`. A feed can answer 200 with well-formed XML and still be
47
+ * abandoned: WSJ Tech's registered feed (feeds.a.dj.com) returns 20 valid
48
+ * items whose newest is dated 2025-01-27. HTTP status cannot see that,
49
+ * which is why every caller should record the newest item date per poll
50
+ * and flag a feed stale rather than trusting a green 200. This is exactly
51
+ * the trap a keyless press-release wire hits too: a 200 with well-formed
52
+ * XML and a stale newest item looks identical to a live one at the
53
+ * transport layer.
54
+ */
55
+
56
+ interface FeedItem {
57
+ title: string;
58
+ link: string;
59
+ publishedAt?: string;
60
+ excerpt?: string;
61
+ /** RAW byline string, exactly as the feed emitted it. Never normalised here. */
62
+ rawByline?: string;
63
+ guid?: string;
64
+ categories?: string[];
65
+ }
66
+
67
+ interface ParsedFeed {
68
+ items: FeedItem[];
69
+ /** ISO date of the newest item that carried a parseable date, or null. */
70
+ newestItemAt: string | null;
71
+ /** How many items carried any byline at all — the per-poll byline coverage. */
72
+ withByline: number;
73
+ }
74
+
75
+ const AUTHOR_TAGS = ['dc:creator', 'media:credit', 'itunes:author', 'dc:contributor'];
76
+
77
+ function parseFeed(xml: string): ParsedFeed {
78
+ const items: FeedItem[] = [];
79
+ const blocks = xml.match(/<(item|entry)[\s>][\s\S]*?<\/\1>/gi) ?? [];
80
+ let newest: number | null = null;
81
+ let withByline = 0;
82
+
83
+ for (const b of blocks) {
84
+ const link = extractLink(b);
85
+ const publishedRaw = tag(b, 'pubDate') || tag(b, 'published') || tag(b, 'updated') || tag(b, 'dc:date');
86
+ const ts = publishedRaw ? Date.parse(publishedRaw) : NaN;
87
+ if (Number.isFinite(ts) && (newest === null || ts > newest)) newest = ts;
88
+ const rawByline = feedByline(b);
89
+ if (rawByline) withByline++;
90
+ items.push({
91
+ title: clean(tag(b, 'title')),
92
+ link,
93
+ publishedAt: Number.isFinite(ts) ? new Date(ts).toISOString() : undefined,
94
+ excerpt: clean(tag(b, 'description') || tag(b, 'summary') || tag(b, 'content:encoded') || tag(b, 'content'))
95
+ .slice(0, EXCERPT_MAX) || undefined,
96
+ rawByline: rawByline || undefined,
97
+ guid: tag(b, 'guid') || tag(b, 'id') || link || undefined,
98
+ categories: cats(b),
99
+ });
100
+ }
101
+
102
+ return {
103
+ items,
104
+ newestItemAt: newest === null ? null : new Date(newest).toISOString(),
105
+ withByline,
106
+ };
107
+ }
108
+
109
+ /**
110
+ * Excerpt cap.
111
+ *
112
+ * `docs/journalist-db-plan.md` draws a rights boundary: URL + metadata + our own
113
+ * summary + a SHORT excerpt, no full-text retention. Several feeds put the
114
+ * entire article in `<content:encoded>` (The Register does), so without a
115
+ * hard cut here "store the excerpt" quietly becomes "mirror the publisher's
116
+ * archive" — of publishers we intend to pitch, or in the wire case, of
117
+ * publishers whose own release text this is.
118
+ */
119
+ const EXCERPT_MAX = 400;
120
+
121
+ function feedByline(b: string): string {
122
+ for (const t of AUTHOR_TAGS) {
123
+ const v = clean(tag(b, t));
124
+ if (v) return v;
125
+ }
126
+ return clean(authorName(b));
127
+ }
128
+
129
+ function extractLink(b: string): string {
130
+ const rss = b.match(/<link>([\s\S]*?)<\/link>/i);
131
+ if (rss && rss[1].trim()) return clean(rss[1]);
132
+ const alt =
133
+ b.match(/<link[^>]*rel=["']alternate["'][^>]*href=["']([^"']+)["']/i) ||
134
+ b.match(/<link[^>]*href=["']([^"']+)["']/i);
135
+ return alt ? decodeXml(alt[1].trim()) : '';
136
+ }
137
+
138
+ function authorName(b: string): string {
139
+ const a = b.match(/<author[\s>]([\s\S]*?)<\/author>/i);
140
+ if (!a) return '';
141
+ const name = a[1].match(/<name>([\s\S]*?)<\/name>/i);
142
+ if (name) return name[1];
143
+ // RSS 2.0 <author> is an email address, optionally "a@b.com (Real Name)".
144
+ const emailWithName = a[1].match(/\(([^)]{2,80})\)/);
145
+ if (emailWithName) return emailWithName[1];
146
+ return /@/.test(a[1]) ? '' : a[1];
147
+ }
148
+
149
+ function cats(b: string): string[] | undefined {
150
+ const list: string[] = [];
151
+ for (const m of b.matchAll(/<category[^>]*?(?:term=["']([^"']+)["'][^>]*)?>([\s\S]*?)<\/category>/gi)) {
152
+ const v = clean(m[1] || m[2]);
153
+ if (v) list.push(v);
154
+ }
155
+ for (const m of b.matchAll(/<category[^>]*term=["']([^"']+)["'][^>]*\/>/gi)) list.push(decodeXml(m[1]));
156
+ return list.length ? [...new Set(list)].slice(0, 10) : undefined;
157
+ }
158
+
159
+ function tag(xml: string, name: string): string {
160
+ const esc = name.replace(':', '\\:');
161
+ const m = xml.match(new RegExp(`<${esc}[^>]*>([\\s\\S]*?)<\\/${esc}>`, 'i'));
162
+ return m ? unwrap(m[1]) : '';
163
+ }
164
+
165
+ function unwrap(s: string): string {
166
+ const m = s.trim().match(/^<!\[CDATA\[([\s\S]*?)\]\]>$/);
167
+ return (m ? m[1] : s).trim();
168
+ }
169
+
170
+ function decodeXml(s: string): string {
171
+ return s
172
+ .replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(Number(d)))
173
+ .replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
174
+ .replace(/&quot;/g, '"')
175
+ .replace(/&apos;/g, "'")
176
+ .replace(/&lt;/g, '<')
177
+ .replace(/&gt;/g, '>')
178
+ .replace(/&amp;/g, '&');
179
+ }
180
+
181
+ function clean(s: unknown): string {
182
+ if (typeof s !== 'string') return '';
183
+ return decodeXml(
184
+ s.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1').replace(/<\/?[a-zA-Z][^>]*>/g, ' '),
185
+ )
186
+ .replace(/&nbsp;/g, ' ')
187
+ .replace(/\s+/g, ' ')
188
+ .trim();
189
+ }
190
+
191
+ /**
192
+ * Split feed XML into raw `<item>`/`<entry>` blocks, in document order — the
193
+ * same blocks {@link parseFeed} iterates internally.
194
+ *
195
+ * Exposed so a caller can read a VENDOR-SPECIFIC field {@link parseFeed} does
196
+ * not know about — GlobeNewswire's `dc:contributor` issuer name, PRNewswire's
197
+ * `prn:industry` list — by zipping this array against `parseFeed(xml).items`;
198
+ * both walk the same blocks in the same order, so index N in one is index N
199
+ * in the other. Added for the press-release wire packs (fleet #2285): those
200
+ * feeds carry the issuer in a tag `parseFeed`'s generic byline heuristic
201
+ * would mis-attribute (PRNewswire's `<media:credit>` names the WIRE, "PR
202
+ * Newswire", not the issuer).
203
+ */
204
+ function feedItemBlocks(xml: string): string[] {
205
+ return xml.match(/<(item|entry)[\s>][\s\S]*?<\/\1>/gi) ?? [];
206
+ }
207
+
208
+ /**
209
+ * Read one XML tag's text out of a raw item/entry block (CDATA-unwrapped,
210
+ * entity-decoded). Exposed alongside {@link feedItemBlocks} for the same
211
+ * reason.
212
+ */
213
+ function feedTag(block: string, name: string): string {
214
+ return tag(block, name);
215
+ }
216
+
217
+ /**
218
+ * Like {@link feedTag}, but returns every occurrence — for a tag a feed
219
+ * repeats per item (PRNewswire's `<prn:industry>`, one element per industry
220
+ * assigned to the release).
221
+ */
222
+ function feedTagAll(block: string, name: string): string[] {
223
+ const esc = name.replace(':', '\\:');
224
+ const re = new RegExp(`<${esc}[^>]*>([\\s\\S]*?)<\\/${esc}>`, 'gi');
225
+ const out: string[] = [];
226
+ for (const m of block.matchAll(re)) {
227
+ const v = clean(unwrap(m[1]));
228
+ if (v) out.push(v);
229
+ }
230
+ return out;
231
+ }
232
+
233
+ /**
234
+ * Canonical URL for dedupe.
235
+ *
236
+ * Strips tracking noise and the per-feed `?mod=` / `utm_*` suffixes that make
237
+ * the same article arrive as two rows from a section feed and a topic feed.
238
+ * Deliberately keeps the path and any non-tracking query, because several
239
+ * sources address articles by query string alone.
240
+ */
241
+ function canonicalUrl(raw: string): string | null {
242
+ const t = (raw || '').trim();
243
+ if (!/^https?:\/\//i.test(t)) return null;
244
+ let u: URL;
245
+ try { u = new URL(t); } catch { return null; }
246
+ u.hash = '';
247
+ u.protocol = 'https:';
248
+ u.hostname = u.hostname.toLowerCase().replace(/^www\./, '');
249
+ const drop: string[] = [];
250
+ u.searchParams.forEach((_v, k) => {
251
+ if (/^(utm_|ic|mod$|cmp$|cmpid$|src$|source$|ref$|fbclid$|gclid$|guccounter$|amp$|guce_)/i.test(k)) drop.push(k);
252
+ });
253
+ for (const k of drop) u.searchParams.delete(k);
254
+ let s = u.toString();
255
+ if (s.endsWith('?')) s = s.slice(0, -1);
256
+ // Trailing-slash variants are the single most common duplicate shape.
257
+ if (s.endsWith('/') && !u.search) s = s.slice(0, -1);
258
+ return s.slice(0, 1500);
259
+ }
260
+
261
+
262
+ /**
263
+ * Was this failure OUR OWN web service? — the other half of `internal-db-class.ts`.
264
+ *
265
+ * fleet #1089 pulled failures from our own Postgres out of `upstream_down` by
266
+ * keying on the SQLSTATE inside PostgREST's four-key error envelope. That
267
+ * covered the majority and structurally could not cover the rest: the rest
268
+ * never reach Postgres, so they carry no SQLSTATE. What was left, measured over
269
+ * the 24h to 2026-09-02T15:00Z (fleet #1096):
270
+ *
271
+ * 5 pipeworx-catalog get_pack_tools Pipeworx catalog error: 522 — error code: 522
272
+ * 3 fleet fleet_list_open … upstream_down: Fleet task queue did not respond within 25s
273
+ *
274
+ * 521/522/523/526 are Cloudflare saying its edge could not reach an ORIGIN, and
275
+ * in both of those rows the origin is ours — `gateway.pipeworx.io` for the
276
+ * catalog pack (it self-fetches when the gateway hasn't injected a manifest),
277
+ * our own Supabase for fleet. There is no third party anywhere in either call.
278
+ * Same defect as #1089: our own outage filed under `upstream_down`, the one
279
+ * class that means "the source is unreachable and there is nothing for us to
280
+ * fix", which is why the problem-tools triage skips it.
281
+ *
282
+ * WHY NOT A WORDING RULE. The obvious fix is to match `fleet db error:` and
283
+ * `Pipeworx catalog error:` in classifyToolError. Each is emitted from exactly
284
+ * one site today, so it would work today. It would also rot the first time
285
+ * somebody rewords a label — silently, and in the direction of hiding our own
286
+ * outage, which is worse than the bug being fixed. Every prose rule in
287
+ * error-class.ts has needed widening as packs invented new wording (#409/#450/
288
+ * #584); that history is most of that file's comment budget.
289
+ *
290
+ * WHAT THIS KEYS ON INSTEAD: **the host the call actually reached.** A URL's
291
+ * hostname is a fact about the call, not a guess about its prose. Two
292
+ * consequences that a pack-level flag could not give us, and the reason the
293
+ * flag was rejected:
294
+ *
295
+ * - It describes the CALL, not the pack. `govcon-intel` fans out to our own
296
+ * Supabase AND to genuine third parties; `court-listener` holds our cache
297
+ * in Supabase and fetches courtlistener.com. An `internallyHosted: true` on
298
+ * either pack would relabel a real third-party outage as ours — inventing
299
+ * work, which is the same class of error in the opposite direction.
300
+ * - It covers every future internal pack for free, instead of one declared
301
+ * slug at a time.
302
+ *
303
+ * WHY IT SURVIVES A REWORD. The marker below is not matched as a literal by two
304
+ * separate files. `markInternalOrigin()` writes it and `internalHostMetricsClass()`
305
+ * reads it, both from the single exported `INTERNAL_ORIGIN_MARKER` constant in
306
+ * this module — so changing the wording changes both sides in the same edit and
307
+ * cannot desynchronise them. The pack's own label (`fleet db error:`,
308
+ * `Pipeworx catalog error:`) is not read at all: reword it freely, the class is
309
+ * unaffected. That is the property `stripClassPrefix` lacked when it drifted
310
+ * from its own classifier three times and needed a CI gate to hold them
311
+ * together.
312
+ *
313
+ * WHERE THE 5xx TEST LIVES. `markInternalOrigin` is called from the places that
314
+ * hold the real `Response` — `httpError`/`httpErrorMessage` and the timeout
315
+ * branch of `fetchWithTimeout` in `shared/src/http.ts` — so "is this an
316
+ * availability failure" is decided from the actual status code, never re-derived
317
+ * by scraping a number out of a sentence. A 404 from our own registry for a slug
318
+ * that does not exist is a caller's bad argument and is deliberately NOT marked.
319
+ */
320
+
321
+ /**
322
+ * OUR OWN web service was unreachable — not an upstream, and never `upstream_down`.
323
+ *
324
+ * ONE value, not three, unlike `internal_db_*`. That split existed because a
325
+ * slow query, an exhausted pool and an unknown SQLSTATE have different owners
326
+ * and different fixes. Here there is only one story to tell — an origin we run
327
+ * did not answer the edge — and one owner. A bucket with no distinct owner per
328
+ * value is decoration; #724 is what happens when a class holds several
329
+ * situations, and inventing sub-values ahead of a reason to act on them
330
+ * differently is the same mistake with the sign flipped.
331
+ *
332
+ * METRICS ONLY, exactly like PLATFORM_KEY_ERROR_CLASS and the internal_db
333
+ * values. `classifyToolError` still answers `upstream_down` for the retry and
334
+ * hint paths, which only care whether retrying or a sibling tool might work —
335
+ * and it might. Nothing a caller sees or is charged changes here.
336
+ *
337
+ * READ SIDE: this value is in BROKEN_TOOL_CLASSES, FAULT_CLASSES and
338
+ * ALL_ERROR_CLASSES in `workers/registry-api/src/index.ts`. All three, or it
339
+ * lands on no dashboard — fleet #721 is the warning, where the #719 split
340
+ * worked on the write side and was invisible for weeks.
341
+ */
342
+ const INTERNAL_SERVICE_UNREACHABLE_CLASS = 'internal_service_unreachable';
343
+
344
+ /**
345
+ * The token that carries "this origin is ours" from the call site to the
346
+ * classifier.
347
+ *
348
+ * Appended to the error message rather than attached to the Error object,
349
+ * because the object does not survive the trip: 275 packs return `{ error:
350
+ * string }` instead of throwing, the gateway reads `observedError` as a string,
351
+ * and the fleet pack rebuilds its error from a captured status + body across a
352
+ * retry loop. A property on an Error would be dropped by every one of those
353
+ * paths and the class would work in tests and vanish in production.
354
+ *
355
+ * WORDING IS LOAD-BEARING, same rule as labelAge's note in authority.ts. This
356
+ * string is appended to a pack's thrown Error message (shared/src/http.ts),
357
+ * and a thrown Error's message is exactly what the gateway hands back to the
358
+ * caller as `content[0].text` when nothing rewrites it (workers/gateway/src
359
+ * catches the throw and sets `rawResult.message = stripClassPrefix(error)`,
360
+ * which does not touch this suffix) — so the original wording,
361
+ * " [pipeworx-hosted origin — our own service, not a third party]", was not a
362
+ * theoretical leak: it shipped live on pipeworx-catalog's 522s, 7 times in 6
363
+ * hours on 2026-09-02 (see tests/golden-internal-service.test.ts), verbatim
364
+ * naming Pipeworx as the host. check:hosting-claims never caught it because it
365
+ * did not scan shared/ at all (task #2009). Reworded to describe the
366
+ * OBSERVATION (the origin did not answer) without a claim about who runs it —
367
+ * the identical fix labelAge got: drop the possessive, keep the fact.
368
+ */
369
+ const INTERNAL_ORIGIN_MARKER = ' [origin did not respond — retry before concluding the named source is down]';
370
+
371
+ /**
372
+ * Supabase's data plane for a project is `<ref>.supabase.co`, where the ref is
373
+ * exactly twenty lowercase letters (ours is `pqauisounztsgdgfkhke`).
374
+ *
375
+ * Matching the shape rather than listing the ref keeps this correct when we add
376
+ * a project — `supabaseEnv` on a pack entry already points some packs at a
377
+ * second one — while still excluding `status.supabase.co`, which is Supabase's
378
+ * own status page and emphatically not our database. Verified 2026-09-02 by
379
+ * `grep -rhoE '[a-z0-9-]+\.supabase\.(co|in)' mcps shared workers scripts`: the
380
+ * only real project ref anywhere in the tree is ours, the rest are doc
381
+ * placeholders (`abc`, `xyz`, `example`) which this pattern also excludes. Same
382
+ * finding internal-db-class.ts relies on for the PostgREST envelope being ours
383
+ * by construction.
384
+ */
385
+ const SUPABASE_PROJECT_HOST = /^[a-z]{20}\.supabase\.(co|in)$/;
386
+
387
+ /**
388
+ * Is this a host WE run?
389
+ *
390
+ * Deliberately NOT including `*.workers.dev`: plenty of third-party APIs are
391
+ * hosted on workers.dev, so the suffix says where something runs and not who
392
+ * owns it. Every internal call we actually make goes to a `pipeworx.io`
393
+ * hostname or to our Supabase project, both of which are ownership facts.
394
+ *
395
+ * `workers/gateway/src/provenance.ts`'s `OUR_HOSTS` answers the same
396
+ * question and DOES include `workers.dev` — a documented divergence
397
+ * (task #2051), not a bug to converge. That list decides what a response may
398
+ * cite as a data SOURCE, where a false negative (citing our own worker as an
399
+ * external source) is the hosting-disclosure leak this whole file exists to
400
+ * prevent, so it errs broad. This one decides who gets BLAMED for a 5xx in
401
+ * outage metrics read by on-call, where a false positive (crediting our own
402
+ * infra with a third party's outage) hides the real failure, so it errs
403
+ * narrow. Same suffix, opposite direction, because they are never called for
404
+ * the same reason.
405
+ *
406
+ * Returns false on anything unparseable rather than throwing — this runs inside
407
+ * an error path, and an error path that can itself throw turns a diagnosable
408
+ * failure into a mystery.
409
+ */
410
+ function isPipeworxOrigin(url: string | URL | undefined | null): boolean {
411
+ if (!url) return false;
412
+ let host: string;
413
+ try {
414
+ host = new URL(url instanceof URL ? url.href : url).hostname.toLowerCase();
415
+ } catch {
416
+ return false;
417
+ }
418
+ if (host === 'pipeworx.io' || host.endsWith('.pipeworx.io')) return true;
419
+ return SUPABASE_PROJECT_HOST.test(host);
420
+ }
421
+
422
+ /**
423
+ * Append the marker when this failure was OUR origin failing to answer.
424
+ *
425
+ * `status` is the HTTP status when there is one, and omitted for a timeout —
426
+ * where there is no response at all, and "the origin did not answer" is the
427
+ * whole observation. Statuses below 500 are left alone: a 404 from our own
428
+ * registry for a slug that does not exist is the caller's argument, not our
429
+ * outage, and marking it would put ordinary 404s on the incident dashboard.
430
+ *
431
+ * Idempotent, so a message that is wrapped and re-marked on the way up (the
432
+ * fleet pack's retry loop re-throws through two layers) carries the marker once.
433
+ */
434
+ function markInternalOrigin(
435
+ message: string,
436
+ url: string | URL | undefined | null,
437
+ status?: number,
438
+ ): string {
439
+ if (status !== undefined && status < 500) return message;
440
+ if (!isPipeworxOrigin(url)) return message;
441
+ if (message.includes(INTERNAL_ORIGIN_MARKER)) return message;
442
+ return message + INTERNAL_ORIGIN_MARKER;
443
+ }
444
+
445
+ /**
446
+ * Which blob4 value a failure from our own web services books as, or undefined
447
+ * if this is not one.
448
+ *
449
+ * Ordered AFTER `internalDbMetricsClass` at the call site: a PostgREST envelope
450
+ * from our own Supabase is a strictly more specific statement about the same
451
+ * row (which of our services, and why), and the two cannot disagree about
452
+ * whether the failure is ours.
453
+ */
454
+ function internalHostMetricsClass(error: string): string | undefined {
455
+ return error.includes(INTERNAL_ORIGIN_MARKER) ? INTERNAL_SERVICE_UNREACHABLE_CLASS : undefined;
456
+ }
457
+
458
+
459
+ /**
460
+ * One place to turn a failed `fetch` into an error a caller can act on.
461
+ *
462
+ * Nearly every pack was written the same way:
463
+ *
464
+ * if (!res.ok) throw new Error(`Unsplash: ${res.status}`);
465
+ *
466
+ * which discards the response body — and the body is usually where the upstream
467
+ * says what was actually wrong ("**symbol** not found: GBP", "parameter `year`
468
+ * out of range", "unknown taxonomy id"). The caller gets a number, cannot
469
+ * self-correct, and retries the same broken call. A 2026-07-31 sweep found this
470
+ * shape in 481 of 1,400 packs, 47 of them PLATFORM-keyed.
471
+ *
472
+ * It also hides bugs one level down. Two of the first three packs audited had a
473
+ * second defect that only existed because of this line: unsplash's rate-limit
474
+ * branch sat BELOW a catch-all and was unreachable, and bea-gov parsed
475
+ * `BEAAPI.Error.APIErrorDescription` below a `!res.ok` throw that made the
476
+ * parsing dead code for every non-200.
477
+ *
478
+ * DELIBERATELY NOT A CLASSIFIER. It does not add `user_error:` /
479
+ * `upstream_down:` prefixes. Those decide which tier a failure lands in, and the
480
+ * `error` tier is what the daily problem-tools list is built from — it means
481
+ * "Pipeworx has a defect". A 400 is genuinely ambiguous: often a caller's bad
482
+ * argument, but sometimes a query WE built wrong (ted-eu comma-joined its CPV
483
+ * values into something TED rejected, and that bug was found only because it sat
484
+ * in `error`). Blanket-classifying 400s as caller mistakes would have hidden it.
485
+ * A pack that KNOWS which it is should keep saying so explicitly; this helper is
486
+ * for the 481 that say nothing at all.
487
+ */
488
+
489
+ /** Longest upstream explanation we'll pass through. Enough for a real message,
490
+ * short enough that an HTML page or a stack trace can't swamp the error. */
491
+
492
+ const MAX_DETAIL = 300;
493
+
494
+ /**
495
+ * Default bound for `fetchWithTimeout` when a pack doesn't state its own.
496
+ *
497
+ * 25s mirrors the number `epo-ops` landed on after measuring the real failure:
498
+ * a degraded upstream that doesn't error, it just never answers, and a Worker
499
+ * sits in `await fetch()` until ITS OWN execution budget kills the request —
500
+ * which can take minutes, not seconds (epo_ops_search_patents measured 4-8
501
+ * MINUTE hangs before this existed). 25s is short enough that a caller gets a
502
+ * fast, actionable error instead of holding the connection, and long enough
503
+ * that it doesn't false-trip on a merely-slow-but-alive upstream.
504
+ */
505
+ const DEFAULT_FETCH_TIMEOUT_MS = 25_000;
506
+
507
+ /**
508
+ * Read the body of a failed response and fold it into a throwable Error.
509
+ *
510
+ * Usage — note the `await`, which is the one thing that makes this a mechanical
511
+ * change rather than a drop-in:
512
+ *
513
+ * if (!res.ok) throw await httpError(res, 'Unsplash');
514
+ *
515
+ * Safe to call on any non-ok response: a body that is missing, empty, unreadable
516
+ * or HTML degrades to exactly the old `Name: 404` string rather than throwing
517
+ * something new from inside the error path.
518
+ */
519
+ async function httpError(res: Response, name: string): Promise<Error> {
520
+ return new Error(await httpErrorMessage(res, name));
521
+ }
522
+
523
+ /** The message text without constructing an Error — for packs that need to wrap
524
+ * it in their own envelope or add an explicit classification prefix. */
525
+ async function httpErrorMessage(res: Response, name: string): Promise<string> {
526
+ // The one place a 5xx from a host WE run gets stamped as ours. `res.url` is
527
+ // the URL the fetch actually resolved to (after redirects), so this is a fact
528
+ // about the call rather than a guess from the `name` the pack passed in —
529
+ // reword that label freely, the class does not move. See
530
+ // internal-host-class.ts; no-op for every third-party upstream, which is why
531
+ // this touches 481 packs' error text and changes none of it.
532
+ return markInternalOrigin(
533
+ `${name}: ${res.status}${detailSuffix(await readDetail(res))}`,
534
+ res.url,
535
+ res.status,
536
+ );
537
+ }
538
+
539
+ /**
540
+ * Just the upstream's own explanation — no name, no status.
541
+ *
542
+ * For a pack that has already said both in its own sentence. epo-ops reads
543
+ * `EPO rejected this search as too large (HTTP 413) — ${httpErrorMessage(…)}`,
544
+ * which rendered as `… (HTTP 413) — EPO: 413.` once the XML detail was being
545
+ * dropped: the upstream named twice, the status twice, and the one thing EPO
546
+ * actually said ("Not enough characters before truncation character") nowhere
547
+ * (fleet #712). Returns '' when the body carries nothing readable, so a caller
548
+ * can fall back to its own wording.
549
+ */
550
+ async function upstreamDetail(res: Response): Promise<string> {
551
+ return readDetail(res);
552
+ }
553
+
554
+ /**
555
+ * Read a SUCCESSFUL response as JSON, failing loudly when it isn't JSON.
556
+ *
557
+ * `httpError` above only ever runs on `!res.ok`, which leaves the nastier half
558
+ * of the problem unhandled: an upstream that answers **HTTP 200 with an HTML
559
+ * page**. A bot wall, a login redirect, a maintenance interstitial and a CDN
560
+ * error page are all 200s, so `res.ok` is true, and `res.json()` then throws
561
+ * `Unexpected token '<', "<!DOCTYPE "... is not valid JSON`.
562
+ *
563
+ * That string is the problem. It names no upstream, carries no status, and
564
+ * reads like a parser bug in Pipeworx — so it lands in the `error` tier, which
565
+ * means "we have a defect", and the caller is told nothing they can act on.
566
+ * data.govt.nz sat dead behind an Imperva challenge this way and every
567
+ * status-code health check we own reported it green (7889a845). A zero-length
568
+ * body has the same shape: `Unexpected end of JSON input`, seen this week on
569
+ * uk-gazette (83% of external calls) and census.
570
+ *
571
+ * UNLIKE `httpError`, this one DOES classify, and the asymmetry is deliberate.
572
+ * A 400 is genuinely ambiguous — often the caller's bad argument, sometimes a
573
+ * query we built wrong — so blanket-classifying it would hide our own bugs.
574
+ * There is no such ambiguity here: **no argument a caller can pass makes a JSON
575
+ * API return an HTML page.** It is always the upstream, so `upstream_down:` is
576
+ * a statement of fact rather than a guess, and it keeps these out of the
577
+ * problem-tools list where they crowd out real defects.
578
+ *
579
+ * const data = await parseJson<Feed>(res, 'UK Gazette');
580
+ *
581
+ * Call it only after the `!res.ok` check — on a failed response you want
582
+ * `httpError`, which mines the body for the upstream's own explanation.
583
+ */
584
+ async function parseJson<T>(res: Response, name: string): Promise<T> {
585
+ let raw: string;
586
+ try {
587
+ raw = await res.text();
588
+ } catch {
589
+ throw new Error(
590
+ `upstream_down: ${name} returned a body that could not be read (HTTP ${res.status}). ` +
591
+ 'The connection most likely dropped mid-response; retrying is reasonable.',
592
+ );
593
+ }
594
+
595
+ const type = res.headers.get('content-type') ?? 'no content-type';
596
+
597
+ if (!raw.trim()) {
598
+ throw new Error(
599
+ `upstream_down: ${name} answered HTTP ${res.status} with an EMPTY body where JSON was expected (${type}). ` +
600
+ 'Nothing about the request can cause this — it is an upstream fault, and the same call may well work on retry.',
601
+ );
602
+ }
603
+
604
+ // Checked before parsing rather than in the catch, because knowing it is
605
+ // markup is what turns "we failed to parse something" into "they served a
606
+ // web page" — the second is diagnosable, the first is not.
607
+ const head = raw.slice(0, 200).trimStart().toLowerCase();
608
+ if (head.startsWith('<!doctype') || head.startsWith('<html') || head.startsWith('<?xml')) {
609
+ const kind = head.startsWith('<?xml') ? 'an XML document' : 'an HTML page';
610
+ // The summary, not the source. Pasting the first 120 characters of a web
611
+ // page handed the agent `<!DOCTYPE html><html lang="en"…` — the same leak
612
+ // this branch exists to describe (fleet #712).
613
+ throw new Error(
614
+ `upstream_down: ${name} answered HTTP ${res.status} with ${kind} instead of JSON (${type}). ` +
615
+ 'That is typically a bot wall, a login redirect or a maintenance page — it is returned as a SUCCESS, ' +
616
+ `so status-code health checks read it as fine. No argument change will get past it. ` +
617
+ `The page says: ${summarizeErrorBody(raw) || 'nothing readable'}`,
618
+ );
619
+ }
620
+
621
+ try {
622
+ return JSON.parse(raw) as T;
623
+ } catch {
624
+ throw new Error(
625
+ `upstream_down: ${name} answered HTTP ${res.status} with a body that is not valid JSON (${type}). ` +
626
+ `It begins: ${stripMarkup(raw).slice(0, 120) || '(unreadable)'}`,
627
+ );
628
+ }
629
+ }
630
+
631
+ /**
632
+ * `fetch`, but bounded — the fix for a systemic gap found 2026-08-30: a grep
633
+ * audit of every pack's `mcps/*\/src/index.ts` found 1,339 of ~1,500 call
634
+ * `fetch()` with NO timeout guard anywhere in the file. Two of those
635
+ * (epo-ops, statcan) were confirmed live-hanging for 4-8 minutes before this
636
+ * existed — every unguarded call carries the same risk, just unconfirmed.
637
+ *
638
+ * Mirrors the `epoFetch` wrapper `mcps/epo-ops/src/index.ts` shipped first:
639
+ * bound the request with `AbortSignal.timeout`, and on a timeout/abort throw
640
+ * an `upstream_down:` error that names the upstream and the bound rather than
641
+ * letting the raw `TimeoutError`/`AbortError` (which names neither) propagate.
642
+ * `upstream_down:` is deliberate, same reasoning as `parseJson` above — no
643
+ * argument a caller passes can make an upstream hang, so it is always the
644
+ * upstream's fault, and marking it that way keeps a slow API off the
645
+ * problem-tools list where it would crowd out our own defects.
646
+ *
647
+ * Usage — a mechanical swap for a bare `fetch(url, init)`:
648
+ *
649
+ * const res = await fetchWithTimeout(url, init, 'Some API');
650
+ *
651
+ * Pass `timeoutMs` as a fourth argument to override the default for a pack
652
+ * with a known-slower upstream; the label should be the same short name you'd
653
+ * pass to `httpError`/`httpErrorMessage` for that call.
654
+ */
655
+ async function fetchWithTimeout(
656
+ url: string | URL,
657
+ init: RequestInit = {},
658
+ name: string,
659
+ timeoutMs: number = DEFAULT_FETCH_TIMEOUT_MS,
660
+ ): Promise<Response> {
661
+ try {
662
+ return await fetch(url, { ...init, signal: AbortSignal.timeout(timeoutMs) });
663
+ } catch (err) {
664
+ if (err instanceof Error && (err.name === 'TimeoutError' || err.name === 'AbortError')) {
665
+ // States the OBSERVATION (no response in N seconds), not a diagnosis.
666
+ // "appears to be degraded" is an inference about the vendor that we have
667
+ // not checked, and it is wrong in a way that misdirects whoever reads it:
668
+ // a timeout from a Worker can equally mean OUR egress is blocked.
669
+ //
670
+ // Measured today (2026-09-01, fleet #1047): every call to
671
+ // mainnet.base.org failed from the x402 facilitator while the identical
672
+ // request from a laptop returned 200. Base was entirely healthy; the
673
+ // public RPC refuses Cloudflare Worker egress. Had this message fired
674
+ // there it would have blamed Base by name, and the next person would have
675
+ // waited for a vendor outage to clear that did not exist.
676
+ // A timeout has no status to test — there is no response at all — so
677
+ // `markInternalOrigin` is called without one: an origin we run that never
678
+ // answered is an availability failure by definition. This is the half of
679
+ // fleet #1096 with neither a SQLSTATE nor a status code to key on.
680
+ throw new Error(
681
+ markInternalOrigin(
682
+ `upstream_down: ${name} did not respond within ${timeoutMs / 1000}s. ` +
683
+ `That can be ${name} being slow or down, or this environment being unable to reach it ` +
684
+ `(some hosts refuse datacenter/Worker egress) — retry shortly, and check reachability ` +
685
+ `from elsewhere before concluding ${name} is down.`,
686
+ url,
687
+ ),
688
+ );
689
+ }
690
+ throw err;
691
+ }
692
+ }
693
+
694
+ function detailSuffix(detail: string): string {
695
+ return detail ? ` — ${detail}` : '';
696
+ }
697
+
698
+ async function readDetail(res: Response): Promise<string> {
699
+ let raw: string;
700
+ try {
701
+ raw = await res.text();
702
+ } catch {
703
+ // Body already consumed, or the connection died mid-read. The status alone
704
+ // is still worth throwing — never let the error path throw its own error.
705
+ return '';
706
+ }
707
+ return summarizeErrorBody(raw);
708
+ }
709
+
710
+ /**
711
+ * Turn ANY error body — JSON, HTML, XML or plain text — into one short phrase
712
+ * that never contains markup.
713
+ *
714
+ * This used to just drop an HTML or XML body on the floor, on the reasoning
715
+ * that markup crowds out the status. That was half right. Dropping it loses the
716
+ * one sentence a caller could have acted on: an `Access Denied` title, an SDMX
717
+ * `<message:Error>` text, an OPS fault string. A 2026-08-30 support sweep
718
+ * measured 13 of 291 caller-facing error rows carrying a raw page or document
719
+ * verbatim, across 11 packs, and in every one of them the useful content —
720
+ * "Access Denied", "Invalid country code", "SCRAPE_TIMEOUT" — was in there,
721
+ * buried in markup the agent had to parse out of a string (fleet #712).
722
+ *
723
+ * So: extract the meaning, discard the markup. The output is passed through
724
+ * `stripMarkup` unconditionally, which is what lets `check:error-body-leak`
725
+ * assert mechanically that no caller-facing message can contain `<?xml`,
726
+ * `<!DOCTYPE` or `<html`.
727
+ */
728
+ function summarizeErrorBody(raw: string): string {
729
+ if (!raw || !raw.trim()) return '';
730
+
731
+ const head = raw.slice(0, 400).trimStart().toLowerCase();
732
+
733
+ // An HTML error page (Cloudflare interstitial, nginx default, a login
734
+ // redirect) says what it is in its <title>, and almost nowhere else.
735
+ if (head.startsWith('<!doctype') || head.startsWith('<html')) {
736
+ const title = htmlTitle(raw);
737
+ return title
738
+ ? `${title} (upstream returned an HTML error page, not an API response)`
739
+ : 'upstream returned an HTML error page, not an API response';
740
+ }
741
+
742
+ // XML fault documents — EPO OPS, SDMX (`<message:Error>`), SOAP faults. The
743
+ // human sentence sits in a child element whose tag name says what it is.
744
+ if (head.startsWith('<?xml') || head.startsWith('<')) {
745
+ const fault = xmlFaultText(raw);
746
+ return fault
747
+ ? `${stripMarkup(fault).slice(0, MAX_DETAIL)} (from the upstream's XML error document)`
748
+ : 'upstream returned an XML error document with no readable message';
749
+ }
750
+
751
+ // Most JSON error bodies bury one human sentence among ids and echoed request
752
+ // params. Prefer that sentence; fall back to the whole body when the shape is
753
+ // unfamiliar, since an unfamiliar shape is exactly when we can least afford to
754
+ // guess wrong and show nothing.
755
+ const fromJson = messageFromJson(raw);
756
+ return stripMarkup(fromJson ?? raw).slice(0, MAX_DETAIL);
757
+ }
758
+
759
+ /** The `<title>` of an HTML error page, or its first `<h1>` — the two places a
760
+ * bot wall, a 502 and an "Access Denied" all state what happened. */
761
+ function htmlTitle(raw: string): string | null {
762
+ const head = raw.slice(0, 4000);
763
+ for (const re of [/<title[^>]*>([\s\S]*?)<\/title>/i, /<h1[^>]*>([\s\S]*?)<\/h1>/i]) {
764
+ const m = re.exec(head);
765
+ const text = m ? stripMarkup(m[1]) : '';
766
+ if (text) return text.slice(0, 160);
767
+ }
768
+ return null;
769
+ }
770
+
771
+ /** Tag names that carry the explanation in an XML fault document, namespace
772
+ * prefix optional (`<message:Error>`, `<com:Text>`, `<faultstring>`). */
773
+ const XML_FAULT_TAG_RE =
774
+ /<(?:[A-Za-z0-9_.-]+:)?(?:text|message|description|faultstring|reason|detail|title|errormessage|error)\b[^>]*>([^<]{2,400})</i;
775
+
776
+ function xmlFaultText(raw: string): string | null {
777
+ const head = raw.slice(0, 8000);
778
+ const tagged = XML_FAULT_TAG_RE.exec(head);
779
+ if (tagged && tagged[1].trim()) return tagged[1];
780
+
781
+ // Nothing conventionally named — take the longest text node instead. A fault
782
+ // document with one sentence in an oddly named element is still readable;
783
+ // returning nothing at all is not.
784
+ let best = '';
785
+ for (const m of head.matchAll(/>([^<>]{8,400})</g)) {
786
+ const text = m[1].trim();
787
+ if (text.length > best.length) best = text;
788
+ }
789
+ return best || null;
790
+ }
791
+
792
+ /**
793
+ * Remove every tag and stray angle bracket, then collapse whitespace.
794
+ *
795
+ * Applied to everything on the way out, including the JSON and plain-text
796
+ * paths, because an upstream is free to embed markup in a JSON string field —
797
+ * and a leak is a leak regardless of which branch produced it.
798
+ */
799
+ function stripMarkup(s: string): string {
800
+ return collapse(decodeEntities(s.replace(/<[^>]*>/g, ' ')).replace(/[<>]/g, ' '));
801
+ }
802
+
803
+ /** The handful of entities that show up in error-page titles. Decoded AFTER
804
+ * tags are stripped and BEFORE the angle-bracket sweep, so `&lt;script&gt;`
805
+ * in a title cannot decode into markup that survives — EMBL-EBI's ChEMBL 500
806
+ * page renders as `500 Internal Server Error &lt; EMBL-EBI` otherwise. */
807
+ function decodeEntities(s: string): string {
808
+ return s
809
+ .replace(/&(?:amp|#0*38);/gi, '&')
810
+ .replace(/&(?:lt|#0*60);/gi, '<')
811
+ .replace(/&(?:gt|#0*62);/gi, '>')
812
+ .replace(/&(?:quot|#0*34);/gi, '"')
813
+ .replace(/&(?:#0*39|apos|#x0*27);/gi, "'")
814
+ .replace(/&nbsp;/gi, ' ');
815
+ }
816
+
817
+ /** The conventional "what went wrong" field, under any of the names upstreams
818
+ * actually use. Checked in order; first non-empty string wins. */
819
+ const MESSAGE_KEYS = [
820
+ 'message', 'error_message', 'errorMessage', 'detail', 'details',
821
+ 'description', 'error_description', 'reason', 'title', 'fault',
822
+ ];
823
+
824
+ function messageFromJson(raw: string): string | null {
825
+ let parsed: unknown;
826
+ try {
827
+ parsed = JSON.parse(raw);
828
+ } catch {
829
+ return null;
830
+ }
831
+ return pickMessage(parsed, 0);
832
+ }
833
+
834
+ function pickMessage(node: unknown, depth: number): string | null {
835
+ // Two levels covers `{error: {message}}` and `{errors: [{detail}]}`, the two
836
+ // shapes that account for nearly all of them, without walking a large payload.
837
+ if (depth > 2 || node == null) return null;
838
+
839
+ if (typeof node === 'string') return node.trim() || null;
840
+
841
+ if (Array.isArray(node)) {
842
+ for (const item of node) {
843
+ const found = pickMessage(item, depth + 1);
844
+ if (found) return found;
845
+ }
846
+ return null;
847
+ }
848
+
849
+ if (typeof node !== 'object') return null;
850
+ const obj = node as Record<string, unknown>;
851
+
852
+ for (const key of MESSAGE_KEYS) {
853
+ const v = obj[key];
854
+ if (typeof v === 'string' && v.trim()) return v.trim();
855
+ }
856
+ // `{error: …}` where error is itself an object or a string — the single most
857
+ // common wrapper, so it is worth descending into by name rather than scanning
858
+ // every key and risking picking up an echoed request parameter.
859
+ for (const key of ['error', 'errors', 'fault', 'Error', 'data']) {
860
+ if (key in obj) {
861
+ const found = pickMessage(obj[key], depth + 1);
862
+ if (found) return found;
863
+ }
864
+ }
865
+ return null;
866
+ }
867
+
868
+ /** Errors are read in a single line of log output; newlines and runs of
869
+ * whitespace make a multi-line body unreadable there. */
870
+ function collapse(s: string): string {
871
+ return s.replace(/\s+/g, ' ').trim();
872
+ }
873
+ /**
874
+ * Business Wire MCP — press releases from Business Wire's all-news RSS feed.
875
+ *
876
+ * Sourced from feed.businesswire.com, keyless, no account, no clickthrough
877
+ * terms (probed live 2026-09-21..22). Public data a Disallow line does not
878
+ * gate — see the "public data is public data" ruling in project CLAUDE.md.
879
+ *
880
+ * WHY WAVE 1 CALLED THIS WIRE DEAD, AND WHY IT IS NOT (fleet #2286): the
881
+ * wave-1 probe recorded "a 993-byte 200 with an empty items array". Those
882
+ * bytes are an RSS envelope with ZERO items whose <description> reads
883
+ * "RSS channel ID is not available in the request. Please make sure you have
884
+ * a valid RSS link." — Business Wire answers an invalid or missing `rss=`
885
+ * token with HTTP 200 and an error written inside the channel, not with a
886
+ * 4xx. Reproduced at 1,001 bytes with `?rss=bogus`. The status line cannot
887
+ * tell that apart from a quiet feed; `count` / `newest_item_at` can.
888
+ *
889
+ * WHERE THE TOKEN CAME FROM: www.businesswire.com is behind Akamai and
890
+ * answers a non-browser User-Agent with 403 on every path, including its
891
+ * own robots.txt and the RSS index page. feed.businesswire.com is a
892
+ * different host that answers our own identity fine, and ITS robots.txt
893
+ * lists this feed as a Sitemap line:
894
+ * Sitemap: http://feed.businesswire.com/mrss/home/?rss=G1QFDERJXkJcFVJYWQ==
895
+ * The same token on the /rss/ path (below) is Business Wire's full all-news
896
+ * channel. Tokens are opaque per-channel ids, not readable paths; per-
897
+ * industry channels exist (e.g. G1QFDERJXkJeEFpRVQ== is "Communications
898
+ * News", vnsId 31209) but the index that maps them is on the blocked host,
899
+ * so this pack reads the all-news channel only.
900
+ *
901
+ * COVERAGE, STATED HONESTLY: the all-news channel is DENSE — measured live
902
+ * 2026-09-22: 3,492 items spanning a rolling ~7-day window (oldest
903
+ * 2026-09-15 01:37 UTC, newest 2026-09-22 01:30 UTC), ~4 MB of XML fetched
904
+ * in under half a second. That is a week of the whole wire, which is far
905
+ * more than the ~20-item windows of the GlobeNewswire / PR Newswire packs,
906
+ * but it is still a rolling window and NOT an archive: every call re-fetches
907
+ * it live, and a release older than about a week is gone.
908
+ *
909
+ * ITEM SHAPE: title, pubDate, description, link (with a `feedref=` tracking
910
+ * token this pack strips), guid (Business Wire's own release id, e.g.
911
+ * `20260921847964en`), and an image enclosure. There is NO issuer tag. The
912
+ * description opens with the wire's dateline — "BENGALURU, India--(BUSINESS
913
+ * WIRE)--Rippling, the ..." — so this pack splits on the `--(BUSINESS WIRE)--`
914
+ * marker into `dateline` and `excerpt`. The issuer usually leads the excerpt
915
+ * but is not a separate field; search matches headline + excerpt.
916
+ *
917
+ * feed-parse.ts`, hardened against 85 live feeds, fleet #2285). Its
918
+ * `newestItemAt` is how a caller tells a quiet wire from a dead one.
919
+ */
920
+
921
+
922
+ const SOURCE = 'Business Wire';
923
+ const FEED_URL = 'https://feed.businesswire.com/rss/home/?rss=G1QFDERJXkJcFVJYWQ==';
924
+ /** Our own identity. feed.businesswire.com serves this UA; forging a browser is neither needed nor done. */
925
+ const UA = 'pipeworx-mcp/1.0 (+https://pipeworx.io)';
926
+ const COVERAGE_NOTE =
927
+ "Business Wire's all-news RSS channel: every release on the wire for a rolling window of roughly the last 7 days " +
928
+ '(measured ~3,500 releases). Every call re-fetches this window live; it is not an archive, and releases older ' +
929
+ 'than about a week are no longer in it.';
930
+ const DATELINE_MARKER = /--\(BUSINESS WIRE\)--/;
931
+ const MAX_LIMIT = 100;
932
+
933
+ /**
934
+ * Retries a non-2xx up to 3 attempts total (the wave-1 wires found transient
935
+ * bot-management redirects that 404 on ~1 in 3 fresh requests at PR Newswire;
936
+ * not observed here, but cheap insurance against reporting the wire dead on
937
+ * a hiccup). A 200 whose channel carries the "RSS channel ID is not available"
938
+ * error is reported as exactly that, because it is what the wave-1 probe
939
+ * mistook for a dead wire.
940
+ */
941
+ const FEED_TTL_MS = 60_000;
942
+ /** The channel is ~4 MB; one fetch per isolate per minute, whatever the arguments. Populated lazily at request time. */
943
+ let memo: { xml: string; at: number } | null = null;
944
+
945
+ async function bwFetch(): Promise<{ xml: string; via: string }> {
946
+ if (memo && Date.now() - memo.at < FEED_TTL_MS) return { xml: memo.xml, via: FEED_URL };
947
+ let res: Response | null = null;
948
+ for (let attempt = 0; attempt < 3; attempt++) {
949
+ res = await fetchWithTimeout(FEED_URL, { headers: { 'User-Agent': UA, Accept: 'application/rss+xml, application/xml, text/xml, */*' } }, SOURCE);
950
+ if (res.ok) break;
951
+ }
952
+ if (!res || !res.ok) throw new Error(`upstream_down: ${SOURCE} answered HTTP ${res?.status} for its RSS feed (after 3 attempts).`);
953
+ const xml = await res.text();
954
+ if (!/<item[\s>]/i.test(xml)) {
955
+ const channelDesc = feedTag(xml, 'description');
956
+ if (/RSS channel ID is not available/i.test(channelDesc)) {
957
+ throw new Error(
958
+ `upstream_down: ${SOURCE} answered HTTP 200 with zero items and the channel description "${channelDesc}" — ` +
959
+ 'the feed token this pack uses is no longer accepted. Business Wire reports an invalid token as a 200, ' +
960
+ 'not a 4xx.',
961
+ );
962
+ }
963
+ throw new Error(
964
+ `upstream_down: ${SOURCE} answered HTTP ${res.status} with no <item> elements (${xml.length} bytes) — ` +
965
+ 'the feed may be temporarily empty or its shape has changed.',
966
+ );
967
+ }
968
+ memo = { xml, at: Date.now() };
969
+ return { xml, via: FEED_URL };
970
+ }
971
+
972
+ interface Release {
973
+ headline: string;
974
+ release_id: string | null;
975
+ /** BCP-47-ish code from the release id / URL path: "en", "zh-CN", "ja", "de"… The channel is MULTILINGUAL. */
976
+ language: string | null;
977
+ dateline: string | null;
978
+ url: string;
979
+ canonical_url: string;
980
+ published_at?: string;
981
+ excerpt?: string;
982
+ image_url?: string;
983
+ }
984
+
985
+ /** Business Wire links carry a per-feed `feedref=` tracking token; the path alone is the release. */
986
+ function stripTracking(link: string): string {
987
+ try {
988
+ const u = new URL(link);
989
+ u.search = '';
990
+ u.hash = '';
991
+ u.protocol = 'https:';
992
+ let s = u.toString();
993
+ if (s.endsWith('/')) s = s.slice(0, -1);
994
+ return s;
995
+ } catch {
996
+ return link.split('?')[0].replace(/\/$/, '');
997
+ }
998
+ }
999
+
1000
+ function splitDateline(description: string): { dateline: string | null; excerpt: string | undefined } {
1001
+ const m = DATELINE_MARKER.exec(description);
1002
+ if (!m) return { dateline: null, excerpt: description.slice(0, 400) || undefined };
1003
+ const dateline = description.slice(0, m.index).trim() || null;
1004
+ const body = description.slice(m.index + m[0].length).trim();
1005
+ return { dateline, excerpt: body.slice(0, 400) || undefined };
1006
+ }
1007
+
1008
+ function toReleases(xml: string): { releases: Release[]; newestItemAt: string | null } {
1009
+ const parsed = parseFeed(xml);
1010
+ const blocks = feedItemBlocks(xml);
1011
+ const releases: Release[] = parsed.items.map((item, i) => {
1012
+ const block = blocks[i] ?? '';
1013
+ // parseFeed's excerpt is already entity-decoded and tag-stripped; the raw
1014
+ // <description> is needed only for the dateline split, which is plain text.
1015
+ const { dateline, excerpt } = splitDateline(item.excerpt ?? '');
1016
+ const enclosure = /<enclosure[^>]*url=["']([^"']+)["']/i.exec(block);
1017
+ const releaseId = feedTag(block, 'guid') || null;
1018
+ // guid is <14-digit id><language>, e.g. 20260921847964en / 20260917064237zh-CN;
1019
+ // the same code is the URL path segment after the id.
1020
+ const langFromId = releaseId ? /^\d{14}([A-Za-z]{2}(?:-[A-Za-z]{2,4})?)$/.exec(releaseId) : null;
1021
+ const langFromUrl = /\/news\/home\/\d{14}\/([A-Za-z]{2}(?:-[A-Za-z]{2,4})?)\//.exec(item.link);
1022
+ return {
1023
+ headline: item.title,
1024
+ release_id: releaseId,
1025
+ language: langFromId?.[1] ?? langFromUrl?.[1] ?? null,
1026
+ dateline,
1027
+ url: item.link,
1028
+ canonical_url: stripTracking(item.link),
1029
+ published_at: item.publishedAt,
1030
+ excerpt,
1031
+ image_url: enclosure ? enclosure[1] : undefined,
1032
+ };
1033
+ });
1034
+ releases.sort((a, b) => (Date.parse(b.published_at ?? '') || 0) - (Date.parse(a.published_at ?? '') || 0));
1035
+ return { releases, newestItemAt: parsed.newestItemAt };
1036
+ }
1037
+
1038
+ function applySince(releases: Release[], sinceHours: number | null): Release[] {
1039
+ if (sinceHours == null) return releases;
1040
+ const cutoff = Date.now() - sinceHours * 3_600_000;
1041
+ return releases.filter((r) => r.published_at && Date.parse(r.published_at) >= cutoff);
1042
+ }
1043
+
1044
+ /**
1045
+ * The all-news channel carries every language Business Wire distributes in
1046
+ * (measured 2026-09-22: the newest item was a zh-CN re-release of a 09-17
1047
+ * English original), so an unfiltered "latest" can lead with Chinese or
1048
+ * Japanese rows. `language` narrows to one code; "en" is the usual ask.
1049
+ * Matching is case-insensitive on the full code ("zh-CN") or its primary
1050
+ * subtag ("zh").
1051
+ */
1052
+ function applyLanguage(releases: Release[], language: string | null): Release[] {
1053
+ if (!language) return releases;
1054
+ const want = language.toLowerCase();
1055
+ return releases.filter((r) => {
1056
+ const have = (r.language ?? '').toLowerCase();
1057
+ return have === want || have.split('-')[0] === want;
1058
+ });
1059
+ }
1060
+
1061
+ function strArg(v: unknown): string | null {
1062
+ const s = typeof v === 'string' ? v.trim() : '';
1063
+ return s ? s : null;
1064
+ }
1065
+
1066
+ const LANGUAGE_ARG = {
1067
+ type: 'string',
1068
+ description: 'Restrict to one language code, e.g. "en" (the channel is multilingual: en, zh-CN, zh-HK, ja, de, fr, es… — a release can appear once per language). Omit for all languages.',
1069
+ };
1070
+
1071
+ const tools: McpToolExport['tools'] = [
1072
+ {
1073
+ name: 'businesswire_latest_releases',
1074
+ description:
1075
+ "Latest press releases from Business Wire's all-news RSS channel (source: businesswire.com). Returns headline, " +
1076
+ 'dateline, language, published time, excerpt and release id for each release, newest first. The channel holds ' +
1077
+ "roughly the last 7 days of the whole wire (~3,500 releases, every language Business Wire distributes in) — " +
1078
+ "not Business Wire's full archive. Pass language \"en\" for English only.",
1079
+ inputSchema: {
1080
+ type: 'object' as const,
1081
+ properties: {
1082
+ since_hours: { type: 'number', description: 'Only return releases published within the last N hours (optional; the window covers about 168).' },
1083
+ language: LANGUAGE_ARG,
1084
+ limit: { type: 'number', description: `Max releases to return, 1-${MAX_LIMIT} (default 20).` },
1085
+ },
1086
+ },
1087
+ },
1088
+ {
1089
+ name: 'businesswire_search_releases',
1090
+ description:
1091
+ "Keyword/company search over Business Wire's current all-news channel (source: businesswire.com). Matches " +
1092
+ 'against headline, dateline and excerpt across roughly the last 7 days of the wire (~3,500 releases, all ' +
1093
+ 'languages unless language is set) — not a historical archive.',
1094
+ inputSchema: {
1095
+ type: 'object' as const,
1096
+ properties: {
1097
+ query: { type: 'string', description: 'Company name or keyword to match.' },
1098
+ since_hours: { type: 'number', description: 'Only consider releases published within the last N hours (optional).' },
1099
+ language: LANGUAGE_ARG,
1100
+ limit: { type: 'number', description: `Max matches to return, 1-${MAX_LIMIT} (default 20).` },
1101
+ },
1102
+ required: ['query'],
1103
+ },
1104
+ },
1105
+ {
1106
+ name: 'businesswire_get_release',
1107
+ description:
1108
+ 'Fetch one specific Business Wire release by its URL or release id (source: businesswire.com), as returned ' +
1109
+ 'by businesswire_latest_releases or businesswire_search_releases. Re-fetches the live channel each call — a ' +
1110
+ 'release older than the ~7-day window will not be found.',
1111
+ inputSchema: {
1112
+ type: 'object' as const,
1113
+ properties: {
1114
+ url: { type: 'string', description: 'The release URL or release_id (e.g. 20260921847964en) from a prior businesswire tool call.' },
1115
+ },
1116
+ required: ['url'],
1117
+ },
1118
+ },
1119
+ ];
1120
+
1121
+ async function callTool(name: string, args: Record<string, unknown>): Promise<unknown> {
1122
+ switch (name) {
1123
+ case 'businesswire_latest_releases': {
1124
+ const { xml, via } = await bwFetch();
1125
+ const { releases, newestItemAt } = toReleases(xml);
1126
+ const language = strArg(args.language);
1127
+ const out = applyLanguage(applySince(releases, numArg(args.since_hours)), language);
1128
+ const limit = clamp(numArg(args.limit) ?? 20, 1, MAX_LIMIT);
1129
+ return {
1130
+ source: SOURCE,
1131
+ feed_url: via,
1132
+ coverage: COVERAGE_NOTE,
1133
+ newest_item_at: newestItemAt,
1134
+ oldest_item_at: releases.length ? releases[releases.length - 1].published_at ?? null : null,
1135
+ ...(language ? { language } : {}),
1136
+ count: Math.min(out.length, limit),
1137
+ total_in_window: releases.length,
1138
+ releases: out.slice(0, limit),
1139
+ };
1140
+ }
1141
+ case 'businesswire_search_releases': {
1142
+ const query = String(args.query ?? '').trim();
1143
+ if (!query) throw new Error('user_error: Pass a non-empty `query` (company name or keyword).');
1144
+ const { xml } = await bwFetch();
1145
+ const { releases, newestItemAt } = toReleases(xml);
1146
+ const language = strArg(args.language);
1147
+ const pool = applyLanguage(applySince(releases, numArg(args.since_hours)), language);
1148
+ const q = query.toLowerCase();
1149
+ const matches = pool.filter(
1150
+ (r) =>
1151
+ r.headline.toLowerCase().includes(q) ||
1152
+ (r.dateline ?? '').toLowerCase().includes(q) ||
1153
+ (r.excerpt ?? '').toLowerCase().includes(q),
1154
+ );
1155
+ const limit = clamp(numArg(args.limit) ?? 20, 1, MAX_LIMIT);
1156
+ return {
1157
+ source: SOURCE,
1158
+ coverage: COVERAGE_NOTE,
1159
+ newest_item_at: newestItemAt,
1160
+ query,
1161
+ ...(language ? { language } : {}),
1162
+ scanned: pool.length,
1163
+ count: Math.min(matches.length, limit),
1164
+ releases: matches.slice(0, limit),
1165
+ };
1166
+ }
1167
+ case 'businesswire_get_release': {
1168
+ const raw = String(args.url ?? '').trim();
1169
+ if (!raw) throw new Error('user_error: Pass the release `url` (or release_id) from a prior businesswire tool call.');
1170
+ const { xml } = await bwFetch();
1171
+ const { releases, newestItemAt } = toReleases(xml);
1172
+ const target = /^https?:\/\//i.test(raw) ? stripTracking(raw) : null;
1173
+ const hit = releases.find(
1174
+ (r) =>
1175
+ (target !== null && (r.canonical_url === target || r.url === raw)) ||
1176
+ (r.release_id !== null && (r.release_id === raw || (target === null && raw.includes(r.release_id)))),
1177
+ );
1178
+ if (!hit) {
1179
+ throw new Error(
1180
+ "user_error: Not found in the current channel window (Business Wire's all-news channel keeps roughly the " +
1181
+ 'last 7 days). This tool re-fetches the live channel on every call; call businesswire_latest_releases or ' +
1182
+ 'businesswire_search_releases first to get a URL or release_id still in the window.',
1183
+ );
1184
+ }
1185
+ return { source: SOURCE, coverage: COVERAGE_NOTE, newest_item_at: newestItemAt, release: hit };
1186
+ }
1187
+ default:
1188
+ throw new Error(`Unknown tool: ${name}`);
1189
+ }
1190
+ }
1191
+
1192
+ function numArg(v: unknown): number | null {
1193
+ const n = typeof v === 'number' ? v : typeof v === 'string' ? Number(v) : NaN;
1194
+ return Number.isFinite(n) ? n : null;
1195
+ }
1196
+ function clamp(n: number, lo: number, hi: number): number {
1197
+ return Math.max(lo, Math.min(hi, Math.round(n)));
1198
+ }
1199
+
1200
+ export default { tools, callTool, meter: { credits: 1 } } satisfies McpToolExport;