Google MCP Server

Google MCP Server

Enables appending to Google Docs and creating Gmail drafts with OAuth 2.0 authentication and an approval gate that requires manual confirmation before each action.

Category
访问服务器

README

Google MCP Server

A FastAPI server that integrates with Google Docs and Gmail. Every action requires an explicit confirmation in the request before it runs, so nothing happens by accident — and this works both locally and when deployed.

Features

  • Append to Google Doc — append text to any document you have access to
  • Create Gmail draft — create a draft email without sending it
  • OAuth 2.0 — credentials are saved locally after the first login
  • Approval gate — every action requires "confirm": true in the request body. Without it, the server returns HTTP 428 with a preview and does nothing.
  • API-key auth — when API_SECRET_KEY is set, requests must include an X-API-Key header.

Project Structure

google-mcp-server/
├── server.py          → FastAPI app with tool endpoints
├── auth.py            → Google OAuth authentication
├── docs_tool.py       → Google Docs tool (append content)
├── gmail_tool.py      → Gmail tool (create draft)
├── requirements.txt   → All dependencies
├── README.md          → Setup and usage instructions
├── credentials.json   → (NOT committed — downloaded from Google Cloud)
└── token.json         → (NOT committed — auto-generated after OAuth)

Prerequisites

  • Python 3.10+
  • A Google Cloud project with the Google Docs API and Gmail API enabled

Google Cloud Setup

  1. Go to Google Cloud Console.
  2. Create a project (or select an existing one).
  3. Enable these APIs:
  4. Go to APIs & Services → Credentials.
  5. Click Create Credentials → OAuth client ID.
  6. Choose Desktop app as the application type.
  7. Download the JSON file and save it as credentials.json in this directory.

Installation

cd google-mcp-server
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt

First-Time Authentication

On the first API call (or when you run the server and trigger an endpoint), a browser window opens for Google sign-in. After you approve, a token.json file is created automatically. Subsequent runs reuse that token and skip the browser flow.

To re-authenticate, delete token.json and restart the server.

Running the Server

python server.py

Or with uvicorn directly:

uvicorn server:app --host 0.0.0.0 --port 8000

The server listens on http://localhost:8000. Interactive API docs are at http://localhost:8000/docs.

Approval model

Approval has two interchangeable modes, selected by the REQUIRE_TERMINAL_APPROVAL environment variable:

  • REQUIRE_TERMINAL_APPROVAL=true (local, foreground terminal) → the server prints the action and payload and prompts Approve? (y/n) in the terminal. Answering anything but y returns HTTP 403.
  • REQUIRE_TERMINAL_APPROVAL=false (default; required when deployed) → the caller must include "confirm": true in the request body. Without it, the server returns HTTP 428 (Precondition Required) with a preview and executes nothing.

The headless confirm mode exists because a deployed server (e.g. on Railway) has no terminal to prompt in. See DEPLOYMENT_PLAN.md for details.

API Endpoints

If API_SECRET_KEY is configured, add -H "X-API-Key: YOUR_API_KEY" to every request below.

POST /append_to_doc

Append text to a Google Doc.

Request body:

{
  "doc_id": "YOUR_GOOGLE_DOC_ID",
  "content": "Text to append at the end of the document.\n",
  "confirm": true
}

The doc_id is the string between /d/ and /edit in the document URL:

https://docs.google.com/document/d/ABC123xyz/edit
                                      ^^^^^^^^^^
                                      doc_id

Example:

curl -X POST http://localhost:8000/append_to_doc \
  -H "Content-Type: application/json" \
  -d '{"doc_id": "ABC123xyz", "content": "Hello from the MCP server!\n", "confirm": true}'

If you omit "confirm": true, the server responds with HTTP 428 and a preview instead of running the action:

{
  "detail": {
    "message": "Confirmation required for 'append_to_doc'. Resend the same request with \"confirm\": true to proceed.",
    "action": "append_to_doc",
    "payload": {"doc_id": "ABC123xyz", "content": "Hello from the MCP server!\n"}
  }
}

POST /create_email_draft

Create a Gmail draft.

Request body:

{
  "to": "recipient@example.com",
  "subject": "Draft subject line",
  "body": "Plain-text body of the email.",
  "confirm": true
}

Example:

curl -X POST http://localhost:8000/create_email_draft \
  -H "Content-Type: application/json" \
  -d '{"to": "recipient@example.com", "subject": "Hello", "body": "This is a draft.", "confirm": true}'

The same confirm approval applies before the draft is created.

OAuth Scopes

Scope Purpose
https://www.googleapis.com/auth/documents Read and edit Google Docs
https://www.googleapis.com/auth/gmail.compose Create and manage Gmail drafts

Security Notes

  • Never commit credentials.json or token.json — both are listed in .gitignore.
  • Every action requires "confirm": true; requests without it return HTTP 428 and execute nothing.
  • Set API_SECRET_KEY in any shared/deployed environment so requests must carry a valid X-API-Key header.

Troubleshooting

Issue Fix
Missing credentials.json Download OAuth credentials from Google Cloud and place the file in this directory.
403 Access Not Configured Enable the Docs and Gmail APIs in your Google Cloud project.
Token expired / invalid Delete token.json and re-authenticate.
Doc not found Confirm the doc ID and that the signed-in account has edit access.
428 Precondition Required Add "confirm": true to the request body to approve the action.
401 Invalid or missing API key Include the X-API-Key header matching the server's API_SECRET_KEY.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选