feat: add list_recent_files tool#534
Conversation
Adds a new Drive tool that lists recently modified, viewed, shared, or created files across ALL of Google Drive (not limited to a single folder). Features: - Sort by modifiedTime, viewedByMeTime, sharedWithMeTime, or createdTime - Filter by ownership: all, owned (my files), or shared (others' files) - Filter by file type (doc, sheet, pdf, etc.) - Returns lastModifyingUser and owner info for each file - Pagination support via page_token Also adds backward-compatible `order_by` parameter to build_drive_list_params helper. Uses existing Drive API v3 files.list — no new scopes or auth required. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThese changes enhance Google Drive integration by adding support for ordered results in list parameters and introducing a new tool to list recently modified files with filtering, sorting, and pagination capabilities. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
gdrive/drive_tools.py (2)
566-583: Consider raising exceptions instead of returning error strings for validation failures.Other tools in this file (e.g.,
manage_drive_access,set_drive_file_permissions) raiseValueErrorfor invalid parameters, which allows the@handle_http_errorsdecorator to properly handle and log these asUserInputError. Returning a plain string bypasses this pattern and may lead to inconsistent error handling/logging.♻️ Suggested refactor to use exceptions
if sort_by not in valid_sort_options: - return ( - f"Invalid sort_by '{sort_by}'. Must be one of: " - f"{', '.join(valid_sort_options.keys())}" - ) + raise ValueError( + f"Invalid sort_by '{sort_by}'. Must be one of: " + f"{', '.join(valid_sort_options.keys())}" + ) order_by = valid_sort_options[sort_by] # Build query based on ownership filter query_parts = ["trashed=false"] if ownership == "owned": query_parts.append("'me' in owners") elif ownership == "shared": query_parts.append("not 'me' in owners") elif ownership != "all": - return ( - f"Invalid ownership '{ownership}'. Must be one of: all, owned, shared" - ) + raise ValueError( + f"Invalid ownership '{ownership}'. Must be one of: all, owned, shared" + )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@gdrive/drive_tools.py` around lines 566 - 583, Replace the validation-return branches that return error strings with raised ValueError exceptions so they follow the same error handling pattern as other helpers like manage_drive_access and set_drive_file_permissions; specifically, in the section using valid_sort_options and sort_by raise a ValueError with a message listing valid_sort_options.keys() instead of returning a string, and likewise replace the ownership validation branch (ownership != "all") with a ValueError describing allowed values ("all, owned, shared"); keep the same message content but throw ValueError to let `@handle_http_errors` convert it to UserInputError and preserve consistent logging/handling.
602-617: Consider reusingbuild_drive_list_paramsor documenting the deviation.The new
order_byparameter was added tobuild_drive_list_paramsin this PR, butlist_recent_filesbuilds its params manually. This is likely intentional due to the customfieldsrequirement (includinglastModifyingUserandowners), but it creates slight duplication.Either approach is fine, but a brief comment explaining why the helper isn't used would help future maintainers understand the design choice.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@gdrive/drive_tools.py` around lines 602 - 617, list_recent_files currently builds list_params manually (setting fields to include lastModifyingUser and owners and passing order_by) instead of reusing build_drive_list_params, which causes duplication; either call build_drive_list_params(final_query, page_size, order_by, page_token, include_items_from_all_drives, supportsAllDrives=True) and then override the "fields" entry to add lastModifyingUser/owners, or keep the manual construction but add a short comment above this block stating that build_drive_list_params is intentionally not used because list_recent_files requires a custom fields string (include reference to variables fields, list_params and parameter order_by) so future maintainers understand the deviation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@gdrive/drive_tools.py`:
- Around line 566-583: Replace the validation-return branches that return error
strings with raised ValueError exceptions so they follow the same error handling
pattern as other helpers like manage_drive_access and
set_drive_file_permissions; specifically, in the section using
valid_sort_options and sort_by raise a ValueError with a message listing
valid_sort_options.keys() instead of returning a string, and likewise replace
the ownership validation branch (ownership != "all") with a ValueError
describing allowed values ("all, owned, shared"); keep the same message content
but throw ValueError to let `@handle_http_errors` convert it to UserInputError and
preserve consistent logging/handling.
- Around line 602-617: list_recent_files currently builds list_params manually
(setting fields to include lastModifyingUser and owners and passing order_by)
instead of reusing build_drive_list_params, which causes duplication; either
call build_drive_list_params(final_query, page_size, order_by, page_token,
include_items_from_all_drives, supportsAllDrives=True) and then override the
"fields" entry to add lastModifyingUser/owners, or keep the manual construction
but add a short comment above this block stating that build_drive_list_params is
intentionally not used because list_recent_files requires a custom fields string
(include reference to variables fields, list_params and parameter order_by) so
future maintainers understand the deviation.
|
I just spent several days collapsing down tools that were too specific, I think this is a good idea but I want to build it into an existing tool rather than spinning out another one. I think the most obvious option would be to add logic to search_drive_files that allows it to be used without a query term if a param (ie "recent") is included with the request. Thanks! |
Summary
Adds a new
list_recent_filesDrive tool that lists recently modified, viewed, shared, or created files across all of Google Drive — not limited to a single folder or root.This fills a gap where
search_drive_filesrequires a keyword query andlist_drive_itemsis scoped to a single folder. There's currently no way to ask "show me what's been recently touched across my whole Drive, including files shared with me."Features
modifiedTime,viewedByMeTime,sharedWithMeTime, orcreatedTime(all descending)all(default),owned(my files only),shared(files others shared with me)doc,sheet,pdf, etc.)lastModifyingUser(name + email) andownersfor each filepage_tokensupportChanges
gdrive/drive_helpers.py: Added backward-compatibleorder_byparameter tobuild_drive_list_paramsgdrive/drive_tools.py: Addedlist_recent_filestool functionWhy
AI assistants using this MCP server often need to find recently modified files without knowing exact keywords — for example, finding team documents shared in the last few days, or seeing what collaborators have been working on. The
ownership: "shared"+sort_by: "modifiedTime"combination is particularly useful for discovering documents you didn't create but need to access.No new OAuth scopes or API dependencies — uses existing Drive API v3
files.listwithorderBy.Test plan
list_recent_fileswith default params — returns 20 most recently modified filesownership: "shared"— returns only files not owned by the callersort_by: "viewedByMeTime"— returns files sorted by last viewedfile_type: "doc"— returns only Google DocslastModifyingUserandownersfields appear in outputnextPageToken/page_tokensearch_drive_filesandlist_drive_itemsare unaffected byorder_byhelper change🤖 Generated with Claude Code
Summary by CodeRabbit