@cliwant/mcp-sam-gov 1.4.0 → 1.6.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 (140) hide show
  1. package/LICENSE +21 -21
  2. package/README.ja.md +240 -231
  3. package/README.ko.md +240 -231
  4. package/README.md +725 -706
  5. package/dist/cbp-border.d.ts +51 -0
  6. package/dist/cbp-border.d.ts.map +1 -0
  7. package/dist/cbp-border.js +123 -0
  8. package/dist/cbp-border.js.map +1 -0
  9. package/dist/datagov-catalog.d.ts.map +1 -1
  10. package/dist/datagov-catalog.js +16 -2
  11. package/dist/datagov-catalog.js.map +1 -1
  12. package/dist/ecfr.d.ts +2 -2
  13. package/dist/ecfr.d.ts.map +1 -1
  14. package/dist/ecfr.js +24 -10
  15. package/dist/ecfr.js.map +1 -1
  16. package/dist/edgar.d.ts.map +1 -1
  17. package/dist/edgar.js +26 -6
  18. package/dist/edgar.js.map +1 -1
  19. package/dist/epa-envirofacts.d.ts.map +1 -1
  20. package/dist/epa-envirofacts.js +14 -1
  21. package/dist/epa-envirofacts.js.map +1 -1
  22. package/dist/errors.d.ts +10 -0
  23. package/dist/errors.d.ts.map +1 -1
  24. package/dist/errors.js +11 -0
  25. package/dist/errors.js.map +1 -1
  26. package/dist/far.d.ts.map +1 -1
  27. package/dist/far.js +3 -1
  28. package/dist/far.js.map +1 -1
  29. package/dist/federal-register.d.ts +2 -2
  30. package/dist/federal-register.d.ts.map +1 -1
  31. package/dist/federal-register.js +26 -10
  32. package/dist/federal-register.js.map +1 -1
  33. package/dist/feedback.d.ts +64 -0
  34. package/dist/feedback.d.ts.map +1 -0
  35. package/dist/feedback.js +131 -0
  36. package/dist/feedback.js.map +1 -0
  37. package/dist/fema.d.ts +36 -0
  38. package/dist/fema.d.ts.map +1 -1
  39. package/dist/fema.js +124 -0
  40. package/dist/fema.js.map +1 -1
  41. package/dist/gov-domains.d.ts +66 -0
  42. package/dist/gov-domains.d.ts.map +1 -0
  43. package/dist/gov-domains.js +211 -0
  44. package/dist/gov-domains.js.map +1 -0
  45. package/dist/nist-controls.d.ts +48 -0
  46. package/dist/nist-controls.d.ts.map +1 -0
  47. package/dist/nist-controls.js +174 -0
  48. package/dist/nist-controls.js.map +1 -0
  49. package/dist/nws-weather.d.ts +57 -0
  50. package/dist/nws-weather.d.ts.map +1 -0
  51. package/dist/nws-weather.js +131 -0
  52. package/dist/nws-weather.js.map +1 -0
  53. package/dist/openfda-drugsfda.d.ts +72 -0
  54. package/dist/openfda-drugsfda.d.ts.map +1 -0
  55. package/dist/openfda-drugsfda.js +230 -0
  56. package/dist/openfda-drugsfda.js.map +1 -0
  57. package/dist/openfda.d.ts.map +1 -1
  58. package/dist/openfda.js +31 -8
  59. package/dist/openfda.js.map +1 -1
  60. package/dist/server.d.ts.map +1 -1
  61. package/dist/server.js +374 -11
  62. package/dist/server.js.map +1 -1
  63. package/dist/treasury.d.ts +2 -0
  64. package/dist/treasury.d.ts.map +1 -1
  65. package/dist/treasury.js +7 -0
  66. package/dist/treasury.js.map +1 -1
  67. package/dist/usaspending.d.ts +32 -1
  68. package/dist/usaspending.d.ts.map +1 -1
  69. package/dist/usaspending.js +143 -16
  70. package/dist/usaspending.js.map +1 -1
  71. package/package.json +111 -111
  72. package/src/attachments.ts +652 -652
  73. package/src/bea.ts +372 -372
  74. package/src/bls.ts +1943 -1943
  75. package/src/cache.ts +73 -73
  76. package/src/cbp-border.ts +177 -0
  77. package/src/census-economic.ts +431 -431
  78. package/src/census.ts +735 -735
  79. package/src/ckan.ts +495 -495
  80. package/src/clinicaltrials.ts +923 -923
  81. package/src/cms-facility.ts +379 -379
  82. package/src/cms-hospital.ts +344 -344
  83. package/src/cms-supplier.ts +527 -527
  84. package/src/cms-utilization.ts +389 -389
  85. package/src/cms.ts +634 -634
  86. package/src/coerce.ts +47 -47
  87. package/src/courtlistener.ts +465 -465
  88. package/src/cpsc.ts +333 -333
  89. package/src/datagov-catalog.ts +312 -296
  90. package/src/datagov.ts +907 -907
  91. package/src/datagovKey.ts +68 -68
  92. package/src/datasource.ts +721 -721
  93. package/src/disclosure.ts +61 -61
  94. package/src/dol.ts +515 -515
  95. package/src/ecfr.ts +248 -231
  96. package/src/echo.ts +496 -496
  97. package/src/edgar.ts +3046 -3014
  98. package/src/epa-envirofacts.ts +358 -342
  99. package/src/errors.ts +324 -303
  100. package/src/fac.ts +529 -529
  101. package/src/far.ts +1009 -1007
  102. package/src/fdic.ts +2052 -2052
  103. package/src/federal-register.ts +725 -706
  104. package/src/feedback.ts +160 -0
  105. package/src/fema.ts +680 -541
  106. package/src/fpds.ts +620 -620
  107. package/src/fred.ts +464 -464
  108. package/src/gao.ts +744 -744
  109. package/src/gov-domains.ts +237 -0
  110. package/src/govinfo.ts +497 -497
  111. package/src/grants.ts +290 -290
  112. package/src/gsa-csv.ts +992 -992
  113. package/src/gsa-perdiem.ts +361 -361
  114. package/src/integrity.ts +928 -928
  115. package/src/keys.ts +268 -268
  116. package/src/lda.ts +385 -385
  117. package/src/meta.ts +292 -292
  118. package/src/nhtsa.ts +352 -352
  119. package/src/nih.ts +375 -375
  120. package/src/nist-controls.ts +219 -0
  121. package/src/nonprofit.ts +460 -460
  122. package/src/nppes.ts +834 -834
  123. package/src/nsf.ts +706 -706
  124. package/src/nvd.ts +1124 -1124
  125. package/src/nws-weather.ts +167 -0
  126. package/src/ofac.ts +1166 -1166
  127. package/src/openfda-device.ts +356 -356
  128. package/src/openfda-drugsfda.ts +313 -0
  129. package/src/openfda.ts +518 -495
  130. package/src/pricing.ts +1075 -1075
  131. package/src/sam-gov/client.ts +774 -774
  132. package/src/sam-gov/index.ts +32 -32
  133. package/src/sam-gov/types.ts +152 -152
  134. package/src/sba.ts +357 -357
  135. package/src/server.ts +6688 -6297
  136. package/src/snapshot.ts +223 -223
  137. package/src/socrata.ts +532 -532
  138. package/src/treasury.ts +582 -575
  139. package/src/usaspending.ts +2852 -2680
  140. package/src/usitc.ts +420 -420
package/src/ecfr.ts CHANGED
@@ -1,231 +1,248 @@
1
- /**
2
- * eCFR (Electronic Code of Federal Regulations) wrappers (keyless).
3
- *
4
- * eCFR is the up-to-date version of the CFR — Title 48 = FAR (Federal
5
- * Acquisition Regulation), Title 2 = Federal financial assistance, etc.
6
- * For a federal contractor, eCFR is the primary source for regulation
7
- * text the agent should quote when answering compliance questions.
8
- *
9
- * Endpoints:
10
- * - /versioner/v1/titles.json — list 50 CFR titles + last-amended dates
11
- * - /search/v1/results — full-text search across the entire CFR
12
- *
13
- * Both keyless. Documented at https://www.ecfr.gov/developers/.
14
- */
15
-
16
- import { fetchWithRetry } from "./errors.js";
17
- import { driftError } from "./datasource.js";
18
- import { memoize } from "./cache.js";
19
- import { withMeta } from "./meta.js";
20
-
21
- const ECFR = "https://www.ecfr.gov/api";
22
-
23
- // eCFR's /search/v1/results `meta.total_count` is capped by Elasticsearch's
24
- // `index.max_result_window` (live-verified 10,000). A total AT OR ABOVE this
25
- // sentinel is a LOWER BOUND, not an exact count — see the totalIsLowerBound
26
- // wiring in `search` (D1). A genuine count below this stays exact.
27
- const ECFR_TOTAL_COUNT_CAP = 10000;
28
-
29
- async function fetchJson<T>(url: string): Promise<T> {
30
- const r = await fetchWithRetry(
31
- url,
32
- {
33
- headers: { Accept: "application/json" },
34
- signal: AbortSignal.timeout(15_000),
35
- },
36
- `ecfr:${url.split("/api/")[1] ?? url}`,
37
- );
38
- return (await r.json()) as T;
39
- }
40
-
41
- export async function listTitles() {
42
- // 50 CFR titles change very infrequently. Cache aggressively (5 min).
43
- return memoize("ecfr:titles", async () => {
44
- type Resp = {
45
- titles?: {
46
- number?: number;
47
- name?: string;
48
- latest_amended_on?: string;
49
- latest_issue_date?: string;
50
- up_to_date_as_of?: string;
51
- reserved?: boolean;
52
- }[];
53
- };
54
- const json = await fetchJson<Resp>(`${ECFR}/versioner/v1/titles.json`);
55
- return {
56
- titles: (json.titles ?? []).map((t) => ({
57
- number: t.number ?? 0,
58
- name: t.name ?? "",
59
- latestAmendedOn: t.latest_amended_on,
60
- latestIssueDate: t.latest_issue_date,
61
- upToDateAsOf: t.up_to_date_as_of,
62
- reserved: !!t.reserved,
63
- })),
64
- };
65
- });
66
- }
67
-
68
- export async function search(args: {
69
- query: string;
70
- titleNumber?: number;
71
- chapter?: number;
72
- perPage?: number;
73
- }) {
74
- const url = new URL(`${ECFR}/search/v1/results`);
75
- url.searchParams.set("query", args.query);
76
- url.searchParams.set("per_page", String(args.perPage ?? 5));
77
- if (args.titleNumber) {
78
- // eCFR search filter: hierarchy[title]=N (NOT just title=N — that's
79
- // an "unpermitted parameter" error from the eCFR API).
80
- url.searchParams.set("hierarchy[title]", String(args.titleNumber));
81
- }
82
- // Optional chapter filter (additive; existing ecfr_search callers pass none).
83
- // Within Title 48: chapter 1 = FAR, chapter 2 = DFARS, chapter 5 = GSAM, etc.
84
- // This is what lets far_search scope to FAR/DFARS and keep GSAM/agency
85
- // supplements out server-side. Same hierarchy[…] contract as the title filter.
86
- if (args.chapter !== undefined) {
87
- url.searchParams.set("hierarchy[chapter]", String(args.chapter));
88
- }
89
-
90
- type Resp = {
91
- results?: {
92
- starts_on?: string;
93
- ends_on?: string | null;
94
- type?: string;
95
- hierarchy?: {
96
- title?: string;
97
- chapter?: string;
98
- subchapter?: string;
99
- part?: string;
100
- subpart?: string;
101
- section?: string;
102
- };
103
- hierarchy_headings?: Record<string, string | null>;
104
- headings?: Record<string, string | null>;
105
- full_text_excerpt?: string;
106
- score?: number;
107
- }[];
108
- meta?: {
109
- current_page?: number;
110
- total_pages?: number;
111
- total_count?: number;
112
- max_score?: number;
113
- description?: string;
114
- };
115
- };
116
- const json = await fetchJson<Resp>(url.toString());
117
- // F6 (P2 empty-vs-outage): a 200 whose `results` is PRESENT-but-non-array — or
118
- // a body carrying NEITHER a `results` array NOR a `meta` object — is drift / an
119
- // unexpected shape, NOT a genuine no-match. Throw (schema_drift) rather than
120
- // letting `(json.results ?? []).map` coalesce it into a fake AUTHORITATIVE empty.
121
- // A GENUINE empty (results:[] with meta.total_count:0) flows through honestly
122
- // below. (eCFR normally signals errors via HTTP status caught by fetchWithRetry;
123
- // this closes the previously-unhandled + untested 200-body drift path.)
124
- if (json.results !== undefined && !Array.isArray(json.results)) {
125
- throw driftError(
126
- "ecfr.gov",
127
- "eCFR search returned HTTP 200 but `results` is not an array — treating it as schema drift, NOT an empty result set.",
128
- );
129
- }
130
- if (json.results === undefined && json.meta === undefined) {
131
- throw driftError(
132
- "ecfr.gov",
133
- "eCFR search returned HTTP 200 with neither a `results` array nor a `meta` object — an unexpected shape; treating it as schema drift, NOT an empty result set.",
134
- );
135
- }
136
- const data = {
137
- results: (json.results ?? []).map((r) => ({
138
- type: r.type ?? "",
139
- title: r.hierarchy?.title ?? "",
140
- chapter: r.hierarchy?.chapter,
141
- part: r.hierarchy?.part,
142
- subpart: r.hierarchy?.subpart,
143
- section: r.hierarchy?.section,
144
- headingPath: Object.values(r.hierarchy_headings ?? {})
145
- .filter(Boolean)
146
- .join(" › "),
147
- excerpt: stripHtml(r.full_text_excerpt ?? ""),
148
- score: r.score ?? 0,
149
- // Stable ecfr.gov URL pattern from the hierarchy
150
- ecfrUrl: r.hierarchy
151
- ? buildEcfrUrl(r.hierarchy)
152
- : "",
153
- effectiveOn: r.starts_on ?? "",
154
- // Additive: the version's end date. null = the CURRENT (in-force) version;
155
- // a non-null date = a HISTORICAL version. eCFR search returns ~5 versions
156
- // per section; existing ecfr_search callers simply ignore this extra field,
157
- // while far_search uses it to collapse historical dups to the current one.
158
- endsOn: r.ends_on ?? null,
159
- })),
160
- };
161
-
162
- // Truthful `_meta` (spec §1.2 A6, §2.3). eCFR returns a hit count in
163
- // `meta.total_count`, BUT that count is capped by Elasticsearch's
164
- // `index.max_result_window` at 10,000 (a real ceiling — unlike a genuine
165
- // small count, a value at/above the cap is a LOWER BOUND, not an exact
166
- // total). Below the cap `totalAvailable` is exact (the AI can tell a top-N
167
- // slice from the full match set); at/above the cap we flag
168
- // `totalIsLowerBound:true` + a note so a broad query that truly matches
169
- // >10,000 sections is NOT reported as if exactly 10,000 (D1). A6: echo the
170
- // applied title scope so the AI can VERIFY it searched the intended corpus —
171
- // Title 48 (FAR) vs every CFR title — rather than silently trusting a filter
172
- // that could return cross-title results if the eCFR param contract ever changes.
173
- const returned = data.results.length;
174
- const totalAvailable =
175
- typeof json.meta?.total_count === "number" ? json.meta.total_count : null;
176
- // eCFR's search total_count saturates at the Elasticsearch max_result_window
177
- // (live-verified 10,000). Use `>= cap` (not `=== cap`) so a total AT OR ABOVE
178
- // the ceiling is treated as a lower bound — strictly safe if the window were
179
- // ever configured higher. Mirrors federal-register.ts's FR_COUNT_CAP and
180
- // edgar.ts's FTS_WINDOW.
181
- const totalIsLowerBound =
182
- totalAvailable !== null && totalAvailable >= ECFR_TOTAL_COUNT_CAP;
183
- const scopeNote =
184
- args.titleNumber !== undefined
185
- ? `searched CFR Title ${args.titleNumber}${
186
- args.titleNumber === 48 ? " (FAR — Federal Acquisition Regulation)" : ""
187
- } only`
188
- : "searched all CFR titles (no title filter applied)";
189
- const notes = [scopeNote];
190
- if (totalIsLowerBound) {
191
- notes.push(
192
- `eCFR caps total_count at ${ECFR_TOTAL_COUNT_CAP} (Elasticsearch index.max_result_window); totalAvailable is a LOWER BOUND — the true match count may be higher and is UNKNOWN. See totalIsLowerBound. Narrow by title/chapter/date for an exact count.`,
193
- );
194
- }
195
- return withMeta(data, {
196
- source: "ecfr.gov/api (search/v1)",
197
- keylessMode: true,
198
- returned,
199
- totalAvailable,
200
- // Explicit boolean (not conditional): a below-cap count is DEFINITIVELY
201
- // exact (totalIsLowerBound:false), an at/above-cap count is a lower bound
202
- // (true). The AI can trust `false` as "this is the real total".
203
- totalIsLowerBound,
204
- truncated:
205
- totalAvailable !== null ? returned < totalAvailable : undefined,
206
- filtersApplied: args.titleNumber !== undefined ? ["titleNumber"] : [],
207
- filtersDropped: [],
208
- fieldsUnavailable: [],
209
- notes,
210
- });
211
- }
212
-
213
- function stripHtml(s: string): string {
214
- return s
215
- .replace(/<[^>]+>/g, "")
216
- .replace(/\s+/g, " ")
217
- .trim();
218
- }
219
-
220
- function buildEcfrUrl(h: {
221
- title?: string;
222
- chapter?: string;
223
- part?: string;
224
- section?: string;
225
- }): string {
226
- const base = `https://www.ecfr.gov/current/title-${h.title}`;
227
- if (h.section) return `${base}/section-${h.section}`;
228
- if (h.part) return `${base}/part-${h.part}`;
229
- if (h.chapter) return `${base}/chapter-${h.chapter}`;
230
- return base;
231
- }
1
+ /**
2
+ * eCFR (Electronic Code of Federal Regulations) wrappers (keyless).
3
+ *
4
+ * eCFR is the up-to-date version of the CFR — Title 48 = FAR (Federal
5
+ * Acquisition Regulation), Title 2 = Federal financial assistance, etc.
6
+ * For a federal contractor, eCFR is the primary source for regulation
7
+ * text the agent should quote when answering compliance questions.
8
+ *
9
+ * Endpoints:
10
+ * - /versioner/v1/titles.json — list 50 CFR titles + last-amended dates
11
+ * - /search/v1/results — full-text search across the entire CFR
12
+ *
13
+ * Both keyless. Documented at https://www.ecfr.gov/developers/.
14
+ */
15
+
16
+ import { fetchWithRetry } from "./errors.js";
17
+ import { driftError } from "./datasource.js";
18
+ import { memoize } from "./cache.js";
19
+ import { withMeta } from "./meta.js";
20
+
21
+ const ECFR = "https://www.ecfr.gov/api";
22
+
23
+ // eCFR's /search/v1/results `meta.total_count` is capped by Elasticsearch's
24
+ // `index.max_result_window` (live-verified 10,000). A total AT OR ABOVE this
25
+ // sentinel is a LOWER BOUND, not an exact count — see the totalIsLowerBound
26
+ // wiring in `search` (D1). A genuine count below this stays exact.
27
+ const ECFR_TOTAL_COUNT_CAP = 10000;
28
+
29
+ async function fetchJson<T>(url: string): Promise<T> {
30
+ const r = await fetchWithRetry(
31
+ url,
32
+ {
33
+ headers: { Accept: "application/json" },
34
+ signal: AbortSignal.timeout(15_000),
35
+ },
36
+ `ecfr:${url.split("/api/")[1] ?? url}`,
37
+ );
38
+ return (await r.json()) as T;
39
+ }
40
+
41
+ export async function listTitles() {
42
+ // 50 CFR titles change very infrequently. Cache aggressively (5 min).
43
+ return memoize("ecfr:titles", async () => {
44
+ type Resp = {
45
+ titles?: {
46
+ number?: number;
47
+ name?: string;
48
+ latest_amended_on?: string;
49
+ latest_issue_date?: string;
50
+ up_to_date_as_of?: string;
51
+ reserved?: boolean;
52
+ }[];
53
+ };
54
+ const json = await fetchJson<Resp>(`${ECFR}/versioner/v1/titles.json`);
55
+ const titles = (json.titles ?? []).map((t) => ({
56
+ number: t.number ?? 0,
57
+ name: t.name ?? "",
58
+ latestAmendedOn: t.latest_amended_on,
59
+ latestIssueDate: t.latest_issue_date,
60
+ upToDateAsOf: t.up_to_date_as_of,
61
+ reserved: !!t.reserved,
62
+ }));
63
+ // Carry the honesty envelope every other tool has (dogfooding 2026-07-15: this
64
+ // list tool previously returned a bare {titles} with no _meta). The titles
65
+ // endpoint returns the COMPLETE canonical CFR title set in one response — no
66
+ // pagination, no filter — so the count IS the total and complete derives true.
67
+ return withMeta(
68
+ { titles },
69
+ {
70
+ source: "ecfr.gov/api (versioner/v1/titles)",
71
+ keylessMode: true,
72
+ returned: titles.length,
73
+ totalAvailable: titles.length,
74
+ truncated: false,
75
+ filtersApplied: [],
76
+ filtersDropped: [],
77
+ notes: [
78
+ "Complete canonical list of all CFR titles — the endpoint returns every title in a single response (no pagination or filtering).",
79
+ ],
80
+ },
81
+ );
82
+ });
83
+ }
84
+
85
+ export async function search(args: {
86
+ query: string;
87
+ titleNumber?: number;
88
+ chapter?: number;
89
+ perPage?: number;
90
+ }) {
91
+ const url = new URL(`${ECFR}/search/v1/results`);
92
+ url.searchParams.set("query", args.query);
93
+ url.searchParams.set("per_page", String(args.perPage ?? 5));
94
+ if (args.titleNumber) {
95
+ // eCFR search filter: hierarchy[title]=N (NOT just title=N — that's
96
+ // an "unpermitted parameter" error from the eCFR API).
97
+ url.searchParams.set("hierarchy[title]", String(args.titleNumber));
98
+ }
99
+ // Optional chapter filter (additive; existing ecfr_search callers pass none).
100
+ // Within Title 48: chapter 1 = FAR, chapter 2 = DFARS, chapter 5 = GSAM, etc.
101
+ // This is what lets far_search scope to FAR/DFARS and keep GSAM/agency
102
+ // supplements out server-side. Same hierarchy[…] contract as the title filter.
103
+ if (args.chapter !== undefined) {
104
+ url.searchParams.set("hierarchy[chapter]", String(args.chapter));
105
+ }
106
+
107
+ type Resp = {
108
+ results?: {
109
+ starts_on?: string;
110
+ ends_on?: string | null;
111
+ type?: string;
112
+ hierarchy?: {
113
+ title?: string;
114
+ chapter?: string;
115
+ subchapter?: string;
116
+ part?: string;
117
+ subpart?: string;
118
+ section?: string;
119
+ };
120
+ hierarchy_headings?: Record<string, string | null>;
121
+ headings?: Record<string, string | null>;
122
+ full_text_excerpt?: string;
123
+ score?: number;
124
+ }[];
125
+ meta?: {
126
+ current_page?: number;
127
+ total_pages?: number;
128
+ total_count?: number;
129
+ max_score?: number;
130
+ description?: string;
131
+ };
132
+ };
133
+ const json = await fetchJson<Resp>(url.toString());
134
+ // F6 (P2 empty-vs-outage): a 200 whose `results` is PRESENT-but-non-array — or
135
+ // a body carrying NEITHER a `results` array NOR a `meta` object — is drift / an
136
+ // unexpected shape, NOT a genuine no-match. Throw (schema_drift) rather than
137
+ // letting `(json.results ?? []).map` coalesce it into a fake AUTHORITATIVE empty.
138
+ // A GENUINE empty (results:[] with meta.total_count:0) flows through honestly
139
+ // below. (eCFR normally signals errors via HTTP status caught by fetchWithRetry;
140
+ // this closes the previously-unhandled + untested 200-body drift path.)
141
+ if (json.results !== undefined && !Array.isArray(json.results)) {
142
+ throw driftError(
143
+ "ecfr.gov",
144
+ "eCFR search returned HTTP 200 but `results` is not an array — treating it as schema drift, NOT an empty result set.",
145
+ );
146
+ }
147
+ if (json.results === undefined && json.meta === undefined) {
148
+ throw driftError(
149
+ "ecfr.gov",
150
+ "eCFR search returned HTTP 200 with neither a `results` array nor a `meta` object — an unexpected shape; treating it as schema drift, NOT an empty result set.",
151
+ );
152
+ }
153
+ const data = {
154
+ results: (json.results ?? []).map((r) => ({
155
+ type: r.type ?? "",
156
+ title: r.hierarchy?.title ?? "",
157
+ chapter: r.hierarchy?.chapter,
158
+ part: r.hierarchy?.part,
159
+ subpart: r.hierarchy?.subpart,
160
+ section: r.hierarchy?.section,
161
+ headingPath: Object.values(r.hierarchy_headings ?? {})
162
+ .filter(Boolean)
163
+ .join(" › "),
164
+ excerpt: stripHtml(r.full_text_excerpt ?? ""),
165
+ score: r.score ?? 0,
166
+ // Stable ecfr.gov URL pattern from the hierarchy
167
+ ecfrUrl: r.hierarchy
168
+ ? buildEcfrUrl(r.hierarchy)
169
+ : "",
170
+ effectiveOn: r.starts_on ?? "",
171
+ // Additive: the version's end date. null = the CURRENT (in-force) version;
172
+ // a non-null date = a HISTORICAL version. eCFR search returns ~5 versions
173
+ // per section; existing ecfr_search callers simply ignore this extra field,
174
+ // while far_search uses it to collapse historical dups to the current one.
175
+ endsOn: r.ends_on ?? null,
176
+ })),
177
+ };
178
+
179
+ // Truthful `_meta` (spec §1.2 A6, §2.3). eCFR returns a hit count in
180
+ // `meta.total_count`, BUT that count is capped by Elasticsearch's
181
+ // `index.max_result_window` at 10,000 (a real ceiling — unlike a genuine
182
+ // small count, a value at/above the cap is a LOWER BOUND, not an exact
183
+ // total). Below the cap `totalAvailable` is exact (the AI can tell a top-N
184
+ // slice from the full match set); at/above the cap we flag
185
+ // `totalIsLowerBound:true` + a note so a broad query that truly matches
186
+ // >10,000 sections is NOT reported as if exactly 10,000 (D1). A6: echo the
187
+ // applied title scope so the AI can VERIFY it searched the intended corpus —
188
+ // Title 48 (FAR) vs every CFR title — rather than silently trusting a filter
189
+ // that could return cross-title results if the eCFR param contract ever changes.
190
+ const returned = data.results.length;
191
+ const totalAvailable =
192
+ typeof json.meta?.total_count === "number" ? json.meta.total_count : null;
193
+ // eCFR's search total_count saturates at the Elasticsearch max_result_window
194
+ // (live-verified 10,000). Use `>= cap` (not `=== cap`) so a total AT OR ABOVE
195
+ // the ceiling is treated as a lower bound — strictly safe if the window were
196
+ // ever configured higher. Mirrors federal-register.ts's FR_COUNT_CAP and
197
+ // edgar.ts's FTS_WINDOW.
198
+ const totalIsLowerBound =
199
+ totalAvailable !== null && totalAvailable >= ECFR_TOTAL_COUNT_CAP;
200
+ const scopeNote =
201
+ args.titleNumber !== undefined
202
+ ? `searched CFR Title ${args.titleNumber}${
203
+ args.titleNumber === 48 ? " (FAR — Federal Acquisition Regulation)" : ""
204
+ } only`
205
+ : "searched all CFR titles (no title filter applied)";
206
+ const notes = [scopeNote];
207
+ if (totalIsLowerBound) {
208
+ notes.push(
209
+ `eCFR caps total_count at ${ECFR_TOTAL_COUNT_CAP} (Elasticsearch index.max_result_window); totalAvailable is a LOWER BOUND — the true match count may be higher and is UNKNOWN. See totalIsLowerBound. Narrow by title/chapter/date for an exact count.`,
210
+ );
211
+ }
212
+ return withMeta(data, {
213
+ source: "ecfr.gov/api (search/v1)",
214
+ keylessMode: true,
215
+ returned,
216
+ totalAvailable,
217
+ // Explicit boolean (not conditional): a below-cap count is DEFINITIVELY
218
+ // exact (totalIsLowerBound:false), an at/above-cap count is a lower bound
219
+ // (true). The AI can trust `false` as "this is the real total".
220
+ totalIsLowerBound,
221
+ truncated:
222
+ totalAvailable !== null ? returned < totalAvailable : undefined,
223
+ filtersApplied: args.titleNumber !== undefined ? ["titleNumber"] : [],
224
+ filtersDropped: [],
225
+ fieldsUnavailable: [],
226
+ notes,
227
+ });
228
+ }
229
+
230
+ function stripHtml(s: string): string {
231
+ return s
232
+ .replace(/<[^>]+>/g, "")
233
+ .replace(/\s+/g, " ")
234
+ .trim();
235
+ }
236
+
237
+ function buildEcfrUrl(h: {
238
+ title?: string;
239
+ chapter?: string;
240
+ part?: string;
241
+ section?: string;
242
+ }): string {
243
+ const base = `https://www.ecfr.gov/current/title-${h.title}`;
244
+ if (h.section) return `${base}/section-${h.section}`;
245
+ if (h.part) return `${base}/part-${h.part}`;
246
+ if (h.chapter) return `${base}/chapter-${h.chapter}`;
247
+ return base;
248
+ }