brainx-skill 1.0.13 → 1.0.14
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/README.md +26 -0
- package/installation/lib/cli.js +16 -4
- package/installation/lib/literature-mcp.js +536 -0
- package/manifest.json +2 -0
- package/mcp-servers/codex/README.md +24 -4
- package/mcp-servers/codex/UPSTREAM.md +3 -1
- package/mcp-servers/codex/server.mjs +2 -0
- package/mcp-servers/codex/system-prompt.md +30 -19
- package/mcp-servers/codex/test/server.test.mjs +12 -1
- package/mcp-servers/fulltext-resolver/README.md +136 -0
- package/mcp-servers/fulltext-resolver/REUSE.md +32 -0
- package/mcp-servers/fulltext-resolver/THIRD_PARTY_NOTICES.md +57 -0
- package/mcp-servers/fulltext-resolver/server.mjs +128 -0
- package/mcp-servers/fulltext-resolver/src/document.mjs +253 -0
- package/mcp-servers/fulltext-resolver/src/http.mjs +173 -0
- package/mcp-servers/fulltext-resolver/src/identifiers.mjs +123 -0
- package/mcp-servers/fulltext-resolver/src/model.mjs +105 -0
- package/mcp-servers/fulltext-resolver/src/resolver.mjs +305 -0
- package/mcp-servers/fulltext-resolver/src/sources/biorxiv.mjs +135 -0
- package/mcp-servers/fulltext-resolver/src/sources/europe-pmc.mjs +132 -0
- package/mcp-servers/fulltext-resolver/src/sources/openalex.mjs +165 -0
- package/mcp-servers/fulltext-resolver/test/document.test.mjs +82 -0
- package/mcp-servers/fulltext-resolver/test/resolver.test.mjs +287 -0
- package/mcp-servers/fulltext-resolver/test/server.test.mjs +63 -0
- package/mcp-servers/fulltext-resolver/test/sources.test.mjs +229 -0
- package/package.json +10 -3
- package/skills/bio-neuro-lit/SKILL.md +206 -0
- package/skills/bio-neuro-lit/THIRD_PARTY_NOTICES.md +34 -0
- package/skills/bio-neuro-lit/agents/openai.yaml +4 -0
- package/skills/bio-neuro-lit/references/tool-contracts.md +73 -0
- package/skills/bio-neuro-lit/scripts/deepxiv_fetch.py +110 -0
- package/skills/bio-neuro-lit/scripts/exa_search.py +190 -0
- package/skills/brainx-general-guard/SKILL.md +17 -0
- package/skills/brainx-modeling-loop/SKILL.md +33 -20
- package/skills/brainx-modeling-loop/references/parameter-fitting-workflow.md +47 -7
- package/skills/brainx-visualization/SKILL.md +157 -0
- package/skills/brainx-visualization/agents/openai.yaml +4 -0
- package/skills/brainx-visualization/references/interactive-and-visualization-styling.md +155 -0
- package/skills/brainx-visualization/references/neural-data-visualization.md +141 -0
- package/skills/brainx-visualization/references/statistical-and-model-visualization.md +133 -0
- package/skills/package-skills/braincell/SKILL.md +16 -6
- package/skills/package-skills/braincell/references/braintools/input-current.md +200 -0
- package/skills/package-skills/braincell/references/braintools/metric.md +152 -0
- package/skills/package-skills/braincell/references/braintools/optimizer.md +179 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { htmlToDocument, jatsToDocument } from './document.mjs';
|
|
2
|
+
import { HttpClient } from './http.mjs';
|
|
3
|
+
import { normalizeRequest } from './identifiers.mjs';
|
|
4
|
+
import {
|
|
5
|
+
deduplicateCandidates,
|
|
6
|
+
publicCandidate,
|
|
7
|
+
relationshipFor,
|
|
8
|
+
requestedWorkFrom,
|
|
9
|
+
sortCandidates,
|
|
10
|
+
} from './model.mjs';
|
|
11
|
+
import { BiorxivSource } from './sources/biorxiv.mjs';
|
|
12
|
+
import { EuropePmcSource } from './sources/europe-pmc.mjs';
|
|
13
|
+
import { OpenAlexSource } from './sources/openalex.mjs';
|
|
14
|
+
|
|
15
|
+
function errorRecord(source, error) {
|
|
16
|
+
return {
|
|
17
|
+
source,
|
|
18
|
+
message: error instanceof Error ? error.message : String(error),
|
|
19
|
+
status: error?.status ?? null,
|
|
20
|
+
retryable: Boolean(error?.retryable),
|
|
21
|
+
retry_after_seconds: error?.retryAfter ?? null,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mergeMetadata(...records) {
|
|
26
|
+
const result = {};
|
|
27
|
+
for (const record of records) {
|
|
28
|
+
if (!record) continue;
|
|
29
|
+
for (const [key, value] of Object.entries(record)) {
|
|
30
|
+
if (result[key] === undefined || result[key] === null || result[key] === '' || result[key]?.length === 0) {
|
|
31
|
+
if (value !== undefined && value !== null && value !== '' && value?.length !== 0) result[key] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function enrichRequest(request, metadata) {
|
|
39
|
+
return {
|
|
40
|
+
...request,
|
|
41
|
+
doi: request.doi || metadata.doi || '',
|
|
42
|
+
pmid: request.pmid || metadata.pmid || '',
|
|
43
|
+
pmcid: request.pmcid || metadata.pmcid || '',
|
|
44
|
+
title: request.title || metadata.title || '',
|
|
45
|
+
authors: request.authors.length ? request.authors : metadata.authors || [],
|
|
46
|
+
year: request.year || metadata.year,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolutionResult(request, metadata, candidates, sourceErrors) {
|
|
51
|
+
const versions = candidates.map(publicCandidate);
|
|
52
|
+
const resolvedDocument = versions[0] || null;
|
|
53
|
+
return {
|
|
54
|
+
resolved: Boolean(resolvedDocument),
|
|
55
|
+
requested_work: requestedWorkFrom(request, metadata),
|
|
56
|
+
resolved_document: resolvedDocument,
|
|
57
|
+
source: resolvedDocument?.source ?? null,
|
|
58
|
+
version: resolvedDocument?.version ?? null,
|
|
59
|
+
relationship: resolvedDocument?.relationship ?? null,
|
|
60
|
+
format: resolvedDocument?.normalized_format ?? resolvedDocument?.original_format ?? null,
|
|
61
|
+
location: resolvedDocument?.source_location ?? null,
|
|
62
|
+
versions,
|
|
63
|
+
source_errors: sourceErrors,
|
|
64
|
+
unresolved_reason: resolvedDocument ? null : 'No accessible full-text version was found.',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class FullTextResolver {
|
|
69
|
+
constructor(options = {}) {
|
|
70
|
+
this.http = options.http ?? new HttpClient(options.httpOptions);
|
|
71
|
+
this.europePmc = options.europePmc ?? new EuropePmcSource(this.http, options.europePmcOptions);
|
|
72
|
+
this.openAlex = options.openAlex ?? new OpenAlexSource(this.http, options.openAlexOptions);
|
|
73
|
+
this.biorxiv = options.biorxiv ?? new BiorxivSource(this.http, options.biorxivOptions);
|
|
74
|
+
this.cache = new Map();
|
|
75
|
+
this.cacheSize = options.cacheSize ?? 16;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async resolveInternal(input, options = {}) {
|
|
79
|
+
const request = normalizeRequest(input);
|
|
80
|
+
const sourceErrors = [];
|
|
81
|
+
let europePmcResult = { metadata: {}, candidates: [] };
|
|
82
|
+
try {
|
|
83
|
+
europePmcResult = await this.europePmc.lookup(request, options);
|
|
84
|
+
for (const error of europePmcResult.sourceErrors || []) {
|
|
85
|
+
sourceErrors.push(errorRecord('PMC', error));
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
sourceErrors.push(errorRecord('PMC', error));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const enriched = enrichRequest(request, europePmcResult.metadata);
|
|
92
|
+
const secondary = await Promise.allSettled([
|
|
93
|
+
this.openAlex.lookup(enriched, options),
|
|
94
|
+
this.biorxiv.lookup(enriched, options),
|
|
95
|
+
]);
|
|
96
|
+
const [openAlexResult, biorxivResult] = secondary.map((result, index) => {
|
|
97
|
+
if (result.status === 'fulfilled') return result.value;
|
|
98
|
+
sourceErrors.push(errorRecord(index === 0 ? 'OpenAlex' : 'bioRxiv/medRxiv', result.reason));
|
|
99
|
+
return { metadata: {}, candidates: [] };
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const metadata = mergeMetadata(
|
|
103
|
+
europePmcResult.metadata,
|
|
104
|
+
openAlexResult.metadata,
|
|
105
|
+
biorxivResult.metadata,
|
|
106
|
+
);
|
|
107
|
+
const candidates = deduplicateCandidates([
|
|
108
|
+
...europePmcResult.candidates,
|
|
109
|
+
...openAlexResult.candidates,
|
|
110
|
+
...biorxivResult.candidates,
|
|
111
|
+
]);
|
|
112
|
+
return { request, metadata, candidates, sourceErrors };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async resolve(input, options = {}) {
|
|
116
|
+
const internal = await this.resolveInternal(input, options);
|
|
117
|
+
return resolutionResult(
|
|
118
|
+
internal.request,
|
|
119
|
+
internal.metadata,
|
|
120
|
+
internal.candidates,
|
|
121
|
+
internal.sourceErrors,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async listVersions(input, options = {}) {
|
|
126
|
+
const result = await this.resolve(input, options);
|
|
127
|
+
return {
|
|
128
|
+
resolved: result.resolved,
|
|
129
|
+
requested_work: result.requested_work,
|
|
130
|
+
versions: result.versions,
|
|
131
|
+
source_errors: result.source_errors,
|
|
132
|
+
unresolved_reason: result.unresolved_reason,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
selectCandidates(candidates, selection = {}) {
|
|
137
|
+
const source = String(selection.source || '').toLowerCase();
|
|
138
|
+
return sortCandidates(candidates).filter((candidate) => {
|
|
139
|
+
if (selection.version && candidate.version !== selection.version) return false;
|
|
140
|
+
if (source && candidate.source.toLowerCase() !== source && candidate.provider_name.toLowerCase() !== source) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
remember(key, value) {
|
|
148
|
+
if (this.cache.has(key)) this.cache.delete(key);
|
|
149
|
+
this.cache.set(key, value);
|
|
150
|
+
while (this.cache.size > this.cacheSize) {
|
|
151
|
+
this.cache.delete(this.cache.keys().next().value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async fetchCandidate(candidate, options = {}) {
|
|
156
|
+
const retrievals = [candidate.retrieval, ...(candidate.retrieval.alternatives || [])];
|
|
157
|
+
let lastError;
|
|
158
|
+
for (const retrieval of retrievals) {
|
|
159
|
+
try {
|
|
160
|
+
return await this.fetchRetrieval(candidate, retrieval, options);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
lastError = error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
throw lastError;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async fetchRetrieval(candidate, retrieval, options = {}) {
|
|
169
|
+
if (retrieval.kind === 'pdf') {
|
|
170
|
+
return {
|
|
171
|
+
title: candidate.title,
|
|
172
|
+
authors: candidate.authors,
|
|
173
|
+
abstract: candidate.abstract ?? null,
|
|
174
|
+
sections: [],
|
|
175
|
+
figure_captions: [],
|
|
176
|
+
tables: [],
|
|
177
|
+
references: [],
|
|
178
|
+
markdown: null,
|
|
179
|
+
content_retrieved: false,
|
|
180
|
+
retrieval_note: 'This accessible version is PDF-only. The resolver preserves the PDF URL but does not parse PDFs in v1.',
|
|
181
|
+
source_url: retrieval.url,
|
|
182
|
+
original_format: 'pdf',
|
|
183
|
+
normalized_format: null,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const cacheKey = `${retrieval.kind}:${retrieval.url}`;
|
|
188
|
+
if (this.cache.has(cacheKey)) {
|
|
189
|
+
const cached = this.cache.get(cacheKey);
|
|
190
|
+
const document = structuredClone(cached.document);
|
|
191
|
+
if (options.includeRaw) document.raw = cached.raw;
|
|
192
|
+
return document;
|
|
193
|
+
}
|
|
194
|
+
const response = await this.http.text(retrieval.url, {
|
|
195
|
+
signal: options.signal,
|
|
196
|
+
accept: retrieval.kind === 'jats'
|
|
197
|
+
? 'application/xml,text/xml'
|
|
198
|
+
: 'text/html,application/xhtml+xml',
|
|
199
|
+
});
|
|
200
|
+
let document;
|
|
201
|
+
if (retrieval.kind === 'jats' || /(?:xml|jats)/i.test(response.contentType)) {
|
|
202
|
+
document = jatsToDocument(response.body);
|
|
203
|
+
} else if (/pdf/i.test(response.contentType)) {
|
|
204
|
+
document = {
|
|
205
|
+
title: candidate.title,
|
|
206
|
+
authors: candidate.authors,
|
|
207
|
+
abstract: candidate.abstract ?? null,
|
|
208
|
+
sections: [],
|
|
209
|
+
figure_captions: [],
|
|
210
|
+
tables: [],
|
|
211
|
+
references: [],
|
|
212
|
+
markdown: null,
|
|
213
|
+
content_retrieved: false,
|
|
214
|
+
retrieval_note: 'The source returned a PDF. The resolver preserves its URL but does not parse PDFs in v1.',
|
|
215
|
+
source_url: response.url,
|
|
216
|
+
original_format: 'pdf',
|
|
217
|
+
normalized_format: null,
|
|
218
|
+
};
|
|
219
|
+
} else {
|
|
220
|
+
document = htmlToDocument(response.body, response.url);
|
|
221
|
+
}
|
|
222
|
+
document.content_retrieved = Boolean(document.markdown);
|
|
223
|
+
document.source_url = response.url;
|
|
224
|
+
this.remember(cacheKey, {
|
|
225
|
+
document: structuredClone(document),
|
|
226
|
+
raw: response.body,
|
|
227
|
+
});
|
|
228
|
+
const result = structuredClone(document);
|
|
229
|
+
if (options.includeRaw) result.raw = response.body;
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async getFulltext(input, selection = {}, options = {}) {
|
|
234
|
+
const internal = await this.resolveInternal(input, options);
|
|
235
|
+
const candidates = this.selectCandidates(internal.candidates, selection);
|
|
236
|
+
const fetchErrors = [];
|
|
237
|
+
for (const candidate of candidates) {
|
|
238
|
+
try {
|
|
239
|
+
const document = await this.fetchCandidate(candidate, {
|
|
240
|
+
signal: options.signal,
|
|
241
|
+
includeRaw: selection.include_raw,
|
|
242
|
+
});
|
|
243
|
+
if (selection.max_chars && document.markdown?.length > selection.max_chars) {
|
|
244
|
+
const originalCharacterCount = document.markdown.length;
|
|
245
|
+
document.markdown = document.markdown.slice(0, selection.max_chars);
|
|
246
|
+
document.truncated = true;
|
|
247
|
+
document.original_character_count = originalCharacterCount;
|
|
248
|
+
} else {
|
|
249
|
+
document.truncated = false;
|
|
250
|
+
}
|
|
251
|
+
const resolvedCandidate = candidate.version === 'unknown' && document.version
|
|
252
|
+
? {
|
|
253
|
+
...candidate,
|
|
254
|
+
version: document.version,
|
|
255
|
+
relationship: candidate.requested_doi || candidate.resolved_doi
|
|
256
|
+
? relationshipFor(document.version, candidate.requested_doi, candidate.resolved_doi)
|
|
257
|
+
: candidate.relationship,
|
|
258
|
+
}
|
|
259
|
+
: candidate;
|
|
260
|
+
const resolvedCandidates = internal.candidates.map((item) => (
|
|
261
|
+
item === candidate ? resolvedCandidate : item
|
|
262
|
+
));
|
|
263
|
+
return {
|
|
264
|
+
...resolutionResult(
|
|
265
|
+
internal.request,
|
|
266
|
+
internal.metadata,
|
|
267
|
+
resolvedCandidates,
|
|
268
|
+
internal.sourceErrors,
|
|
269
|
+
),
|
|
270
|
+
resolved_document: publicCandidate(resolvedCandidate),
|
|
271
|
+
source: resolvedCandidate.source,
|
|
272
|
+
version: resolvedCandidate.version,
|
|
273
|
+
relationship: resolvedCandidate.relationship,
|
|
274
|
+
format: document.normalized_format || document.original_format,
|
|
275
|
+
location: resolvedCandidate.source_location,
|
|
276
|
+
document,
|
|
277
|
+
fetch_errors: fetchErrors,
|
|
278
|
+
};
|
|
279
|
+
} catch (error) {
|
|
280
|
+
fetchErrors.push(errorRecord(candidate.source, error));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
...resolutionResult(
|
|
286
|
+
internal.request,
|
|
287
|
+
internal.metadata,
|
|
288
|
+
internal.candidates,
|
|
289
|
+
internal.sourceErrors,
|
|
290
|
+
),
|
|
291
|
+
resolved: false,
|
|
292
|
+
resolved_document: null,
|
|
293
|
+
source: null,
|
|
294
|
+
version: null,
|
|
295
|
+
relationship: null,
|
|
296
|
+
format: null,
|
|
297
|
+
location: null,
|
|
298
|
+
document: null,
|
|
299
|
+
fetch_errors: fetchErrors,
|
|
300
|
+
unresolved_reason: candidates.length
|
|
301
|
+
? 'Accessible versions were identified, but none could be retrieved.'
|
|
302
|
+
: 'No accessible full-text version matched the requested selection.',
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Adapted from cyanheads/biorxiv-mcp-server services (Apache-2.0); modified to
|
|
2
|
+
// support published-DOI crosswalk resolution. See ../../REUSE.md.
|
|
3
|
+
|
|
4
|
+
import { encodeDoiPath, normalizeDoi } from '../identifiers.mjs';
|
|
5
|
+
import { inferFormat, relationshipFor } from '../model.mjs';
|
|
6
|
+
|
|
7
|
+
function normalizeUpstreamText(text) {
|
|
8
|
+
if (!text) return '';
|
|
9
|
+
return String(text)
|
|
10
|
+
.replace(/O_FIG[\s\S]*?C_FIG/g, ' ')
|
|
11
|
+
.replace(/AO_SCPLOWBSTRACTC_SCPLOW/g, ' ')
|
|
12
|
+
.replace(/[OC]_SCPLOW/g, '')
|
|
13
|
+
.replace(/org\.highwire\.dtl\.\S+/g, ' ')
|
|
14
|
+
.replace(/[OCM]_FIG\b/g, ' ')
|
|
15
|
+
.replace(/\bSRC=\S*/gi, ' ')
|
|
16
|
+
.replace(/Graphical Abstract/gi, ' ')
|
|
17
|
+
.replace(/<\/?(?:i|b|u|em|strong|sub|sup|small|br|p|span|div|a|h[1-6])\b[^>]*>/gi, '')
|
|
18
|
+
.replace(/\s+/g, ' ')
|
|
19
|
+
.trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function latestRevision(revisions) {
|
|
23
|
+
return [...revisions].sort((left, right) => {
|
|
24
|
+
const versionDifference = Number(right.version || 0) - Number(left.version || 0);
|
|
25
|
+
if (versionDifference !== 0) return versionDifference;
|
|
26
|
+
return String(right.date || '').localeCompare(String(left.date || ''));
|
|
27
|
+
})[0];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function authorsFromString(value) {
|
|
31
|
+
return String(value || '').split(/\s*;\s*/).map((author) => author.trim()).filter(Boolean);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class BiorxivSource {
|
|
35
|
+
constructor(http, options = {}) {
|
|
36
|
+
this.http = http;
|
|
37
|
+
this.baseUrl = (options.baseUrl
|
|
38
|
+
?? process.env.BIORXIV_API_BASE_URL
|
|
39
|
+
?? 'https://api.biorxiv.org').replace(/\/$/, '');
|
|
40
|
+
this.webBaseUrls = {
|
|
41
|
+
biorxiv: options.biorxivWebBaseUrl ?? 'https://www.biorxiv.org',
|
|
42
|
+
medrxiv: options.medrxivWebBaseUrl ?? 'https://www.medrxiv.org',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getCrosswalk(doi, server, options = {}) {
|
|
47
|
+
const url = `${this.baseUrl}/pubs/${server}/${encodeDoiPath(doi)}/na/json`;
|
|
48
|
+
const data = await this.http.json(url, options);
|
|
49
|
+
return data.collection?.[0] || null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getDetails(doi, server, options = {}) {
|
|
53
|
+
const url = `${this.baseUrl}/details/${server}/${encodeDoiPath(doi)}/0/json`;
|
|
54
|
+
const data = await this.http.json(url, options);
|
|
55
|
+
return Array.isArray(data.collection) ? data.collection : [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async lookup(request, options = {}) {
|
|
59
|
+
if (!request.doi) return { metadata: {}, candidates: [] };
|
|
60
|
+
|
|
61
|
+
const requestedDoi = normalizeDoi(request.doi);
|
|
62
|
+
const directPreprint = requestedDoi.startsWith('10.1101/');
|
|
63
|
+
const serverResults = await Promise.allSettled(['biorxiv', 'medrxiv'].map(async (server) => {
|
|
64
|
+
if (directPreprint) {
|
|
65
|
+
const revisions = await this.getDetails(requestedDoi, server, options);
|
|
66
|
+
return revisions.length ? { server, revisions, crosswalk: null } : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const crosswalk = await this.getCrosswalk(requestedDoi, server, options);
|
|
70
|
+
const preprintDoi = normalizeDoi(crosswalk?.preprint_doi || crosswalk?.biorxiv_doi);
|
|
71
|
+
const publishedDoi = normalizeDoi(crosswalk?.published_doi);
|
|
72
|
+
if (!preprintDoi || publishedDoi !== requestedDoi) return null;
|
|
73
|
+
const revisions = await this.getDetails(preprintDoi, server, options);
|
|
74
|
+
return { server, revisions, crosswalk };
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
const candidates = [];
|
|
78
|
+
let metadata = {};
|
|
79
|
+
for (const result of serverResults) {
|
|
80
|
+
if (result.status !== 'fulfilled' || !result.value) continue;
|
|
81
|
+
const { server, revisions, crosswalk } = result.value;
|
|
82
|
+
const revision = latestRevision(revisions);
|
|
83
|
+
if (!revision) continue;
|
|
84
|
+
const preprintDoi = normalizeDoi(revision.doi || crosswalk?.preprint_doi);
|
|
85
|
+
const versionNumber = String(revision.version || '1');
|
|
86
|
+
const fallbackHtml = `${this.webBaseUrls[server]}/content/${preprintDoi}v${versionNumber}.full`;
|
|
87
|
+
const location = {
|
|
88
|
+
landing_page_url: `${this.webBaseUrls[server]}/content/${preprintDoi}v${versionNumber}`,
|
|
89
|
+
fulltext_url: revision.jatsxml || fallbackHtml,
|
|
90
|
+
pdf_url: `${this.webBaseUrls[server]}/content/${preprintDoi}v${versionNumber}.full.pdf`,
|
|
91
|
+
};
|
|
92
|
+
const title = normalizeUpstreamText(revision.title || crosswalk?.preprint_title);
|
|
93
|
+
const authors = authorsFromString(revision.authors || crosswalk?.preprint_authors);
|
|
94
|
+
const source = server === 'biorxiv' ? 'bioRxiv' : 'medRxiv';
|
|
95
|
+
const originalFormat = inferFormat(location);
|
|
96
|
+
metadata = {
|
|
97
|
+
title: title || request.title || '',
|
|
98
|
+
authors,
|
|
99
|
+
doi: crosswalk?.published_doi || (directPreprint ? preprintDoi : requestedDoi),
|
|
100
|
+
journal: crosswalk?.published_journal || '',
|
|
101
|
+
year: Number(String(crosswalk?.published_date || revision.date || '').slice(0, 4)) || undefined,
|
|
102
|
+
};
|
|
103
|
+
candidates.push({
|
|
104
|
+
identifier: preprintDoi,
|
|
105
|
+
title: title || request.title || null,
|
|
106
|
+
authors,
|
|
107
|
+
requested_doi: requestedDoi,
|
|
108
|
+
resolved_doi: preprintDoi,
|
|
109
|
+
pmid: null,
|
|
110
|
+
pmcid: null,
|
|
111
|
+
journal: crosswalk?.published_journal || null,
|
|
112
|
+
year: Number(String(revision.date || '').slice(0, 4)) || request.year || null,
|
|
113
|
+
source,
|
|
114
|
+
provider_name: source,
|
|
115
|
+
version: 'submittedVersion',
|
|
116
|
+
relationship: relationshipFor('submittedVersion', requestedDoi, preprintDoi),
|
|
117
|
+
license: revision.license && revision.license !== 'NA' ? revision.license : null,
|
|
118
|
+
source_location: location.landing_page_url,
|
|
119
|
+
location,
|
|
120
|
+
original_format: originalFormat,
|
|
121
|
+
normalized_format: 'markdown',
|
|
122
|
+
preprint_revision: versionNumber,
|
|
123
|
+
published_doi: normalizeDoi(crosswalk?.published_doi || revision.published) || null,
|
|
124
|
+
abstract: normalizeUpstreamText(revision.abstract || crosswalk?.preprint_abstract) || null,
|
|
125
|
+
retrieval: {
|
|
126
|
+
kind: originalFormat === 'jats_xml' ? 'jats' : 'html',
|
|
127
|
+
url: location.fulltext_url,
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return { metadata, candidates };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export { normalizeUpstreamText };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Adapted from CoChatAI/europepmc-mcp-server server.py (MIT); modified into a
|
|
2
|
+
// resolver adapter with version provenance. See ../../REUSE.md.
|
|
3
|
+
|
|
4
|
+
import { normalizeTitle } from '../identifiers.mjs';
|
|
5
|
+
import { inferFormat, normalizeVersion, relationshipFor } from '../model.mjs';
|
|
6
|
+
|
|
7
|
+
function escapeLucene(value) {
|
|
8
|
+
return String(value).replace(/([+\-&|!(){}[\]^"~*?:\\/])/g, '\\$1');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Europe PMC embeds inline tags such as <i> in otherwise plain metadata titles.
|
|
12
|
+
function plainText(value = '') {
|
|
13
|
+
return String(value).replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function authorsFromRecord(record) {
|
|
17
|
+
const authors = record.authorList?.author;
|
|
18
|
+
if (Array.isArray(authors)) {
|
|
19
|
+
return authors.map((author) => author.fullName || author.lastName).filter(Boolean);
|
|
20
|
+
}
|
|
21
|
+
return String(record.authorString || '').split(/\s*,\s*/).filter(Boolean);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function metadataFromRecord(record) {
|
|
25
|
+
return {
|
|
26
|
+
title: plainText(record.title),
|
|
27
|
+
authors: authorsFromRecord(record),
|
|
28
|
+
doi: record.doi || '',
|
|
29
|
+
pmid: record.pmid || (record.source === 'MED' ? record.id : ''),
|
|
30
|
+
pmcid: record.pmcid || '',
|
|
31
|
+
journal: record.journalInfo?.journal?.title || record.journalTitle || '',
|
|
32
|
+
year: Number(record.pubYear || record.journalInfo?.printPublicationDate?.slice(0, 4)) || undefined,
|
|
33
|
+
license: record.license || null,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function queryFor(request) {
|
|
38
|
+
if (request.doi) return `DOI:"${escapeLucene(request.doi)}"`;
|
|
39
|
+
if (request.pmcid) return `PMCID:${escapeLucene(request.pmcid)}`;
|
|
40
|
+
if (request.pmid) return `EXT_ID:${escapeLucene(request.pmid)} AND SRC:MED`;
|
|
41
|
+
const clauses = [`TITLE:"${escapeLucene(request.title)}"`];
|
|
42
|
+
if (request.authors[0]) clauses.push(`AUTH:"${escapeLucene(request.authors[0])}"`);
|
|
43
|
+
if (request.year) clauses.push(`PUB_YEAR:${request.year}`);
|
|
44
|
+
return clauses.join(' AND ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class EuropePmcSource {
|
|
48
|
+
constructor(http, options = {}) {
|
|
49
|
+
this.http = http;
|
|
50
|
+
this.baseUrl = (options.baseUrl
|
|
51
|
+
?? process.env.EUROPE_PMC_BASE_URL
|
|
52
|
+
?? 'https://www.ebi.ac.uk/europepmc/webservices/rest').replace(/\/$/, '');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async lookup(request, options = {}) {
|
|
56
|
+
const params = new URLSearchParams({
|
|
57
|
+
query: queryFor(request),
|
|
58
|
+
format: 'json',
|
|
59
|
+
resultType: 'core',
|
|
60
|
+
pageSize: '10',
|
|
61
|
+
});
|
|
62
|
+
let records = [];
|
|
63
|
+
let lookupError;
|
|
64
|
+
try {
|
|
65
|
+
const data = await this.http.json(`${this.baseUrl}/search?${params}`, options);
|
|
66
|
+
records = Array.isArray(data.resultList?.result) ? data.resultList.result : [];
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (!request.pmcid) throw error;
|
|
69
|
+
lookupError = error;
|
|
70
|
+
}
|
|
71
|
+
let record = records[0];
|
|
72
|
+
if (request.title && !request.doi && !request.pmid && !request.pmcid) {
|
|
73
|
+
const normalizedRequested = normalizeTitle(request.title);
|
|
74
|
+
record = records.find((candidate) => normalizeTitle(candidate.title) === normalizedRequested);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const metadata = record ? metadataFromRecord(record) : {};
|
|
78
|
+
const pmcid = metadata.pmcid || request.pmcid;
|
|
79
|
+
const candidates = [];
|
|
80
|
+
if (pmcid) {
|
|
81
|
+
const version = record
|
|
82
|
+
? normalizeVersion(
|
|
83
|
+
['authMan', 'epmcAuthMan', 'nihAuthMan'].some((field) => record[field] === 'Y')
|
|
84
|
+
? 'acceptedVersion'
|
|
85
|
+
: 'publishedVersion',
|
|
86
|
+
)
|
|
87
|
+
: 'unknown';
|
|
88
|
+
const location = {
|
|
89
|
+
landing_page_url: `https://europepmc.org/articles/${pmcid}`,
|
|
90
|
+
fulltext_url: `${this.baseUrl}/${pmcid}/fullTextXML`,
|
|
91
|
+
};
|
|
92
|
+
const resolvedDoi = metadata.doi || request.doi || '';
|
|
93
|
+
candidates.push({
|
|
94
|
+
identifier: pmcid,
|
|
95
|
+
title: metadata.title || request.title || null,
|
|
96
|
+
authors: metadata.authors || request.authors,
|
|
97
|
+
requested_doi: request.doi || metadata.doi || null,
|
|
98
|
+
resolved_doi: resolvedDoi || null,
|
|
99
|
+
pmid: metadata.pmid || request.pmid || null,
|
|
100
|
+
pmcid,
|
|
101
|
+
journal: metadata.journal || null,
|
|
102
|
+
year: metadata.year || request.year || null,
|
|
103
|
+
source: 'PMC',
|
|
104
|
+
provider_name: 'Europe PMC',
|
|
105
|
+
version,
|
|
106
|
+
relationship: relationshipFor(version, request.doi || metadata.doi, resolvedDoi),
|
|
107
|
+
license: metadata.license,
|
|
108
|
+
source_location: location.landing_page_url,
|
|
109
|
+
location,
|
|
110
|
+
original_format: inferFormat(location),
|
|
111
|
+
normalized_format: 'markdown',
|
|
112
|
+
retrieval: {
|
|
113
|
+
kind: 'jats',
|
|
114
|
+
url: location.fulltext_url,
|
|
115
|
+
// Some NIH author manuscripts have a PMC page but no Europe PMC JATS endpoint.
|
|
116
|
+
alternatives: [{
|
|
117
|
+
kind: 'html',
|
|
118
|
+
url: `https://pmc.ncbi.nlm.nih.gov/articles/${pmcid}/`,
|
|
119
|
+
}],
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return { metadata, candidates, sourceErrors: lookupError ? [lookupError] : [] };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async fetchJats(pmcid, options = {}) {
|
|
127
|
+
return this.http.text(`${this.baseUrl}/${encodeURIComponent(pmcid)}/fullTextXML`, {
|
|
128
|
+
...options,
|
|
129
|
+
accept: 'application/xml,text/xml',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|