search
Query a Meilisearch index to retrieve documents, filter results, and customize attributes like sorting, highlighting, and faceting using defined parameters.
Instructions
Search for documents in a Meilisearch index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| attributesToCrop | No | Attributes to crop | |
| attributesToHighlight | No | Attributes to highlight | |
| attributesToRetrieve | No | Attributes to include in results | |
| cropLength | No | Length at which to crop cropped attributes | |
| facets | No | Facets to return | |
| filter | No | Filter query to apply | |
| highlightPostTag | No | Tag to insert after highlighted text | |
| highlightPreTag | No | Tag to insert before highlighted text | |
| indexUid | Yes | Unique identifier of the index | |
| limit | No | Maximum number of results to return (default: 20) | |
| matchingStrategy | No | Matching strategy: 'all' or 'last' | |
| offset | No | Number of results to skip (default: 0) | |
| q | Yes | Search query | |
| showMatchesPosition | No | Whether to include match positions in results | |
| sort | No | Attributes to sort by, e.g. ["price:asc"] |
Implementation Reference
- src/tools/search-tools.ts:63-103 (handler)The handler function for the 'search' tool. It takes search parameters, makes a POST request to the Meilisearch /indexes/{indexUid}/search endpoint using apiClient, and returns the JSON response or an error.async ({ indexUid, q, limit, offset, filter, sort, facets, attributesToRetrieve, attributesToCrop, cropLength, attributesToHighlight, highlightPreTag, highlightPostTag, showMatchesPosition, matchingStrategy }: SearchParams) => { try { const response = await apiClient.post(`/indexes/${indexUid}/search`, { q, limit, offset, filter, sort, facets, attributesToRetrieve, attributesToCrop, cropLength, attributesToHighlight, highlightPreTag, highlightPostTag, showMatchesPosition, matchingStrategy, }); return { content: [{ type: 'text', text: JSON.stringify(response.data, null, 2) }], }; } catch (error) { return createErrorResponse(error); } }
- src/tools/search-tools.ts:46-62 (schema)Zod input schema defining parameters for the 'search' tool, including indexUid, query (q), pagination, filters, sorting, facets, and highlighting options.{ indexUid: z.string().describe('Unique identifier of the index'), q: z.string().describe('Search query'), limit: z.number().min(1).max(1000).optional().describe('Maximum number of results to return (default: 20)'), offset: z.number().min(0).optional().describe('Number of results to skip (default: 0)'), filter: z.string().optional().describe('Filter query to apply'), sort: z.array(z.string()).optional().describe('Attributes to sort by, e.g. ["price:asc"]'), facets: z.array(z.string()).optional().describe('Facets to return'), attributesToRetrieve: z.array(z.string()).optional().describe('Attributes to include in results'), attributesToCrop: z.array(z.string()).optional().describe('Attributes to crop'), cropLength: z.number().optional().describe('Length at which to crop cropped attributes'), attributesToHighlight: z.array(z.string()).optional().describe('Attributes to highlight'), highlightPreTag: z.string().optional().describe('Tag to insert before highlighted text'), highlightPostTag: z.string().optional().describe('Tag to insert after highlighted text'), showMatchesPosition: z.boolean().optional().describe('Whether to include match positions in results'), matchingStrategy: z.string().optional().describe("Matching strategy: 'all' or 'last'"), },
- src/tools/search-tools.ts:43-104 (registration)The server.tool call that registers the 'search' tool with its name, description, input schema, and handler function.server.tool( 'search', 'Search for documents in a Meilisearch index', { indexUid: z.string().describe('Unique identifier of the index'), q: z.string().describe('Search query'), limit: z.number().min(1).max(1000).optional().describe('Maximum number of results to return (default: 20)'), offset: z.number().min(0).optional().describe('Number of results to skip (default: 0)'), filter: z.string().optional().describe('Filter query to apply'), sort: z.array(z.string()).optional().describe('Attributes to sort by, e.g. ["price:asc"]'), facets: z.array(z.string()).optional().describe('Facets to return'), attributesToRetrieve: z.array(z.string()).optional().describe('Attributes to include in results'), attributesToCrop: z.array(z.string()).optional().describe('Attributes to crop'), cropLength: z.number().optional().describe('Length at which to crop cropped attributes'), attributesToHighlight: z.array(z.string()).optional().describe('Attributes to highlight'), highlightPreTag: z.string().optional().describe('Tag to insert before highlighted text'), highlightPostTag: z.string().optional().describe('Tag to insert after highlighted text'), showMatchesPosition: z.boolean().optional().describe('Whether to include match positions in results'), matchingStrategy: z.string().optional().describe("Matching strategy: 'all' or 'last'"), }, async ({ indexUid, q, limit, offset, filter, sort, facets, attributesToRetrieve, attributesToCrop, cropLength, attributesToHighlight, highlightPreTag, highlightPostTag, showMatchesPosition, matchingStrategy }: SearchParams) => { try { const response = await apiClient.post(`/indexes/${indexUid}/search`, { q, limit, offset, filter, sort, facets, attributesToRetrieve, attributesToCrop, cropLength, attributesToHighlight, highlightPreTag, highlightPostTag, showMatchesPosition, matchingStrategy, }); return { content: [{ type: 'text', text: JSON.stringify(response.data, null, 2) }], }; } catch (error) { return createErrorResponse(error); } } );
- src/index.ts:66-66 (registration)Invocation of registerSearchTools on the MCP server instance, which in turn registers the 'search' tool.registerSearchTools(server);
- src/tools/search-tools.ts:14-30 (schema)TypeScript interface defining the shape of SearchParams used in the handler type annotation.interface SearchParams { indexUid: string; q: string; limit?: number; offset?: number; filter?: string; sort?: string[]; facets?: string[]; attributesToRetrieve?: string[]; attributesToCrop?: string[]; cropLength?: number; attributesToHighlight?: string[]; highlightPreTag?: string; highlightPostTag?: string; showMatchesPosition?: boolean; matchingStrategy?: string; }