-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsearch.ts
More file actions
337 lines (288 loc) · 10.4 KB
/
search.ts
File metadata and controls
337 lines (288 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { contentClient } from "../clients/content.js";
import type { SearchResult, SearchEntry, FacetEntry } from "../types/index.js";
interface SpellcheckResult {
suggestions: string[];
}
interface SuggestResult {
suggestions: string[];
}
interface PathwaySearchResult {
dbId: number;
stId: string;
name: string;
species: string;
}
const searchQuerySchema = z.string().trim().min(1);
function stripHtml(text: string): string {
return text.replace(/<[^>]*>/g, '');
}
function formatSearchEntry(entry: SearchEntry): string {
const name = stripHtml(entry.name);
const lines = [
`- **${name}** (${entry.stId})`,
` - Type: ${entry.exactType}`,
];
if (entry.species && entry.species.length > 0) {
lines.push(` - Species: ${entry.species.join(", ")}`);
}
if (entry.referenceIdentifier) {
lines.push(` - Reference: ${entry.referenceIdentifier}${entry.referenceName ? ` (${entry.referenceName})` : ""}`);
}
if (entry.summation) {
const summaryText = stripHtml(entry.summation);
const summary = summaryText.length > 200 ? summaryText.substring(0, 200) + "..." : summaryText;
lines.push(` - ${summary}`);
}
return lines.join("\n");
}
function flattenSearchResults(result: SearchResult): { entries: SearchEntry[]; totalCount: number } {
const entries: SearchEntry[] = [];
let totalCount = 0;
for (const group of result.results) {
totalCount += group.entriesCount;
entries.push(...group.entries);
}
return { entries, totalCount };
}
export function registerSearchTools(server: McpServer) {
// Main search
server.tool(
"reactome_search",
"Search the Reactome knowledgebase for pathways, reactions, proteins, genes, compounds, and other entities.",
{
query: searchQuerySchema.describe("Search term (gene name, protein, pathway name, disease, etc.)"),
species: z.string().optional().describe("Filter by species (e.g., 'Homo sapiens', 'Mus musculus')"),
types: z.array(z.string()).optional().describe("Filter by type (Pathway, Reaction, Protein, Gene, Complex, etc.)"),
compartments: z.array(z.string()).optional().describe("Filter by cellular compartment"),
keywords: z.array(z.string()).optional().describe("Filter by keywords"),
rows: z.number().optional().default(25).describe("Number of results to return"),
cluster: z.boolean().optional().default(true).describe("Cluster related results"),
},
async ({ query, species, types, compartments, keywords, rows, cluster }) => {
const params: Record<string, string | number | boolean | undefined> = {
query,
species,
rows,
cluster,
};
if (types && types.length > 0) {
params.types = types.join(",");
}
if (compartments && compartments.length > 0) {
params.compartments = compartments.join(",");
}
if (keywords && keywords.length > 0) {
params.keywords = keywords.join(",");
}
const result = await contentClient.get<SearchResult>("/search/query", params);
const { entries, totalCount } = flattenSearchResults(result);
const lines = [
`## Search Results for "${query}"`,
`**Found:** ${totalCount} results`,
"",
...entries.slice(0, rows).map(formatSearchEntry),
];
if (totalCount > rows) {
lines.push("", `*Showing ${Math.min(rows, entries.length)} of ${totalCount} results*`);
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Search with pagination
server.tool(
"reactome_search_paginated",
"Search Reactome with pagination support for browsing through large result sets.",
{
query: searchQuerySchema.describe("Search term"),
page: z.number().optional().default(1).describe("Page number (1-based)"),
rows_per_page: z.number().optional().default(20).describe("Results per page"),
species: z.string().optional().describe("Filter by species"),
types: z.array(z.string()).optional().describe("Filter by type"),
},
async ({ query, page, rows_per_page, species, types }) => {
const params: Record<string, string | number | boolean | undefined> = {
query,
page,
rowCount: rows_per_page,
species,
};
if (types && types.length > 0) {
params.types = types.join(",");
}
const result = await contentClient.get<SearchResult>("/search/query/paginated", params);
const { entries, totalCount } = flattenSearchResults(result);
const totalPages = Math.ceil(totalCount / rows_per_page);
const lines = [
`## Search Results for "${query}" (Page ${page}/${totalPages})`,
`**Total:** ${totalCount} results`,
"",
...entries.map(formatSearchEntry),
"",
`*Page ${page} of ${totalPages}*`,
];
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Get search suggestions
server.tool(
"reactome_search_suggest",
"Get auto-complete suggestions for a search query.",
{
query: searchQuerySchema.describe("Partial search term"),
},
async ({ query }) => {
const result = await contentClient.get<SuggestResult>("/search/suggest", { query });
const lines = [
`## Suggestions for "${query}"`,
"",
...result.suggestions.map(s => `- ${s}`),
];
if (result.suggestions.length === 0) {
lines.push("*No suggestions found*");
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Spellcheck
server.tool(
"reactome_search_spellcheck",
"Get spell-check suggestions for a search query.",
{
query: searchQuerySchema.describe("Search term to check"),
},
async ({ query }) => {
const result = await contentClient.get<SpellcheckResult>("/search/spellcheck", { query });
const lines = [
`## Spellcheck for "${query}"`,
"",
];
if (result.suggestions && result.suggestions.length > 0) {
lines.push("**Did you mean:**");
lines.push(...result.suggestions.map(s => `- ${s}`));
} else {
lines.push("*No spelling suggestions*");
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Get search facets
server.tool(
"reactome_search_facets",
"Get available facets (filters) for search results, either globally or for a specific query.",
{
query: searchQuerySchema.optional().describe("Search term (optional, returns global facets if omitted)"),
},
async ({ query }) => {
interface FacetResult {
typeFacet?: FacetEntry[];
speciesFacet?: FacetEntry[];
compartmentFacet?: FacetEntry[];
keywordFacet?: FacetEntry[];
}
const endpoint = query ? "/search/facet_query" : "/search/facet";
const params = query ? { query } : undefined;
const result = await contentClient.get<FacetResult>(endpoint, params);
const lines = [
query ? `## Facets for "${query}"` : "## Available Search Facets",
"",
];
if (result.typeFacet && result.typeFacet.length > 0) {
lines.push("### Types:");
result.typeFacet.slice(0, 15).forEach(f => {
lines.push(`- ${f.name}: ${f.count}`);
});
lines.push("");
}
if (result.speciesFacet && result.speciesFacet.length > 0) {
lines.push("### Species:");
result.speciesFacet.slice(0, 10).forEach(f => {
lines.push(`- ${f.name}: ${f.count}`);
});
lines.push("");
}
if (result.compartmentFacet && result.compartmentFacet.length > 0) {
lines.push("### Compartments:");
result.compartmentFacet.slice(0, 10).forEach(f => {
lines.push(`- ${f.name}: ${f.count}`);
});
lines.push("");
}
if (result.keywordFacet && result.keywordFacet.length > 0) {
lines.push("### Keywords:");
result.keywordFacet.slice(0, 10).forEach(f => {
lines.push(`- ${f.name}: ${f.count}`);
});
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Find pathways containing an entity
server.tool(
"reactome_search_pathways_of",
"Find all pathways that contain a specific entity by its database ID.",
{
db_id: z.number().describe("Reactome database ID of the entity"),
species: z.string().optional().describe("Filter by species"),
include_interactors: z.boolean().optional().default(false).describe("Include interactor pathways"),
direct_only: z.boolean().optional().default(false).describe("Only pathways where entity appears directly in diagram"),
},
async ({ db_id, species, include_interactors, direct_only }) => {
const result = await contentClient.get<PathwaySearchResult[]>(`/search/pathways/of/${db_id}`, {
species,
includeInteractors: include_interactors,
directlyInDiagram: direct_only,
});
const lines = [
`## Pathways Containing Entity ${db_id}`,
`**Found:** ${result.length} pathways`,
"",
...result.slice(0, 50).map(p => `- **${p.name}** (${p.stId}) - ${p.species}`),
];
if (result.length > 50) {
lines.push(`... and ${result.length - 50} more pathways`);
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
// Search within a diagram
server.tool(
"reactome_search_diagram",
"Search for entities within a specific pathway diagram.",
{
diagram: z.string().describe("Pathway stable ID for the diagram"),
query: searchQuerySchema.describe("Search term"),
include_interactors: z.boolean().optional().default(false).describe("Include interactors"),
},
async ({ diagram, query, include_interactors }) => {
const result = await contentClient.get<SearchResult>(`/search/diagram/${encodeURIComponent(diagram)}`, {
query,
includeInteractors: include_interactors,
rows: 50,
});
const { entries, totalCount } = flattenSearchResults(result);
const lines = [
`## Search in Diagram ${diagram} for "${query}"`,
`**Found:** ${totalCount} results`,
"",
...entries.map(formatSearchEntry),
];
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
);
}