google-docs-mcp

repository·main·Indexed 19 days ago

https://github.com/a-bonus/google-docs-mcp

An MCP (Model Context Protocol) server that provides AI clients, such as Claude Desktop or Cursor, with access to Google Docs, Sheets, Drive, Gmail, and Google Calendar. It includes comprehensive tools for content manipulation, spreadsheet data operations, file management, email automation, and calendar event scheduling.

Tokens
26.8K
Snippets
63
Records
109
Agent score
69%

What's inside @a-bonus/google-docs-mcp

  1. Use utility tools for Markdown orchestration

    main
    The Utils package provides higher-level tools designed to orchestrate multiple API operations. Instead of single API calls, these tools combine parsing, content manipulation, and batch updates to support common workflows, specifically focusing on converting and applying Markdown formatting to Google Docs.
  2. Manage files and folders in Google Drive

    main

    The Google Drive toolset allows you to perform various management tasks within Google Drive, including searching, creating, organizing, and deleting documents and folders. You can use these tools to list contents, retrieve metadata, move files between folders, rename items, or create new documents from templates.

    | Tool                         | Description                                                                  |
    | ---------------------------- | ---------------------------------------------------------------------------- |
    | `listDocuments`              | Lists Google Documents in your Drive, optionally filtered by name or content |
    | `searchDocuments`            | Searches for documents by name, content, or both                             |
    | `getDocumentInfo`            | Gets metadata about a document (owner, sharing, modification history)        |
    | `createDocument`             | Creates a new empty Google Document                                          |
    | `createDocumentFromTemplate` | Creates a new document by copying a template with placeholder replacements   |
    | `createFolder`               | Creates a new folder in Google Drive                                         |
    | `listFolderContents`         | Lists files and subfolders within a Drive folder                             |
    | `getFolderInfo`              | Gets metadata about a Drive folder                                           |
    | `moveFile`                   | Moves a file or folder to a different Drive folder                           |
    | `copyFile`                   | Creates a copy of a file or document                                         |
    | `renameFile`                 | Renames a file or folder                                                     |
    | `deleteFile`                 | Moves a file or folder to the trash, or permanently deletes it               |
  3. Specify Calendar IDs

    main

    When using Calendar tools, the calendarId parameter defaults to "primary", which refers to the user's main calendar. To interact with shared or secondary calendars, you must provide the specific calendar ID.

    Examples of Calendar ID formats:

    • Email addresses: username@example.com
    • Group/Secondary calendars: c_abc123def456@group.calendar.google.com
  4. Use RichCellContent for structured cell data

    main

    Standard Markdown is insufficient for complex table cells. The RichCellContent model allows for structured, multi-line content within a single cell, supporting:

    • Paragraphs: Text blocks with individual TextRun styling (bold, italic, underline, color, links).
    • Bulleted Lists: Items containing multiple text runs.
    • Numbered Lists: Ordered sequences of text runs.

    This enables patterns like a bold label followed by a bulleted list within one cell.

    type RichCellContent = {
      blocks: Array<
        | { type: 'paragraph'; runs: TextRun[]; }
        | { type: 'bulletedList'; items: Array<{ runs: TextRun[] }>; }
        | { type: 'numberedList'; items: Array<{ runs: TextRun[] }>; }
      >;
    };
    
    type TextRun = {
      text: string;
      bold?: boolean;
      italic?: boolean;
      underline?: boolean;
      foregroundColor?: string;
      linkUrl?: string;
    };
  5. Identify and target tables using stable identifiers

    main

    Because Google Docs does not expose user-friendly table IDs directly, the MCP layer uses stable identifiers to target specific tables. These identifiers are generated using a combination of the table's start index, its ordinal position in a tab, and optional nearby heading anchors.

    Identifier Formats:

    • table:t.0:3 (Index and position)
    • table:t.0:heading-NAME:0 (Index, position, and heading anchor)

    Use these identifiers when calling tools that require a specific table target to ensure your operations (like row replacement or styling) hit the correct element.

  6. Design principles for rich Google Docs formatting

    main

    When working with complex Google Docs (like planning documents), avoid using full-document Markdown replacement. Markdown is insufficient for rich features like tables, smart chips, and specific cell styling.

    Instead, follow a template-aware, table-aware, chip-aware, and patch-based architecture. This ensures that updates to specific sections (like a task table) do not corrupt the rest of the document's formatting or structure.

  7. Understand the MCP Tool Architecture

    main

    The server organizes its Model Context Protocol (MCP) tools into domain-specific modules. A top-level router (tools/index.ts) delegates tool registration to individual domain routers. Each domain (e.g., docs, drive, gmail) acts as a sub-module containing its own tools, documentation, and a router that registers those tools with the main server.

    Directory Structure:

    • tools/index.ts: Top-level router.
    • tools/[domain]/index.ts: Domain-specific router.
    • tools/[domain]/[toolName].ts: Individual tool implementation.
    • tools/utils/: Cross-cutting workflow utilities.
  8. Configure Google Cloud for Google Docs MCP Server

    main

    Follow these steps to set up your Google Cloud project. Note that for remote deployment, you must create an OAuth client of type Web application. For local stdio usage, use Desktop app.

    1. Project and API Setup

    1. Go to Google Cloud Console.
    2. Create a new project (e.g., "MCP Docs Server").
    3. Navigate to APIs & Services > Library and enable:
      • Google Docs API
      • Google Sheets API
      • Google Drive API
      • Gmail API
      • Google Calendar API
    1. Go to APIs & Services > OAuth consent screen.
    2. Choose External and click CREATE.
    3. Provide an App name, User support email, and Developer contact email.
    4. Add the following scopes: documents, spreadsheets, drive, gmail.modify, calendar.events.
    5. Add your Google email address as a Test User.

    3. Create Credentials

    1. Go to APIs & Services > Credentials.
    2. Click + CREATE CREDENTIALS > OAuth client ID.
    3. Select the appropriate Application type (Web application for remote, Desktop app for local).
    4. Copy the generated Client ID and Client Secret for your MCP configuration.
  9. Apply table presentation and styling

    main

    To recreate the visual structure of professional planning documents (e.g., shaded headers, specific column widths), use the presentation control tools.

    Available Styling Tools:

    • updateTableCellStyle: Sets cell background, alignment, or text styles.
    • updateTableBorders: Modifies border styles.
    • updateTableColumnWidth: Adjusts the width of specific columns.
    • updateTableRowStyle: Modifies row height or background.

    Common Use Cases:

    • Shading a header row (e.g., blue background).
    • Setting a narrow width for a No. column.
    • Setting a wide width for a メモ / 相談 (Memo/Consultation) column.
  10. Create a Google Cloud OAuth Client

    main

    To use this MCP server, you must first set up credentials in the Google Cloud Console:

    1. Go to the Google Cloud Console.
    2. Create or select a project.
    3. Enable the following APIs: Google Docs API, Google Sheets API, Google Drive API, Gmail API, and Google Calendar API.
    4. Configure the OAuth consent screen:
      • Set user type to External.
      • Add your email as a test user.
      • Add the following scopes: gmail.modify, calendar.events, and the standard Docs/Sheets/Drive scopes.
    5. Create an OAuth client ID using the Desktop app type.
    6. Save the Client ID and Client Secret provided.
  11. Run live integration tests for Google Docs

    main

    The repository includes an opt-in live integration test for cloneTable against the real Google Docs API. This test creates temporary source/target Google Docs, verifies the functionality, and then deletes them.

    Requirements:

    • Valid GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET.
    • An authorized token stored via npx -y @a-bonus/google-docs-mcp auth.

    Command:

    GOOGLE_DOCS_LIVE_TESTS=1 npm run test:live:docs