evidence-mcp-dev 1.0.5 → 1.0.6
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/dist/index.js +120 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2638,8 +2638,8 @@ function walled(url, stage2, dir = FH_EVIDENCE_DIR) {
|
|
|
2638
2638
|
const h = host(url);
|
|
2639
2639
|
if (!h) return null;
|
|
2640
2640
|
const all = read(dir, h);
|
|
2641
|
-
const
|
|
2642
|
-
return
|
|
2641
|
+
const live3 = (r) => r && (r.streak >= WALL_STREAK || RULE.test(r.reason)) && daysSince(r.date) < settings().retry_after_days ? r : null;
|
|
2642
|
+
return live3(all[stage2] ?? all[legacy(stage2)]) ?? live3(all[pathKey(url, stage2)]);
|
|
2643
2643
|
}
|
|
2644
2644
|
function recordStage(url, stage2, ok, reason = "", dir = FH_EVIDENCE_DIR) {
|
|
2645
2645
|
const h = host(url);
|
|
@@ -3614,7 +3614,7 @@ function publish() {
|
|
|
3614
3614
|
}
|
|
3615
3615
|
|
|
3616
3616
|
// src/tools.ts
|
|
3617
|
-
import { existsSync as
|
|
3617
|
+
import { existsSync as existsSync15, readFileSync as readFileSync13 } from "fs";
|
|
3618
3618
|
import { z } from "zod";
|
|
3619
3619
|
|
|
3620
3620
|
// src/cascade.ts
|
|
@@ -5271,6 +5271,84 @@ async function searchArticles(raw, libraryDir = FH_EVIDENCE_DIR, fetchImpl = liv
|
|
|
5271
5271
|
return { ok: true, result, cached: false };
|
|
5272
5272
|
}
|
|
5273
5273
|
|
|
5274
|
+
// src/dailymedsearch.ts
|
|
5275
|
+
import { createHash as createHash6 } from "crypto";
|
|
5276
|
+
import { existsSync as existsSync13, mkdirSync as mkdirSync10, readFileSync as readFileSync11 } from "fs";
|
|
5277
|
+
import { dirname as dirname7 } from "path";
|
|
5278
|
+
var MAX_RESULTS3 = 100;
|
|
5279
|
+
var DEFAULT_RESULTS3 = 20;
|
|
5280
|
+
var COLUMNS3 = ["set_id", "title", "labeler", "published", "version", "level"];
|
|
5281
|
+
var API3 = "https://dailymed.nlm.nih.gov/dailymed/services/v2/spls.json";
|
|
5282
|
+
var MONTHS2 = { Jan: "01", Feb: "02", Mar: "03", Apr: "04", May: "05", Jun: "06", Jul: "07", Aug: "08", Sep: "09", Oct: "10", Nov: "11", Dec: "12" };
|
|
5283
|
+
function normalizeQuery3(q) {
|
|
5284
|
+
const drug_name = (q.drug_name ?? "").replace(/\s+/g, " ").trim();
|
|
5285
|
+
if (!drug_name) return { error: "empty drug_name" };
|
|
5286
|
+
const labeler = (q.labeler ?? "").replace(/\s+/g, " ").trim();
|
|
5287
|
+
const out = { drug_name, max_results: Math.min(MAX_RESULTS3, Math.max(1, Math.floor(q.max_results ?? DEFAULT_RESULTS3))) };
|
|
5288
|
+
if (labeler) out.labeler = labeler;
|
|
5289
|
+
return out;
|
|
5290
|
+
}
|
|
5291
|
+
var searchKey3 = (q) => createHash6("sha1").update(JSON.stringify(["dailymed", q.drug_name.toLowerCase(), (q.labeler ?? "").toLowerCase(), q.max_results])).digest("hex").slice(0, 16);
|
|
5292
|
+
function searchUrl2(q) {
|
|
5293
|
+
const u = new URL(API3);
|
|
5294
|
+
u.searchParams.set("drug_name", q.drug_name);
|
|
5295
|
+
if (q.labeler) u.searchParams.set("labeler", q.labeler);
|
|
5296
|
+
u.searchParams.set("pagesize", String(q.max_results));
|
|
5297
|
+
return u.toString();
|
|
5298
|
+
}
|
|
5299
|
+
var isoDate2 = (d) => {
|
|
5300
|
+
const m = /^([A-Z][a-z]{2}) (\d{1,2}), (\d{4})$/.exec(String(d ?? "").trim());
|
|
5301
|
+
return m && MONTHS2[m[1]] ? `${m[3]}-${MONTHS2[m[1]]}-${m[2].padStart(2, "0")}` : null;
|
|
5302
|
+
};
|
|
5303
|
+
function splitTitle(t) {
|
|
5304
|
+
const m = /^(.*?)\s*\[([^\]]+)\]\s*$/.exec(t.trim());
|
|
5305
|
+
return m ? [m[1], m[2]] : [t.trim(), null];
|
|
5306
|
+
}
|
|
5307
|
+
function levelOf2(setid, libraryDir) {
|
|
5308
|
+
const paths = entryPaths(libraryDir, "dailymed", setid);
|
|
5309
|
+
return entryExists(paths) ? String(readStatus(paths, emptyStatus(`dailymed:${setid}`, "dailymed", setid, libraryDir)).level) : null;
|
|
5310
|
+
}
|
|
5311
|
+
function parseSearch(json2, libraryDir) {
|
|
5312
|
+
const d = JSON.parse(json2);
|
|
5313
|
+
const rows = [];
|
|
5314
|
+
for (const x of d.data ?? []) {
|
|
5315
|
+
if (!x.setid) continue;
|
|
5316
|
+
const [title, labeler] = splitTitle(String(x.title ?? ""));
|
|
5317
|
+
rows.push([x.setid, title, labeler, isoDate2(x.published_date), typeof x.spl_version === "number" ? x.spl_version : null, levelOf2(x.setid, libraryDir)]);
|
|
5318
|
+
}
|
|
5319
|
+
return { total: Number(d.metadata?.total_elements ?? rows.length) || rows.length, rows };
|
|
5320
|
+
}
|
|
5321
|
+
var live2 = (url) => fetchText(url, { "User-Agent": LIBRARY_UA, Accept: "application/json" });
|
|
5322
|
+
async function searchLabels(raw, libraryDir = FH_EVIDENCE_DIR, fetchImpl = live2) {
|
|
5323
|
+
const q = normalizeQuery3(raw);
|
|
5324
|
+
if ("error" in q) return { ok: false, error: q.error };
|
|
5325
|
+
const file3 = searchFile(libraryDir, searchKey3(q));
|
|
5326
|
+
if (existsSync13(file3)) {
|
|
5327
|
+
try {
|
|
5328
|
+
const r = JSON.parse(readFileSync11(file3, "utf8"));
|
|
5329
|
+
if (daysSince(r.searched) < searchCacheDays()) return { ok: true, result: { ...r, rows: r.rows.map((row3) => {
|
|
5330
|
+
const c = [...row3];
|
|
5331
|
+
c[5] = levelOf2(row3[0], libraryDir);
|
|
5332
|
+
return c;
|
|
5333
|
+
}) }, cached: true };
|
|
5334
|
+
} catch {
|
|
5335
|
+
}
|
|
5336
|
+
}
|
|
5337
|
+
let parsed;
|
|
5338
|
+
try {
|
|
5339
|
+
parsed = parseSearch(await fetchImpl(searchUrl2(q)), libraryDir);
|
|
5340
|
+
} catch (e) {
|
|
5341
|
+
return { ok: false, error: `dailymed: ${e instanceof Error ? e.message : String(e)}` };
|
|
5342
|
+
}
|
|
5343
|
+
const result = { source: "dailymed", query: q, total: parsed.total, columns: COLUMNS3, rows: parsed.rows, searched: today() };
|
|
5344
|
+
try {
|
|
5345
|
+
mkdirSync10(dirname7(file3), { recursive: true });
|
|
5346
|
+
writeAtomic(file3, JSON.stringify(result, null, 2) + "\n");
|
|
5347
|
+
} catch {
|
|
5348
|
+
}
|
|
5349
|
+
return { ok: true, result, cached: false };
|
|
5350
|
+
}
|
|
5351
|
+
|
|
5274
5352
|
// src/trialrecord.ts
|
|
5275
5353
|
var list4 = (a) => Array.isArray(a) ? a : [];
|
|
5276
5354
|
var para2 = (s, max = Infinity) => {
|
|
@@ -5388,13 +5466,13 @@ function resultsText(nct, md, maxChars) {
|
|
|
5388
5466
|
}
|
|
5389
5467
|
|
|
5390
5468
|
// src/missing.ts
|
|
5391
|
-
import { existsSync as
|
|
5469
|
+
import { existsSync as existsSync14, mkdirSync as mkdirSync11, readFileSync as readFileSync12 } from "fs";
|
|
5392
5470
|
import { join as join12 } from "path";
|
|
5393
5471
|
var missingFile = (p) => join12(p.entryDir, `${p.entry}-missing.md`);
|
|
5394
5472
|
function readMissing(p) {
|
|
5395
5473
|
const f = missingFile(p);
|
|
5396
|
-
if (!
|
|
5397
|
-
const md =
|
|
5474
|
+
if (!existsSync14(f)) return null;
|
|
5475
|
+
const md = readFileSync12(f, "utf8");
|
|
5398
5476
|
const get4 = (k) => /^$/.test(k) ? "" : (new RegExp(`^${k}:[ \\t]*(.*)$`, "m").exec(md)?.[1] ?? "").trim();
|
|
5399
5477
|
const checked = get4("checked"), reason = get4("reason");
|
|
5400
5478
|
return checked ? { checked, reason: reason || "not found" } : null;
|
|
@@ -5405,7 +5483,7 @@ function freshMissing(p, retryDays) {
|
|
|
5405
5483
|
}
|
|
5406
5484
|
function rememberMissing(p, reason) {
|
|
5407
5485
|
const m = { checked: now(), reason };
|
|
5408
|
-
|
|
5486
|
+
mkdirSync11(p.entryDir, { recursive: true });
|
|
5409
5487
|
writeAtomic(missingFile(p), `checked: ${m.checked}
|
|
5410
5488
|
reason: ${m.reason}
|
|
5411
5489
|
`);
|
|
@@ -5415,7 +5493,7 @@ var missingNote = (m, retryDays) => `${m.reason} (checked ${m.checked.slice(0, 1
|
|
|
5415
5493
|
|
|
5416
5494
|
// src/pubmedbatch.ts
|
|
5417
5495
|
var MAX_PMIDS = 200;
|
|
5418
|
-
var
|
|
5496
|
+
var COLUMNS4 = ["pmid", "title", "first_author", "year", "publication_types", "retracted", "level"];
|
|
5419
5497
|
var EFETCH2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi";
|
|
5420
5498
|
function efetchBatchUrl(pmids) {
|
|
5421
5499
|
const u = new URL(EFETCH2);
|
|
@@ -5439,7 +5517,7 @@ function splitArticles(xml) {
|
|
|
5439
5517
|
}
|
|
5440
5518
|
var row2 = (pmid, s, level) => s ? [pmid, s.title ? s.title.replace(/\.$/, "") : s.title, s.first_author, s.year, reviewTypes(s.publication_types).join("; ") || null, s.retracted, level] : [pmid, null, null, null, null, null, level];
|
|
5441
5519
|
async function fetchArticles(ids, libraryDir = FH_EVIDENCE_DIR, converter = "evidence-mcp", fetchImpl = fetchText) {
|
|
5442
|
-
const result = { columns:
|
|
5520
|
+
const result = { columns: COLUMNS4, rows: [], new: 0, cached: 0, not_found: 0, invalid: 0, asked: 0 };
|
|
5443
5521
|
const order2 = [];
|
|
5444
5522
|
const invalid = /* @__PURE__ */ new Map();
|
|
5445
5523
|
for (const raw of ids.slice(0, MAX_PMIDS)) {
|
|
@@ -5501,7 +5579,7 @@ async function fetchArticles(ids, libraryDir = FH_EVIDENCE_DIR, converter = "evi
|
|
|
5501
5579
|
// src/tools.ts
|
|
5502
5580
|
var pkg = (() => {
|
|
5503
5581
|
try {
|
|
5504
|
-
return JSON.parse(
|
|
5582
|
+
return JSON.parse(readFileSync13(new URL("../package.json", import.meta.url), "utf8"));
|
|
5505
5583
|
} catch {
|
|
5506
5584
|
return { name: "evidence-mcp", version: "0.0.0" };
|
|
5507
5585
|
}
|
|
@@ -5511,7 +5589,7 @@ var INSTRUCTIONS = `evidence-mcp keeps a local library of scientific papers, FDA
|
|
|
5511
5589
|
IDs \u2014 always "type:value"
|
|
5512
5590
|
- pmid:30955454 \u2014 a paper by PubMed ID. Bare digits are taken as a PMID.
|
|
5513
5591
|
- doi:10.\u2026 \u2014 a paper by DOI. A DOI PubMed indexes is stored under its PMID; any other gets its record from Crossref.
|
|
5514
|
-
- dailymed:<set ID> \u2014 an FDA prescribing-information label, current version. The set ID is the "setid=" value in the label's DailyMed URL.
|
|
5592
|
+
- dailymed:<set ID> \u2014 an FDA prescribing-information label, current version. The set ID comes from fh_dailymed_search_labels, or is the "setid=" value in the label's DailyMed URL.
|
|
5515
5593
|
- nct:05173896 \u2014 a ClinicalTrials.gov trial record, current version. NCT05173896 is accepted too.
|
|
5516
5594
|
|
|
5517
5595
|
Tools
|
|
@@ -5523,6 +5601,7 @@ Tools
|
|
|
5523
5601
|
- fh_pubmed_fetch_articles(pmids) \u2014 title, first author, year, publication types, retracted and level for up to 200 PMIDs at once; faster and far smaller than asking PubMed, known papers from disk, the rest in one request and stored. For abstract or full text: fh_library_read.
|
|
5524
5602
|
- fh_pubmed_search_articles(query, max_results?, sort?, years?) \u2014 search PubMed in its own syntax: the total and rows in columns order (PMID, title, first author, year, journal, publication types, PMCID, library level); faster and far smaller than asking PubMed. Cached a week.
|
|
5525
5603
|
- fh_clinicaltrials_search_studies(query | intervention | condition, status?, sort?, max_results?) \u2014 search ClinicalTrials.gov: the total and rows in columns order (NCT ID, title, status, phase, enrollment, dates, primary outcome, conditions, interventions, has_results). Cached a week.
|
|
5604
|
+
- fh_dailymed_search_labels(drug_name, labeler?, max_results?) \u2014 find FDA drug labels by generic or brand name: the total and rows in columns order (set ID, title, labeler, published, version, level), newest first; labeler narrows to one company. Use it rather than DailyMed's search page, which shows no set IDs as Markdown. Cached a week.
|
|
5526
5605
|
- fh_clinicaltrials_get_study_record(nct) \u2014 one trial as a review needs it (status, design, enrollment, dates, outcomes, eligibility, result publications, summary), from the stored record; added first when missing.
|
|
5527
5606
|
- fh_clinicaltrials_get_study_results(nct, max_chars?) \u2014 a trial's posted results as Markdown, capped; the outcome titles listed for fh_library_read.
|
|
5528
5607
|
- fh_fetch_url(url, max_length?, start_index?, raw?, title_only?) \u2014 a web page as Markdown, same call and same answer as the standard fetch tool ("Contents of <url>:" and the page, cut at max_length with a note on how to continue). Prefer it for every URL: a cached page answers from disk, a walled or script-rendered page runs through the browser and unlocker stages in one call. PDFs as Markdown; XML, JSON and other text as is. title_only returns the title line alone \u2014 enough to verify a link, at a few tokens.
|
|
@@ -5896,8 +5975,8 @@ function registerTools(server2) {
|
|
|
5896
5975
|
}
|
|
5897
5976
|
added = true;
|
|
5898
5977
|
}
|
|
5899
|
-
const full =
|
|
5900
|
-
const md =
|
|
5978
|
+
const full = existsSync15(paths.fulltextMd);
|
|
5979
|
+
const md = readFileSync13(full ? paths.fulltextMd : paths.md, "utf8");
|
|
5901
5980
|
const text4 = full ? "full_text" : "abstract";
|
|
5902
5981
|
const base = added ? { requested: parsed.requested, entry: paths.entry, text: text4, added: true } : { requested: parsed.requested, entry: paths.entry, text: text4 };
|
|
5903
5982
|
const headings2 = () => outline(md, text4).sections;
|
|
@@ -5917,8 +5996,8 @@ function registerTools(server2) {
|
|
|
5917
5996
|
const cap = max_chars ?? 6e3;
|
|
5918
5997
|
let sec = readSection(md, section2, cap);
|
|
5919
5998
|
let via = "";
|
|
5920
|
-
if (!sec && /abstract/i.test(section2) && full &&
|
|
5921
|
-
sec = readSection(
|
|
5999
|
+
if (!sec && /abstract/i.test(section2) && full && existsSync15(paths.md)) {
|
|
6000
|
+
sec = readSection(readFileSync13(paths.md, "utf8"), "abstract", cap);
|
|
5922
6001
|
via = " (from the abstract file)";
|
|
5923
6002
|
}
|
|
5924
6003
|
if (!sec && /declaration/i.test(section2)) {
|
|
@@ -6047,8 +6126,8 @@ ${st.funding.map((f) => `- ${f}`).join("\n")}` : ""].filter(Boolean).join("\n\n"
|
|
|
6047
6126
|
return { content: [{ type: "text", text: `Failed to fetch ${url}: ${added.error}` }], isError: true };
|
|
6048
6127
|
}
|
|
6049
6128
|
}
|
|
6050
|
-
const file3 = pid.type === "pmid" ? paths.md :
|
|
6051
|
-
const md =
|
|
6129
|
+
const file3 = pid.type === "pmid" ? paths.md : existsSync15(paths.fulltextMd) ? paths.fulltextMd : paths.md;
|
|
6130
|
+
const md = existsSync15(file3) ? readFileSync13(file3, "utf8") : "";
|
|
6052
6131
|
const text4 = title_only ? `Contents of ${url}:
|
|
6053
6132
|
${(md.split("\n").find((l) => l.trim()) ?? "").trim()}` : fetchAnswer(url, md, "", max_length, start_index);
|
|
6054
6133
|
bump("fetch", had);
|
|
@@ -6132,6 +6211,28 @@ ${(md.split("\n").find((l) => l.trim()) ?? "").trim()}` : fetchAnswer(url, md, "
|
|
|
6132
6211
|
return { content: [{ type: "text", text: searchAnswer(r.result) }] };
|
|
6133
6212
|
}
|
|
6134
6213
|
);
|
|
6214
|
+
server2.tool(
|
|
6215
|
+
"fh_dailymed_search_labels",
|
|
6216
|
+
`Find FDA drug labels on DailyMed by drug name, generic or brand: the total and up to ${MAX_RESULTS3} rows as arrays in columns order \u2014 set ID, title (drug and form), labeler (the company), published date, version, and the library level when the label is already stored. Newest first. A generic name lists every maker's label; labeler narrows to one company ("Boehringer" for Micardis's maker), a brand name to its own labels. Pass the set ID as "dailymed:<set ID>" to fh_library_read or fh_library_add. Use this, not DailyMed's search page: fetched as Markdown the page shows no set IDs, raw it is some 150,000 characters. The same search is answered from disk for ${searchCacheDays()} days.`,
|
|
6217
|
+
{
|
|
6218
|
+
drug_name: z.string().describe('Generic or brand name: "telmisartan", "Micardis".'),
|
|
6219
|
+
labeler: z.string().optional().describe(`Part of the company's name, to narrow to its labels: "Boehringer".`),
|
|
6220
|
+
max_results: z.number().int().min(1).max(MAX_RESULTS3).default(20).describe("Rows to return (1\u2013100).")
|
|
6221
|
+
},
|
|
6222
|
+
async (args) => {
|
|
6223
|
+
const t0 = Date.now();
|
|
6224
|
+
const r = await searchLabels(args, FH_EVIDENCE_DIR);
|
|
6225
|
+
const secs = ((Date.now() - t0) / 1e3).toFixed(1).padStart(4, "0") + "s";
|
|
6226
|
+
if (!r.ok) {
|
|
6227
|
+
log("search", "dailymed", `error: ${r.error}`);
|
|
6228
|
+
return { content: [{ type: "text", text: compact2({ error: r.error }) }], isError: true };
|
|
6229
|
+
}
|
|
6230
|
+
bump("search", r.cached);
|
|
6231
|
+
const q = r.result.query;
|
|
6232
|
+
log("search", "dailymed", `ok ${r.cached ? "cached" : secs} rows=${r.result.rows.length} total=${r.result.total} | ${q.drug_name}${q.labeler ? ` labeler=${q.labeler}` : ""}`);
|
|
6233
|
+
return { content: [{ type: "text", text: searchAnswer(r.result) }] };
|
|
6234
|
+
}
|
|
6235
|
+
);
|
|
6135
6236
|
const nctEntry = async (id) => {
|
|
6136
6237
|
const p = parseId(/^\d{8}$/.test(id.trim()) ? `nct:${id.trim()}` : id);
|
|
6137
6238
|
if (p.error) return { error: p.error };
|
|
@@ -6157,7 +6258,7 @@ ${(md.split("\n").find((l) => l.trim()) ?? "").trim()}` : fetchAnswer(url, md, "
|
|
|
6157
6258
|
return { content: [{ type: "text", text: compact2({ error: e.error }) }], isError: true };
|
|
6158
6259
|
}
|
|
6159
6260
|
const raw = e.paths.fulltextRaw("json");
|
|
6160
|
-
const r =
|
|
6261
|
+
const r = existsSync15(raw) ? recordSummary(readFileSync13(raw, "utf8")) : null;
|
|
6161
6262
|
if (!r) {
|
|
6162
6263
|
log("trials", e.requested, "record error: no stored record");
|
|
6163
6264
|
return { content: [{ type: "text", text: compact2({ error: "no stored record" }) }], isError: true };
|
|
@@ -6176,7 +6277,7 @@ ${(md.split("\n").find((l) => l.trim()) ?? "").trim()}` : fetchAnswer(url, md, "
|
|
|
6176
6277
|
log("trials", nct, `results error: ${e.error}`);
|
|
6177
6278
|
return { content: [{ type: "text", text: compact2({ error: e.error }) }], isError: true };
|
|
6178
6279
|
}
|
|
6179
|
-
const md =
|
|
6280
|
+
const md = existsSync15(e.paths.fulltextMd) ? readFileSync13(e.paths.fulltextMd, "utf8") : "";
|
|
6180
6281
|
const r = resultsText(e.requested.replace(/^nct:/, "NCT"), md, max_chars);
|
|
6181
6282
|
log("trials", e.requested, `results ${r.has_results ? `${r.outcomes.length} outcomes${r.truncated ? " truncated" : ""}` : "none"}`);
|
|
6182
6283
|
return { content: [{ type: "text", text: leanJson(r) }] };
|