@cliwant/mcp-sam-gov 0.2.1 → 1.0.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 (228) hide show
  1. package/LICENSE +21 -21
  2. package/README.ja.md +48 -16
  3. package/README.ko.md +48 -16
  4. package/README.md +279 -67
  5. package/dist/attachments.d.ts +63 -0
  6. package/dist/attachments.d.ts.map +1 -0
  7. package/dist/attachments.js +576 -0
  8. package/dist/attachments.js.map +1 -0
  9. package/dist/bls.d.ts +267 -0
  10. package/dist/bls.d.ts.map +1 -0
  11. package/dist/bls.js +1549 -0
  12. package/dist/bls.js.map +1 -0
  13. package/dist/cache.d.ts +42 -0
  14. package/dist/cache.d.ts.map +1 -0
  15. package/dist/cache.js +64 -0
  16. package/dist/cache.js.map +1 -0
  17. package/dist/census.d.ts +182 -0
  18. package/dist/census.d.ts.map +1 -0
  19. package/dist/census.js +576 -0
  20. package/dist/census.js.map +1 -0
  21. package/dist/ckan.d.ts +141 -0
  22. package/dist/ckan.d.ts.map +1 -0
  23. package/dist/ckan.js +393 -0
  24. package/dist/ckan.js.map +1 -0
  25. package/dist/clinicaltrials.d.ts +180 -0
  26. package/dist/clinicaltrials.d.ts.map +1 -0
  27. package/dist/clinicaltrials.js +730 -0
  28. package/dist/clinicaltrials.js.map +1 -0
  29. package/dist/cms.d.ts +140 -0
  30. package/dist/cms.d.ts.map +1 -0
  31. package/dist/cms.js +482 -0
  32. package/dist/cms.js.map +1 -0
  33. package/dist/coerce.d.ts +32 -0
  34. package/dist/coerce.d.ts.map +1 -0
  35. package/dist/coerce.js +50 -0
  36. package/dist/coerce.js.map +1 -0
  37. package/dist/datagov-catalog.d.ts +84 -0
  38. package/dist/datagov-catalog.d.ts.map +1 -0
  39. package/dist/datagov-catalog.js +233 -0
  40. package/dist/datagov-catalog.js.map +1 -0
  41. package/dist/datagov.d.ts +146 -0
  42. package/dist/datagov.d.ts.map +1 -0
  43. package/dist/datagov.js +689 -0
  44. package/dist/datagov.js.map +1 -0
  45. package/dist/datagovKey.d.ts +36 -0
  46. package/dist/datagovKey.d.ts.map +1 -0
  47. package/dist/datagovKey.js +60 -0
  48. package/dist/datagovKey.js.map +1 -0
  49. package/dist/datasource.d.ts +313 -0
  50. package/dist/datasource.d.ts.map +1 -0
  51. package/dist/datasource.js +551 -0
  52. package/dist/datasource.js.map +1 -0
  53. package/dist/disclosure.d.ts +55 -0
  54. package/dist/disclosure.d.ts.map +1 -0
  55. package/dist/disclosure.js +57 -0
  56. package/dist/disclosure.js.map +1 -0
  57. package/dist/ecfr.d.ts +4 -2
  58. package/dist/ecfr.d.ts.map +1 -1
  59. package/dist/ecfr.js +92 -17
  60. package/dist/ecfr.js.map +1 -1
  61. package/dist/echo.d.ts +143 -0
  62. package/dist/echo.d.ts.map +1 -0
  63. package/dist/echo.js +424 -0
  64. package/dist/echo.js.map +1 -0
  65. package/dist/edgar.d.ts +377 -0
  66. package/dist/edgar.d.ts.map +1 -0
  67. package/dist/edgar.js +2418 -0
  68. package/dist/edgar.js.map +1 -0
  69. package/dist/errors.d.ts +102 -0
  70. package/dist/errors.d.ts.map +1 -0
  71. package/dist/errors.js +247 -0
  72. package/dist/errors.js.map +1 -0
  73. package/dist/fac.d.ts +180 -0
  74. package/dist/fac.d.ts.map +1 -0
  75. package/dist/fac.js +416 -0
  76. package/dist/fac.js.map +1 -0
  77. package/dist/far.d.ts +170 -0
  78. package/dist/far.d.ts.map +1 -0
  79. package/dist/far.js +804 -0
  80. package/dist/far.js.map +1 -0
  81. package/dist/fdic.d.ts +599 -0
  82. package/dist/fdic.d.ts.map +1 -0
  83. package/dist/fdic.js +1624 -0
  84. package/dist/fdic.js.map +1 -0
  85. package/dist/federal-register.d.ts +139 -2
  86. package/dist/federal-register.d.ts.map +1 -1
  87. package/dist/federal-register.js +432 -15
  88. package/dist/federal-register.js.map +1 -1
  89. package/dist/fema.d.ts +181 -0
  90. package/dist/fema.d.ts.map +1 -0
  91. package/dist/fema.js +436 -0
  92. package/dist/fema.js.map +1 -0
  93. package/dist/fpds.d.ts +108 -0
  94. package/dist/fpds.d.ts.map +1 -0
  95. package/dist/fpds.js +519 -0
  96. package/dist/fpds.js.map +1 -0
  97. package/dist/gao.d.ts +64 -0
  98. package/dist/gao.d.ts.map +1 -0
  99. package/dist/gao.js +640 -0
  100. package/dist/gao.js.map +1 -0
  101. package/dist/govinfo.d.ts +111 -0
  102. package/dist/govinfo.d.ts.map +1 -0
  103. package/dist/govinfo.js +422 -0
  104. package/dist/govinfo.js.map +1 -0
  105. package/dist/grants.d.ts +27 -4
  106. package/dist/grants.d.ts.map +1 -1
  107. package/dist/grants.js +114 -11
  108. package/dist/grants.js.map +1 -1
  109. package/dist/gsa-csv.d.ts +249 -0
  110. package/dist/gsa-csv.d.ts.map +1 -0
  111. package/dist/gsa-csv.js +784 -0
  112. package/dist/gsa-csv.js.map +1 -0
  113. package/dist/integrity.d.ts +212 -0
  114. package/dist/integrity.d.ts.map +1 -0
  115. package/dist/integrity.js +707 -0
  116. package/dist/integrity.js.map +1 -0
  117. package/dist/meta.d.ts +165 -0
  118. package/dist/meta.d.ts.map +1 -0
  119. package/dist/meta.js +162 -0
  120. package/dist/meta.js.map +1 -0
  121. package/dist/nih.d.ts +117 -0
  122. package/dist/nih.d.ts.map +1 -0
  123. package/dist/nih.js +291 -0
  124. package/dist/nih.js.map +1 -0
  125. package/dist/nppes.d.ts +157 -0
  126. package/dist/nppes.d.ts.map +1 -0
  127. package/dist/nppes.js +648 -0
  128. package/dist/nppes.js.map +1 -0
  129. package/dist/nsf.d.ts +176 -0
  130. package/dist/nsf.d.ts.map +1 -0
  131. package/dist/nsf.js +554 -0
  132. package/dist/nsf.js.map +1 -0
  133. package/dist/nvd.d.ts +176 -0
  134. package/dist/nvd.d.ts.map +1 -0
  135. package/dist/nvd.js +912 -0
  136. package/dist/nvd.js.map +1 -0
  137. package/dist/ofac.d.ts +205 -0
  138. package/dist/ofac.d.ts.map +1 -0
  139. package/dist/ofac.js +919 -0
  140. package/dist/ofac.js.map +1 -0
  141. package/dist/pricing.d.ts +110 -0
  142. package/dist/pricing.d.ts.map +1 -0
  143. package/dist/pricing.js +843 -0
  144. package/dist/pricing.js.map +1 -0
  145. package/dist/sam-gov/client.d.ts +60 -2
  146. package/dist/sam-gov/client.d.ts.map +1 -1
  147. package/dist/sam-gov/client.js +320 -54
  148. package/dist/sam-gov/client.js.map +1 -1
  149. package/dist/sam-gov/index.d.ts +1 -1
  150. package/dist/sam-gov/index.d.ts.map +1 -1
  151. package/dist/sam-gov/index.js +1 -1
  152. package/dist/sam-gov/index.js.map +1 -1
  153. package/dist/sam-gov/types.d.ts +24 -0
  154. package/dist/sam-gov/types.d.ts.map +1 -1
  155. package/dist/sba.d.ts +72 -0
  156. package/dist/sba.d.ts.map +1 -0
  157. package/dist/sba.js +281 -0
  158. package/dist/sba.js.map +1 -0
  159. package/dist/server.d.ts +14 -2
  160. package/dist/server.d.ts.map +1 -1
  161. package/dist/server.js +3897 -295
  162. package/dist/server.js.map +1 -1
  163. package/dist/snapshot.d.ts +98 -0
  164. package/dist/snapshot.d.ts.map +1 -0
  165. package/dist/snapshot.js +146 -0
  166. package/dist/snapshot.js.map +1 -0
  167. package/dist/socrata.d.ts +157 -0
  168. package/dist/socrata.d.ts.map +1 -0
  169. package/dist/socrata.js +448 -0
  170. package/dist/socrata.js.map +1 -0
  171. package/dist/treasury.d.ts +143 -0
  172. package/dist/treasury.d.ts.map +1 -0
  173. package/dist/treasury.js +436 -0
  174. package/dist/treasury.js.map +1 -0
  175. package/dist/usaspending.d.ts +260 -65
  176. package/dist/usaspending.d.ts.map +1 -1
  177. package/dist/usaspending.js +1664 -228
  178. package/dist/usaspending.js.map +1 -1
  179. package/dist/usitc.d.ts +142 -0
  180. package/dist/usitc.d.ts.map +1 -0
  181. package/dist/usitc.js +339 -0
  182. package/dist/usitc.js.map +1 -0
  183. package/package.json +24 -2
  184. package/src/attachments.ts +652 -0
  185. package/src/bls.ts +1943 -0
  186. package/src/cache.ts +73 -0
  187. package/src/census.ts +735 -0
  188. package/src/ckan.ts +495 -0
  189. package/src/clinicaltrials.ts +923 -0
  190. package/src/cms.ts +634 -0
  191. package/src/coerce.ts +47 -0
  192. package/src/datagov-catalog.ts +296 -0
  193. package/src/datagov.ts +907 -0
  194. package/src/datagovKey.ts +68 -0
  195. package/src/datasource.ts +721 -0
  196. package/src/disclosure.ts +61 -0
  197. package/src/ecfr.ts +231 -127
  198. package/src/echo.ts +496 -0
  199. package/src/edgar.ts +3014 -0
  200. package/src/errors.ts +303 -0
  201. package/src/fac.ts +529 -0
  202. package/src/far.ts +1007 -0
  203. package/src/fdic.ts +2052 -0
  204. package/src/federal-register.ts +706 -191
  205. package/src/fema.ts +541 -0
  206. package/src/fpds.ts +620 -0
  207. package/src/gao.ts +744 -0
  208. package/src/govinfo.ts +497 -0
  209. package/src/grants.ts +290 -155
  210. package/src/gsa-csv.ts +992 -0
  211. package/src/integrity.ts +928 -0
  212. package/src/meta.ts +292 -0
  213. package/src/nih.ts +375 -0
  214. package/src/nppes.ts +834 -0
  215. package/src/nsf.ts +706 -0
  216. package/src/nvd.ts +1124 -0
  217. package/src/ofac.ts +1166 -0
  218. package/src/pricing.ts +1075 -0
  219. package/src/sam-gov/client.ts +345 -63
  220. package/src/sam-gov/index.ts +5 -1
  221. package/src/sam-gov/types.ts +22 -0
  222. package/src/sba.ts +357 -0
  223. package/src/server.ts +4559 -327
  224. package/src/snapshot.ts +192 -0
  225. package/src/socrata.ts +532 -0
  226. package/src/treasury.ts +575 -0
  227. package/src/usaspending.ts +2680 -925
  228. package/src/usitc.ts +420 -0
package/src/meta.ts ADDED
@@ -0,0 +1,292 @@
1
+ /**
2
+ * @cliwant/mcp-sam-gov/meta — the `_meta` completeness/provenance convention.
3
+ *
4
+ * Why this exists
5
+ * ----------------
6
+ * These tools' outputs are consumed by an AI, not a human who can eyeball
7
+ * a table. For an AI consumer, silently-misleading data is strictly worse
8
+ * than missing data: the AI cannot tell a filtered-but-blank field from a
9
+ * genuinely-empty one, a hard cap from a complete list, or a swallowed
10
+ * error from a real zero — and will state the wrong answer confidently.
11
+ *
12
+ * Every tool's success payload therefore gains a sibling `_meta` object.
13
+ * `data` is UNCHANGED (backward compat): the envelope becomes
14
+ * `{ ok, data, _meta }` where existing consumers reading `ok`/`data.*`
15
+ * are 100% unaffected.
16
+ *
17
+ * See docs/research/02-truthful-outputs-spec.md §2 for the normative spec.
18
+ */
19
+
20
+ /** Two-phase (enrich) tools: rows lost to failed per-record enrichment. */
21
+ export type MetaDegraded = {
22
+ attempted: number;
23
+ succeeded: number;
24
+ failed: number;
25
+ };
26
+
27
+ /** Offset-based pagination descriptor for list/search/aggregate tools. */
28
+ export type MetaPagination = {
29
+ /**
30
+ * The row offset of this page. `number` for offset-paginated tools (unchanged —
31
+ * every existing tool sets an integer). ADR-0010: widened to `number | null` for
32
+ * OPAQUE-CURSOR tools (GovInfo), where no meaningful numeric offset exists — those
33
+ * tools set `offset:null` (the honest "not a numeric offset"; continuation is via
34
+ * `_meta.nextCursor`, not a numeric `nextOffset`). Additive: no existing tool changes.
35
+ */
36
+ offset: number | null;
37
+ limit: number;
38
+ nextOffset: number | null;
39
+ hasMore: boolean;
40
+ };
41
+
42
+ /**
43
+ * The normative `_meta` shape (spec §2.1). Rides alongside `data` in the
44
+ * success envelope so the AI can branch on completeness/provenance
45
+ * deterministically instead of guessing from null fields.
46
+ */
47
+ export type ResponseMeta = {
48
+ /** Human+machine label of the upstream + layer that served THIS response. */
49
+ source: string;
50
+ /** SAM tools: true when no SAM_GOV_API_KEY. Non-SAM tools: always true. */
51
+ keylessMode: boolean;
52
+ /** true iff this response contains the ENTIRE result set for the query. */
53
+ complete: boolean;
54
+ /** true iff a hard cap / top-N limited the rows. */
55
+ truncated: boolean;
56
+ /** Number of primary records in `data`. */
57
+ returned: number;
58
+ /** Upstream's total match count; null when the endpoint doesn't report it. */
59
+ totalAvailable: number | null;
60
+ /**
61
+ * Present (and true) when `totalAvailable` is a KNOWN LOWER BOUND rather than
62
+ * an exact count — i.e. the upstream reported the total as "≥ N" (e.g. SEC
63
+ * EDGAR full-text search returns `hits.total.relation: "gte"` with the value
64
+ * pinned at 10000). The true total is UNKNOWN and ≥ `totalAvailable`. Absent
65
+ * on endpoints that report an exact total (do NOT read absence as `false`
66
+ * meaning "exact" for tools that never set it — it is simply not applicable).
67
+ */
68
+ totalIsLowerBound?: boolean;
69
+ /**
70
+ * Present (and true) when `totalAvailable` (or, for CKAN, the value disclosed
71
+ * in `notes`) is an upstream STATISTICAL ESTIMATE rather than an exact count —
72
+ * i.e. the source reported the total as an approximation that may be ABOVE OR
73
+ * BELOW the true count (CKAN `datastore_search` returns `total_was_estimated:
74
+ * true` for a PostgreSQL `reltuples`-style estimate, live-verified to overshoot
75
+ * — so it is NOT a lower bound, unlike `totalIsLowerBound`). CKAN sets this on
76
+ * the estimated path alongside `totalAvailable:null` (the estimate never drives
77
+ * pagination — ADR-0006 B1) + a disclosing note carrying the estimate value.
78
+ * Absent on endpoints that report an exact total (do NOT read absence as
79
+ * `false` meaning "exact" for tools that never set it — it is not applicable).
80
+ */
81
+ totalIsEstimated?: boolean;
82
+ /**
83
+ * Present when this response comes from an OPAQUE-CURSOR-paginated source
84
+ * (ADR-0010 — GovInfo's `offsetMark`): the opaque continuation token to pass back
85
+ * to fetch the NEXT page (GovInfo: as the `pageMark` argument), or `null` on the
86
+ * last page (no further cursor). It is a source-minted token, NOT derived from any
87
+ * secret and NEVER the raw upstream `nextPage` URL (which embeds pageSize + the
88
+ * api_key). Cursor tools set `nextOffset:null`/`offset:null` (a numeric offset is
89
+ * meaningless for a cursor) and use THIS as the sole continuation surface. Absent
90
+ * on offset-paginated tools (do NOT read absence as "no more" for those). Added as
91
+ * a conditional passthrough exactly like totalIsLowerBound/totalIsEstimated —
92
+ * only surfaced when the tool provides it, so existing tools' `_meta` stays
93
+ * byte-identical and the tools/list snapshot is unaffected (it is a runtime `_meta`
94
+ * field, not part of any tool's input schema).
95
+ */
96
+ nextCursor?: string | null;
97
+ /** Request filters the upstream verifiably honored. */
98
+ filtersApplied: string[];
99
+ /** Request filters sent but NOT honored (results are unfiltered on these). */
100
+ filtersDropped: string[];
101
+ /** Fields null/absent BY LIMITATION (keyless/endpoint), not "no data". */
102
+ fieldsUnavailable: string[];
103
+ /** Two-phase tools only: rows for which per-record detail was fetched. */
104
+ enrichedCount?: number;
105
+ /** Two-phase tools: enrichment accounting (see MetaDegraded). */
106
+ degraded?: MetaDegraded;
107
+ /** Present on list/search/aggregate tools. */
108
+ pagination?: MetaPagination;
109
+ /**
110
+ * P5 PROVENANCE (ADR-0045 B2/M1). The access path that served THIS response:
111
+ * `"live"` (the current, and in Phase 1 the ONLY, path) or `"snapshot"` (a
112
+ * self-hosted cache served when the live upstream was unreachable). Set by the
113
+ * resilience port's provenance-returning primitive, then threaded by the
114
+ * adapter ONLY when NON-live — so a live response OMITS this field entirely and
115
+ * stays byte-identical to pre-P5 output. Surfaced via the guarded
116
+ * `if (partial.dataPath !== undefined)` passthrough below (NO `??` default).
117
+ * Absent ⇒ live; do NOT read absence as any other value. Freshness enum, not a
118
+ * topology label (M2): an independent host serving live data is still `"live"`.
119
+ */
120
+ dataPath?: "live" | "snapshot";
121
+ /**
122
+ * P5 FRESHNESS (ADR-0045 m3). ISO-8601 UTC timestamp of when a NON-live
123
+ * (`snapshot`) body was retrieved from the origin — the as-of instant for
124
+ * deterministic cross-tool freshness comparison. Present ONLY alongside a
125
+ * non-live `dataPath`; absent on a live response (byte-identical). When a
126
+ * snapshot carries a `totalAvailable`, it is qualified as an as-of figure via
127
+ * the existing `totalIsEstimated` flag (m1 forbids a dedicated `totalAsOf`
128
+ * field — the only NEW P5 fields are `dataPath` and `asOf`).
129
+ */
130
+ asOf?: string;
131
+ /** Short, AI-actionable caveats (natural language). */
132
+ notes: string[];
133
+ };
134
+
135
+ /**
136
+ * Build a fully-populated, invariant-consistent ResponseMeta from a partial.
137
+ *
138
+ * Safe defaults (spec §2.1): a tool that supplies nothing gets a truthful,
139
+ * "single-record / known-complete" meta (`complete:true, truncated:false`).
140
+ *
141
+ * The server (or the caller) hands over whatever it knows; this helper fills
142
+ * the rest and then ENFORCES the §2.1 invariants so the flags the AI trusts
143
+ * are internally consistent:
144
+ * - `complete === true` ⟺ NOT truncated AND filtersDropped empty AND
145
+ * (no pagination OR hasMore===false) AND (no degraded OR failed===0).
146
+ * - If totalAvailable is known and returned < totalAvailable ⇒
147
+ * complete:false, truncated:true.
148
+ * - filtersDropped non-empty ⇒ at least one note is present.
149
+ */
150
+ export function buildMeta(partial: Partial<ResponseMeta> = {}): ResponseMeta {
151
+ const source = partial.source ?? "unknown";
152
+ const keylessMode = partial.keylessMode ?? true;
153
+ const returned = partial.returned ?? 0;
154
+ const totalAvailable =
155
+ partial.totalAvailable === undefined ? null : partial.totalAvailable;
156
+ const filtersApplied = partial.filtersApplied ?? [];
157
+ const filtersDropped = partial.filtersDropped ?? [];
158
+ const fieldsUnavailable = partial.fieldsUnavailable ?? [];
159
+ const notes = partial.notes ? [...partial.notes] : [];
160
+ const pagination = partial.pagination;
161
+ const degraded = partial.degraded;
162
+
163
+ // --- Derive truncated -------------------------------------------------
164
+ // A known total that exceeds what we returned proves truncation.
165
+ const totalProvesTruncation =
166
+ totalAvailable !== null && returned < totalAvailable;
167
+ const paginationHasMore = pagination ? pagination.hasMore : false;
168
+ let truncated =
169
+ partial.truncated ?? (totalProvesTruncation || paginationHasMore);
170
+ if (totalProvesTruncation || paginationHasMore) truncated = true;
171
+
172
+ // --- P5 provenance (ADR-0045 B2/M1) -----------------------------------
173
+ // A NON-live response (a snapshot served because the live upstream was
174
+ // unreachable) is honesty-qualified below. For a live/absent dataPath this
175
+ // whole strand is INERT — `nonLiveProvenance` is false, so `complete`, the
176
+ // notes[], and totalIsEstimated are all derived EXACTLY as before P5 (Phase 1:
177
+ // no adapter threads a dataPath ⇒ byte-identical output on every one of the
178
+ // 110 tools).
179
+ const nonLiveProvenance =
180
+ partial.dataPath !== undefined && partial.dataPath !== "live";
181
+
182
+ // --- Derive complete (single source of truth = the §2.1 invariant) ----
183
+ const degradedLoss = degraded ? degraded.failed > 0 : false;
184
+ const derivedComplete =
185
+ !truncated &&
186
+ filtersDropped.length === 0 &&
187
+ !paginationHasMore &&
188
+ !degradedLoss &&
189
+ !totalProvesTruncation &&
190
+ // M1a: `complete` is defined against the LIVE result set (:53), so a snapshot
191
+ // can NEVER claim complete:true — gate the derivation behind a live/absent
192
+ // dataPath. (Inert for live: `!false` === true, no change.)
193
+ !nonLiveProvenance;
194
+ // Honor an explicit complete:false, but never let a caller claim
195
+ // complete:true when the invariant says otherwise.
196
+ const complete =
197
+ partial.complete === false ? false : derivedComplete;
198
+
199
+ // --- Invariant: filtersDropped non-empty ⇒ a note explaining it -------
200
+ if (filtersDropped.length > 0 && notes.length === 0) {
201
+ notes.push(
202
+ `The following requested filters were NOT applied by the upstream and the results are unfiltered on them: ${filtersDropped.join(", ")}. Treat these results as unfiltered on those facets.`,
203
+ );
204
+ }
205
+
206
+ // --- P5 (ADR-0045 M1c/m1): staleness note on a non-live response ------
207
+ // Push into the EXISTING notes[] (m1 killed the separate `stalenessNote`
208
+ // field). Inert for live/absent dataPath. Names the as-of instant when known.
209
+ if (nonLiveProvenance) {
210
+ const asOfPhrase = partial.asOf ? ` (as of ${partial.asOf})` : "";
211
+ notes.push(
212
+ `Live upstream was unreachable — this response is served from a ${partial.dataPath} snapshot${asOfPhrase}. Freshness is NOT guaranteed; treat completeness and totals as of the snapshot time, not live.`,
213
+ );
214
+ }
215
+
216
+ const meta: ResponseMeta = {
217
+ source,
218
+ keylessMode,
219
+ complete,
220
+ truncated,
221
+ returned,
222
+ totalAvailable,
223
+ filtersApplied,
224
+ filtersDropped,
225
+ fieldsUnavailable,
226
+ notes,
227
+ };
228
+ if (partial.enrichedCount !== undefined)
229
+ meta.enrichedCount = partial.enrichedCount;
230
+ if (degraded) meta.degraded = degraded;
231
+ if (pagination) meta.pagination = pagination;
232
+ // Conditional passthrough (like enrichedCount/pagination): only surfaced when
233
+ // the tool provides it, so existing tools' `_meta` output stays byte-identical.
234
+ if (partial.totalIsLowerBound !== undefined)
235
+ meta.totalIsLowerBound = partial.totalIsLowerBound;
236
+ // Conditional passthrough (identical shape to totalIsLowerBound above): only
237
+ // surfaced when the tool provides it (CKAN's estimated-total path), so every
238
+ // existing tool's `_meta` output stays byte-identical. NO new derivation logic.
239
+ if (partial.totalIsEstimated !== undefined)
240
+ meta.totalIsEstimated = partial.totalIsEstimated;
241
+ // Conditional passthrough (IDENTICAL shape to totalIsLowerBound/totalIsEstimated
242
+ // above): only surfaced when the tool provides it (GovInfo's opaque-cursor path),
243
+ // so every existing tool's `_meta` output stays byte-identical. NO new derivation
244
+ // logic — the value (the offsetMark token, or null on the last page) is set by the
245
+ // cursor tool and passed through verbatim.
246
+ if (partial.nextCursor !== undefined) meta.nextCursor = partial.nextCursor;
247
+ // P5 provenance passthrough (ADR-0045 B2) — IDENTICAL guarded idiom to
248
+ // totalIsLowerBound/nextCursor above: surfaced ONLY when the adapter provides
249
+ // it, so every existing (live, dataPath-absent) tool `_meta` stays
250
+ // byte-identical. NO `??` default. In Phase 1 no adapter threads these ⇒ inert.
251
+ if (partial.dataPath !== undefined) meta.dataPath = partial.dataPath;
252
+ if (partial.asOf !== undefined) meta.asOf = partial.asOf;
253
+ // M1b: a snapshot's totalAvailable is an as-of figure, NOT a live exact total —
254
+ // qualify it via the EXISTING totalIsEstimated flag (m1 forbids a new
255
+ // totalAsOf field) so an AI never reads it as a live count. Only when a total
256
+ // is actually present. Inert for live (nonLiveProvenance false).
257
+ if (nonLiveProvenance && totalAvailable !== null) meta.totalIsEstimated = true;
258
+ return meta;
259
+ }
260
+
261
+ /**
262
+ * Branded wrapper a tool handler returns to attach `_meta` to its payload.
263
+ *
264
+ * A handler may return either its raw domain object (server synthesizes a
265
+ * minimal truthful default) OR `withMeta(data, partialMeta)`. The brand lets
266
+ * the server distinguish "handler attached meta" from "domain object that
267
+ * happens to have `data`/`_meta` keys" with zero ambiguity.
268
+ */
269
+ export class MetaBundle<T = unknown> {
270
+ readonly __isMetaBundle = true as const;
271
+ constructor(
272
+ readonly data: T,
273
+ readonly meta: Partial<ResponseMeta>,
274
+ ) {}
275
+ }
276
+
277
+ /** Attach a partial `_meta` to a handler's `data`. Server finalizes it. */
278
+ export function withMeta<T>(
279
+ data: T,
280
+ meta: Partial<ResponseMeta>,
281
+ ): MetaBundle<T> {
282
+ return new MetaBundle(data, meta);
283
+ }
284
+
285
+ /** Type guard: did the handler hand back a MetaBundle? */
286
+ export function isMetaBundle(v: unknown): v is MetaBundle {
287
+ return (
288
+ typeof v === "object" &&
289
+ v !== null &&
290
+ (v as { __isMetaBundle?: unknown }).__isMetaBundle === true
291
+ );
292
+ }
package/src/nih.ts ADDED
@@ -0,0 +1,375 @@
1
+ /**
2
+ * NIH RePORTER v2 — federal research-GRANT project records (keyless).
3
+ *
4
+ * A NEW capability axis: **federal research-funding footprint by organization /
5
+ * UEI / state** — awarded NIH project money keyed to a recipient org (with UEI +
6
+ * DUNS), a recipient-ENRICHMENT layer orthogonal to the spend/contract/regulatory
7
+ * sources. It joins the SAM/USAspending recipient graph via `primary_uei` (the
8
+ * SAME UEI space) — but the award nature DIFFERS (grants fund research, not
9
+ * goods/services), so every response carries the grant-vs-contract caveat (M2).
10
+ *
11
+ * ON-DOMAIN HONESTY: this is research-funding / grants-adjacent, the AWARDED-money
12
+ * sibling of grants.gov opportunities and the biomedical-R&D slice of USAspending
13
+ * assistance awards — NOT core procurement. Positioned as recipient-enrichment /
14
+ * R&D market intel, never as a contract source.
15
+ *
16
+ * THE R2 PORT'S FIRST NON-GET CONSUMER (ADR-0014): NIH RePORTER is a
17
+ * POST-with-JSON-body API. `getJson` (ADR-0005) gained a byte-neutral,
18
+ * backward-compatible `method`/`body` passthrough for exactly this; this module
19
+ * writes ZERO fetch/coercion/error code — it REUSES `getJson` / `driftError` /
20
+ * `num`·`str` / `withMeta` and COPIES (does not import) the fixed-host SSRF +
21
+ * honesty PATTERN from CKAN/ECHO/FPDS, adapted:
22
+ *
23
+ * ★ SSRF GUARD (policy① — the SMALLEST surface of any source): the request URL is
24
+ * a compile-time CONSTANT (fixed host `api.reporter.nih.gov` + fixed path
25
+ * `/v2/projects/search`); NO caller input touches it. All filters ride in the POST
26
+ * body, which is MODULE-BUILT from a validated typed criteria object then
27
+ * `JSON.stringify`'d — there is NO raw-body / raw-criteria passthrough, so a
28
+ * caller value cannot break out of its criterion into another key. A
29
+ * post-construction hostname/protocol assertion + `redirect:"error"` lock it
30
+ * (fail closed on any off-host 3xx; its body is never read).
31
+ *
32
+ * ★ FILTER HONESTY (M1 — the load-bearing discipline): a criterion is shipped ONLY
33
+ * after being LIVE-CONFIRMED to actually narrow the result set. The confirmed
34
+ * v1 set is `orgStates` + `orgNames` + `fiscalYears` (each live-verified to
35
+ * reduce the total below the unfiltered baseline). `agency_ic_codes` is EXCLUDED
36
+ * — it is silently DROPPED upstream (a filter that no-ops is never presented as
37
+ * applied). A shipped filter goes in `_meta.filtersApplied`; a silent-drop filter
38
+ * is never listed there and never exposed in the schema.
39
+ *
40
+ * ★ 15,000-RECORD RETRIEVAL WINDOW (M3+M4 — the critical disclosure): NIH caps
41
+ * keyless retrieval at `offset 0..14,999` (`limit ≤ 500`), so only the first
42
+ * 15,000 records of any result set are reachable. This is a cap on RETRIEVAL, NOT
43
+ * on the COUNT: `meta.total` stays EXACT (never truncated to 15,000, never marked
44
+ * `totalIsLowerBound`). `offset >= 15,000` is refused pre-fetch (`invalid_input`);
45
+ * after a page, `nextOffset` is null once `offset+returned` reaches the cap or the
46
+ * exact total (never a dead-end `nextOffset=15000`). `truncated` is derived by
47
+ * buildMeta from `returned < totalAvailable`; when the count exceeds the window a
48
+ * `_meta.notes` line discloses the unreachable remainder and recommends narrowing.
49
+ *
50
+ * ★ HONESTY `_meta`: shape-guard FIRST (a 200 body that is not `{meta,results:[]}`
51
+ * → driftError, NEVER a fake empty); a non-numeric/absent `meta.total` →
52
+ * driftError BEFORE `num(meta.total)`; genuine-empty (`total:0`) → complete:true /
53
+ * totalAvailable:0; outage/5xx/timeout → getJson throws; 400 (bad offset/limit/
54
+ * type) → invalid_input (surfaced, never `[]`). `num` is null-never-0 on
55
+ * `award_amount` (a real $0 award is 0; an absent amount is null). Every response
56
+ * discloses the grant-vs-contract caveat + a data-currency note.
57
+ */
58
+
59
+ import { ToolErrorCarrier } from "./errors.js";
60
+ import { getJson, driftError } from "./datasource.js";
61
+ import { num, str } from "./coerce.js";
62
+ import { withMeta, type MetaBundle, type ResponseMeta } from "./meta.js";
63
+
64
+ // Re-export the shared honesty coercion (single audited copy in ./coerce.js —
65
+ // ADR-0005 v2 FIX-C) so the fault suite's num-parity guard resolves the SAME
66
+ // `num` (nih.num === coerce.num === treasury.num — a num regression fails together).
67
+ export { num };
68
+
69
+ // ─── Fixed endpoint (SSRF core — a compile-time CONSTANT) ─────────
70
+ const NIH_HOST = "api.reporter.nih.gov";
71
+ const NIH_PATH = "/v2/projects/search";
72
+ const NIH_PROJECTS_URL = `https://${NIH_HOST}${NIH_PATH}`;
73
+ // HOST-only-ish label (path is fixed + carries no token — keyless). Surfaces in
74
+ // ToolError.upstreamEndpoint; no secret can appear here (the API is anonymous).
75
+ const NIH_LABEL = "nih:/v2/projects/search";
76
+
77
+ // ─── The 15,000-record RETRIEVAL window (live-verified: offset ≤ 14,999,
78
+ // limit ≤ 500). A cap on RETRIEVAL, not on the exact meta.total count. The
79
+ // limit ≤ 500 ceiling is enforced by the server's Zod schema; this module owns
80
+ // the offset-window guard + the never-a-dead-end nextOffset boundary. ──
81
+ const RETRIEVAL_CAP = 15_000;
82
+
83
+ // ─── Frozen US state/territory 2-letter USPS enum (UPPERCASE-only) ─
84
+ // Built FROM this array by the Zod enum in server.ts (single source of truth).
85
+ // It is BOTH an org_states value guard AND the silent-zero guard: live, an unknown
86
+ // but well-typed value (e.g. "ZZ") returns a genuine total:0 indistinguishable
87
+ // from a real empty, and a lowercase "ca" silently returns zeros — so a typo must
88
+ // be an invalid_input, never read as "no NIH funding" (a silent honesty failure).
89
+ export const NIH_ORG_STATES = [
90
+ "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL",
91
+ "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",
92
+ "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
93
+ "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI",
94
+ "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI",
95
+ "WY", "AS", "GU", "MP", "PR", "VI",
96
+ ] as const;
97
+
98
+ export type NihOrgState = (typeof NIH_ORG_STATES)[number];
99
+
100
+ // ─── Disclosure constants (honesty obligations) ──────────────────
101
+ /** M2 — the mandatory grant-vs-contract caveat carried in EVERY response. */
102
+ const NIH_GRANT_CAVEAT =
103
+ "NIH RePORTER records are RESEARCH GRANTS awarded by NIH, NOT federal procurement contracts. The primary_uei joins to SAM/USAspending recipient records, but the award nature differs (grants fund research, not goods/services) — do not present these amounts as contract awards.";
104
+
105
+ /** m-currency — a conservative data-currency note (not API-verifiable). */
106
+ const DATA_CURRENCY_NOTE =
107
+ "NIH RePORTER updates on a rolling basis; per-record refresh lag is not API-verifiable.";
108
+
109
+ /** The UEI join disclosure — how to bridge to the SAM/USAspending recipient graph. */
110
+ const UEI_JOIN_NOTE =
111
+ "organization.primaryUei is the join key to SAM entities and USAspending recipients (same UEI space); a grant recipient is not necessarily a federal contractor.";
112
+
113
+ /** M3+M4 — the retrieval-window disclosure when the count exceeds the 15,000 cap. */
114
+ function retrievalCapNote(total: number): string {
115
+ return `NIH caps keyless retrieval at the first ${RETRIEVAL_CAP} of ${total} matching records; the count is EXACT but records beyond ${RETRIEVAL_CAP} cannot be retrieved via this API — narrow criteria (org, fiscal year, state) to bring the target set under ${RETRIEVAL_CAP}.`;
116
+ }
117
+
118
+ const SOURCE = "api.reporter.nih.gov v2 (keyless)";
119
+
120
+ // ─── Record shape (recipient-enrichment payload) ─────────────────
121
+ export type NihPrincipalInvestigator = {
122
+ profileId: number | null;
123
+ firstName: string | null;
124
+ lastName: string | null;
125
+ fullName: string | null;
126
+ isContactPi: boolean | null;
127
+ title: string | null;
128
+ };
129
+
130
+ export type NihProject = {
131
+ projectNum: string | null;
132
+ projectTitle: string | null;
133
+ fiscalYear: number | null;
134
+ awardAmount: number | null;
135
+ awardType: string | null;
136
+ activityCode: string | null;
137
+ isActive: boolean | null;
138
+ organization: {
139
+ name: string | null;
140
+ city: string | null;
141
+ state: string | null;
142
+ country: string | null;
143
+ /** The SAM/USAspending recipient join key (but grant ≠ contract — M2). */
144
+ primaryUei: string | null;
145
+ primaryDuns: string | null;
146
+ ueis: string[];
147
+ duns: string[];
148
+ };
149
+ principalInvestigators: NihPrincipalInvestigator[];
150
+ contactPiName: string | null;
151
+ fundingIc: {
152
+ code: string | null;
153
+ abbreviation: string | null;
154
+ name: string | null;
155
+ };
156
+ };
157
+
158
+ /** "true"/true → true, "false"/false → false, absent/other → null (never a
159
+ * fabricated false). */
160
+ function boolOrNull(x: unknown): boolean | null {
161
+ if (x === true) return true;
162
+ if (x === false) return false;
163
+ if (x === "true") return true;
164
+ if (x === "false") return false;
165
+ return null;
166
+ }
167
+
168
+ /** A 2-letter uppercase string array from a mixed value, else []. */
169
+ function strArray(x: unknown): string[] {
170
+ if (!Array.isArray(x)) return [];
171
+ return x.map((v) => str(v)).filter((v): v is string => v !== null);
172
+ }
173
+
174
+ /** Map ONE NIH results[] row → the curated enrichment shape. Every scalar is
175
+ * null-never-fabricated (str/num); `award_amount` is num (a real $0 → 0, absent
176
+ * → null). Field names are NIH v2 documented keys; an absent field maps to null
177
+ * (honest "unknown"), never a crash. */
178
+ function mapProject(raw: unknown): NihProject {
179
+ const r = (raw ?? {}) as Record<string, unknown>;
180
+ const org = (r.organization ?? {}) as Record<string, unknown>;
181
+ const ic = (r.agency_ic_admin ?? {}) as Record<string, unknown>;
182
+ const pis = Array.isArray(r.principal_investigators)
183
+ ? (r.principal_investigators as Record<string, unknown>[])
184
+ : [];
185
+ return {
186
+ projectNum: str(r.project_num),
187
+ projectTitle: str(r.project_title),
188
+ fiscalYear: num(r.fiscal_year),
189
+ awardAmount: num(r.award_amount),
190
+ awardType: str(r.award_type),
191
+ activityCode: str(r.activity_code),
192
+ isActive: boolOrNull(r.is_active),
193
+ organization: {
194
+ name: str(org.org_name),
195
+ city: str(org.org_city),
196
+ state: str(org.org_state),
197
+ country: str(org.org_country),
198
+ primaryUei: str(org.primary_uei),
199
+ primaryDuns: str(org.primary_duns),
200
+ ueis: strArray(org.org_ueis),
201
+ duns: strArray(org.org_duns),
202
+ },
203
+ principalInvestigators: pis.map((p) => ({
204
+ profileId: num(p.profile_id),
205
+ firstName: str(p.first_name),
206
+ lastName: str(p.last_name),
207
+ fullName: str(p.full_name),
208
+ isContactPi: boolOrNull(p.is_contact_pi),
209
+ title: str(p.title),
210
+ })),
211
+ contactPiName: str(r.contact_pi_name),
212
+ fundingIc: {
213
+ code: str(ic.code),
214
+ abbreviation: str(ic.abbreviation),
215
+ name: str(ic.name),
216
+ },
217
+ };
218
+ }
219
+
220
+ // ─── The confirmed criteria surface (v1) ─────────────────────────
221
+ export type NihSearchArgs = {
222
+ orgStates?: NihOrgState[];
223
+ orgNames?: string[];
224
+ fiscalYears?: number[];
225
+ limit?: number;
226
+ offset?: number;
227
+ };
228
+
229
+ /**
230
+ * Search awarded NIH research projects (POST /v2/projects/search). Structured,
231
+ * LIVE-CONFIRMED-narrowing criteria only (orgStates / orgNames / fiscalYears),
232
+ * AND-combined in a MODULE-BUILT criteria object → JSON.stringify (no raw
233
+ * passthrough). Returns curated recipient-enrichment rows + honest `_meta`:
234
+ * exact totalAvailable, exact within-window pagination, the 15,000-retrieval-cap
235
+ * disclosure when the count exceeds it, and the mandatory grant-vs-contract
236
+ * caveat. Disclose-not-refuse: an unscoped query is NOT refused — it returns the
237
+ * first page + the exact total + a note recommending a criterion when the total
238
+ * exceeds the window.
239
+ */
240
+ export async function searchProjects(args: NihSearchArgs): Promise<MetaBundle> {
241
+ const limit = args.limit ?? 50;
242
+ const offset = args.offset ?? 0;
243
+
244
+ // ── 15k window pre-fetch guard (M3): offset >= 15,000 is UNREACHABLE — refuse
245
+ // BEFORE any fetch (also enforced by the server's Zod .max(14_999); this is
246
+ // the belt-and-suspenders module guard so a direct caller can't slip past). ──
247
+ if (offset >= RETRIEVAL_CAP) {
248
+ throw new ToolErrorCarrier({
249
+ kind: "invalid_input",
250
+ message: `NIH RePORTER caps pagination at the first ${RETRIEVAL_CAP} records (offset 0..${RETRIEVAL_CAP - 1}); offset ${offset} is unreachable via this keyless API. Narrow criteria (org, fiscal year, state) to bring the target set under ${RETRIEVAL_CAP}.`,
251
+ retryable: false,
252
+ upstreamEndpoint: NIH_LABEL,
253
+ });
254
+ }
255
+
256
+ // ── Build the criteria object from VALIDATED typed input (SSRF: no raw
257
+ // passthrough; every value is enum/typed by the server's Zod before here). A
258
+ // filter is added — and listed in filtersApplied — ONLY when it is one of the
259
+ // LIVE-CONFIRMED narrowing criteria (M1). agency_ic_codes is never built (it
260
+ // silently no-ops upstream and is not in the schema). ──
261
+ const criteria: Record<string, unknown> = {};
262
+ const filtersApplied: string[] = [];
263
+ if (args.fiscalYears && args.fiscalYears.length > 0) {
264
+ criteria.fiscal_years = args.fiscalYears;
265
+ filtersApplied.push("fiscalYears");
266
+ }
267
+ if (args.orgStates && args.orgStates.length > 0) {
268
+ criteria.org_states = args.orgStates;
269
+ filtersApplied.push("orgStates");
270
+ }
271
+ if (args.orgNames && args.orgNames.length > 0) {
272
+ criteria.org_names = args.orgNames;
273
+ filtersApplied.push("orgNames");
274
+ }
275
+
276
+ const payload = { criteria, limit, offset };
277
+
278
+ // ── SSRF belt-and-suspenders: the URL is a compile-time constant, but assert it
279
+ // cannot have drifted (a future typo) — exactly the CKAN/FPDS post-construction
280
+ // check, adapted (no URLSearchParams: there are no query params). ──
281
+ const built = new URL(NIH_PROJECTS_URL);
282
+ if (built.hostname !== NIH_HOST || built.protocol !== "https:") {
283
+ throw new ToolErrorCarrier({
284
+ kind: "invalid_input",
285
+ message: `Constructed NIH URL host ${JSON.stringify(built.hostname)} (${built.protocol}) is not ${NIH_HOST} over https — refusing to fetch (SSRF safety).`,
286
+ retryable: false,
287
+ upstreamEndpoint: NIH_LABEL,
288
+ });
289
+ }
290
+
291
+ // ── The R2 port's first POST call. Content-Type is MANDATORY for NIH (415
292
+ // without it, 405 on GET). redirect:"error" fails closed on any off-host 3xx
293
+ // (its body is never read). The body is the module-built, JSON.stringify'd
294
+ // typed payload — never string-concatenated. ──
295
+ const body = await getJson<unknown>(NIH_PROJECTS_URL, {
296
+ label: NIH_LABEL,
297
+ method: "POST",
298
+ headers: { "Content-Type": "application/json" },
299
+ body: JSON.stringify(payload),
300
+ redirect: "error",
301
+ });
302
+
303
+ // ── Shape guard FIRST → THROW (never fake-empty). A 200 body must be an OBJECT
304
+ // with a `meta` object and an array `results`. An array body (a 200-with-array
305
+ // drift), a missing `meta`, or a non-array `results` → driftError; NEVER read
306
+ // `results` as []. ──
307
+ if (
308
+ typeof body !== "object" ||
309
+ body === null ||
310
+ Array.isArray(body)
311
+ ) {
312
+ throw driftError(
313
+ NIH_LABEL,
314
+ "NIH /v2/projects/search returned a 200 body that is not an object {meta,results} (an array or scalar) — refusing to report it as an empty result.",
315
+ );
316
+ }
317
+ const b = body as { meta?: unknown; results?: unknown };
318
+ if (typeof b.meta !== "object" || b.meta === null || !Array.isArray(b.results)) {
319
+ throw driftError(
320
+ NIH_LABEL,
321
+ "NIH /v2/projects/search returned an unexpected shape (expected an object with a `meta` object and an array `results`).",
322
+ );
323
+ }
324
+ const meta = b.meta as { total?: unknown };
325
+
326
+ // ── m-total-guard: a non-number `meta.total` (or absent) is drift — num() cannot
327
+ // tell a non-number from an absent one, so typeof-check BEFORE num() (CKAN m6).
328
+ // Nothing trustworthy to report as a total → THROW, never proceed with null. ──
329
+ if (typeof meta.total !== "number") {
330
+ throw driftError(
331
+ NIH_LABEL,
332
+ "NIH meta.total absent/non-numeric — nothing trustworthy to report as a total (treating as schema drift).",
333
+ );
334
+ }
335
+ // EXACT total (§1a) — NEVER results.length, NEVER a lower bound. num() defensively
336
+ // (a finite number passes through; a non-finite would be caught, but the typeof
337
+ // guard above already rejected non-numbers).
338
+ const totalAvailable = num(meta.total);
339
+
340
+ const projects = (b.results as unknown[]).map(mapProject);
341
+ const returned = projects.length;
342
+
343
+ // ── Pagination (M3+M4): NEVER hand a dead-end nextOffset=15000. nextOffset is
344
+ // null once offset+returned reaches the cap OR the exact total; hasMore mirrors
345
+ // it. NO mid-page clamp (offset+limit>15000 does NOT 400 — only offset≥15000). ──
346
+ const candidateNext = offset + returned;
347
+ const nextOffset =
348
+ candidateNext >= RETRIEVAL_CAP ||
349
+ (totalAvailable !== null && candidateNext >= totalAvailable)
350
+ ? null
351
+ : candidateNext;
352
+ const hasMore = nextOffset !== null;
353
+
354
+ const notes: string[] = [NIH_GRANT_CAVEAT, UEI_JOIN_NOTE, DATA_CURRENCY_NOTE];
355
+ // Disclose-not-refuse + the reachability-cap disclosure: fire whenever the exact
356
+ // count exceeds the reachable window (records beyond 15,000 are UNREACHABLE — the
357
+ // honest superset of "truncated by the cap" and "an unscoped broad query").
358
+ if (totalAvailable !== null && totalAvailable > RETRIEVAL_CAP) {
359
+ notes.push(retrievalCapNote(totalAvailable));
360
+ }
361
+
362
+ const metaOut: Partial<ResponseMeta> = {
363
+ source: SOURCE,
364
+ keylessMode: true,
365
+ returned,
366
+ totalAvailable,
367
+ filtersApplied,
368
+ filtersDropped: [],
369
+ fieldsUnavailable: [],
370
+ pagination: { offset, limit, hasMore, nextOffset },
371
+ notes,
372
+ };
373
+
374
+ return withMeta({ projects }, metaOut);
375
+ }