export
Export data from a collection to CSV or JSONL format; returns content directly or saves to a file when filepath is specified.
Instructions
Export data from collection. If no filepath specified, returns the exported content directly. If filepath specified, saves to file inside Docker container.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection to export | |
| filepath | No | File to save export data (optional, will return content if not specified) | |
| csv | No | Export to CSV format | |
| jsonl | No | Export to JSONL format |
Implementation Reference
- src/index.ts:142-147 (schema)Zod schema defining the input parameters for the 'export' tool: collection (required), filepath (optional file to save to), csv (optional boolean), jsonl (optional boolean).
const exportSchema = z.object({ collection: z.string().describe("Collection to export"), filepath: z.string().optional().describe("File to save export data (optional, will return content if not specified)"), csv: z.boolean().optional().describe("Export to CSV format"), jsonl: z.boolean().optional().describe("Export to JSONL format") }); - src/index.ts:418-432 (registration)Tool registration entry for 'export' in the tools array, including its description and inputSchema (JSON Schema format) defining the same parameters.
{ name: "export", description: "Export data from collection. If no filepath specified, returns the exported content directly. If filepath specified, saves to file inside Docker container.", schema: exportSchema, inputSchema: { type: "object", properties: { collection: { type: "string", description: "Collection to export" }, filepath: { type: "string", description: "File to save export data (optional, will return content if not specified)" }, csv: { type: "boolean", description: "Export to CSV format" }, jsonl: { type: "boolean", description: "Export to JSONL format" } }, required: ["collection"] } }, - src/index.ts:1184-1207 (handler)Handler case for the 'export' tool. It constructs coho CLI arguments for the 'export' command with project, space, collection, and optional flags (filepath, csv, jsonl), then executes the command via executeCohoCommand and returns the result as text content.
case "export": { const { collection, filepath, csv, jsonl } = args as ExportArgs; const exportArgs = [ 'export', '--project', config.projectId, '--space', config.space, '--collection', collection ]; if (filepath) exportArgs.push('--file', filepath); if (csv) exportArgs.push('--csv'); if (jsonl) exportArgs.push('--jsonl'); const result = await executeCohoCommand(exportArgs); return { content: [ { type: "text", text: result } ], isError: false }; } - src/index.ts:529-564 (helper)Helper function executeCohoCommand used by the export handler (and all other tools) to execute coho CLI commands with the admin token.
async function executeCohoCommand(args: string[]): Promise<string> { const safeArgs = ['coho', ...args, '--admintoken', '***']; console.error(`Executing command: ${safeArgs.join(' ')}`); try { const { stdout, stderr } = await execFile('coho', [...args, '--admintoken', config.adminToken], { timeout: 120000 // 2 minutes timeout for CLI operations }); if (stderr) { // Sanitize stderr before logging to avoid token exposure const safeSterr = stderr.replace(new RegExp(config.adminToken, 'g'), '***'); console.error(`Command output to stderr:`, safeSterr); } console.error(`Command successful`); const result = stdout || stderr; // Sanitize result to ensure admin token is not exposed return result ? result.replace(new RegExp(config.adminToken, 'g'), '***') : result; } catch (error: any) { // Comprehensive sanitization of all error properties to avoid admin token exposure const sanitizeText = (text: string): string => text ? text.replace(new RegExp(config.adminToken, 'g'), '***') : text; const sanitizedMessage = sanitizeText(error?.message || 'Unknown error'); const sanitizedCmd = sanitizeText(error?.cmd || ''); const sanitizedStdout = sanitizeText(error?.stdout || ''); const sanitizedStderr = sanitizeText(error?.stderr || ''); // Log sanitized error details console.error(`Command failed: ${sanitizedMessage}`); if (sanitizedCmd) console.error(`Command: ${sanitizedCmd}`); if (sanitizedStdout) console.error(`Stdout: ${sanitizedStdout}`); if (sanitizedStderr) console.error(`Stderr: ${sanitizedStderr}`); // Return sanitized error message const errorDetails = [sanitizedMessage, sanitizedStderr].filter(Boolean).join(' - '); throw new McpError(ErrorCode.InvalidRequest, `Command failed: ${errorDetails}`); } }