add-documents
Upload JSON documents to a Meilisearch index, specifying the index identifier and optional primary key for efficient data management and search functionality.
Instructions
Add documents to a Meilisearch index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| documents | Yes | JSON array of documents to add | |
| indexUid | Yes | Unique identifier of the index | |
| primaryKey | No | Primary key for the documents |
Implementation Reference
- src/tools/document-tools.ts:124-151 (handler)The main handler function for the 'add-documents' tool. It parses the input documents JSON, validates it's an array, prepares parameters including optional primaryKey, and performs a POST request to the Meilisearch /documents endpoint using apiClient.async ({ indexUid, documents, primaryKey }: AddDocumentsParams) => { 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.post(`/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:119-123 (schema)Zod schema for input validation of the 'add-documents' tool parameters.{ indexUid: z.string().describe('Unique identifier of the index'), documents: z.string().describe('JSON array of documents to add'), primaryKey: z.string().optional().describe('Primary key for the documents'), },
- src/tools/document-tools.ts:28-32 (schema)TypeScript interface defining the parameters for the add-documents handler.interface AddDocumentsParams { indexUid: string; documents: string; primaryKey?: string; }
- src/tools/document-tools.ts:116-152 (registration)The server.tool() call that registers the 'add-documents' tool with the MCP server, including description, schema, and handler.server.tool( 'add-documents', 'Add documents to a Meilisearch index', { indexUid: z.string().describe('Unique identifier of the index'), documents: z.string().describe('JSON array of documents to add'), primaryKey: z.string().optional().describe('Primary key for the documents'), }, async ({ indexUid, documents, primaryKey }: AddDocumentsParams) => { 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.post(`/indexes/${indexUid}/documents`, parsedDocuments, { params, }); return { content: [{ type: 'text', text: JSON.stringify(response.data, null, 2) }], }; } catch (error) { return createErrorResponse(error); } } );
- src/index.ts:65-65 (registration)Invocation of registerDocumentTools in the main server setup, which registers the add-documents tool among others.registerDocumentTools(server);