search_data_file
Locate specific entries in OSRS game data files by searching within a specified file and query term, with pagination support for efficient results navigation.
Instructions
Search any file in the data directory for matching entries.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | The filename to search in the data directory (e.g., 'varptypes.txt') | |
| page | No | Page number for pagination | |
| pageSize | No | Number of results per page | |
| query | Yes | The term to search for in the file |
Input Schema (JSON Schema)
{
"additionalProperties": false,
"properties": {
"filename": {
"description": "The filename to search in the data directory (e.g., 'varptypes.txt')",
"type": "string"
},
"page": {
"default": 1,
"description": "Page number for pagination",
"minimum": 1,
"type": "integer"
},
"pageSize": {
"default": 10,
"description": "Number of results per page",
"maximum": 100,
"minimum": 1,
"type": "integer"
},
"query": {
"description": "The term to search for in the file",
"type": "string"
}
},
"required": [
"filename",
"query"
],
"type": "object"
}
Implementation Reference
- index.ts:456-472 (handler)Main handler for the 'search_data_file' tool. Validates input arguments, performs security checks on the filename to prevent path traversal, checks if the file exists, constructs the file path, and invokes the searchFile helper to perform the actual search, then returns the paginated results.case "search_data_file": const genericSearchArgs = getSchemaForTool(name).parse(args) as { filename: string; query: string; page?: number; pageSize?: number }; const { filename: genericFilename, query: searchQuery, page: genericFilePage = 1, pageSize: genericFilePageSize = 10 } = genericSearchArgs; // Security check to prevent directory traversal if (genericFilename.includes('..') || genericFilename.includes('/') || genericFilename.includes('\\')) { throw new Error('Invalid filename'); } if (!fileExists(genericFilename)) { return responseToString({ error: `${genericFilename} not found in data directory` }); } const genericFilePath = path.join(getDataDir(), genericFilename); const genericFileResults = await searchFile(genericFilePath, searchQuery, genericFilePage, genericFilePageSize); return responseToString(genericFileResults);
- index.ts:58-63 (schema)Zod schema defining the input parameters for the 'search_data_file' tool: filename (required), query (required), page (optional, default 1), pageSize (optional, default 10).const GenericFileSearchSchema = z.object({ filename: z.string().describe("The filename to search in the data directory (e.g., 'varptypes.txt')"), query: z.string().describe("The term to search for in the file"), page: z.number().int().min(1).optional().default(1).describe("Page number for pagination"), pageSize: z.number().int().min(1).max(100).optional().default(10).describe("Number of results per page") });
- index.ts:366-368 (registration)Tool registration entry in the getToolDefinitions() function, providing the tool name and description for discovery via ListTools.name: "search_data_file", description: "Search any file in the data directory for matching entries.", },
- index.ts:102-168 (helper)Core helper function implementing the file search logic: reads file line-by-line using readline, performs case-insensitive search (converting spaces to underscores in query), collects matching lines with line numbers, applies pagination, formats results as ID-value pairs when possible, and returns structured results with pagination metadata.async function searchFile(filePath: string, searchTerm: string, page: number = 1, pageSize: number = 10): Promise<any> { //replace spaces with underscores searchTerm = searchTerm.replace(" ", "_"); return new Promise((resolve, reject) => { if (!fs.existsSync(filePath)) { reject(new Error(`File not found: ${filePath}`)); return; } const results: {line: string, lineNumber: number}[] = []; const fileStream = fs.createReadStream(filePath); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); let lineNumber = 0; rl.on('line', (line) => { lineNumber++; if (line.toLowerCase().includes(searchTerm.toLowerCase())) { results.push({ line, lineNumber }); } }); rl.on('close', () => { const totalResults = results.length; const totalPages = Math.ceil(totalResults / pageSize); const startIndex = (page - 1) * pageSize; const endIndex = startIndex + pageSize; const paginatedResults = results.slice(startIndex, endIndex); // Process the results to extract key-value pairs if possible const formattedResults = paginatedResults.map(result => { // Try to format as key-value pair (common for ID data files) const parts = result.line.split(/\s+/); if (parts.length >= 2) { const id = parts[0]; const value = parts.slice(1).join(' '); return { ...result, id, value, formatted: `${id}\t${value}` }; } return result; }); resolve({ results: formattedResults, pagination: { page, pageSize, totalResults, totalPages, hasNextPage: page < totalPages, hasPreviousPage: page > 1 } }); }); rl.on('error', (err) => { reject(err); }); }); }