upload_file
Transfer files such as images and documents to the Emlog blog system via API, specifying the file path and resource category ID for efficient content management.
Instructions
Upload a file (image, document, etc.)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Local path to the file to upload | |
| sid | No | Resource category ID |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"file_path": {
"description": "Local path to the file to upload",
"type": "string"
},
"sid": {
"description": "Resource category ID",
"type": "number"
}
},
"required": [
"file_path"
],
"type": "object"
}
Implementation Reference
- src/index.ts:523-541 (handler)MCP tool handler for 'upload_file' that invokes emlogClient.uploadFile and formats the response or error.async ({ file_path, sid }) => { try { const result = await emlogClient.uploadFile(file_path, sid); return { content: [{ type: "text", text: `Successfully uploaded file: ${result.url}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- src/index.ts:518-521 (schema)Input schema using Zod for validating tool parameters: file_path (required string) and sid (optional number).inputSchema: { file_path: z.string().describe("Local path to the file to upload"), sid: z.number().optional().describe("Resource category ID") }
- src/index.ts:513-542 (registration)Registration of the 'upload_file' tool with server.registerTool, including title, description, schema, and handler.server.registerTool( "upload_file", { title: "Upload File", description: "Upload a file (image, document, etc.)", inputSchema: { file_path: z.string().describe("Local path to the file to upload"), sid: z.number().optional().describe("Resource category ID") } }, async ({ file_path, sid }) => { try { const result = await emlogClient.uploadFile(file_path, sid); return { content: [{ type: "text", text: `Successfully uploaded file: ${result.url}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/emlog-client.ts:433-450 (helper)Core file upload logic in EmlogClient class: checks file existence, prepares FormData, posts to API, returns media object.async uploadFile(filePath: string, sid?: number): Promise<EmlogMedia> { if (!fs.existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } const formData = new FormData(); formData.append('file', fs.createReadStream(filePath)); if (sid) formData.append('sid', String(sid)); formData.append('api_key', this.apiKey); const response = await this.api.post('/?rest-api=upload', formData, { headers: { ...formData.getHeaders() } }); return response.data.data; }