mcp-google-multi 6.0.0-beta.1 → 6.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/README.md +5 -3
  2. package/dist/accounts.d.ts +22 -1
  3. package/dist/accounts.js +64 -16
  4. package/dist/api-probe.d.ts +5 -0
  5. package/dist/api-probe.js +23 -15
  6. package/dist/arg-normalize.d.ts +61 -4
  7. package/dist/arg-normalize.js +229 -4
  8. package/dist/arg-strict.d.ts +61 -0
  9. package/dist/arg-strict.js +193 -0
  10. package/dist/auth.d.ts +4 -3
  11. package/dist/auth.js +26 -85
  12. package/dist/boot-gates.d.ts +26 -0
  13. package/dist/boot-gates.js +38 -0
  14. package/dist/cimd-cache.d.ts +38 -0
  15. package/dist/cimd-cache.js +117 -0
  16. package/dist/client.d.ts +32 -2
  17. package/dist/client.js +0 -0
  18. package/dist/compose.d.ts +11 -0
  19. package/dist/compose.js +109 -0
  20. package/dist/config-file.d.ts +14 -0
  21. package/dist/config-file.js +41 -1
  22. package/dist/discover.js +38 -15
  23. package/dist/discovery-client.d.ts +11 -1
  24. package/dist/discovery-client.js +120 -18
  25. package/dist/doctor.d.ts +15 -3
  26. package/dist/doctor.js +151 -27
  27. package/dist/env-load.d.ts +11 -0
  28. package/dist/env-load.js +23 -1
  29. package/dist/executor.d.ts +3 -0
  30. package/dist/executor.js +16 -3
  31. package/dist/fanout.d.ts +9 -1
  32. package/dist/fanout.js +83 -14
  33. package/dist/fs-atomic.d.ts +11 -0
  34. package/dist/fs-atomic.js +219 -28
  35. package/dist/hint-provider.d.ts +129 -0
  36. package/dist/hint-provider.js +64 -0
  37. package/dist/http-transport.d.ts +73 -6
  38. package/dist/http-transport.js +350 -26
  39. package/dist/identity.d.ts +4 -1
  40. package/dist/identity.js +25 -6
  41. package/dist/index.js +142 -114
  42. package/dist/master-key.js +13 -5
  43. package/dist/mcp-token.d.ts +136 -19
  44. package/dist/mcp-token.js +569 -72
  45. package/dist/metrics-cli.d.ts +21 -0
  46. package/dist/metrics-cli.js +206 -0
  47. package/dist/metrics-tap.d.ts +12 -0
  48. package/dist/metrics-tap.js +95 -0
  49. package/dist/migrate-config.js +2 -2
  50. package/dist/oauth-as.d.ts +103 -3
  51. package/dist/oauth-as.js +374 -79
  52. package/dist/oauth-consent.d.ts +35 -10
  53. package/dist/oauth-consent.js +101 -39
  54. package/dist/outbound-allowlist.d.ts +34 -0
  55. package/dist/outbound-allowlist.js +111 -0
  56. package/dist/reauth-hint.d.ts +1 -1
  57. package/dist/reauth-hint.js +12 -8
  58. package/dist/registry-transfer.d.ts +5 -0
  59. package/dist/registry-transfer.js +65 -5
  60. package/dist/registry.d.ts +66 -2
  61. package/dist/registry.js +214 -18
  62. package/dist/scope-catalog.d.ts +11 -0
  63. package/dist/scope-catalog.js +39 -6
  64. package/dist/scope-observability.d.ts +8 -6
  65. package/dist/scope-observability.js +29 -13
  66. package/dist/services.d.ts +17 -4
  67. package/dist/services.js +77 -6
  68. package/dist/setup-prompt.d.ts +1 -1
  69. package/dist/setup-prompt.js +1 -1
  70. package/dist/ssrf-guard.d.ts +23 -9
  71. package/dist/ssrf-guard.js +101 -62
  72. package/dist/tenant-purge.d.ts +26 -0
  73. package/dist/tenant-purge.js +67 -0
  74. package/dist/token-store.d.ts +20 -2
  75. package/dist/token-store.js +0 -0
  76. package/dist/tools/_coerce.d.ts +5 -0
  77. package/dist/tools/_coerce.js +11 -0
  78. package/dist/tools/_errors.d.ts +34 -3
  79. package/dist/tools/_errors.js +346 -53
  80. package/dist/tools/_local-files.d.ts +9 -0
  81. package/dist/tools/_local-files.js +25 -2
  82. package/dist/tools/account-wizard.d.ts +34 -2
  83. package/dist/tools/account-wizard.js +223 -65
  84. package/dist/tools/accounts-tool.d.ts +7 -0
  85. package/dist/tools/accounts-tool.js +34 -18
  86. package/dist/tools/admin.d.ts +10 -1
  87. package/dist/tools/admin.js +21 -16
  88. package/dist/tools/analytics.d.ts +27 -0
  89. package/dist/tools/analytics.js +289 -0
  90. package/dist/tools/calendar.d.ts +9 -1
  91. package/dist/tools/calendar.js +93 -29
  92. package/dist/tools/chat.d.ts +10 -1
  93. package/dist/tools/chat.js +20 -12
  94. package/dist/tools/contacts.d.ts +2 -1
  95. package/dist/tools/contacts.js +74 -43
  96. package/dist/tools/docs.d.ts +2 -1
  97. package/dist/tools/docs.js +90 -84
  98. package/dist/tools/drive.d.ts +2 -1
  99. package/dist/tools/drive.js +197 -120
  100. package/dist/tools/forms.d.ts +10 -1
  101. package/dist/tools/forms.js +26 -19
  102. package/dist/tools/generated/_shared.d.ts +8 -1
  103. package/dist/tools/generated/_shared.js +18 -4
  104. package/dist/tools/generated/admin.d.ts +2 -1
  105. package/dist/tools/generated/admin.js +610 -479
  106. package/dist/tools/generated/analytics.d.ts +3 -0
  107. package/dist/tools/generated/analytics.js +981 -0
  108. package/dist/tools/generated/appsmarket.d.ts +2 -1
  109. package/dist/tools/generated/appsmarket.js +9 -9
  110. package/dist/tools/generated/calendar.d.ts +2 -1
  111. package/dist/tools/generated/calendar.js +83 -83
  112. package/dist/tools/generated/chat.d.ts +2 -1
  113. package/dist/tools/generated/chat.js +178 -151
  114. package/dist/tools/generated/classroom.d.ts +2 -1
  115. package/dist/tools/generated/classroom.js +467 -425
  116. package/dist/tools/generated/cloudidentity.d.ts +2 -1
  117. package/dist/tools/generated/cloudidentity.js +209 -201
  118. package/dist/tools/generated/cloudsearch.d.ts +2 -1
  119. package/dist/tools/generated/cloudsearch.js +139 -138
  120. package/dist/tools/generated/contacts.d.ts +2 -1
  121. package/dist/tools/generated/contacts.js +49 -40
  122. package/dist/tools/generated/drive.d.ts +2 -1
  123. package/dist/tools/generated/drive.js +133 -124
  124. package/dist/tools/generated/driveactivity.d.ts +2 -1
  125. package/dist/tools/generated/driveactivity.js +3 -3
  126. package/dist/tools/generated/drivelabels.d.ts +2 -1
  127. package/dist/tools/generated/drivelabels.js +113 -85
  128. package/dist/tools/generated/forms.d.ts +2 -1
  129. package/dist/tools/generated/forms.js +14 -14
  130. package/dist/tools/generated/gmail.d.ts +2 -1
  131. package/dist/tools/generated/gmail.js +253 -226
  132. package/dist/tools/generated/groupsmigration.d.ts +2 -1
  133. package/dist/tools/generated/groupsmigration.js +4 -4
  134. package/dist/tools/generated/groupssettings.d.ts +2 -1
  135. package/dist/tools/generated/groupssettings.js +10 -10
  136. package/dist/tools/generated/index.d.ts +2 -1
  137. package/dist/tools/generated/index.js +2 -0
  138. package/dist/tools/generated/keep.d.ts +2 -1
  139. package/dist/tools/generated/keep.js +23 -22
  140. package/dist/tools/generated/licensing.d.ts +2 -1
  141. package/dist/tools/generated/licensing.js +54 -38
  142. package/dist/tools/generated/meet.d.ts +2 -1
  143. package/dist/tools/generated/meet.js +41 -41
  144. package/dist/tools/generated/method-map.d.ts +2 -0
  145. package/dist/tools/generated/method-map.js +894 -0
  146. package/dist/tools/generated/postmaster.d.ts +2 -1
  147. package/dist/tools/generated/postmaster.js +12 -12
  148. package/dist/tools/generated/reseller.d.ts +2 -1
  149. package/dist/tools/generated/reseller.js +69 -63
  150. package/dist/tools/generated/script.d.ts +2 -1
  151. package/dist/tools/generated/script.js +62 -52
  152. package/dist/tools/generated/searchconsole.d.ts +2 -1
  153. package/dist/tools/generated/searchconsole.js +8 -6
  154. package/dist/tools/generated/sheets.d.ts +2 -1
  155. package/dist/tools/generated/sheets.js +25 -24
  156. package/dist/tools/generated/tasks.d.ts +2 -1
  157. package/dist/tools/generated/tasks.js +15 -9
  158. package/dist/tools/generated/vault.d.ts +2 -1
  159. package/dist/tools/generated/vault.js +132 -123
  160. package/dist/tools/generated/workspaceevents.d.ts +2 -1
  161. package/dist/tools/generated/workspaceevents.js +48 -47
  162. package/dist/tools/gmail.d.ts +12 -2
  163. package/dist/tools/gmail.js +95 -51
  164. package/dist/tools/google-api.d.ts +2 -3
  165. package/dist/tools/google-api.js +76 -33
  166. package/dist/tools/meet.d.ts +10 -1
  167. package/dist/tools/meet.js +18 -11
  168. package/dist/tools/searchconsole.d.ts +2 -1
  169. package/dist/tools/searchconsole.js +27 -23
  170. package/dist/tools/sheets.d.ts +2 -1
  171. package/dist/tools/sheets.js +72 -80
  172. package/dist/tools/slides.d.ts +10 -1
  173. package/dist/tools/slides.js +23 -16
  174. package/dist/tools/tasks.d.ts +2 -1
  175. package/dist/tools/tasks.js +35 -31
  176. package/dist/trim.d.ts +26 -0
  177. package/dist/trim.js +67 -0
  178. package/dist/usage-metrics.d.ts +178 -0
  179. package/dist/usage-metrics.js +621 -0
  180. package/dist/write-control.d.ts +27 -1
  181. package/dist/write-control.js +99 -12
  182. package/package.json +34 -3
package/dist/index.js CHANGED
@@ -2,78 +2,25 @@
2
2
  // First import: triggers accounts.js module load (env files + registry) before
3
3
  // anything else. Named to also pull the server-only empty-registry guard (BR-4).
4
4
  import { assertServerAccountsConfigured } from './accounts.js';
5
+ import { accountsAssertRequired, assertNoEnvAccountsMode, assertNoEnvOptionalScopesMode, isMultiTenantBoot, mtBootGates } from './boot-gates.js';
5
6
  import { readFileSync } from 'node:fs';
6
7
  import { fileURLToPath } from 'node:url';
7
8
  import path from 'node:path';
8
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
9
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
10
- import { GENERATED_SERVICES } from './tools/generated/index.js';
11
- import { GENERATED_GATES, SERVICES } from './services.js';
12
- import { ToolRegistry } from './registry.js';
13
- import { registerDiscoverTools } from './discover.js';
14
- import { registerEscapeTools } from './tools/google-api.js';
15
- import { registerAccountTools } from './tools/accounts-tool.js';
16
- import { registerDiagnoseTool } from './doctor.js';
17
- import { registerAccountWizardTools } from './tools/account-wizard.js';
18
- import { getToolsets, toolsetEnabled } from './toolsets.js';
9
+ import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
10
+ import { McpServer } from "@modelcontextprotocol/server";
11
+ import { resolveDiscoveryMode } from './registry.js';
19
12
  import { isAllowed, describePolicy } from './write-control.js';
20
13
  import { buildIdentityContext } from './identity.js';
14
+ import { buildRegistry, requestHooksFor } from './compose.js';
21
15
  import { registerSetupPrompt } from './setup-prompt.js';
22
16
  import { applyNetTuning } from './net-tuning.js';
23
- import { argNormalizationEnabled, withArgNormalization } from './arg-normalize.js';
17
+ import { withArgNormalization, withValidationEnvelope } from './arg-normalize.js';
18
+ import { envValueSource } from './env-load.js';
19
+ import { loadConfigFile } from './config-file.js';
20
+ import { initUsageMetrics, resolveUsageMetrics, sourceLabel } from './usage-metrics.js';
24
21
  applyNetTuning();
25
22
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
26
23
  const pkg = JSON.parse(readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf-8'));
27
- function buildRegistry(server, ctx, mode) {
28
- const policy = ctx.policy;
29
- const registry = new ToolRegistry(server, policy, mode);
30
- const toolsets = getToolsets();
31
- if (toolsets !== 'all') {
32
- const known = new Set([...SERVICES.map((s) => s.name), ...GENERATED_SERVICES.map((s) => s.name)]);
33
- for (const requested of toolsets) {
34
- if (!known.has(requested)) {
35
- process.stderr.write(`GOOGLE_TOOLSETS: unknown service "${requested}" ignored\n`);
36
- }
37
- }
38
- }
39
- for (const svc of SERVICES) {
40
- if (!toolsetEnabled(toolsets, svc.name))
41
- continue;
42
- if (svc.enabled && !svc.enabled()) {
43
- if (toolsets !== 'all') {
44
- const hint = svc.name === 'admin' ? 'set admin on an account/profile (or GOOGLE_ADMIN_ACCOUNTS)' : `add "${svc.name}" to an account's scope profile (or legacy GOOGLE_OPTIONAL_SCOPES)`;
45
- process.stderr.write(`GOOGLE_TOOLSETS: "${svc.name}" requested but not enabled — ${hint}\n`);
46
- }
47
- continue;
48
- }
49
- svc.register(registry);
50
- }
51
- for (const gen of GENERATED_SERVICES) {
52
- if (!toolsetEnabled(toolsets, gen.name))
53
- continue;
54
- const curated = SERVICES.find((s) => s.name === gen.name);
55
- const gate = curated?.enabled ?? GENERATED_GATES[gen.name]?.enabled;
56
- if (gate && !gate()) {
57
- if (!curated && toolsets !== 'all') {
58
- process.stderr.write(`GOOGLE_TOOLSETS: "${gen.name}" requested but not enabled — ${GENERATED_GATES[gen.name].hint}\n`);
59
- }
60
- continue;
61
- }
62
- gen.register(registry);
63
- }
64
- if (registry.services().length === 0) {
65
- const known = [...new Set([...SERVICES.map((s) => s.name), ...GENERATED_SERVICES.map((s) => s.name)])].sort();
66
- throw new Error(`GOOGLE_TOOLSETS="${process.env.GOOGLE_TOOLSETS ?? ''}" selected no enabled services. ` +
67
- `Known services: ${known.join(', ')}. ` +
68
- `Note: optional services need their bundle in an account's scope profile (or legacy GOOGLE_OPTIONAL_SCOPES); admin needs an admin account/profile.`);
69
- }
70
- registerDiscoverTools(registry, policy);
71
- registerEscapeTools(registry, policy);
72
- registerAccountTools(registry);
73
- registerDiagnoseTool(registry);
74
- registerAccountWizardTools(registry, server);
75
- return registry;
76
- }
77
24
  async function main() {
78
25
  if (process.argv.includes('auth')) {
79
26
  const { runAuthFlow } = await import('./auth.js');
@@ -100,6 +47,22 @@ async function main() {
100
47
  process.exitCode = await runResetCli(process.argv);
101
48
  return;
102
49
  }
50
+ if (process.argv.includes('metrics')) {
51
+ const { runMetricsCli } = await import('./metrics-cli.js');
52
+ const { GENERATED_METHOD_TOOLS, CURATED_METHOD_IDS } = await import('./tools/generated/method-map.js');
53
+ const envSrc = envValueSource('GOOGLE_USAGE_METRICS');
54
+ let configValue;
55
+ try {
56
+ configValue = loadConfigFile(undefined, 'throw')?.usageMetrics;
57
+ }
58
+ catch { /* an invalid config never blocks reading local metrics files */ }
59
+ const state = resolveUsageMetrics(process.env, configValue, envSrc?.kind === 'file' ? envSrc.file : undefined);
60
+ process.exitCode = runMetricsCli(process.argv, {
61
+ enabled: state.enabled,
62
+ promotion: { methodMap: GENERATED_METHOD_TOOLS, curatedIds: CURATED_METHOD_IDS },
63
+ });
64
+ return;
65
+ }
103
66
  if (process.argv.includes('write-client-config')) {
104
67
  const { runWriteClientConfigCli } = await import('./client-config.js');
105
68
  process.exitCode = await runWriteClientConfigCli(process.argv);
@@ -164,14 +127,8 @@ async function main() {
164
127
  }
165
128
  return;
166
129
  }
167
- // BR-4: the SERVER never boots with an empty registry. The bootstrap and
168
- // diagnostic CLIs above already returned; a fresh user configures accounts
169
- // (env / migrate-config / account import / auth) before the server runs.
170
- assertServerAccountsConfigured();
171
- // Resolve (or provision) the master key BEFORE serving: the hard guard is
172
- // specced "fatal at startup", never mid-dispatch.
173
- const { resolveMasterKey } = await import('./master-key.js');
174
- resolveMasterKey();
130
+ // Transport derivation FIRST: the accounts gate below is transport-aware
131
+ // (a multi-tenant HTTP deploy legitimately boots with zero accounts).
175
132
  const { resolveHttpConfig, transportIncludesHttp } = await import('./http-config.js');
176
133
  let httpCfg;
177
134
  try {
@@ -183,24 +140,99 @@ async function main() {
183
140
  }
184
141
  const wantStdio = httpCfg.transport === 'stdio' || httpCfg.transport === 'both';
185
142
  const wantHttp = transportIncludesHttp(httpCfg.transport);
143
+ // BR-4: the SERVER never boots with an empty registry — stdio always, HTTP
144
+ // when single-owner. The bootstrap and diagnostic CLIs above already
145
+ // returned. A multi-tenant boot instead refuses the two process-wide env
146
+ // vectors that would leak one operator's accounts/scopes into every tenant.
147
+ const mt = isMultiTenantBoot();
148
+ if (accountsAssertRequired(wantStdio, mt))
149
+ assertServerAccountsConfigured();
150
+ if (mt) {
151
+ assertNoEnvAccountsMode();
152
+ assertNoEnvOptionalScopesMode();
153
+ }
154
+ // Resolve (or provision) the master key BEFORE serving: the hard guard is
155
+ // specced "fatal at startup", never mid-dispatch.
156
+ const { resolveMasterKey } = await import('./master-key.js');
157
+ resolveMasterKey();
158
+ // Local usage metrics: fail-closed resolve, self-announcing source, null
159
+ // when off (no wrapper, no dir, nothing initializes). One instance per
160
+ // transport so `boots` keys stay honest under `both`.
161
+ const envSrc = envValueSource('GOOGLE_USAGE_METRICS');
162
+ const metricsState = resolveUsageMetrics(process.env, loadConfigFile()?.usageMetrics, envSrc?.kind === 'file' ? envSrc.file : undefined);
163
+ if (metricsState.warning)
164
+ process.stderr.write(metricsState.warning);
165
+ const metricsInstances = [];
166
+ const initMetricsFor = (transport, mode) => {
167
+ const m = initUsageMetrics(metricsState, { version: pkg.version, mode, transport });
168
+ if (m) {
169
+ metricsInstances.push(m);
170
+ process.stderr.write(`local usage metrics: on ${sourceLabel(metricsState.source)} -> ${m.statusLine()}\n`);
171
+ }
172
+ return m;
173
+ };
174
+ if (!metricsState.enabled && metricsState.source.kind !== 'default') {
175
+ process.stderr.write(`local usage metrics: off ${sourceLabel(metricsState.source)}\n`);
176
+ }
177
+ const flushMetrics = () => { for (const m of metricsInstances)
178
+ m.shutdown(); };
179
+ process.on('exit', flushMetrics);
180
+ if (metricsState.enabled) {
181
+ for (const sig of ['SIGTERM', 'SIGINT']) {
182
+ process.once(sig, () => {
183
+ flushMetrics();
184
+ // stdio has no other signal handler; preserve terminate-on-signal.
185
+ if (!wantHttp)
186
+ process.exit(sig === 'SIGINT' ? 130 : 143);
187
+ });
188
+ }
189
+ }
186
190
  // Build one McpServer + registry per transport at boot (P1 / BV gap #4:
187
191
  // never rebuilt per request); `both` runs the two concurrently.
188
192
  if (wantStdio) {
189
193
  const server = new McpServer({ name: 'mcp-google-multi', version: pkg.version });
190
- const registry = buildRegistry(server, buildIdentityContext(process.env, { transport: 'stdio' }));
194
+ const stdioMetrics = initMetricsFor('stdio', resolveDiscoveryMode());
195
+ const stdioCtx = buildIdentityContext(process.env, { transport: 'stdio' });
196
+ const registry = buildRegistry(server, stdioCtx, undefined, stdioMetrics);
191
197
  registry.installListHandler();
192
198
  registerSetupPrompt(server);
193
- const stdioTransport = new StdioServerTransport();
194
- await server.connect(argNormalizationEnabled() ? withArgNormalization(stdioTransport, (n) => registry.argShape(n)) : stdioTransport);
199
+ const stdioHooks = requestHooksFor(registry, stdioCtx, stdioMetrics);
200
+ // Outbound runs outermost-first, so the composition is deliberate: the
201
+ // envelope rewrite is INNERMOST (last to touch the frame), the tap sits
202
+ // above it (classifying the ORIGINAL validation prose), arg normalization
203
+ // outermost. The tap only counts outbound JSON-RPC error frames (no
204
+ // handler ran) plus id->tool names.
205
+ let transport = withValidationEnvelope(new StdioServerTransport(), stdioHooks.validationEnvelope);
206
+ if (stdioMetrics) {
207
+ const { tapUsageMetrics } = await import('./metrics-tap.js');
208
+ transport = tapUsageMetrics(transport, stdioMetrics, (n) => registry.hasTool(n));
209
+ }
210
+ await server.connect(stdioHooks.argShapeFor || stdioHooks.strictArgs
211
+ ? withArgNormalization(transport,
212
+ // Gated exactly as the HTTP leg gates `argShapeFor`. Passing the
213
+ // shape unconditionally made stdio rename keys while
214
+ // GOOGLE_ARG_NORMALIZE=off, so the same call succeeded on stdio
215
+ // and failed on HTTP once screening rejects.
216
+ stdioHooks.argShapeFor ?? (() => undefined), undefined, stdioMetrics ? (tool, n) => stdioMetrics.recordArgFix(tool, n) : undefined, stdioHooks.strictArgs)
217
+ : transport);
195
218
  }
196
219
  if (wantHttp) {
197
220
  const { HttpTransportHost, parseOwnerEmails } = await import('./http-transport.js');
198
- // BR3 / C13: the owner allowlist is the entire multi-tenant collapse; refuse
199
- // to open an ungated HTTP endpoint.
200
221
  const owners = parseOwnerEmails(process.env);
201
- if (owners.length === 0) {
202
- process.stderr.write('E_OWNER_EMAILS_REQUIRED: MCP_TRANSPORT includes http but MCP_OWNER_EMAILS is empty. Set MCP_OWNER_EMAILS to the Google email(s) allowed to authenticate.\n');
203
- process.exit(1);
222
+ const gates = mtBootGates();
223
+ if (gates?.multiTenant) {
224
+ // Same fail-fast SHAPE, different condition: an ungated HTTP endpoint
225
+ // must never boot; under tenancy the gate is "a valid provisioning
226
+ // mechanism exists", not a flat owner allowlist.
227
+ gates.assertProvisioningGate();
228
+ }
229
+ else {
230
+ // BR3 / C13: the owner allowlist is the entire multi-tenant collapse;
231
+ // refuse to open an ungated HTTP endpoint.
232
+ if (owners.length === 0) {
233
+ process.stderr.write('E_OWNER_EMAILS_REQUIRED: MCP_TRANSPORT includes http but MCP_OWNER_EMAILS is empty. Set MCP_OWNER_EMAILS to the Google email(s) allowed to authenticate.\n');
234
+ process.exit(1);
235
+ }
204
236
  }
205
237
  // BR7: stateless HTTP cannot push tools/list_changed, so it forces curated.
206
238
  const configuredMode = (process.env.GOOGLE_DISCOVERY ?? '').trim().toLowerCase();
@@ -208,33 +240,23 @@ async function main() {
208
240
  process.stderr.write(`GOOGLE_DISCOVERY="${configuredMode}" is ignored over HTTP; the stateless transport forces "curated".\n`);
209
241
  }
210
242
  const httpServer = new McpServer({ name: 'mcp-google-multi', version: pkg.version });
211
- const registry = buildRegistry(httpServer, buildIdentityContext(process.env, { transport: 'http' }), 'curated');
243
+ const httpMetrics = initMetricsFor('http', 'curated');
244
+ const httpCtx = buildIdentityContext(process.env, { transport: 'http' });
245
+ const registry = buildRegistry(httpServer, httpCtx, 'curated', httpMetrics);
212
246
  registry.installListHandler();
213
247
  registerSetupPrompt(httpServer);
214
248
  // B13: mount the OAuth 2.1 AS (legs A + B) + the Bearer authenticator.
215
- const { buildAuthServer } = await import('./oauth-as.js');
249
+ const { buildAuthServer, verifiedEmailFromIdToken } = await import('./oauth-as.js');
216
250
  const { jwtSecretFrom } = await import('./mcp-token.js');
217
251
  const { resolveJwtKey, resolveMasterKeyForDispatch } = await import('./master-key.js');
218
252
  const { OAuth2Client } = await import('googleapis-common');
219
- const { writeToken } = await import('./token-store.js');
253
+ const { readToken, writeToken } = await import('./token-store.js');
220
254
  const { resolveScopesForAccount } = await import('./auth.js');
255
+ const { ownerAuthUrlOptions } = await import('./oauth-consent.js');
256
+ const { scopeGrantDiff, setWizardHttpConsent } = await import('./tools/account-wizard.js');
221
257
  const { configDir } = await import('./config-file.js');
222
258
  const { getAccountSet } = await import('./accounts.js');
223
259
  const googleClient = () => new OAuth2Client(process.env.GOOGLE_CLIENT_ID, process.env.GOOGLE_CLIENT_SECRET, `${httpCfg.publicUrl}/callback`);
224
- // Only trust the id_token email when Google marks it verified (#8).
225
- const verifiedEmailFromIdToken = (idToken) => {
226
- if (!idToken)
227
- return undefined;
228
- try {
229
- const [, payload] = idToken.split('.');
230
- const claims = JSON.parse(Buffer.from(payload, 'base64url').toString('utf-8'));
231
- const verified = claims.email_verified === true || claims.email_verified === 'true';
232
- return verified ? claims.email : undefined;
233
- }
234
- catch {
235
- return undefined;
236
- }
237
- };
238
260
  const authServer = buildAuthServer({
239
261
  base: httpCfg.publicUrl,
240
262
  resourceUri: httpCfg.resourceUri,
@@ -245,20 +267,7 @@ async function main() {
245
267
  masterKey: resolveMasterKeyForDispatch().key,
246
268
  refreshStorePath: path.join(configDir(), 'mcp-tokens.enc'),
247
269
  }, {
248
- buildGoogleAuthUrl: ({ flow, alias, state }) => {
249
- if (flow === 'alias_reauth' && alias) {
250
- const cfg = getAccountSet().configs[alias];
251
- // openid+email so /callback can bind the returned identity to the alias.
252
- return googleClient().generateAuthUrl({
253
- access_type: 'offline',
254
- prompt: 'consent',
255
- scope: [...new Set([...resolveScopesForAccount(alias), 'openid', 'email'])],
256
- login_hint: cfg?.email,
257
- state,
258
- });
259
- }
260
- return googleClient().generateAuthUrl({ scope: ['openid', 'email'], prompt: 'select_account', state });
261
- },
270
+ buildGoogleAuthUrl: ({ flow, alias, state }) => googleClient().generateAuthUrl(ownerAuthUrlOptions({ flow, alias, state }, (a) => resolveScopesForAccount(a))),
262
271
  exchangeCode: async (code, flow) => {
263
272
  const { tokens } = await googleClient().getToken(code);
264
273
  const email = verifiedEmailFromIdToken(tokens.id_token ?? undefined);
@@ -267,13 +276,30 @@ async function main() {
267
276
  return { tokens: flow === 'owner_gate' ? {} : tokens, email };
268
277
  },
269
278
  writeToken: (alias, tokens) => writeToken(alias, tokens),
279
+ missingScopes: (alias, granted) => scopeGrantDiff(resolveScopesForAccount(alias), granted),
280
+ hasToken: (alias) => {
281
+ try {
282
+ return readToken(alias) != null;
283
+ }
284
+ catch {
285
+ return false; // undecryptable: nothing usable to keep
286
+ }
287
+ },
270
288
  aliasEmail: (alias) => getAccountSet().configs[alias]?.email,
271
289
  log: (l) => process.stderr.write(`[as] ${l}\n`),
272
290
  });
273
- // BV-1: over HTTP, a dead/missing per-account token surfaces a clickable
274
- // re-auth link into the AS's alias_reauth flow instead of a stdio CLI hint.
275
- const { setHttpReauthBase } = await import('./reauth-hint.js');
276
- setHttpReauthBase(httpCfg.publicUrl);
291
+ // BV-1: over HTTP, a dead/missing per-account token surfaces a clickable,
292
+ // signed re-auth link into the AS's alias_reauth flow instead of a CLI hint.
293
+ const { setHttpReauthLink } = await import('./reauth-hint.js');
294
+ setHttpReauthLink((alias) => authServer.reauthLink(alias));
295
+ // Wizard consent over HTTP: hand out the same signed link instead of
296
+ // binding a loopback listener.
297
+ setWizardHttpConsent({ mintConsentUrl: (alias) => authServer.reauthLink(alias) });
298
+ let httpTap;
299
+ if (httpMetrics) {
300
+ const { tapUsageMetrics } = await import('./metrics-tap.js');
301
+ httpTap = (t) => tapUsageMetrics(t, httpMetrics, (n) => registry.hasTool(n));
302
+ }
277
303
  const host = new HttpTransportHost({
278
304
  server: httpServer,
279
305
  config: httpCfg,
@@ -282,7 +308,9 @@ async function main() {
282
308
  authenticate: authServer.authenticate,
283
309
  routes: authServer.routes,
284
310
  log: (l) => process.stderr.write(`[http] ${l}\n`),
285
- argShapeFor: argNormalizationEnabled() ? (n) => registry.argShape(n) : undefined,
311
+ ...requestHooksFor(registry, httpCtx, httpMetrics),
312
+ metricsTap: httpTap,
313
+ onArgRename: httpMetrics ? (tool, n) => httpMetrics.recordArgFix(tool, n) : undefined,
286
314
  });
287
315
  await host.start();
288
316
  process.stderr.write(`HTTP transport listening on http://${httpCfg.host}:${httpCfg.port} (public ${httpCfg.publicUrl})\n`);
@@ -38,17 +38,25 @@ let keychainFactory = systemKeychain;
38
38
  export function __setKeychainFactoryForTest(fn) {
39
39
  keychainFactory = fn ?? systemKeychain;
40
40
  }
41
- // Glob the token dir instead of iterating registered aliases: an orphan .enc
42
- // (alias removed from the registry, or a partial GOOGLE_ACCOUNTS override in
43
- // one client) must still stop a fresh key from being minted.
44
- function anyTokenFileExists() {
41
+ function anyEncFileUnder(dir) {
45
42
  try {
46
- return fs.readdirSync(getTokenDir()).some((f) => f.endsWith('.enc'));
43
+ return fs.readdirSync(dir, { recursive: true }).some((f) => String(f).endsWith('.enc'));
47
44
  }
48
45
  catch {
49
46
  return false;
50
47
  }
51
48
  }
49
+ // Glob the token dir instead of iterating registered aliases: an orphan .enc
50
+ // (alias removed from the registry, or a partial GOOGLE_ACCOUNTS override in
51
+ // one client) must still stop a fresh key from being minted. TOKEN_STORE_PATH
52
+ // may relocate the token dir outside configDir(), so scan it directly; the
53
+ // recursive configDir() walk then covers BOTH the tenant-namespaced token root
54
+ // (tenants/<id>) AND any top-level master-key-encrypted registry beside it
55
+ // (e.g. a downstream tenant map / invite store). Minting a fresh MASTER_KEY
56
+ // over any existing .enc bricks it irreversibly, so the scan must see them all.
57
+ function anyTokenFileExists() {
58
+ return anyEncFileUnder(getTokenDir()) || anyEncFileUnder(configDir());
59
+ }
52
60
  function readKeyFile(filePath) {
53
61
  try {
54
62
  const raw = fs.readFileSync(filePath, 'utf8').trim();
@@ -1,7 +1,7 @@
1
1
  export declare const ACCESS_TTL_DEFAULT = 600;
2
2
  export declare const STATE_TTL_DEFAULT = 600;
3
3
  export declare const CODE_TTL_DEFAULT = 60;
4
- export declare const REPLAY_CAP_DEFAULT = 10000;
4
+ export declare const REPLAY_CAP_DEFAULT = 100000;
5
5
  /** Derive the HS256 secret (32 bytes) from the provisioned MCP_JWT_KEY string. */
6
6
  export declare function jwtSecretFrom(jwtKey: string): Uint8Array;
7
7
  export interface AccessTokenParams {
@@ -9,6 +9,7 @@ export interface AccessTokenParams {
9
9
  secret: Uint8Array;
10
10
  ttlSec?: number;
11
11
  iat: number;
12
+ sub: string;
12
13
  }
13
14
  export declare function signAccessToken(p: AccessTokenParams): Promise<string>;
14
15
  export interface AccessClaims {
@@ -19,63 +20,179 @@ export interface AccessClaims {
19
20
  /** Verify an access token for `/mcp`. Throws on bad sig / aud / iss / exp. */
20
21
  export declare function verifyAccessToken(token: string, base: string, secret: Uint8Array): Promise<AccessClaims>;
21
22
  export interface StatePayload {
22
- flow: 'owner_gate' | 'alias_reauth';
23
+ flow: 'owner_gate' | 'alias_reauth' | 'alias_add';
23
24
  client_id: string;
24
25
  redirect_uri: string;
25
26
  code_challenge: string;
26
27
  client_state?: string;
27
28
  resource: string;
28
29
  alias?: string;
30
+ /** alias_add only: the tenant the new alias binds under. Signed server-side
31
+ * at mint time — never caller-supplied at /authorize or /callback. */
32
+ tenantId?: string;
33
+ /** alias_add only: scope bundles chosen when the link was minted. */
34
+ bundles?: string[];
35
+ /** alias_add only: an opaque value the minting caller chose, signed like
36
+ * tenantId and handed back to its binder (e.g. a server-side link record). */
37
+ nonce?: string;
38
+ /** owner_gate only: base64url sha256 of the browser-binding cookie the
39
+ * Google redirect set; /callback refuses a browser that lacks it. */
40
+ bind?: string;
29
41
  }
30
42
  export interface CodePayload {
31
43
  redirect_uri: string;
32
44
  code_challenge: string;
33
45
  resource: string;
34
- sub: 'owner';
46
+ sub: string;
35
47
  }
36
48
  export declare function signState(payload: StatePayload, base: string, secret: Uint8Array, iat: number, ttlSec?: number): Promise<string>;
37
- export declare function verifyState(token: string, base: string, secret: Uint8Array): Promise<StatePayload & {
49
+ export declare function verifyState(token: string, base: string, secret: Uint8Array, nowSec?: number): Promise<StatePayload & {
38
50
  jti: string;
51
+ exp: number;
39
52
  }>;
53
+ /** A re-auth link is handed out in a tool error, so it has to survive until
54
+ * the user clicks it; an hour, not the 10-minute state TTL. */
55
+ export declare const REAUTH_LINK_TTL_SEC = 3600;
56
+ /** Query string of a server-issued alias_reauth link: the alias, an expiry and
57
+ * an HMAC over both. Synchronous, so the error hints that carry it stay so. */
58
+ export declare function signReauthLink(base: string, secret: Uint8Array, alias: string, nowSec: number): string;
59
+ /** The alias a link was issued for, or null when it is missing, forged or expired. */
60
+ export declare function verifyReauthLink(base: string, secret: Uint8Array, params: {
61
+ alias: string;
62
+ exp: string;
63
+ sig: string;
64
+ }, nowSec: number): string | null;
40
65
  /** A pending-authorization artifact for the DCR consent interstitial (same
41
66
  * shape as `state`, distinct purpose so the two can't be confused). */
42
67
  export declare function signPending(payload: StatePayload, base: string, secret: Uint8Array, iat: number, ttlSec?: number): Promise<string>;
43
- export declare function verifyPending(token: string, base: string, secret: Uint8Array): Promise<StatePayload & {
68
+ export declare function verifyPending(token: string, base: string, secret: Uint8Array, nowSec?: number): Promise<StatePayload & {
44
69
  jti: string;
70
+ exp: number;
45
71
  }>;
46
72
  export declare function signAuthzCode(payload: CodePayload, base: string, secret: Uint8Array, iat: number, ttlSec?: number): Promise<string>;
47
- export declare function verifyAuthzCode(token: string, base: string, secret: Uint8Array): Promise<CodePayload & {
73
+ export declare function verifyAuthzCode(token: string, base: string, secret: Uint8Array, nowSec?: number): Promise<CodePayload & {
48
74
  jti: string;
75
+ exp: number;
49
76
  }>;
77
+ /** `full`: there is no room without forgetting a jti that is still live. */
78
+ export type ReplaySpend = 'ok' | 'replay' | 'full';
50
79
  export declare class ReplayGuard {
51
80
  private readonly cap;
52
81
  private readonly seen;
82
+ private earliest;
83
+ private sweptAt;
53
84
  constructor(cap?: number);
54
- /** Record a jti as spent. Returns false if it was already spent (replay). */
85
+ /** Hold a slot for an artifact about to be handed out, so its spend can
86
+ * never meet a full guard. The flood then lands where the artifact is
87
+ * minted, and refusing there loses nothing. */
88
+ reserve(jti: string, ttlMs: number, nowMs: number): 'ok' | 'full';
89
+ /** Record a jti as spent. A full guard refuses instead of evicting a live
90
+ * jti: a flood of spends would otherwise make a spent artifact usable again,
91
+ * such as a callback the browser binding refused (H4 F1). `artifactExpMs`
92
+ * keeps the record alive as long as the artifact itself, even when the clock
93
+ * stepped back between its signing and this spend. */
94
+ spend(jti: string, ttlMs: number, nowMs: number, artifactExpMs?: number): ReplaySpend;
95
+ /** Undo a spend whose next step could not go ahead. The slot stays held, so
96
+ * nothing is forgotten, and the artifact can be spent once more. */
97
+ unspend(jti: string): void;
98
+ /** `spend` as a boolean: false for a replay and for a full guard alike. */
55
99
  consume(jti: string, ttlMs: number, nowMs: number): boolean;
56
100
  get size(): number;
57
- private evictExpired;
101
+ private record;
102
+ private room;
58
103
  }
104
+ /** An earlier release's record, keyed by its live token. */
59
105
  export interface RefreshRecord {
60
- sub: 'owner';
106
+ sub: string;
61
107
  issuedAt: number;
62
108
  family: string;
63
109
  }
64
110
  /**
65
- * Persisted (encrypted under MASTER_KEY) opaque refresh-token store. Rotates on
66
- * every use (a stolen token is usable at most once), and detects REUSE of a
67
- * rotated-away token as theft: the whole token family is revoked (#7 / C14).
68
- * All read-modify-write goes through a file lock so concurrent stdio+HTTP
69
- * processes can't lost-update or double-spend (#12).
111
+ * The family tag of a refresh token this `masterKey`'s store minted, or null
112
+ * for anything else. Derives the key once; the returned function does no I/O,
113
+ * so a host can key a pre-auth rate limit on it.
114
+ */
115
+ export declare function refreshFamilyTagger(masterKey: string): (token: string) => string | null;
116
+ /** Boot check for a refresh store file: returns when it is absent or this
117
+ * release can read it; throws when it does not decrypt under `masterKey`
118
+ * (E_REFRESH_STORE_UNREADABLE) or a newer release wrote it
119
+ * (E_REFRESH_STORE_FORMAT). */
120
+ export declare function assertRefreshStoreReadable(file: string, masterKey: string): void;
121
+ export interface RefreshStoreOptions {
122
+ /** Absolute family lifetime in seconds, counted from the sign-in. Unset: unlimited. */
123
+ maxAgeSec?: number;
124
+ /** Idle lifetime in seconds, counted from the last issue or rotation. Unset: unlimited. */
125
+ idleSec?: number;
126
+ /** Max live refresh-token families per subject (both formats). Unset:
127
+ * unlimited (default; the single-owner server passes none). When issuing a
128
+ * new family would put a subject over this, that subject's own
129
+ * least-recently-used family is dropped (by its last issue or rotation
130
+ * time) until it fits; a dropped family behaves exactly like a revoked
131
+ * one. Only the subject signing in ever triggers it, and only its own
132
+ * families are ever touched. */
133
+ maxFamiliesPerSub?: number;
134
+ /** Server-side log for a revoked, dropped or expired family. Never given a token or a subject. */
135
+ log?: (line: string) => void;
136
+ }
137
+ /**
138
+ * Persisted (encrypted under MASTER_KEY) refresh-token store. Rotates on every
139
+ * use, and a token carries its family and generation under a MAC, so
140
+ * presenting any generation the family has rotated past is REUSE: the family
141
+ * is revoked for the rest of its life (#7 / C14, H4 F2). The store keeps one
142
+ * record per family, whatever its rotation count. Every read-modify-write goes
143
+ * through a file lock so concurrent stdio+HTTP processes can't lost-update or
144
+ * double-spend (#12). See docs/internals.md.
70
145
  */
71
146
  export declare class RefreshStore {
72
147
  private readonly path;
73
148
  private readonly masterKey;
74
- constructor(path: string, masterKey: string);
149
+ private readonly log;
150
+ private readonly maxAgeMs;
151
+ private readonly idleMs;
152
+ private readonly maxFamiliesPerSub;
153
+ private macKey;
154
+ private legacyEmpty;
155
+ constructor(path: string, masterKey: string, options?: RefreshStoreOptions);
156
+ private get timed();
157
+ private live;
158
+ /** Drop expired sessions in both formats; the count of those dropped. */
159
+ private prune;
160
+ /** A session written while the clock ran ahead would otherwise outlive its
161
+ * limit by the size of the jump. */
162
+ private clamp;
163
+ private key;
164
+ /** With `nowMs` (every issue and rotation), also clamps and prunes. */
75
165
  private load;
76
166
  private save;
77
- issue(nowMs: number, family?: string): string;
78
- /** Rotate a presented refresh token; null if unknown OR if the presented
79
- * token was already rotated away (reuse => the family is revoked). */
80
- rotate(oldToken: string, nowMs: number): string | null;
167
+ private emit;
168
+ /** Evict `sub`'s own least-recently-used sessions (both formats) so at
169
+ * most `limit` of its live sessions remain; dropping one here is the same
170
+ * as deleting it from `data.families` / `data.active` on a revoke, so it
171
+ * can never be presented again (reuse detection for every OTHER family is
172
+ * untouched). Only `sub`'s own entries are ever inspected.
173
+ *
174
+ * `keepKey` is the family just issued: it is excluded from the ranking so
175
+ * a clock step back, or a concurrent rotate's `usedAt = max(usedAt, nowMs)`
176
+ * racing ahead of it under the lock, can never make the brand-new family
177
+ * look like the least recently used one and drop itself. */
178
+ private enforceFamilyCap;
179
+ private newFamilyId;
180
+ issue(nowMs: number, sub: string): string;
181
+ /** Rotate a presented refresh token; null if it is unknown, malformed, or
182
+ * rotated away (reuse => the family is revoked). The record's sub is copied
183
+ * forward and returned so the caller can mint the matching access token
184
+ * without trusting anything client-supplied.
185
+ *
186
+ * `accept` (optional) is called inside the store lock, before any mutation,
187
+ * with the record's sub. false: every session of that sub is dropped and
188
+ * null returned. A throw: nothing is written and the error propagates, so
189
+ * the presented token stays valid. Absent: unchanged. */
190
+ rotate(oldToken: string, nowMs: number, accept?: (sub: string) => boolean): {
191
+ token: string;
192
+ sub: string;
193
+ } | null;
194
+ /** Drop every session minted under `sub`, in both formats (linear scan under
195
+ * the store lock); the save then drops the earlier-release spent entries of
196
+ * the families that died with them. Returns the number of sessions dropped. */
197
+ purgeTenant(sub: string): number;
81
198
  }