|
| 1 | +/** |
| 2 | + * @file src/tools/lookup.ts |
| 3 | + * @description MCP tool for looking up topics in the catalog and searching APIs. |
| 4 | + */ |
| 5 | + |
| 6 | +import { z } from 'zod'; |
| 7 | +import { addTopicToCatalog, runLookupWorkflow } from '../workflows/lookup-workflow'; |
| 8 | +import { textContent } from './utils'; |
| 9 | + |
| 10 | +export const LookupInputSchema = z |
| 11 | + .object({ |
| 12 | + query: z.string(), |
| 13 | + searchApis: z.boolean().optional(), |
| 14 | + limit: z.number().min(1).max(20).optional(), |
| 15 | + action: z.enum(['search', 'add']).optional(), |
| 16 | + title: z.string().optional(), |
| 17 | + wikipediaSlug: z.string().optional(), |
| 18 | + grokipediaSlug: z.string().optional(), |
| 19 | + }) |
| 20 | + .refine( |
| 21 | + (value) => { |
| 22 | + if (value.action === 'add') { |
| 23 | + return Boolean(value.title && value.wikipediaSlug && value.grokipediaSlug); |
| 24 | + } |
| 25 | + return true; |
| 26 | + }, |
| 27 | + { |
| 28 | + message: 'When action is "add", title, wikipediaSlug, and grokipediaSlug are required.', |
| 29 | + path: ['action'], |
| 30 | + }, |
| 31 | + ); |
| 32 | + |
| 33 | +export const lookupTool = { |
| 34 | + title: 'Lookup topics', |
| 35 | + description: |
| 36 | + 'Searches for topics in the local catalog and optionally searches Grokipedia and Wikipedia APIs. Can also add topics to the catalog.', |
| 37 | + inputSchema: LookupInputSchema, |
| 38 | +}; |
| 39 | + |
| 40 | +const normalizeGrokSlug = (slug: string): string => { |
| 41 | + if (!slug.startsWith('page/')) { |
| 42 | + return `page/${slug}`; |
| 43 | + } |
| 44 | + return slug; |
| 45 | +}; |
| 46 | + |
| 47 | +export const lookupHandler = async (input: z.infer<typeof LookupInputSchema>) => { |
| 48 | + const { query, searchApis = false, limit = 5, action = 'search' } = input; |
| 49 | + |
| 50 | + if (action === 'add') { |
| 51 | + if (!input.title || !input.wikipediaSlug || !input.grokipediaSlug) { |
| 52 | + throw new Error( |
| 53 | + 'Missing required fields for adding topic: title, wikipediaSlug, grokipediaSlug', |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const grokSlug = normalizeGrokSlug(input.grokipediaSlug); |
| 58 | + const newTopic = addTopicToCatalog({ |
| 59 | + title: input.title, |
| 60 | + wikipediaSlug: input.wikipediaSlug, |
| 61 | + grokipediaSlug: grokSlug, |
| 62 | + }); |
| 63 | + |
| 64 | + return { |
| 65 | + content: textContent( |
| 66 | + `[lookup] Added topic "${newTopic.title}" to catalog (ID: ${newTopic.id})`, |
| 67 | + ), |
| 68 | + structuredContent: { |
| 69 | + action: 'add', |
| 70 | + topic: newTopic, |
| 71 | + }, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + // Search action |
| 76 | + const result = await runLookupWorkflow({ |
| 77 | + query, |
| 78 | + searchApis: searchApis ?? false, |
| 79 | + limit, |
| 80 | + }); |
| 81 | + |
| 82 | + if (result.found && result.topic) { |
| 83 | + return { |
| 84 | + content: textContent( |
| 85 | + `[lookup] Found topic "${result.topic.title}" in local catalog (ID: ${result.topic.id})`, |
| 86 | + ), |
| 87 | + structuredContent: { |
| 88 | + action: 'search', |
| 89 | + found: true, |
| 90 | + topic: result.topic, |
| 91 | + searchResults: null, |
| 92 | + }, |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + if (result.searchResults) { |
| 97 | + const { grokipedia, wikipedia } = result.searchResults; |
| 98 | + const hasResults = grokipedia.length > 0 || wikipedia.length > 0; |
| 99 | + |
| 100 | + return { |
| 101 | + content: textContent( |
| 102 | + hasResults |
| 103 | + ? `[lookup] Found ${grokipedia.length} Grokipedia and ${wikipedia.length} Wikipedia results for "${query}"` |
| 104 | + : `[lookup] No results found for "${query}"`, |
| 105 | + ), |
| 106 | + structuredContent: { |
| 107 | + action: 'search', |
| 108 | + found: false, |
| 109 | + topic: null, |
| 110 | + searchResults: { |
| 111 | + grokipedia, |
| 112 | + wikipedia, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + content: textContent(`[lookup] Topic "${query}" not found in local catalog`), |
| 120 | + structuredContent: { |
| 121 | + action: 'search', |
| 122 | + found: false, |
| 123 | + topic: null, |
| 124 | + searchResults: null, |
| 125 | + }, |
| 126 | + }; |
| 127 | +}; |
0 commit comments