generic-client.js•4.26 kB
#!/usr/bin/env node
/**
* Generic MCP Client Example for Google Drive MCP Server
*
* This example demonstrates how to connect to and use the Google Drive MCP Server
* from a generic MCP client application.
*/
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
class GoogleDriveMCPClient {
constructor(serverPath, env = {}) {
this.serverPath = serverPath;
this.env = env;
this.client = null;
this.transport = null;
}
async connect() {
// Create transport
this.transport = new StdioClientTransport({
command: 'node',
args: [this.serverPath],
env: {
...process.env,
...this.env
}
});
// Create client
this.client = new Client({
name: "google-drive-example-client",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
// Connect to server
await this.client.connect(this.transport);
console.log('Connected to Google Drive MCP Server');
// List available tools
const tools = await this.client.listTools();
console.log('Available tools:', tools.tools.map(t => t.name));
return this.client;
}
async disconnect() {
if (this.client) {
await this.client.close();
}
}
async searchFiles(query, options = {}) {
return await this.client.callTool('drive_search_files', {
query,
...options
});
}
async getFile(fileId, includeContent = true) {
return await this.client.callTool('drive_get_file', {
fileId,
includeContent
});
}
async getContentChunk(fileId, options = {}) {
return await this.client.callTool('drive_get_content_chunk', {
fileId,
...options
});
}
async getFileMetadata(fileId, includePermissions = false) {
return await this.client.callTool('drive_get_file_metadata', {
fileId,
includePermissions
});
}
async listFolder(folderId, options = {}) {
return await this.client.callTool('drive_list_folder', {
folderId,
...options
});
}
async getComments(fileId, options = {}) {
return await this.client.callTool('drive_get_comments', {
fileId,
...options
});
}
}
// Example usage
async function main() {
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',
LOG_LEVEL: 'info'
});
try {
await client.connect();
// Example 1: Search for files
console.log('\n--- Searching for files ---');
const searchResults = await client.searchFiles('presentation', {
limit: 5
});
console.log(`Found ${searchResults.files?.length || 0} files`);
if (searchResults.files && searchResults.files.length > 0) {
const firstFile = searchResults.files[0];
console.log(`First file: ${firstFile.name} (${firstFile.id})`);
// Example 2: Get file metadata
console.log('\n--- Getting file metadata ---');
const metadata = await client.getFileMetadata(firstFile.id, true);
console.log(`File size: ${metadata.size} bytes`);
console.log(`Modified: ${metadata.modifiedTime}`);
console.log(`Owner: ${metadata.owners?.[0]?.displayName || 'Unknown'}`);
// Example 3: Get content chunk
console.log('\n--- Getting content chunk ---');
const chunk = await client.getContentChunk(firstFile.id, {
startChar: 0,
endChar: 1000
});
console.log(`Content preview (first 1000 chars):`);
console.log(chunk.content?.substring(0, 200) + '...');
}
// Example 4: List folder contents (using root folder)
console.log('\n--- Listing root folder ---');
const folderContents = await client.listFolder('root', {
limit: 10
});
console.log(`Root folder contains ${folderContents.files?.length || 0} items`);
} catch (error) {
console.error('Error:', error.message);
} finally {
await client.disconnect();
}
}
// Run the example
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}
export { GoogleDriveMCPClient };