How to use Google Drive MCP server
Quick answer
Use the
mcp Python SDK to run a Google Drive MCP server that connects AI agents to Google Drive. This involves authenticating with Google OAuth, configuring the MCP server with Drive scopes, and running it to enable AI agents to access and manipulate Drive files via the MCP protocol.PREREQUISITES
Python 3.8+Google Cloud project with Drive API enabledGoogle OAuth 2.0 credentials (client ID and secret)pip install mcp google-auth google-auth-oauthlib google-api-python-client
Setup
Install the required Python packages and set up Google Cloud credentials for Drive API access.
- Enable Google Drive API in your Google Cloud Console.
- Create OAuth 2.0 Client ID credentials and download
credentials.json. - Install Python packages:
pip install mcp google-auth google-auth-oauthlib google-api-python-client Step by step
This example shows how to create and run a Google Drive MCP server using the mcp SDK. It authenticates with Google, starts the MCP server, and listens for AI agent requests to access Drive files.
import os
from mcp.server import Server
from mcp.server.stdio import stdio_server
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# Define the scopes for Google Drive access
SCOPES = ['https://www.googleapis.com/auth/drive.file']
# Authenticate and get credentials
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
def google_drive_handler(request):
"""Handle MCP requests to interact with Google Drive."""
service = build('drive', 'v3', credentials=creds)
# Example: list first 10 files
results = service.files().list(pageSize=10, fields="files(id, name)").execute()
files = results.get('files', [])
return {'files': files}
# Create MCP server instance
server = Server()
# Register the Google Drive handler for MCP requests
server.register_handler('google_drive', google_drive_handler)
# Run the MCP server using stdio transport
stdio_server(server) Common variations
You can run the MCP server asynchronously or use different transports like SSE instead of stdio. Also, adjust Google Drive scopes for broader access (e.g., full Drive access with https://www.googleapis.com/auth/drive).
Example async run snippet:
import asyncio
from mcp.server import Server
from mcp.server.sse import sse_server
async def main():
server = Server()
# Register handlers as needed
await sse_server(server, port=8080)
asyncio.run(main()) Troubleshooting
- If authentication fails, ensure your
credentials.jsonis valid and the Drive API is enabled. - If the MCP server does not respond, check that the transport (stdio or SSE) is correctly configured and the client is connected.
- For permission errors, verify the OAuth scopes match the Drive API operations you intend to perform.
Key Takeaways
- Use the
mcpPython SDK to create a Google Drive MCP server that connects AI agents to Drive. - Authenticate with Google OAuth 2.0 and enable Drive API with proper scopes for file access.
- Run the MCP server with stdio or SSE transport to handle AI agent requests seamlessly.