Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

34 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Google Workspace MCP - Cloudflare Worker

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.

πŸš€ Features

Triple Interface Architecture

  1. REST API - Standard HTTP endpoints for Google Docs operations
  2. MCP Protocol - Model Context Protocol (JSON-RPC 2.0) for AI agents
  3. WebSocket API - Real-time MCP protocol over WebSocket

Key Capabilities

  • βœ… 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

πŸ“š Quick Start

1. Setup

# 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 deploy

2. Authentication

The 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

3. API Documentation

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

πŸ”§ API Endpoints

REST API

Document Operations

# 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

MCP Protocol

HTTP Endpoint

# 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"
    }
  }
}

WebSocket Endpoint

const ws = new WebSocket('wss://your-worker.workers.dev/mcp/ws');

ws.send(JSON.stringify({
  jsonrpc: '2.0',
  id: 1,
  method: 'tools/list'
}));

πŸ› οΈ Available MCP Tools

Google Docs (5 tools)

  1. google_docs_structure - Inspect document structure and metadata
  2. google_docs_batch_update - Apply multiple updates in a single operation
  3. google_docs_markdown_insert - Insert markdown-formatted content
  4. google_docs_delete_content_range - Delete content ranges
  5. google_docs_create - Create new Google Docs documents

Google Drive (5 tools)

  1. google_drive_search - Search for files using query syntax
  2. google_drive_get_file - Get file or folder metadata
  3. google_drive_create_folder - Create new folders
  4. google_drive_move_file - Move files or folders to different locations
  5. google_drive_delete_file - Delete files or folders (moves to trash)

Google Sheets (6 tools)

  1. google_sheets_get - Get spreadsheet metadata and structure
  2. google_sheets_get_values - Read cell values from ranges
  3. google_sheets_update_values - Update cell values in ranges
  4. google_sheets_append_values - Append rows to tables
  5. google_sheets_create - Create new spreadsheets
  6. google_sheets_batch_update - Apply multiple formatting/structure updates

See MCP Documentation for detailed usage.

πŸ—οΈ Architecture

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

πŸ“– Examples

Google Docs: Create a 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": 1,
    "method": "tools/call",
    "params": {
      "name": "google_docs_create",
      "arguments": {
        "title": "My New Document"
      }
    }
  }'

Google Drive: Search for Files

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
      }
    }
  }'

Google Sheets: Update Values

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"]
        ]
      }
    }
  }'

πŸ” Environment Variables

Configure these secrets using Wrangler:

# Required
npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET

πŸ§ͺ Testing

# Run tests
npm test

# Run in development mode
npm run dev

πŸ“¦ Deployment

# Deploy to production
npm run deploy

# Deploy with minification
npm run deploy

🀝 Contributing

Contributions are welcome! Please see the contributing guidelines for more information.

πŸ“„ License

Apache 2.0 - See LICENSE file for details.

πŸ”— Resources

About

Google Docs Cloudflare Worker for Chat Agents

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages