Skip to main content
Glama

search_onyx

Search the Onyx backend to retrieve relevant document chunks or full documents based on a query. Customize results by specifying document sets, surrounding chunks, and max results for precise information retrieval.

Instructions

Search the Onyx backend for relevant documents

Input Schema

NameRequiredDescriptionDefault
chunksAboveNoNumber of chunks to include above the matching chunk (default: 1)
chunksBelowNoNumber of chunks to include below the matching chunk (default: 1)
documentSetsNoList of document set names to search within (empty for all)
maxResultsNoMaximum number of results to return (default: 5)
queryYesThe topic to search for
retrieveFullDocumentsNoWhether to retrieve full documents instead of just matching chunks (default: false)

Input Schema (JSON Schema)

{ "properties": { "chunksAbove": { "default": 1, "description": "Number of chunks to include above the matching chunk (default: 1)", "type": "integer" }, "chunksBelow": { "default": 1, "description": "Number of chunks to include below the matching chunk (default: 1)", "type": "integer" }, "documentSets": { "description": "List of document set names to search within (empty for all)", "items": { "type": "string" }, "type": "array" }, "maxResults": { "description": "Maximum number of results to return (default: 5)", "maximum": 10, "minimum": 1, "type": "integer" }, "query": { "description": "The topic to search for", "type": "string" }, "retrieveFullDocuments": { "default": false, "description": "Whether to retrieve full documents instead of just matching chunks (default: false)", "type": "boolean" } }, "required": [ "query" ], "type": "object" }

Implementation Reference

  • Main handler function that executes the search_onyx tool logic: validates input, performs semantic search via OnyxApiService, sorts and limits results, fetches content/chunks, builds context, and returns MCP-formatted response.
    export async function handleSearchOnyx(args: unknown, onyxApiService: OnyxApiService) { try { if (typeof args !== 'object' || args === null) { throw new McpError(ErrorCode.InvalidParams, 'Invalid arguments'); } const { query, documentSets = [], maxResults = 5, chunksAbove = 1, chunksBelow = 1, retrieveFullDocuments = false } = args as SearchParams; if (!query || typeof query !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'Query is required'); } // Step 1: Perform semantic search to find relevant documents const searchResults = await onyxApiService.searchOnyx(query, documentSets, chunksAbove, chunksBelow); // Step 2: Get the most relevant results const relevantResults = searchResults .sort((a, b) => b.score - a.score) .slice(0, maxResults); // Step 3: For each relevant document, fetch either the specific chunk or full document content const relevantContent = await Promise.all( relevantResults.map(result => retrieveFullDocuments ? onyxApiService.fetchDocumentContent(result.document_id) : onyxApiService.fetchDocumentChunk(result.document_id, result.chunk_ind) ) ); // Step 4: Combine into comprehensive context const context = onyxApiService.buildContext(relevantResults, relevantContent); return { content: [ { type: 'text', text: context, }, ], }; } catch (error) { console.error('Error in handleSearchOnyx:', error); return { content: [ { type: 'text', text: `Error searching Onyx: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
  • Input schema and metadata definition for the search_onyx tool, used for validation and tool listing.
    search_onyx: { name: 'search_onyx', description: 'Search the Onyx backend for relevant documents', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The topic to search for', }, chunksAbove: { type: 'integer', description: 'Number of chunks to include above the matching chunk (default: 1)', default: 1 }, chunksBelow: { type: 'integer', description: 'Number of chunks to include below the matching chunk (default: 1)', default: 1 }, retrieveFullDocuments: { type: 'boolean', description: 'Whether to retrieve full documents instead of just matching chunks (default: false)', default: false }, documentSets: { type: 'array', items: { type: 'string', }, description: 'List of document set names to search within (empty for all)', }, maxResults: { type: 'integer', description: 'Maximum number of results to return (default: 5)', minimum: 1, maximum: 10, }, }, required: ['query'], }, },
  • src/server.ts:62-75 (registration)
    Registration of the search_onyx tool handler in the MCP server's CallToolRequestSchema handler (switch case dispatching to handleSearchOnyx).
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { switch (request.params.name) { case 'search_onyx': return handleSearchOnyx(request.params.arguments, this.onyxApiService); case 'chat_with_onyx': return handleChatWithOnyx(request.params.arguments, this.onyxApiService); default: throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } }); }
  • src/server.ts:87-94 (registration)
    Registration of the search_onyx tool schema in the MCP server's ListToolsRequestSchema response.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ toolSchemas.search_onyx, toolSchemas.chat_with_onyx, ], }; });
  • Core searchOnyx method in OnyxApiService that performs the actual API call to Onyx backend for semantic search, called by the tool handler.
    async searchOnyx( query: string, documentSets: string[] = [], chunksAbove = 0, chunksBelow = 0 ): Promise<OnyxSearchResult[]> { try { const response = await axios.post( `${this.config.apiUrl}/admin/search`, { query, filters: { document_set: documentSets.length > 0 ? documentSets : null, source_type: null, time_cutoff: null, tags: null }, chunks_above: chunksAbove, chunks_below: chunksBelow, evaluation_type: 'basic' // Enable LLM relevance filtering }, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.config.apiToken}` } } ); return response.data.documents || []; } catch (error) { console.error('Error searching Onyx:', error); throw new Error(`Failed to search Onyx: ${error instanceof Error ? error.message : String(error)}`); } }

Other Tools

Related Tools

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/lupuletic/onyx-mcp-server'

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