search_pages
Search authenticated internal web pages by query. Specify optional limit and section to retrieve relevant content from the secure intranet.
Instructions
Busca contenido dentro de la web interna autenticada.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| limit | No | ||
| section | No |
Implementation Reference
- src/index.js:222-261 (handler)The handler function for the 'search_pages' tool. It fetches a search page, parses HTML with cheerio, filters 'a[href]' elements that match the query and belong to the same base URL, and returns results with title, url, path, snippet, and score.
async ({ query, limit, section }) => { try { const searchUrl = SEARCH_PATH ? `${SEARCH_PATH}${SEARCH_PATH.includes('?') ? '&' : '?'}q=${encodeURIComponent(query)}` : section || '/'; const { html, finalUrl } = await fetchPage(searchUrl); const $ = cheerio.load(html); const results = []; $('a[href]').each((_, el) => { if (results.length >= limit) return false; const title = $(el).text().trim(); const href = $(el).attr('href'); if (!title || !href) return; const snippet = $(el).parent().text().trim().slice(0, 240); const url = normalizeUrl(href); if (!url.startsWith(BASE_URL)) return; if (!`${title} ${snippet}`.toLowerCase().includes(query.toLowerCase())) return; results.push({ title, url, path: new URL(url).pathname, snippet, score: 0.5 }); }); return { content: [ { type: 'text', text: JSON.stringify({ results, total: results.length, searched_from: finalUrl }, null, 2) } ] }; } catch (error) { return mcpError(error); } } - src/index.js:217-221 (schema)Input schema for the 'search_pages' tool: 'query' (string, required), 'limit' (number 1-20, default 10), 'section' (optional string).
{ query: z.string(), limit: z.number().int().min(1).max(20).default(10), section: z.string().optional() }, - src/index.js:214-262 (registration)Registration of the 'search_pages' tool on the MCP server using server.tool(), with the description 'Busca contenido dentro de la web interna autenticada.'
server.tool( 'search_pages', 'Busca contenido dentro de la web interna autenticada.', { query: z.string(), limit: z.number().int().min(1).max(20).default(10), section: z.string().optional() }, async ({ query, limit, section }) => { try { const searchUrl = SEARCH_PATH ? `${SEARCH_PATH}${SEARCH_PATH.includes('?') ? '&' : '?'}q=${encodeURIComponent(query)}` : section || '/'; const { html, finalUrl } = await fetchPage(searchUrl); const $ = cheerio.load(html); const results = []; $('a[href]').each((_, el) => { if (results.length >= limit) return false; const title = $(el).text().trim(); const href = $(el).attr('href'); if (!title || !href) return; const snippet = $(el).parent().text().trim().slice(0, 240); const url = normalizeUrl(href); if (!url.startsWith(BASE_URL)) return; if (!`${title} ${snippet}`.toLowerCase().includes(query.toLowerCase())) return; results.push({ title, url, path: new URL(url).pathname, snippet, score: 0.5 }); }); return { content: [ { type: 'text', text: JSON.stringify({ results, total: results.length, searched_from: finalUrl }, null, 2) } ] }; } catch (error) { return mcpError(error); } } ); - src/index.js:198-207 (helper)Helper function 'fetchPage' used by the handler to fetch authenticated page content.
async function fetchPage(urlOrPath) { await ensureAuthenticated(); const response = await request(urlOrPath, { method: 'GET' }); if (!response.ok) { throw new Error(response.status === 404 ? 'PAGE_NOT_FOUND' : 'INTERNAL_ERROR'); } const finalUrl = response.url || normalizeUrl(urlOrPath); const html = await response.text(); return { html, finalUrl }; } - src/index.js:371-387 (helper)Helper function 'mcpError' used by the handler to return structured error responses.
function mcpError(error) { const message = error instanceof Error ? error.message : 'INTERNAL_ERROR'; return { content: [ { type: 'text', text: JSON.stringify({ error: { code: message.split(':')[0], message } }, null, 2) } ], isError: true }; }