UNPKG

mcp-gdrive-enhanced-markov

Version:

MCP server for Google Drive and Sheets with write capabilities, AI-powered PDF analysis, SQLite document indexing, and advanced directory exploration tools

93 lines (92 loc) 3.09 kB
import { google } from "googleapis"; export const schema = { name: "gdrive_update_file", description: "Update the content of an existing file in Google Drive", inputSchema: { type: "object", properties: { fileId: { type: "string", description: "ID of the file to update" }, content: { type: "string", description: "New content for the file" } }, required: ["fileId", "content"] } }; // Maximum file size: 10MB const MAX_FILE_SIZE = 10 * 1024 * 1024; export async function updateFile(args) { try { const validatedArgs = args; // Security validation if (Buffer.byteLength(validatedArgs.content) > MAX_FILE_SIZE) { return { content: [{ type: "text", text: "Error: File size exceeds 10MB limit" }], isError: true }; } const drive = google.drive({ version: "v3" }); // First, get the file metadata to determine its MIME type const fileResponse = await drive.files.get({ fileId: validatedArgs.fileId, fields: "id,name,mimeType" }); const file = fileResponse.data; // Update the file content const updateResponse = await drive.files.update({ fileId: validatedArgs.fileId, media: { mimeType: file.mimeType || "text/plain", body: validatedArgs.content }, fields: "id,name,mimeType,modifiedTime,webViewLink" }); const updatedFile = updateResponse.data; return { content: [{ type: "text", text: `File updated successfully: - Name: ${updatedFile.name} - ID: ${updatedFile.id} - MIME Type: ${updatedFile.mimeType} - Modified: ${updatedFile.modifiedTime} - View Link: ${updatedFile.webViewLink || "N/A"}` }], isError: false }; } catch (error) { if (error.code === 404) { return { content: [{ type: "text", text: "Error: File not found or access denied. With drive.file scope, you can only modify files created by this app." }], isError: true }; } if (error.code === 403 && error.message?.includes("insufficientPermissions")) { return { content: [{ type: "text", text: "Error: Insufficient permissions. This operation requires write access. Please re-authenticate with updated permissions." }], isError: true }; } return { content: [{ type: "text", text: `Error updating file: ${error.message || error}` }], isError: true }; } }