Skip to main content
Glama

kagi_enrichment_enhance

Enhance content with specialized knowledge from web and news indexes to discover non-mainstream information and supplementary insights.

Instructions

Provides supplementary content from specialized indexes (Teclis for web, TinyGem for news). Ideal for discovering non-mainstream results and enriching content with specialized knowledge.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYesContent

Implementation Reference

  • The `enhance_content` method implements the core tool logic: validates API key, fetches data from Kagi's web (`/enrich/web`) and news (`/enrich/news`) endpoints in parallel, filters snippets for relevance to software/AI/development, cleans HTML entities, combines into enhanced content, and returns structured EnhancementResult with sources.
    async enhance_content(content: string): Promise<EnhancementResult> {
    	const api_key = validate_api_key(
    		config.enhancement.kagi_enrichment.api_key,
    		this.name,
    	);
    
    	const enrich_request = async () => {
    		try {
    			// Try both web and news endpoints
    			const [webData, newsData] = await Promise.all([
    				http_json<EnrichmentResponse & { message?: string }>(
    					this.name,
    					`https://kagi.com/api/v0/enrich/web?${new URLSearchParams(
    						{
    							q: sanitize_query(
    								'artificial intelligence software development',
    							),
    							limit: '5',
    						},
    					)}`,
    					{
    						method: 'GET',
    						headers: {
    							Authorization: `Bot ${api_key}`,
    							Accept: 'application/json',
    						},
    						signal: AbortSignal.timeout(
    							config.enhancement.kagi_enrichment.timeout,
    						),
    					},
    				),
    				http_json<EnrichmentResponse & { message?: string }>(
    					this.name,
    					`https://kagi.com/api/v0/enrich/news?${new URLSearchParams(
    						{
    							q: sanitize_query(
    								'artificial intelligence code generation testing',
    							),
    							limit: '5',
    						},
    					)}`,
    					{
    						method: 'GET',
    						headers: {
    							Authorization: `Bot ${api_key}`,
    							Accept: 'application/json',
    						},
    						signal: AbortSignal.timeout(
    							config.enhancement.kagi_enrichment.timeout,
    						),
    					},
    				),
    			]);
    
    			if (!webData?.data || !newsData?.data) {
    				throw new ProviderError(
    					ErrorType.API_ERROR,
    					'Unexpected response: missing data from enrichment endpoints',
    					this.name,
    				);
    			}
    
    			// Combine and filter results
    			const allData = [...webData.data, ...newsData.data].filter(
    				(result) =>
    					// Filter for results about software/development/AI
    					result.snippet?.toLowerCase().includes('software') ||
    					result.snippet?.toLowerCase().includes('develop') ||
    					result.snippet?.toLowerCase().includes('programming') ||
    					result.snippet?.toLowerCase().includes('code') ||
    					result.snippet
    						?.toLowerCase()
    						.includes('artificial intelligence') ||
    					result.snippet?.toLowerCase().includes('ai'),
    			);
    
    			// Clean and combine snippets
    			const enhanced_content = allData
    				.map((result) => result.snippet)
    				.filter(Boolean)
    				.map((snippet) =>
    					// Fix HTML entities
    					snippet
    						.replace(/'/g, "'")
    						.replace(/"/g, '"')
    						.replace(/&/g, '&')
    						.replace(/</g, '<')
    						.replace(/>/g, '>'),
    				)
    				.join('\n\n');
    
    			return {
    				original_content: content,
    				enhanced_content,
    				enhancements: [
    					{
    						type: 'content_enrichment',
    						description:
    							'Added supplementary information from Teclis (web) and TinyGem (news) specialized indexes',
    					},
    				],
    				sources: allData.map((result) => ({
    					title: result.title,
    					url: result.url,
    				})),
    				source_provider: this.name,
    			};
    		} catch (error) {
    			handle_provider_error(error, this.name, 'enrich content');
    		}
    	};
    
    	return retry_with_backoff(enrich_request);
    }
  • Conditional registration of the `KagiEnrichmentProvider` instance using `register_enhancement_provider` if the Kagi enrichment API key is valid. This populates the registry used for dynamic tool registration.
    if (
    	is_api_key_valid(
    		config.enhancement.kagi_enrichment.api_key,
    		'kagi_enrichment',
    	)
    ) {
    	register_enhancement_provider(new KagiEnrichmentProvider());
    }
  • Dynamic registration loop for all enhancement providers: creates MCP tool named `${provider.name}_enhance` (e.g., 'kagi_enrichment_enhance'), defines schema, and provides thin handler wrapper that calls `provider.enhance_content(content)`.
    this.enhancement_providers.forEach((provider) => {
    	server.tool(
    		{
    			name: `${provider.name}_enhance`,
    			description: provider.description,
    			schema: v.object({
    				content: v.pipe(v.string(), v.description('Content')),
    			}),
    		},
    		async ({ content }) => {
    			try {
    				const result = await provider.enhance_content(content);
    				const safe_result = handle_large_result(
    					result,
    					provider.name,
    				);
    				return {
    					content: [
    						{
    							type: 'text' as const,
    							text: JSON.stringify(safe_result, null, 2),
    						},
    					],
    				};
    			} catch (error) {
    				const error_response = create_error_response(
    					error as Error,
    				);
    				return {
    					content: [
    						{
    							type: 'text' as const,
    							text: error_response.error,
    						},
    					],
    					isError: true,
    				};
    			}
    		},
    	);
    });
  • Input schema for enhancement tools: requires a 'content' string parameter.
    schema: v.object({
    	content: v.pipe(v.string(), v.description('Content')),
    }),
  • Type definition for the response from Kagi enrichment APIs, used in the handler for type safety.
    export interface EnrichmentResponse {
    	data: Array<{
    		title: string;
    		url: string;
    		snippet: string;
    		rank?: number;
    	}>;
    	meta?: {
    		total_hits: number;
    		api_balance?: number;
    	};
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/spences10/mcp-omnisearch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server