@a1-x-tech/mcp-google-auth

中文文档
0.1.2发布于 2026/11/05MITTypeScriptES Module近 30 天下载 2580

Reusable Google OAuth onboarding for the mcp-google-* MCP server line: in-chat login tools (loopback + PKCE) and a per-request TokenProvider.

原始使用文档
文档目录6 节
  1. Usage
  2. Registered tools
  3. Credential sources
  4. Security invariants (each pinned by a test)
  5. Development
  6. License

@a1-x-tech/mcp-google-auth

Reusable Google OAuth onboarding for the mcp-google-* MCP server line. One package gives every server the in-chat login of the Yandex line — adapted to Google's rules: a loopback listener on 127.0.0.1 + PKCE (S256) instead of the dead OOB flow, a user-owned Desktop OAuth client (personal-use exemption — no verification, no CASA), and a set_client path so the client_secret never passes through the chat.

This is a library, not a server: it registers six MCP tools on your McpServer and hands back a TokenProvider your API client calls per request.

Usage

import { registerGoogleAuth, unconfiguredPrefix } from "@a1-x-tech/mcp-google-auth";

const options = {
  serverName: "calendar",                 // → ~/.config/mcp-google-calendar/credentials.json
  envPrefix: "GOOGLE_CALENDAR",           // → GOOGLE_CALENDAR_REFRESH_TOKEN, …
  scopes: ["https://www.googleapis.com/auth/calendar.events"],
  // verifyIdentity?: async (accessToken) => ({ email }),  // default: OIDC userinfo
  // defaultClient?: { clientId, clientSecret? },           // built-in A1 client slot
  // finishWaitMs?: 20_000,                                  // finish_login wait for the callback
  // fail?: (err) => CallToolResult,                       // your error renderer (telemetry tag); default `Error: <message>`
};

const provider = registerGoogleAuth(server, options);

// In your API client, per request:
const token = await provider.getAccessToken();
// On a 401 from the API — one re-mint + replay:
const fresh = await provider.getAccessToken(true);
// Before a tool that needs a specific scope (partial grants are stored, not rejected):
provider.assertScopes(["https://www.googleapis.com/auth/calendar.events"]);

// At startup, when provider.hasToken() is false, prepend to the initialize instructions:
const instructions = unconfiguredPrefix(options) + REGULAR_BRIEFING;

The component always adds openid + userinfo.email (non-sensitive) to scopes — that is how finish_login names the account even for APIs without an identity call.

Registered tools

ToolHintPurpose
auth_statusread-onlyConnection state, token source, expiry, granted/missing scopes, paths. No network, no token in the output.
setup_instructionsread-onlyGCP wizard checklist (project → API → consent screen → Publish app → Desktop client → Download JSON). Shortens when a client already exists.
set_clientwriteReads the downloaded client_secret_*.json by path, validates type installed, stores it in the shared client.json (0600). The secret never enters the chat.
start_loginwrite, deliberately not read-onlyMints PKCE + state, binds a one-shot listener on 127.0.0.1 (random port or <PREFIX>_OAUTH_PORT), returns authorizeUrl + nextStep. Does not open the browser. A read-only hint would let a prompt injection start an OAuth flow silently.
finish_loginwriteWaits up to 20 s for the browser callback, awaits the local code exchange, saves tokens atomically, verifies identity (server callback or userinfo), reports accountEmail, grantedScopes, missingScopes, previousAccountEmail on account change.
logoutdestructiveRevokes at oauth2.googleapis.com/revoke, deletes the file, reports envTokenStillSet.

Credential sources

OAuth client (shared by the whole line), priority order:

  1. <PREFIX>_CLIENT_ID / <PREFIX>_CLIENT_SECRET (env override, e.g. CI)
  2. ~/.config/mcp-google-auth/client.json — written by set_client
  3. options.defaultClient — the built-in slot for the verified A1 client

The code exchange first runs without client_secret; if Google answers "client_secret is missing", it retries with the secret when one is on file, otherwise fails with set_client advice.

Tokens (per server), priority order:

  1. <PREFIX>_REFRESH_TOKEN (+ client) — minted in memory, never written to disk; <PREFIX>_ACCESS_TOKEN as a static testing alternative
  2. ~/.config/mcp-google-<serverName>/credentials.json — written by finish_login, re-read on every call (a mid-session login needs no restart)

Paths honor $XDG_CONFIG_HOME; on Windows the base is %APPDATA% (profile ACLs replace the 0600/0700 modes, renames retry on antivirus EPERM). Writes are atomic (temp file + rename); a broken file reads as "not connected".

Refresh: 60 s leeway, concurrent refreshes deduplicated, a rotated refresh_token is persisted immediately.

Security invariants (each pinned by a test)

  • No tool output or error ever contains access_token / refresh_token / client_secret / id_token (sentinel grep over the full test transcript).
  • The PKCE verifier never leaves the process; state is compared exactly; the listener binds 127.0.0.1 only, is one-shot, and the pending login dies after 10 minutes. The success page is static — no reflection, no external resources.
  • Missing credentials raise AuthRequiredError before any fetch; the message names both fixes (start_login, env variables).
  • OAuth failures map to actionable advice (redirect_uri_mismatch, access_denied, accessNotConfigured, invalid_scope, invalid_grant, invalid_client, admin_policy_enforced).
  • A partial consent grant is stored and reported, never rejected; an account change revokes the old refresh token best-effort and names the previous account.

Development

npm install
npm run typecheck   # types for src + tests
npm test            # node:test, no external network (loopback only)
npm run build       # emit dist/

@modelcontextprotocol/sdk is a peer dependency — the host server owns the SDK instance; only zod ships as a runtime dependency.

License

MIT

版本与发布记录

@a1-x-tech/mcp-google-auth 共发布 3 个版本,首次发布于 2026/09/20;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.1.2latest2026/11/053 天前
0.1.12026/11/016 天前
0.1.02026/09/201 个月前

关于 @a1-x-tech/mcp-google-auth 的常见问题

@a1-x-tech/mcp-google-auth 是什么?

Reusable Google OAuth onboarding for the mcp-google-* MCP server line: in-chat login tools (loopback + PKCE) and a per-request TokenProvider.

如何安装 @a1-x-tech/mcp-google-auth?

在项目目录运行 npm install @a1-x-tech/mcp-google-auth,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@modelcontextprotocol/sdk(^1.29.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@a1-x-tech/mcp-google-auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@a1-x-tech/mcp-google-auth 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@a1-x-tech/mcp-google-auth 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@a1-x-tech/mcp-google-auth 可以商用吗?

@a1-x-tech/mcp-google-auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@a1-x-tech/mcp-google-auth 的下载量有多少?

根据 npm 官方下载统计,@a1-x-tech/mcp-google-auth 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,580 次,日均约 86 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@a1-x-tech/mcp-google-auth 还在维护吗?

最新版本 0.1.2 发布于 2026/11/05(3 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/09/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。