uploadFile
Upload files to Directus CMS using the MCP Server by providing a file URL, base64 data, and metadata. Supports file storage, naming, and MIME type specification for efficient file management.
Instructions
Upload a file to Directus
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileData | No | Base64 encoded file data (either fileUrl or fileData must be provided) | |
| fileName | Yes | Name of the file | |
| fileUrl | No | URL of the file to upload (either fileUrl or fileData must be provided) | |
| mimeType | No | MIME type of the file | |
| storage | No | Storage location (optional) | |
| title | No | File title (optional) | |
| token | No | Authentication token (default from config) | |
| url | No | Directus API URL (default from config) |
Implementation Reference
- index.ts:799-856 (handler)The handler for the uploadFile tool within the switch statement in CallToolRequestSchema handler. It processes input arguments, fetches or decodes file data, creates FormData, and uploads to Directus /files endpoint.case "uploadFile": { const token = toolArgs.token || CONFIG.DIRECTUS_ACCESS_TOKEN; const fileName = toolArgs.fileName as string; const fileUrl = toolArgs.fileUrl as string | undefined; const fileData = toolArgs.fileData as string | undefined; const mimeType = toolArgs.mimeType as string | undefined; const storage = toolArgs.storage as string | undefined; const title = toolArgs.title as string | undefined; let fileContent: Buffer; // Get file data either from URL or base64 data if (fileUrl) { const fileResponse = await axios.get(fileUrl, { responseType: 'arraybuffer' }); fileContent = Buffer.from(fileResponse.data); } else if (fileData) { fileContent = Buffer.from(fileData, 'base64'); } else { throw new Error("Either fileUrl or fileData must be provided"); } // Create form data for file upload const FormData = (await import('form-data')).default; const formData = new FormData(); formData.append('file', fileContent, { filename: fileName, contentType: mimeType }); if (storage) { formData.append('storage', storage); } if (title) { formData.append('title', title); } const response = await axios.post( `${url}/files`, formData, { headers: { ...buildHeaders(token), ...formData.getHeaders() } } ); return { content: [ { type: "text", text: JSON.stringify(response.data, null, 2) } ] }; }
- index.ts:376-417 (schema)The input schema definition for the uploadFile tool, registered in the ListToolsRequestSchema handler.{ name: "uploadFile", description: "Upload a file to Directus", inputSchema: { type: "object", properties: { url: { type: "string", description: "Directus API URL (default from config)" }, token: { type: "string", description: "Authentication token (default from config)" }, fileUrl: { type: "string", description: "URL of the file to upload (either fileUrl or fileData must be provided)" }, fileData: { type: "string", description: "Base64 encoded file data (either fileUrl or fileData must be provided)" }, fileName: { type: "string", description: "Name of the file" }, mimeType: { type: "string", description: "MIME type of the file" }, storage: { type: "string", description: "Storage location (optional)" }, title: { type: "string", description: "File title (optional)" } }, required: ["fileName"] } },
- index.ts:376-417 (registration)The registration of the uploadFile tool in the tools list returned by ListToolsRequestSchema.{ name: "uploadFile", description: "Upload a file to Directus", inputSchema: { type: "object", properties: { url: { type: "string", description: "Directus API URL (default from config)" }, token: { type: "string", description: "Authentication token (default from config)" }, fileUrl: { type: "string", description: "URL of the file to upload (either fileUrl or fileData must be provided)" }, fileData: { type: "string", description: "Base64 encoded file data (either fileUrl or fileData must be provided)" }, fileName: { type: "string", description: "Name of the file" }, mimeType: { type: "string", description: "MIME type of the file" }, storage: { type: "string", description: "Storage location (optional)" }, title: { type: "string", description: "File title (optional)" } }, required: ["fileName"] } },