update-documents
Modify or add documents in a Meilisearch index by specifying the index identifier and a JSON array of documents. Ensures data accuracy and synchronization within search indices.
Instructions
Update documents in a Meilisearch index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| documents | Yes | JSON array of documents to update | |
| indexUid | Yes | Unique identifier of the index | |
| primaryKey | No | Primary key for the documents |
Implementation Reference
- src/tools/document-tools.ts:163-189 (handler)The handler function that executes the 'update-documents' tool: parses JSON documents, validates array, performs PUT request to Meilisearch API via apiClient, returns formatted response or error.async ({ indexUid, documents, primaryKey }: UpdateDocumentsParams) => { try { // Parse the documents string to ensure it's valid JSON const parsedDocuments = JSON.parse(documents); // Ensure documents is an array if (!Array.isArray(parsedDocuments)) { return { isError: true, content: [{ type: 'text', text: 'Documents must be a JSON array' }], }; } const params: Record<string, string> = {}; if (primaryKey) { params.primaryKey = primaryKey; } const response = await apiClient.put(`/indexes/${indexUid}/documents`, parsedDocuments, { params, }); return { content: [{ type: 'text', text: JSON.stringify(response.data, null, 2) }], }; } catch (error) { return createErrorResponse(error); }
- src/tools/document-tools.ts:158-162 (schema)Zod input schema for the 'update-documents' tool defining parameters: indexUid, documents (JSON string), and optional primaryKey.{ indexUid: z.string().describe('Unique identifier of the index'), documents: z.string().describe('JSON array of documents to update'), primaryKey: z.string().optional().describe('Primary key for the documents'), },
- src/tools/document-tools.ts:155-191 (registration)MCP server registration of the 'update-documents' tool, specifying name, description, input schema, and handler function.server.tool( 'update-documents', 'Update documents in a Meilisearch index', { indexUid: z.string().describe('Unique identifier of the index'), documents: z.string().describe('JSON array of documents to update'), primaryKey: z.string().optional().describe('Primary key for the documents'), }, async ({ indexUid, documents, primaryKey }: UpdateDocumentsParams) => { try { // Parse the documents string to ensure it's valid JSON const parsedDocuments = JSON.parse(documents); // Ensure documents is an array if (!Array.isArray(parsedDocuments)) { return { isError: true, content: [{ type: 'text', text: 'Documents must be a JSON array' }], }; } const params: Record<string, string> = {}; if (primaryKey) { params.primaryKey = primaryKey; } const response = await apiClient.put(`/indexes/${indexUid}/documents`, parsedDocuments, { params, }); return { content: [{ type: 'text', text: JSON.stringify(response.data, null, 2) }], }; } catch (error) { return createErrorResponse(error); } } );
- src/tools/document-tools.ts:34-38 (helper)TypeScript type definition for the parameters used in the 'update-documents' handler.interface UpdateDocumentsParams { indexUid: string; documents: string; primaryKey?: string; }