A comprehensive Cloudflare Worker implementation that provides three powerful interfaces for interacting with Google Workspace (Docs, Drive, Sheets) designed specifically for AI agents and automation tools.
- REST API - Standard HTTP endpoints for Google Docs operations
- MCP Protocol - Model Context Protocol (JSON-RPC 2.0) for AI agents
- WebSocket API - Real-time MCP protocol over WebSocket
- β Full Google Docs API proxy with authentication
- β Model Context Protocol (MCP) support for AI agents
- β WebSocket support for real-time communication
- β Markdown to Google Docs conversion
- β Document structure inspection and manipulation
- β Batch updates with transaction support
- β OAuth2 authentication flow
- β Dynamic OpenAPI 3.1 specification
- β Swagger UI and Scalar API documentation
- β Zod validation for type safety
# Clone the repository
git clone <repository-url>
cd google-docs-cfworker
# Install dependencies
npm install
# Configure secrets
npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET
# Run locally
npm run dev
# Deploy to Cloudflare
npm run deployThe worker uses OAuth2 authentication with Google:
- Authorization URL:
https://your-worker.workers.dev/auth2/v2/auth - Token URL:
https://your-worker.workers.dev/auth2/token - Scope:
https://www.googleapis.com/auth/drive
Access the interactive API documentation:
- Scalar UI (recommended):
https://your-worker.workers.dev/docs - Swagger UI:
https://your-worker.workers.dev/swagger/public.yaml - OpenAPI Spec:
https://your-worker.workers.dev/openapi.json
# Get document structure
GET /v1/documents/{documentId}/structure
# Apply batch updates
POST /v1/documents/{documentId}/batchUpdate
# Insert markdown content
POST /v1/documents/{documentId}/markdown/insert
# Delete content range
POST /v1/documents/{documentId}/deleteContentRangeRequest# List available tools
POST /mcp
Content-Type: application/json
Authorization: Bearer YOUR_TOKEN
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}
# Execute a tool
POST /mcp
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "google_docs_structure",
"arguments": {
"documentId": "YOUR_DOC_ID"
}
}
}const ws = new WebSocket('wss://your-worker.workers.dev/mcp/ws');
ws.send(JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'tools/list'
}));- google_docs_structure - Inspect document structure and metadata
- google_docs_batch_update - Apply multiple updates in a single operation
- google_docs_markdown_insert - Insert markdown-formatted content
- google_docs_delete_content_range - Delete content ranges
- google_docs_create - Create new Google Docs documents
- google_drive_search - Search for files using query syntax
- google_drive_get_file - Get file or folder metadata
- google_drive_create_folder - Create new folders
- google_drive_move_file - Move files or folders to different locations
- google_drive_delete_file - Delete files or folders (moves to trash)
- google_sheets_get - Get spreadsheet metadata and structure
- google_sheets_get_values - Read cell values from ranges
- google_sheets_update_values - Update cell values in ranges
- google_sheets_append_values - Append rows to tables
- google_sheets_create - Create new spreadsheets
- google_sheets_batch_update - Apply multiple formatting/structure updates
See MCP Documentation for detailed usage.
src/
βββ index.ts # Main Hono router with all endpoints
βββ auth.ts # OAuth2 authentication flow
βββ services/
β βββ GoogleApiClient.ts # Unified Google API client (Docs, Drive, Sheets)
βββ docs/
β βββ routes.ts # REST API routes for Docs
β βββ apis/ # API implementations
βββ mcp/
β βββ schemas.ts # JSON-RPC 2.0 Zod schemas
β βββ tools.ts # All 16 MCP tool definitions
β βββ handler.ts # MCP request handler for all tools
β βββ websocket.ts # WebSocket protocol support
βββ openapi/
β βββ spec.ts # Dynamic OpenAPI 3.1 generator
β βββ scalar.ts # Scalar API documentation UI
β βββ swagger-ui.ts # Swagger UI (legacy)
βββ utils/ # Utility functions
curl -X POST https://your-worker.workers.dev/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "google_docs_create",
"arguments": {
"title": "My New Document"
}
}
}'curl -X POST https://your-worker.workers.dev/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "google_drive_search",
"arguments": {
"query": "name contains '\''report'\'' and mimeType='\''application/pdf'\''",
"pageSize": 10
}
}
}'curl -X POST https://your-worker.workers.dev/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "google_sheets_update_values",
"arguments": {
"spreadsheetId": "YOUR_SHEET_ID",
"range": "Sheet1!A1:C3",
"values": [
["Name", "Age", "City"],
["John", 30, "New York"],
["Jane", 25, "London"]
]
}
}
}'Configure these secrets using Wrangler:
# Required
npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET# Run tests
npm test
# Run in development mode
npm run dev# Deploy to production
npm run deploy
# Deploy with minification
npm run deployContributions are welcome! Please see the contributing guidelines for more information.
Apache 2.0 - See LICENSE file for details.