gdrive-mcp-core 0.1.1

Google Drive MCP server - core library with tools, resources, and prompts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use base64::Engine;
use rmcp::{
    ErrorData as McpError,
    handler::server::wrapper::Parameters,
    model::*,
    tool, tool_router,
};
use serde::Deserialize;

use crate::convert;
use crate::server::GDriveServer;

// ── Parameter types ──────────────────────────────────────────────────

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesListParams {
    /// Google Drive search query (e.g. "name contains 'report'"). See Drive query syntax.
    #[serde(default)]
    pub query: Option<String>,
    /// Maximum number of files to return (1-1000, default 100).
    #[serde(default)]
    pub page_size: Option<i32>,
    /// Page token for pagination.
    #[serde(default)]
    pub page_token: Option<String>,
    /// Sort order (e.g. "modifiedTime desc", "name").
    #[serde(default)]
    pub order_by: Option<String>,
    /// Shared drive ID to search in.
    #[serde(default)]
    pub drive_id: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesGetParams {
    /// The ID of the file to retrieve.
    pub file_id: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesCreateParams {
    /// Name for the new file.
    pub name: String,
    /// MIME type of the file (e.g. "text/plain", "application/vnd.google-apps.document").
    #[serde(default)]
    pub mime_type: Option<String>,
    /// Parent folder ID. Defaults to root.
    #[serde(default)]
    pub parent_id: Option<String>,
    /// Text content for the file.
    #[serde(default)]
    pub content: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesUpdateParams {
    /// The ID of the file to update.
    pub file_id: String,
    /// New name for the file.
    #[serde(default)]
    pub name: Option<String>,
    /// New text content for the file.
    #[serde(default)]
    pub content: Option<String>,
    /// New MIME type.
    #[serde(default)]
    pub mime_type: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesDeleteParams {
    /// The ID of the file to permanently delete.
    pub file_id: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesCopyParams {
    /// The ID of the file to copy.
    pub file_id: String,
    /// Name for the copy.
    #[serde(default)]
    pub name: Option<String>,
    /// Parent folder ID for the copy.
    #[serde(default)]
    pub parent_id: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesMoveParams {
    /// The ID of the file to move.
    pub file_id: String,
    /// The ID of the new parent folder.
    pub new_parent_id: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesTrashParams {
    /// The ID of the file to trash.
    pub file_id: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesExportParams {
    /// The ID of the Google Workspace file to export.
    pub file_id: String,
    /// Export MIME type (e.g. "text/markdown", "text/csv", "application/pdf").
    pub export_mime_type: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FilesDownloadParams {
    /// The ID of the file to download.
    pub file_id: String,
}

// ── Tool implementations ────────────────────────────────────────────

#[tool_router(router = files_tool_router, vis = "pub")]
impl GDriveServer {
    /// Search and list files in Google Drive. Supports Drive query syntax for filtering,
    /// pagination, and sorting. Returns file names, IDs, and types.
    #[tool(name = "gdrive_files_list")]
    async fn files_list(
        &self,
        Parameters(params): Parameters<FilesListParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut req = self.client.hub().files().list();
        req = req.param("fields", "nextPageToken,files(id,name,mimeType,modifiedTime,size,parents,webViewLink)");

        if let Some(q) = &params.query {
            req = req.q(q);
        }
        if let Some(ps) = params.page_size {
            req = req.page_size(ps);
        }
        if let Some(pt) = &params.page_token {
            req = req.page_token(pt);
        }
        if let Some(ob) = &params.order_by {
            req = req.order_by(ob);
        }
        if let Some(drive_id) = &params.drive_id {
            req = req.drive_id(drive_id);
            req = req.include_items_from_all_drives(true);
            req = req.supports_all_drives(true);
            req = req.corpora("drive");
        }

        let (_, file_list) = req.doit().await.map_err(drive_err)?;

        let files = file_list.files.unwrap_or_default();
        let mut result = convert::files_summary(&files);

        if let Some(npt) = file_list.next_page_token {
            result.push_str(&format!("\n\n[Next page token: {npt}]"));
        }

        Ok(CallToolResult::success(vec![Content::text(result)]))
    }

    /// Get detailed metadata for a specific file by its ID.
    #[tool(name = "gdrive_files_get")]
    async fn files_get(
        &self,
        Parameters(params): Parameters<FilesGetParams>,
    ) -> Result<CallToolResult, McpError> {
        let (_, file) = self
            .client
            .hub()
            .files()
            .get(&params.file_id)
            .param("fields", "id,name,mimeType,modifiedTime,createdTime,size,parents,webViewLink,description,starred,trashed,shared,owners,permissions")
            .supports_all_drives(true)
            .doit()
            .await
            .map_err(drive_err)?;

        let json = serde_json::to_string_pretty(&file)
            .map_err(|e| McpError::internal_error(e.to_string(), None))?;

        Ok(CallToolResult::success(vec![Content::text(json)]))
    }

    /// Create a new file in Google Drive with optional content.
    #[tool(name = "gdrive_files_create")]
    async fn files_create(
        &self,
        Parameters(params): Parameters<FilesCreateParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut file_meta = google_drive3::api::File::default();
        file_meta.name = Some(params.name);

        if let Some(mime) = &params.mime_type {
            file_meta.mime_type = Some(mime.clone());
        }
        if let Some(parent) = &params.parent_id {
            file_meta.parents = Some(vec![parent.clone()]);
        }

        let content_bytes = params.content.as_deref().unwrap_or("").as_bytes().to_vec();
        let upload_mime: mime::Mime = file_meta
            .mime_type
            .as_deref()
            .unwrap_or("text/plain")
            .parse()
            .unwrap_or(mime::TEXT_PLAIN);
        let cursor = std::io::Cursor::new(content_bytes);

        let (_, file) = self
            .client
            .hub()
            .files()
            .create(file_meta)
            .upload(cursor, upload_mime)
            .await
            .map_err(drive_err)?;
        Ok(CallToolResult::success(vec![Content::text(format!(
            "Created file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Update a file's metadata and/or content.
    #[tool(name = "gdrive_files_update")]
    async fn files_update(
        &self,
        Parameters(params): Parameters<FilesUpdateParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut file_meta = google_drive3::api::File::default();

        if let Some(name) = &params.name {
            file_meta.name = Some(name.clone());
        }
        if let Some(mime) = &params.mime_type {
            file_meta.mime_type = Some(mime.clone());
        }

        let result = if let Some(content) = &params.content {
            let mime: mime::Mime = params
                .mime_type
                .as_deref()
                .unwrap_or("text/plain")
                .parse()
                .unwrap_or(mime::TEXT_PLAIN);
            let cursor = std::io::Cursor::new(content.as_bytes().to_vec());
            self.client
                .hub()
                .files()
                .update(file_meta, &params.file_id)
                .upload(cursor, mime)
                .await
                .map_err(drive_err)?
        } else {
            self.client
                .hub()
                .files()
                .update(file_meta, &params.file_id)
                .doit_without_upload()
                .await
                .map_err(drive_err)?
        };

        let file = result.1;
        Ok(CallToolResult::success(vec![Content::text(format!(
            "Updated file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Permanently delete a file from Google Drive. This cannot be undone.
    #[tool(name = "gdrive_files_delete")]
    async fn files_delete(
        &self,
        Parameters(params): Parameters<FilesDeleteParams>,
    ) -> Result<CallToolResult, McpError> {
        self.client
            .hub()
            .files()
            .delete(&params.file_id)
            .supports_all_drives(true)
            .doit()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Permanently deleted file: {}",
            params.file_id
        ))]))
    }

    /// Copy a file, optionally with a new name or parent folder.
    #[tool(name = "gdrive_files_copy")]
    async fn files_copy(
        &self,
        Parameters(params): Parameters<FilesCopyParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut file_meta = google_drive3::api::File::default();

        if let Some(name) = &params.name {
            file_meta.name = Some(name.clone());
        }
        if let Some(parent) = &params.parent_id {
            file_meta.parents = Some(vec![parent.clone()]);
        }

        let (_, file) = self
            .client
            .hub()
            .files()
            .copy(file_meta, &params.file_id)
            .supports_all_drives(true)
            .doit()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Copied file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Move a file to a different folder.
    #[tool(name = "gdrive_files_move")]
    async fn files_move(
        &self,
        Parameters(params): Parameters<FilesMoveParams>,
    ) -> Result<CallToolResult, McpError> {
        // First get current parents
        let (_, current) = self
            .client
            .hub()
            .files()
            .get(&params.file_id)
            .param("fields", "parents")
            .supports_all_drives(true)
            .doit()
            .await
            .map_err(drive_err)?;

        let remove_parents = current
            .parents
            .as_ref()
            .map(|p| p.join(","))
            .unwrap_or_default();

        let file_meta = google_drive3::api::File::default();
        let (_, file) = self
            .client
            .hub()
            .files()
            .update(file_meta, &params.file_id)
            .add_parents(&params.new_parent_id)
            .remove_parents(&remove_parents)
            .supports_all_drives(true)
            .doit_without_upload()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Moved file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Move a file to the trash.
    #[tool(name = "gdrive_files_trash")]
    async fn files_trash(
        &self,
        Parameters(params): Parameters<FilesTrashParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut file_meta = google_drive3::api::File::default();
        file_meta.trashed = Some(true);

        let (_, file) = self
            .client
            .hub()
            .files()
            .update(file_meta, &params.file_id)
            .supports_all_drives(true)
            .doit_without_upload()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Trashed file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Restore a file from the trash.
    #[tool(name = "gdrive_files_untrash")]
    async fn files_untrash(
        &self,
        Parameters(params): Parameters<FilesTrashParams>,
    ) -> Result<CallToolResult, McpError> {
        let mut file_meta = google_drive3::api::File::default();
        file_meta.trashed = Some(false);

        let (_, file) = self
            .client
            .hub()
            .files()
            .update(file_meta, &params.file_id)
            .supports_all_drives(true)
            .doit_without_upload()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Restored file: {}",
            convert::file_summary(&file)
        ))]))
    }

    /// Permanently delete all files in the trash.
    #[tool(name = "gdrive_files_empty_trash")]
    async fn files_empty_trash(&self) -> Result<CallToolResult, McpError> {
        self.client
            .hub()
            .files()
            .empty_trash()
            .doit()
            .await
            .map_err(drive_err)?;

        Ok(CallToolResult::success(vec![Content::text(
            "Trash emptied successfully.",
        )]))
    }

    /// Export a Google Workspace document to a specified format (e.g. Docs→Markdown, Sheets→CSV).
    #[tool(name = "gdrive_files_export")]
    async fn files_export(
        &self,
        Parameters(params): Parameters<FilesExportParams>,
    ) -> Result<CallToolResult, McpError> {
        // Check if it's a binary export
        if params.export_mime_type.starts_with("image/")
            || params.export_mime_type == "application/pdf"
        {
            let bytes = convert::export_as_bytes(&self.client, &params.file_id, &params.export_mime_type)
                .await
                .map_err(|e| McpError::internal_error(e.to_string(), None))?;
            let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
            Ok(CallToolResult::success(vec![Content::text(format!(
                "Exported as {} ({} bytes, base64-encoded):\n{encoded}",
                params.export_mime_type,
                bytes.len()
            ))]))
        } else {
            let text = convert::export_as_text(&self.client, &params.file_id, &params.export_mime_type)
                .await
                .map_err(|e| McpError::internal_error(e.to_string(), None))?;
            Ok(CallToolResult::success(vec![Content::text(text)]))
        }
    }

    /// Download a non-Google-Workspace file's content.
    #[tool(name = "gdrive_files_download")]
    async fn files_download(
        &self,
        Parameters(params): Parameters<FilesDownloadParams>,
    ) -> Result<CallToolResult, McpError> {
        // First get file metadata to check type
        let (_, file) = self
            .client
            .hub()
            .files()
            .get(&params.file_id)
            .param("fields", "id,name,mimeType,size")
            .supports_all_drives(true)
            .doit()
            .await
            .map_err(drive_err)?;

        let mime_type = file.mime_type.as_deref().unwrap_or("application/octet-stream");

        // If it's a Google Workspace type, export with default format
        if convert::is_google_workspace_type(mime_type) {
            let export_mime = convert::default_export_mime(mime_type)
                .ok_or_else(|| McpError::internal_error(
                    format!("Cannot determine export format for {mime_type}"),
                    None,
                ))?;

            if export_mime.starts_with("image/") {
                let bytes = convert::export_as_bytes(&self.client, &params.file_id, export_mime)
                    .await
                    .map_err(|e| McpError::internal_error(e.to_string(), None))?;
                let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
                return Ok(CallToolResult::success(vec![Content::text(format!(
                    "[Exported as {export_mime}, {len} bytes, base64]\n{encoded}",
                    len = bytes.len()
                ))]));
            }

            let text = convert::export_as_text(&self.client, &params.file_id, export_mime)
                .await
                .map_err(|e| McpError::internal_error(e.to_string(), None))?;
            return Ok(CallToolResult::success(vec![Content::text(text)]));
        }

        // Regular file: check if text-like
        if is_text_mime(mime_type) {
            let text = convert::download_as_text(&self.client, &params.file_id)
                .await
                .map_err(|e| McpError::internal_error(e.to_string(), None))?;
            Ok(CallToolResult::success(vec![Content::text(text)]))
        } else {
            let bytes = convert::download_as_bytes(&self.client, &params.file_id)
                .await
                .map_err(|e| McpError::internal_error(e.to_string(), None))?;
            let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
            Ok(CallToolResult::success(vec![Content::text(format!(
                "[Binary file {mime_type}, {len} bytes, base64]\n{encoded}",
                len = bytes.len()
            ))]))
        }
    }
}

fn is_text_mime(mime_type: &str) -> bool {
    mime_type.starts_with("text/")
        || mime_type == "application/json"
        || mime_type == "application/xml"
        || mime_type == "application/javascript"
        || mime_type == "application/typescript"
        || mime_type.ends_with("+xml")
        || mime_type.ends_with("+json")
}

fn drive_err(e: google_drive3::Error) -> McpError {
    McpError::internal_error(format!("Google Drive API error: {e}"), None)
}