import
Import data into a collection from a file path or content string, supporting JSON and CSV formats.
Instructions
Import data from file or content. Provide either 'filepath' (for files inside Docker container) or 'content' (JSON/CSV data as string).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | No | File path to import from (optional if content is provided) | |
| content | No | File content to import as JSON or CSV data (optional if filepath is provided) | |
| collection | Yes | Collection to import into | |
| separator | No | CSV separator character | |
| encoding | No | File encoding |
Implementation Reference
- src/index.ts:134-140 (schema)The Zod schema for the 'import' tool, defining input parameters: filepath, content, collection, separator, and encoding.
const importSchema = z.object({ filepath: z.string().optional().describe("File path to import from (optional if content is provided)"), content: z.string().optional().describe("File content to import (JSON or CSV data)"), collection: z.string().describe("Collection to import into"), separator: z.string().optional().describe("CSV separator character"), encoding: z.string().optional().describe("File encoding"), }); - src/index.ts:402-417 (registration)Registration of the 'import' tool in the tools array, with its name, description, schema reference, and inputSchema.
{ name: "import", description: "Import data from file or content. Provide either 'filepath' (for files inside Docker container) or 'content' (JSON/CSV data as string).", schema: importSchema, inputSchema: { type: "object", properties: { filepath: { type: "string", description: "File path to import from (optional if content is provided)" }, content: { type: "string", description: "File content to import as JSON or CSV data (optional if filepath is provided)" }, collection: { type: "string", description: "Collection to import into" }, separator: { type: "string", description: "CSV separator character" }, encoding: { type: "string", description: "File encoding" } }, required: ["collection"] } }, - src/index.ts:1121-1182 (handler)The handler function for the 'import' tool. Executes the coho CLI 'import' command with arguments built from validated input. Supports providing data via filepath or inline content (written to a temp file). Cleans up temp files after execution.
case "import": { const { filepath, content, collection, separator, encoding } = args as ImportArgs; if (!filepath && !content) { throw new McpError(ErrorCode.InvalidRequest, "Either 'filepath' or 'content' must be provided."); } let tempFilePath: string | undefined; try { const importArgs = [ 'import', '--project', config.projectId, '--space', config.space, '--collection', collection ]; if (content) { // Create temporary file with content tempFilePath = `/tmp/import-${Date.now()}.json`; await fs.writeFile(tempFilePath, content, { encoding: (encoding as BufferEncoding) || 'utf8' }); importArgs.push('--filepath', tempFilePath); } else if (filepath) { importArgs.push('--filepath', filepath); } if (separator) { importArgs.push('--separator', separator); } if (encoding) { importArgs.push('--encoding', encoding); } const result = await executeCohoCommand(importArgs); // Clean up temp file if created if (tempFilePath) { await fs.unlink(tempFilePath); } return { content: [ { type: "text", text: result } ], isError: false }; } catch (error) { // Clean up temp file on error if (tempFilePath) { try { await fs.unlink(tempFilePath); } catch (unlinkError) { // Ignore cleanup errors } } throw error; } }