This repository was archived by the owner on Jun 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathindex.ts
More file actions
370 lines (315 loc) · 10.7 KB
/
Copy pathindex.ts
File metadata and controls
370 lines (315 loc) · 10.7 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
type CallToolRequest} from "@modelcontextprotocol/sdk/types.js";
import fetch from "node-fetch";
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import {
GetLLMTxtOptionsSchema,
ListLLMTxtOptionsSchema,
type LLMTxt,
type LLMTxtList,
type LLMTxtListItem
} from './schemas.js';
import { JSDOM } from 'jsdom';
import fs from 'fs';
import path from 'path';
import os from 'os';
// Define schema for search tool
const SearchLLMTxtOptionsSchema = z.object({
id: z.number().describe(
"The ID of the LLM.txt file to search in. Must be obtained first using the list_llm_txt command."
),
queries: z.array(z.string()).min(1).describe(
"Array of substrings to search for. Each query is searched case-insensitively. At least one query is required."
),
context_lines: z.number().optional().default(2).describe(
"Number of lines to show before and after each match for context. Defaults to 2 lines."
),
});
const tools = {
get_llm_txt: {
description: "Fetch an LLM.txt file from a given URL. Format your response in beautiful markdown.",
inputSchema: zodToJsonSchema(GetLLMTxtOptionsSchema)
},
list_llm_txt: {
description: "List available LLM.txt files from the directory. Use this first before fetching a specific LLM.txt file. Format your response in beautiful markdown.",
inputSchema: zodToJsonSchema(ListLLMTxtOptionsSchema)
},
search_llm_txt: {
description: "Search for multiple substrings in an LLM.txt file. Requires a valid ID obtained from list_llm_txt command. Returns snippets with page numbers for each match. Format your response in beautiful markdown, using code blocks for snippets.",
inputSchema: zodToJsonSchema(SearchLLMTxtOptionsSchema)
}
};
const server = new Server({
name: "server-llm-txt",
version: "0.1.0",
author: "Michael Latman (https://michaellatman.com)"
}, {
capabilities: {
tools
}
});
const DIRECTORY_URL = "https://directory.llmstxt.cloud/";
const MAX_RESPONSE_LENGTH = 100000;
const CACHE_DURATION_MS = 24 * 60 * 60 * 1000; // 24 hours
// Get cache directory based on OS
function getCacheDir(): string {
const platform = os.platform();
let cacheDir: string;
switch (platform) {
case 'win32':
cacheDir = path.join(os.homedir(), 'AppData', 'Local', 'llm-txt-mcp');
break;
case 'darwin':
cacheDir = path.join(os.homedir(), 'Library', 'Caches', 'llm-txt-mcp');
break;
default: // linux and others
cacheDir = path.join(os.homedir(), '.cache', 'llm-txt-mcp');
}
// Ensure cache directory exists
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
return cacheDir;
}
// Cache management functions
function getCachedList(): LLMTxtList | null {
const cacheFile = path.join(getCacheDir(), 'llm-list-cache.json');
try {
if (!fs.existsSync(cacheFile)) {
return null;
}
const cacheData = JSON.parse(fs.readFileSync(cacheFile, 'utf-8'));
const now = Date.now();
if (now - cacheData.timestamp > CACHE_DURATION_MS) {
return null; // Cache expired
}
return cacheData.data;
} catch (error) {
console.error('Error reading cache:', error);
return null;
}
}
function saveToCache(data: LLMTxtList): void {
const cacheFile = path.join(getCacheDir(), 'llm-list-cache.json');
const cacheData = {
timestamp: Date.now(),
data
};
try {
fs.writeFileSync(cacheFile, JSON.stringify(cacheData), 'utf-8');
} catch (error) {
console.error('Error writing cache:', error);
}
}
// Simple hash function to convert string to number
function hashUrl(url: string): number {
let hash = 0;
for (let i = 0; i < url.length; i++) {
const char = url.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash); // Ensure positive number
}
async function getLLMTxt(id: number, page: number = 1): Promise<LLMTxt> {
// First get the list to find the URL for this ID
const allItems = await listLLMTxt(); // Get all items
const item = allItems.find(item => item.id === id);
if (!item) {
throw new Error(`No LLM.txt found with ID: ${id}`);
}
const response = await fetch(item.url, {
headers: {
"Accept": "text/plain",
"User-Agent": "llm-txt-mcp-server"
}
});
if (!response.ok) {
throw new Error(`Failed to fetch LLM.txt: ${response.statusText}`);
}
const fullContent = await response.text();
const start = (page - 1) * MAX_RESPONSE_LENGTH;
let content = fullContent.slice(start, start + MAX_RESPONSE_LENGTH);
const hasNextPage = start + content.length < fullContent.length;
return {
id: item.id,
url: item.url,
name: item.name,
description: item.description,
hasNextPage,
currentPage: page,
content,
};
}
// Cache the promise of fetching the list to prevent multiple concurrent fetches
let listFetchPromise: Promise<LLMTxtList> | null = null;
async function listLLMTxt(): Promise<LLMTxtList> {
// If we're already fetching, return that promise
if (listFetchPromise) {
return listFetchPromise;
}
// Check cache first
const cachedList = getCachedList();
if (cachedList) {
return cachedList;
}
// Create a new fetch promise
listFetchPromise = (async () => {
try {
const response = await fetch(DIRECTORY_URL, {
headers: {
"Accept": "text/html",
"User-Agent": "llm-txt-mcp-server"
}
});
if (!response.ok) {
throw new Error(`Failed to fetch LLM.txt list: ${response.statusText}`);
}
const html = await response.text();
const dom = new JSDOM(html);
const document = dom.window.document;
// Find all llm.txt links
const links: LLMTxtListItem[] = Array.from(document.querySelectorAll('li'))
.map(li => {
// Try to find llms-full.txt link first, fall back to llms.txt
const fullLink = li.querySelector('a[href*="llms-full.txt"]');
const link = fullLink || li.querySelector('a[href*="llms.txt"]');
if (!link) return null;
const url = link.getAttribute('href') || '';
return {
id: hashUrl(url),
url,
name: li.querySelector('h3')?.textContent?.trim() || 'Unknown',
description: li.querySelector('p')?.textContent?.trim() || ''
};
})
.filter((item): item is LLMTxtListItem => item !== null && item.url.endsWith('.txt'));
// Save to cache
saveToCache(links);
return links;
} finally {
// Clear the promise after it's done (success or failure)
listFetchPromise = null;
}
})();
return listFetchPromise;
}
async function searchLLMTxt(id: number, queries: string[], contextLines: number = 2): Promise<any> {
// First get the list to find the URL for this ID
const allItems = await listLLMTxt();
const item = allItems.find(item => item.id === id);
if (!item) {
throw new Error(`No LLM.txt found with ID: ${id}`);
}
const response = await fetch(item.url, {
headers: {
"Accept": "text/plain",
"User-Agent": "llm-txt-mcp-server"
}
});
if (!response.ok) {
throw new Error(`Failed to fetch LLM.txt: ${response.statusText}`);
}
const content = await response.text();
const lines = content.split('\n');
const results = [];
const searchRegexes = queries.map(query => new RegExp(query, 'gi'));
let charCount = 0;
for (let i = 0; i < lines.length; i++) {
const matches = searchRegexes.some(regex => regex.test(lines[i]));
if (matches) {
const startLine = Math.max(0, i - contextLines);
const endLine = Math.min(lines.length - 1, i + contextLines);
const snippet = lines.slice(startLine, endLine + 1).join('\n');
const page = Math.floor(charCount / MAX_RESPONSE_LENGTH) + 1;
// Find which queries matched this line
const matchedQueries = queries.filter(query =>
new RegExp(query, 'gi').test(lines[i])
);
results.push({
page,
lineNumber: i + 1,
snippet,
matchedLine: lines[i],
matchedQueries
});
}
charCount += lines[i].length + 1; // +1 for the newline character
}
return {
id: item.id,
url: item.url,
name: item.name,
matches: results
};
}
server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest) => {
try {
if (!request.params.arguments) {
throw new Error("Arguments are required");
}
switch (request.params.name) {
case "get_llm_txt": {
const args = GetLLMTxtOptionsSchema.parse(request.params.arguments);
const result = await getLLMTxt(args.id, args.page);
// Always truncate to MAX_RESPONSE_LENGTH and indicate if there's more
const content = result.content.slice(0, MAX_RESPONSE_LENGTH);
const hasMoreContent = result.content.length > MAX_RESPONSE_LENGTH || result.hasNextPage;
const response = {
currentPage: result.currentPage,
...(hasMoreContent && {
hasNextPage: true
}),
content
};
return {
content: [{
type: "text",
text: JSON.stringify(response, null, 2)
}]
};
}
case "list_llm_txt": {
const result = await listLLMTxt();
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
case "search_llm_txt": {
const args = SearchLLMTxtOptionsSchema.parse(request.params.arguments);
const result = await searchLLMTxt(args.id, args.queries, args.context_lines);
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
} catch (error: unknown) {
if (error instanceof z.ZodError) {
const issues = error.issues.map((issue: z.ZodIssue) => `${issue.path.join('.')}: ${issue.message}`).join(', ');
throw new Error(`Invalid arguments: ${issues}`);
}
throw error;
}
});
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: Object.entries(tools).map(([name, tool]) => ({
name,
description: tool.description,
inputSchema: tool.inputSchema
}))
};
});
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("LLM.txt MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});