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
125 lines (124 loc) • 4.04 kB
JavaScript
import { google } from "googleapis";
export const schema = {
name: "gdrive_create_file",
description: "Create a new file in Google Drive",
inputSchema: {
type: "object",
properties: {
name: {
type: "string",
description: "Name of the file to create"
},
content: {
type: "string",
description: "Content of the file"
},
mimeType: {
type: "string",
description: "MIME type of the file (default: text/plain)",
optional: true
},
folderId: {
type: "string",
description: "ID of the parent folder (optional)",
optional: true
}
},
required: ["name", "content"]
}
};
// Security: Allowed MIME types for file creation
const ALLOWED_MIME_TYPES = [
"text/plain",
"text/csv",
"text/html",
"text/markdown",
"application/json",
"application/xml",
"application/vnd.google-apps.document",
"application/vnd.google-apps.spreadsheet",
"application/vnd.google-apps.presentation"
];
// Maximum file size: 10MB
const MAX_FILE_SIZE = 10 * 1024 * 1024;
export async function createFile(args) {
try {
const validatedArgs = args;
// Security validations
if (!validatedArgs.name || validatedArgs.name.includes("../")) {
return {
content: [{
type: "text",
text: "Error: Invalid file name"
}],
isError: true
};
}
if (Buffer.byteLength(validatedArgs.content) > MAX_FILE_SIZE) {
return {
content: [{
type: "text",
text: "Error: File size exceeds 10MB limit"
}],
isError: true
};
}
if (validatedArgs.mimeType && !ALLOWED_MIME_TYPES.includes(validatedArgs.mimeType)) {
return {
content: [{
type: "text",
text: `Error: MIME type '${validatedArgs.mimeType}' is not allowed. Allowed types: ${ALLOWED_MIME_TYPES.join(", ")}`
}],
isError: true
};
}
const drive = google.drive({ version: "v3" });
// Prepare file metadata
const fileMetadata = {
name: validatedArgs.name,
mimeType: validatedArgs.mimeType || "text/plain"
};
if (validatedArgs.folderId) {
fileMetadata.parents = [validatedArgs.folderId];
}
// Create the file
const response = await drive.files.create({
requestBody: fileMetadata,
media: {
mimeType: validatedArgs.mimeType || "text/plain",
body: validatedArgs.content
},
fields: "id,name,mimeType,webViewLink"
});
const file = response.data;
return {
content: [{
type: "text",
text: `File created successfully:
- Name: ${file.name}
- ID: ${file.id}
- MIME Type: ${file.mimeType}
- View Link: ${file.webViewLink || "N/A"}`
}],
isError: false
};
}
catch (error) {
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 creating file: ${error.message || error}`
}],
isError: true
};
}
}