:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
#!/usr/bin/env tsx
/**
* Generates the scope list for the CLI `agent-cli` login use case.
*
* `posthog-cli login` mints a Personal API Key whose scopes come from the
* `use_cases` it requests in the browser authorize flow. The `agent-cli` use case
* must grant exactly what the MCP / `posthog-cli api` tool surface needs,
* otherwise agent commands fail with `API key missing required scope`.
*
* The authoritative set is `getAdvertisedOAuthScopes()` — the union of every
* tool's `required_scopes`. We strip the OIDC identity scopes (`openid`,
* `profile`, `email`): those ride an OAuth access token, not a Personal API Key.
*
* Reads:
* - src/tools/toolDefinitions.ts (getAdvertisedOAuthScopes, via tool-definitions.json)
*
* Produces:
* - frontend/src/lib/agentScopes.generated.ts
*
* Run via hogli: `hogli build:openapi-cli-agent-scopes` (also runs as part of
* `build:openapi`, after `build:openapi-mcp-tools` which produces the tool
* definitions this reads). See `hogli.yaml`.
*/
import * as fs from 'node:fs'
import * as path from 'node:path'
import { getAdvertisedOAuthScopes } from '../src/tools/toolDefinitions'
const MCP_ROOT = path.resolve(__dirname, '..')
const REPO_ROOT = path.resolve(MCP_ROOT, '../..')
const OUTPUT_TS = path.resolve(REPO_ROOT, 'frontend/src/lib/agentScopes.generated.ts')
function main(): void {
// Identity scopes (openid/profile/email) have no `:` and don't apply to
// Personal API Keys — keep only the resource scopes the tools require.
const scopes = getAdvertisedOAuthScopes().filter((scope) => scope.includes(':'))
// Scope names are snake_case identifiers + colons and never need escaping,
// but assert it so the build fails loudly if that ever changes.
for (const scope of scopes) {
if (scope.includes("'") || scope.includes('\\')) {
throw new Error(`unexpected character in scope name: ${scope}`)
}
}
const body = scopes.map((scope) => ` '${scope}',`).join('\n')
const output =
'// AUTO-GENERATED by services/mcp/scripts/build-cli-agent-scopes.ts.\n' +
'// Regenerate with `hogli build:openapi`. Do not edit.\n' +
'//\n' +
'// The scopes the CLI `agent-cli` login use case grants — the same set the MCP\n' +
'// requests (getAdvertisedOAuthScopes, the union of every tool’s\n' +
'// required_scopes), minus the OIDC identity scopes that only apply to OAuth\n' +
'// access tokens rather than Personal API Keys.\n' +
'\n' +
'export const AGENT_USE_CASE_SCOPES = [\n' +
`${body}\n` +
'] as const\n'
fs.mkdirSync(path.dirname(OUTPUT_TS), { recursive: true })
const existing = fs.existsSync(OUTPUT_TS) ? fs.readFileSync(OUTPUT_TS, 'utf8') : null
if (existing !== output) {
fs.writeFileSync(OUTPUT_TS, output)
// eslint-disable-next-line no-console
console.log(`wrote ${OUTPUT_TS} (${scopes.length} scopes)`)
} else {
// eslint-disable-next-line no-console
console.log(`${OUTPUT_TS} already up to date (${scopes.length} scopes)`)
}
}
main()