search-examples.js•7.75 kB
#!/usr/bin/env node
/**
* Google Drive MCP Server - Search Examples
*
* This file demonstrates various ways to use the drive_search_files tool
* with different search parameters and filters.
*/
import { GoogleDriveMCPClient } from '../mcp-clients/generic-client.js';
async function searchExamples() {
const client = new GoogleDriveMCPClient('/path/to/google-drive-mcp-server/dist/index.js', {
GOOGLE_CLIENT_ID: 'your-client-id.googleusercontent.com',
GOOGLE_CLIENT_SECRET: 'your-client-secret'
});
try {
await client.connect();
console.log('=== Google Drive Search Examples ===\n');
// Example 1: Basic text search
console.log('1. Basic text search for "presentation"');
const basicSearch = await client.searchFiles('presentation');
console.log(`Found ${basicSearch.files?.length || 0} files`);
if (basicSearch.files?.length > 0) {
console.log(`First result: ${basicSearch.files[0].name}`);
}
console.log();
// Example 2: Search with file type filter
console.log('2. Search for PDFs containing "report"');
const pdfSearch = await client.searchFiles('report', {
fileType: ['application/pdf']
});
console.log(`Found ${pdfSearch.files?.length || 0} PDF files`);
console.log();
// Example 3: Search with date range
console.log('3. Search for files modified in the last 30 days');
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const recentSearch = await client.searchFiles('*', {
modifiedAfter: thirtyDaysAgo.toISOString().split('T')[0],
limit: 10
});
console.log(`Found ${recentSearch.files?.length || 0} recent files`);
console.log();
// Example 4: Search in specific folder
console.log('4. Search in specific folder (using root as example)');
const folderSearch = await client.searchFiles('document', {
folderId: 'root',
limit: 5
});
console.log(`Found ${folderSearch.files?.length || 0} documents in root folder`);
console.log();
// Example 5: Advanced search with multiple filters
console.log('5. Advanced search: Google Docs modified this year, ordered by date');
const currentYear = new Date().getFullYear();
const advancedSearch = await client.searchFiles('*', {
fileType: ['application/vnd.google-apps.document'],
modifiedAfter: `${currentYear}-01-01`,
orderBy: 'modifiedTime',
limit: 15
});
console.log(`Found ${advancedSearch.files?.length || 0} Google Docs from this year`);
if (advancedSearch.files?.length > 0) {
console.log('Most recent documents:');
advancedSearch.files.slice(0, 3).forEach((file, index) => {
console.log(` ${index + 1}. ${file.name} (${new Date(file.modifiedTime).toLocaleDateString()})`);
});
}
console.log();
// Example 6: Search by owner
console.log('6. Search for files owned by specific user (using "me" as example)');
const ownerSearch = await client.searchFiles('*', {
owner: 'me',
limit: 10
});
console.log(`Found ${ownerSearch.files?.length || 0} files owned by you`);
console.log();
// Example 7: Search for specific file types
console.log('7. Search for presentations and spreadsheets');
const officeSearch = await client.searchFiles('*', {
fileType: [
'application/vnd.google-apps.presentation',
'application/vnd.google-apps.spreadsheet'
],
limit: 10
});
console.log(`Found ${officeSearch.files?.length || 0} presentations and spreadsheets`);
console.log();
// Example 8: Complex query with multiple keywords
console.log('8. Complex search: "budget" OR "financial" in title');
const complexSearch = await client.searchFiles('budget OR financial', {
limit: 10
});
console.log(`Found ${complexSearch.files?.length || 0} files with budget or financial in title`);
console.log();
// Example 9: Search with size constraints (if supported)
console.log('9. Search for large files (>10MB)');
const largeFileSearch = await client.searchFiles('*', {
minSize: 10 * 1024 * 1024, // 10MB
limit: 5
});
console.log(`Found ${largeFileSearch.files?.length || 0} large files`);
console.log();
// Example 10: Search and display detailed results
console.log('10. Detailed search results with metadata');
const detailedSearch = await client.searchFiles('meeting', {
limit: 3
});
if (detailedSearch.files?.length > 0) {
console.log('Detailed results:');
for (const [index, file] of detailedSearch.files.entries()) {
console.log(`\n File ${index + 1}:`);
console.log(` Name: ${file.name}`);
console.log(` ID: ${file.id}`);
console.log(` Type: ${file.mimeType}`);
console.log(` Size: ${formatFileSize(file.size)}`);
console.log(` Modified: ${new Date(file.modifiedTime).toLocaleString()}`);
console.log(` Owner: ${file.owners?.[0]?.displayName || 'Unknown'}`);
console.log(` Link: ${file.webViewLink}`);
}
}
} catch (error) {
console.error('Search examples error:', error.message);
} finally {
await client.disconnect();
}
}
function formatFileSize(bytes) {
if (!bytes) return 'Unknown';
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
}
// Advanced search patterns
async function advancedSearchPatterns() {
const client = new GoogleDriveMCPClient('/path/to/google-drive-mcp-server/dist/index.js', {
GOOGLE_CLIENT_ID: 'your-client-id.googleusercontent.com',
GOOGLE_CLIENT_SECRET: 'your-client-secret'
});
try {
await client.connect();
console.log('\n=== Advanced Search Patterns ===\n');
// Pattern 1: Find documents by specific keywords in content
console.log('Pattern 1: Content-based search');
const contentSearch = await client.searchFiles('fullText:"quarterly results"', {
limit: 5
});
console.log(`Found ${contentSearch.files?.length || 0} files containing "quarterly results"`);
// Pattern 2: Find recently shared files
console.log('\nPattern 2: Recently shared files');
const sharedSearch = await client.searchFiles('*', {
sharedWithMe: true,
modifiedAfter: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0], // Last 7 days
limit: 10
});
console.log(`Found ${sharedSearch.files?.length || 0} recently shared files`);
// Pattern 3: Find files by extension
console.log('\nPattern 3: Search by file extension');
const extensionSearch = await client.searchFiles('name:*.xlsx OR name:*.xls', {
limit: 10
});
console.log(`Found ${extensionSearch.files?.length || 0} Excel files`);
// Pattern 4: Find starred files
console.log('\nPattern 4: Starred files');
const starredSearch = await client.searchFiles('starred:true', {
limit: 10
});
console.log(`Found ${starredSearch.files?.length || 0} starred files`);
// Pattern 5: Find files in trash
console.log('\nPattern 5: Files in trash');
const trashSearch = await client.searchFiles('trashed:true', {
limit: 5
});
console.log(`Found ${trashSearch.files?.length || 0} files in trash`);
} catch (error) {
console.error('Advanced search patterns error:', error.message);
} finally {
await client.disconnect();
}
}
// Run examples
if (import.meta.url === `file://${process.argv[1]}`) {
(async () => {
await searchExamples();
await advancedSearchPatterns();
})().catch(console.error);
}