MCP Google Workspace Server

MCP Google Workspace Server

Exposes Gmail (send/draft) and Google Docs (append) capabilities as tools for any MCP-compliant agent.

Category
访问服务器

README

MCP Google Workspace Server

A Model Context Protocol (MCP) server that exposes Gmail and Google Docs capabilities as tools any MCP-compliant agent (Claude Desktop, Cursor, custom agents, etc.) can call.

It provides three tools:

Tool Description
send_gmail Send an email through Gmail on behalf of the authenticated account.
draft_gmail Create a Gmail draft without sending it.
append_to_google_doc Append text to the end of an existing Google Doc (never overwrites).

Both stdio and streamable HTTP transports are supported.


Architecture

The codebase is modular so each layer is independently testable:

  • MCP layersrc/server.ts, src/tools/*, src/transports/* (tool registration, validation, structured errors, transports).
  • Google service layersrc/google/gmailService.ts, src/google/docsService.ts, src/lib/mime.ts.
  • Auth / configsrc/auth/*, src/config.ts.
send_gmail / draft_gmail / append_to_google_doc
        │  (zod-validated input, JSON-Schema advertised)
        ▼
   MCP server (stdio | streamable HTTP)
        ▼
 Gmail / Docs services  ──►  Google APIs
        ▲
   OAuth2 client (auto-refresh)

Prerequisites

  • Node.js 18+ and npm.
  • A Google account (Gmail and/or Google Workspace).
  • A Google Cloud project.

1. Google Cloud Console setup

  1. Go to the Google Cloud Console and create (or select) a project.
  2. Enable APIs: APIs & Services → Library → enable Gmail API and Google Docs API.
  3. OAuth consent screen: configure it (External is fine for testing). Add your Google account under Test users.
  4. Add the required scopes (least privilege):
    • https://www.googleapis.com/auth/gmail.send
    • https://www.googleapis.com/auth/gmail.compose
    • https://www.googleapis.com/auth/documents
  5. Create credentials: APIs & Services → Credentials → Create Credentials → OAuth client ID → Desktop app.
  6. Add an authorized redirect URI that matches GOOGLE_OAUTH_REDIRECT (default http://localhost:3000/oauth2callback).
  7. Copy the Client ID and Client secret.

2. Install & configure

npm install
cp .env.example .env   # then edit .env

Fill in at least GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET. See .env.example for every variable.

Variable Required Purpose
GOOGLE_CLIENT_ID yes OAuth client ID
GOOGLE_CLIENT_SECRET yes OAuth client secret
GOOGLE_TOKEN_PATH no (default ./token.json) Where tokens are stored (git-ignored)
GOOGLE_OAUTH_REDIRECT no (default http://localhost:3000/oauth2callback) Consent-flow redirect URI
DEFAULT_SEND_AS no Default From address
MCP_TRANSPORT no (default stdio) stdio or http
MCP_HTTP_HOST / MCP_HTTP_PORT no HTTP bind address (defaults 127.0.0.1:3333)
MCP_HTTP_AUTH_TOKEN required for http Bearer token clients must present
LOG_LEVEL no debug/info/warn/error

3. Authorize (one time)

npm run auth

This opens the Google consent screen, captures the redirect, and stores a refresh token at GOOGLE_TOKEN_PATH. Tokens are refreshed transparently afterward. Re-run it if you change scopes or revoke access.


4. Build & run

npm run build      # compile TypeScript to dist/
npm start          # run the compiled server (uses MCP_TRANSPORT)
# or during development:
npm run dev

stdio transport (local/desktop agents)

Set MCP_TRANSPORT=stdio (default). The server communicates over stdin/stdout; all logs go to stderr.

streamable HTTP transport (remote agents)

MCP_TRANSPORT=http MCP_HTTP_AUTH_TOKEN=$(openssl rand -hex 32) npm start

The endpoint is http://<host>:<port>/mcp. Every request must include Authorization: Bearer <MCP_HTTP_AUTH_TOKEN>.


5. Connect an MCP client

See mcp-client-config.example.json. Example for a stdio client:

{
  "mcpServers": {
    "google-workspace": {
      "command": "node",
      "args": ["/absolute/path/to/dist/index.js"],
      "env": {
        "GOOGLE_CLIENT_ID": "your-client-id.apps.googleusercontent.com",
        "GOOGLE_CLIENT_SECRET": "your-client-secret",
        "GOOGLE_TOKEN_PATH": "/absolute/path/to/token.json",
        "MCP_TRANSPORT": "stdio"
      }
    }
  }
}

You can also inspect the server with the MCP Inspector:

npx @modelcontextprotocol/inspector node dist/index.js

Tool reference

send_gmail

Input: to: string[] (req), subject: string (req), body: string (req), body_type: "text"|"html" (default text), cc?: string[], bcc?: string[], reply_to?: string. Output: { "message_id": string, "thread_id": string, "status": "sent" }

draft_gmail

Input: same as send_gmail. Output: { "draft_id": string, "message_id": string, "status": "drafted" }

append_to_google_doc

Input: document_id: string (req; a raw ID or a full docs URL), content: string (req), add_newline_before: boolean (default true). Output: { "document_id": string, "status": "appended" }


Authentication model & tradeoffs

This server uses OAuth2 user-delegated auth (Approach 1 in the problem statement):

  • Each user authorizes once via the standard consent flow; the server stores and auto-refreshes the refresh token.
  • Works for any Gmail/Workspace account with no admin setup — the best fit for an agent-agnostic server that may act for arbitrary users.

Alternative — service account with domain-wide delegation (not used here):

  • Suited to a single Workspace domain.
  • A plain service account cannot send Gmail as arbitrary users; it needs domain-wide delegation configured by a Workspace admin. For Docs, the target document must be shared with the service account's email.

To switch to a service account, replace getAuthorizedClient in src/auth/googleAuth.ts with a JWT client using domain-wide delegation (subject = impersonated user); the rest of the code is unchanged.


Error handling

Tools never crash the connection; they return structured MCP tool errors (isError: true with a machine-readable error.code). Codes include: INVALID_INPUT, DOCUMENT_NOT_FOUND, INSUFFICIENT_SCOPE, CREDENTIALS_MISSING, RATE_LIMITED, NETWORK_ERROR, GOOGLE_API_ERROR. Inputs are validated against the schema before any Google API call.


Security

  • Least-privilege scopes only.
  • Secrets loaded from env; .env and token.json are git-ignored. Never hard-coded.
  • The logger redacts recipients and never logs email bodies or document contents.
  • Email addresses and document IDs are validated/sanitized; MIME header injection is prevented.
  • The HTTP transport requires a bearer token and binds to 127.0.0.1 by default.

Testing

npm test

Unit tests mock the Google layer and cover each tool's success and error branches, MIME construction, and the Google→MCP error mapping.


Future extensions

Rich-text Docs formatting, email attachments/inline images, Gmail read/reply, and new tools (create Doc, insert at index, Sheets/Calendar) can be added as new modules under src/tools/ and src/google/ without changing existing tool contracts.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选