UNPKG

@iflow-mcp/ansvar-systems-german-law-mcp

Version:

German legal research MCP server with statutes, case law, and preparatory works from gesetze-im-internet.de

85 lines (84 loc) • 2.71 kB
export function searchDocumentsInMemory(documents, request) { const limit = clampLimit(request.limit ?? 20); const query = request.query.trim().toLowerCase(); if (query.length === 0) { return { documents: [], total: 0 }; } const matches = documents .filter((doc) => { const titleMatch = doc.title.toLowerCase().includes(query); const citationMatch = doc.citation?.toLowerCase().includes(query) ?? false; const snippetMatch = doc.textSnippet?.toLowerCase().includes(query) ?? false; const idMatch = doc.id.toLowerCase().includes(query); return titleMatch || citationMatch || snippetMatch || idMatch; }) .slice(0, limit); return { documents: matches, total: matches.length, }; } export function getDocumentFromMemory(documents, id) { return documents.find((doc) => doc.id === id) ?? null; } export function createRegexCitationHandlers(options) { const parseCitation = async (citation) => { const match = citation.trim().match(options.pattern); if (!match?.groups) { return null; } const groups = removeUndefinedValues(match.groups); if (Object.keys(groups).length === 0) { return null; } return { original: citation, normalized: options.normalize(groups), parsed: groups, }; }; const validateCitation = async (citation) => { const parsed = await parseCitation(citation); if (!parsed) { return { valid: false, reason: options.reason, }; } return { valid: true, normalized: parsed.normalized, }; }; return { parseCitation, validateCitation }; } export function createStaticIngestionRunner(options) { return async (request) => { const startedAt = new Date().toISOString(); const sourceId = request.sourceId ?? options.defaultSource; const dryRun = request.dryRun ?? false; const ingestedCount = dryRun ? 0 : options.count; const skippedCount = dryRun ? options.count : 0; const finishedAt = new Date().toISOString(); return { startedAt, finishedAt, sourceId, dryRun, ingestedCount, skippedCount, }; }; } function removeUndefinedValues(values) { return Object.fromEntries(Object.entries(values).filter((entry) => typeof entry[1] === "string")); } function clampLimit(limit) { if (limit < 1) { return 1; } if (limit > 100) { return 100; } return limit; }