search
Query and retrieve data from Meilisearch indices with flexibility. Specify an index or search across all indices, apply filters, sorting, and pagination for precise results.
Instructions
Search through Meilisearch indices. If indexUid is not provided, it will search across all indices.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | ||
| indexUid | No | ||
| limit | No | ||
| offset | No | ||
| query | Yes | ||
| sort | No |
Implementation Reference
- src/meilisearch_mcp/server.py:587-606 (handler)MCP server handler for the 'search' tool: extracts arguments, calls MeilisearchClient.search, formats results as JSON, and returns as TextContent.elif name == "search": search_results = self.meili_client.search( query=arguments["query"], index_uid=arguments.get("indexUid"), limit=arguments.get("limit"), offset=arguments.get("offset"), filter=arguments.get("filter"), sort=arguments.get("sort"), ) # Format the results for better readability formatted_results = json.dumps( search_results, indent=2, default=json_serializer ) return [ types.TextContent( type="text", text=f"Search results for '{arguments['query']}':\n{formatted_results}", ) ]
- Input schema definition and tool registration for 'search' in the list_tools() handler.types.Tool( name="search", description="Search through Meilisearch indices. If indexUid is not provided, it will search across all indices.", inputSchema={ "type": "object", "properties": { "query": {"type": "string"}, "indexUid": {"type": "string"}, "limit": {"type": "integer"}, "offset": {"type": "integer"}, "filter": {"type": "string"}, "sort": { "type": "array", "items": {"type": "string"}, }, }, "required": ["query"], "additionalProperties": False, }, ),
- src/meilisearch_mcp/client.py:51-102 (handler)Core implementation of search logic in MeilisearchClient: supports single-index or multi-index search using the Meilisearch library, handles parameters and errors.def search( self, query: str, index_uid: Optional[str] = None, limit: Optional[int] = 20, offset: Optional[int] = 0, filter: Optional[str] = None, sort: Optional[List[str]] = None, **kwargs, ) -> Dict[str, Any]: """ Search through Meilisearch indices. If index_uid is provided, search in that specific index. If not provided, search across all available indices. """ try: # Prepare search parameters, removing None values search_params = { "limit": limit if limit is not None else 20, "offset": offset if offset is not None else 0, } if filter is not None: search_params["filter"] = filter if sort is not None: search_params["sort"] = sort # Add any additional parameters search_params.update({k: v for k, v in kwargs.items() if v is not None}) if index_uid: # Search in specific index index = self.client.index(index_uid) return index.search(query, search_params) else: # Search across all indices results = {} indexes = self.client.get_indexes() for index in indexes["results"]: try: search_result = index.search(query, search_params) if search_result["hits"]: # Only include indices with matches results[index.uid] = search_result except Exception as e: logger.warning(f"Failed to search index {index.uid}: {str(e)}") continue return {"multi_index": True, "query": query, "results": results} except Exception as e: raise Exception(f"Search failed: {str(e)}")