Repository navigation
feat(cli): Generate command-catalog.json from the route tree #1919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1dc826
feat(cli): Generate command-catalog.json from the route tree
sentry-junior[bot] c8b7e96
Merge remote-tracking branch 'origin/feat/command-access-metadata' in…
sentry-junior[bot] 125b849
fix(cli): Keep the catalog lint-clean without built mcp-core types
sentry-junior[bot] 7805579
Merge remote-tracking branch 'origin/feat/command-access-metadata' in…
sentry-junior[bot] 3babae2
docs(cli): Say MCP servers read the generated catalog
sentry-junior[bot] 3ba73e6
Merge branch 'feat/command-access-metadata' into feat/command-catalog
sentry-junior[bot] 01c6a79
Merge branch 'feat/command-access-metadata' into feat/command-catalog
sentry-junior[bot] b35cdc2
Merge branch 'feat/command-access-metadata' into feat/command-catalog
sentry-junior[bot] 1deff15
Merge branch 'feat/command-access-metadata' into feat/command-catalog
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Generate `command-catalog.json` (`src/lib/command-catalog.ts`) for the MCP | ||
| * servers, which read it instead of building schemas at runtime. It is | ||
| * written to mcp-core, where mcp-cloudflare can import it, and committed, so | ||
| * `--check` fails when it is stale. | ||
| * | ||
| * It runs after `command-api` in `script/generate.ts`, so it reads that | ||
| * step's output from disk: the route tree's own imports of those files were | ||
| * stubs or the previous run's when the tree loaded. | ||
| */ | ||
|
|
||
| import { readFile, writeFile } from "node:fs/promises"; | ||
| import { routes } from "../src/app.js"; | ||
| import type { ApiReference } from "../src/lib/api-schema.js"; | ||
| import type { ApiAccess } from "../src/lib/command-access.js"; | ||
| import { extractCommandCatalog } from "../src/lib/command-catalog.js"; | ||
| import type { RouteMap } from "../src/lib/introspect.js"; | ||
|
|
||
| const COMMAND_CATALOG_PATH = "../mcp-core/src/command-catalog.json"; | ||
|
|
||
| async function readJson<T>(path: string): Promise<T> { | ||
| return JSON.parse(await readFile(path, "utf8")) as T; | ||
| } | ||
|
|
||
| /** Write the catalog, or with `check`, exit 1 when the committed one is stale. */ | ||
| export async function emit({ check }: { check: boolean }): Promise<void> { | ||
| const catalog = extractCommandCatalog( | ||
| routes as unknown as RouteMap, | ||
| await readJson<Record<string, ApiReference[]>>( | ||
| "src/generated/command-api.json", | ||
| ), | ||
| await readJson<Record<string, ApiAccess>>( | ||
| "src/generated/command-api-access.json", | ||
| ), | ||
| ); | ||
| const content = `${JSON.stringify(catalog, null, 2)}\n`; | ||
|
|
||
| if (check) { | ||
| const current = await readFile(COMMAND_CATALOG_PATH, "utf8").catch( | ||
| () => "", | ||
| ); | ||
| if (current !== content) { | ||
| console.error( | ||
| `✗ ${COMMAND_CATALOG_PATH} is stale. Run \`pnpm run generate:command-catalog\` in packages/cli and commit the result.`, | ||
| ); | ||
| process.exitCode = 1; | ||
| } else { | ||
| console.log( | ||
| `✓ ${COMMAND_CATALOG_PATH} is up to date (${catalog.commands.length} commands)`, | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
| await writeFile(COMMAND_CATALOG_PATH, content); | ||
| console.log( | ||
| `Generated ${COMMAND_CATALOG_PATH} (${catalog.commands.length} commands)`, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /** | ||
| * The command catalog: what agents need to find and run each command, from | ||
| * the route tree's introspection. Build-time only: `generate:command-catalog` | ||
| * writes it to `@sentry/mcp-core/command-catalog`, and MCP servers read that | ||
| * generated JSON instead of building schemas at runtime. | ||
| * | ||
| * Commands with exposure `hidden` are left out: no MCP server lists them. | ||
| */ | ||
|
|
||
| import type { | ||
| CommandCatalog, | ||
| CommandCatalogEntry, | ||
| JsonSchema, | ||
| } from "@sentry/mcp-core/command-catalog"; | ||
| import generatedCommandApi from "../generated/command-api.json"; | ||
| import type { ApiReference } from "./api-schema.js"; | ||
| import type { ApiAccess } from "./command-access.js"; | ||
| import { CHOICE_TARGET } from "./completion-target.js"; | ||
| import { | ||
| argumentCompletion, | ||
| buildCommandInfo, | ||
| type Command, | ||
| type FlagDef, | ||
| isCommand, | ||
| type PositionalParam, | ||
| publicTargetPaths, | ||
| type RouteMap, | ||
| } from "./introspect.js"; | ||
|
|
||
| /** | ||
| * Type of one positional or flag value. Only Stricli `enum` flags reject | ||
| * other values; the values of a `choice` parser are suggestions. | ||
| */ | ||
| function valueSchema( | ||
| def: Pick<FlagDef, "parse" | "placeholder" | "default">, | ||
| enumValues?: readonly string[], | ||
| ): JsonSchema { | ||
| if (enumValues) { | ||
| return { type: "string", enum: [...enumValues] }; | ||
| } | ||
| const { kind, values } = argumentCompletion(def.parse); | ||
| if (kind === CHOICE_TARGET && values) { | ||
| return { type: "string", examples: [...values] }; | ||
| } | ||
| // The same inference as the generated SDK's parameter types. | ||
| const numeric = | ||
| def.placeholder === "number" || | ||
| typeof def.default === "number" || | ||
| (typeof def.default === "string" && | ||
| def.default !== "" && | ||
| !Number.isNaN(Number(def.default))); | ||
| return { type: numeric ? "number" : "string" }; | ||
| } | ||
|
|
||
| /** JSON Schema of one flag. */ | ||
| function flagSchema(def: FlagDef): JsonSchema { | ||
| const value: JsonSchema = | ||
| def.kind === "boolean" | ||
| ? { type: "boolean" } | ||
| : valueSchema(def, def.kind === "enum" ? def.values : undefined); | ||
| const schema: JsonSchema = def.variadic | ||
| ? { type: "array", items: value } | ||
| : value; | ||
| if (def.brief) schema.description = def.brief; | ||
| if (def.default !== undefined) { | ||
| schema.default = | ||
| value.type === "number" ? Number(def.default) : def.default; | ||
| } | ||
| return schema; | ||
| } | ||
|
|
||
| /** JSON Schema of one positional. */ | ||
| function positionalSchema(param: PositionalParam, index: number): JsonSchema { | ||
| return { | ||
| ...valueSchema(param), | ||
| title: param.placeholder ?? `arg${index}`, | ||
| ...(param.brief ? { description: param.brief } : {}), | ||
| }; | ||
| } | ||
|
|
||
| /** JSON Schema of a command's positionals and visible flags. */ | ||
| function parametersSchema(command: Command): JsonSchema { | ||
| const { positional, flags = {} } = command.parameters; | ||
| const properties: Record<string, JsonSchema> = {}; | ||
| const required: string[] = []; | ||
|
|
||
| if (positional?.kind === "tuple" && positional.parameters.length > 0) { | ||
| const minItems = positional.parameters.filter((p) => !p.optional).length; | ||
| properties.positional = { | ||
| type: "array", | ||
| prefixItems: positional.parameters.map(positionalSchema), | ||
| items: false, | ||
| ...(minItems > 0 ? { minItems } : {}), | ||
| }; | ||
| if (minItems > 0) required.push("positional"); | ||
| } else if (positional?.kind === "array") { | ||
| properties.positional = { | ||
| type: "array", | ||
| items: positionalSchema(positional.parameter, 0), | ||
| }; | ||
| } | ||
|
|
||
| const flagProperties: Record<string, JsonSchema> = {}; | ||
| const requiredFlags: string[] = []; | ||
| for (const [name, def] of Object.entries(flags)) { | ||
| if (def.hidden) continue; | ||
| flagProperties[name] = flagSchema(def); | ||
| const optional = def.optional ?? def.kind === "boolean"; | ||
| if (!optional && def.default === undefined) requiredFlags.push(name); | ||
| } | ||
| if (Object.keys(flagProperties).length > 0) { | ||
| properties.flags = { | ||
| type: "object", | ||
| properties: flagProperties, | ||
| ...(requiredFlags.length > 0 ? { required: requiredFlags } : {}), | ||
| additionalProperties: false, | ||
| }; | ||
| if (requiredFlags.length > 0) required.push("flags"); | ||
| } | ||
|
|
||
| return { | ||
| type: "object", | ||
| properties, | ||
| ...(required.length > 0 ? { required } : {}), | ||
| additionalProperties: false, | ||
| }; | ||
| } | ||
|
|
||
| /** Completion kinds of a command's positionals and visible flags. */ | ||
| function completions(command: Command): CommandCatalogEntry["completions"] { | ||
| const { positional, flags = {} } = command.parameters; | ||
| const params = | ||
| positional?.kind === "tuple" | ||
| ? positional.parameters | ||
| : positional | ||
| ? [positional.parameter] | ||
| : []; | ||
| const positionalKinds = params.map( | ||
| (param) => argumentCompletion(param.parse).kind, | ||
| ); | ||
| const flagKinds: Record<string, string> = {}; | ||
| for (const [name, def] of Object.entries(flags)) { | ||
| if (def.hidden || def.kind === "boolean") continue; | ||
| const { kind } = argumentCompletion( | ||
| def.parse, | ||
| def.kind === "enum" ? def.values : undefined, | ||
| ); | ||
| if (kind) flagKinds[name] = kind; | ||
| } | ||
| const result: CommandCatalogEntry["completions"] = { | ||
| ...(positionalKinds.some((kind) => kind !== null) | ||
| ? { positional: positionalKinds } | ||
| : {}), | ||
| ...(Object.keys(flagKinds).length > 0 ? { flags: flagKinds } : {}), | ||
| }; | ||
| return Object.keys(result).length > 0 ? result : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Build the catalog of every command an MCP server may list: each public | ||
| * command whose exposure is not `hidden`, sorted by path. | ||
| * | ||
| * @param routeMap - Top-level Stricli route map | ||
| * @param apiByCommand - API references by command path (`command-api.json`) | ||
| * @param apiAccessByCommand - API request class by command path | ||
| * (`command-api-access.json`); introspection's import when omitted | ||
| */ | ||
| export function extractCommandCatalog( | ||
| routeMap: RouteMap, | ||
| apiByCommand: Readonly< | ||
| Record<string, readonly ApiReference[]> | ||
| > = generatedCommandApi, | ||
| apiAccessByCommand?: Readonly<Record<string, ApiAccess>>, | ||
| ): CommandCatalog { | ||
| const commands: CommandCatalogEntry[] = []; | ||
| for (const [command, segments] of publicTargetPaths(routeMap)) { | ||
| if (!isCommand(command)) continue; | ||
| const info = buildCommandInfo( | ||
| command, | ||
| `sentry ${segments.join(" ")}`, | ||
| apiAccessByCommand, | ||
| ); | ||
| const { exposure } = info; | ||
| if (exposure === "hidden") continue; | ||
| const localPathFlags = info.flags | ||
| .filter((flag) => flag.localPath && !flag.hidden) | ||
| .map((flag) => flag.name); | ||
| const completion = completions(command); | ||
| commands.push({ | ||
| path: info.path, | ||
| brief: info.brief, | ||
| permission: info.permission, | ||
| exposure, | ||
| parameters: parametersSchema(command), | ||
| ...(completion ? { completions: completion } : {}), | ||
| ...(localPathFlags.length > 0 ? { localPathFlags } : {}), | ||
| examples: info.examples, | ||
| ...(info.jsonFields ? { outputFields: info.jsonFields } : {}), | ||
| apiOperations: [...(apiByCommand[info.path] ?? [])], | ||
| }); | ||
| } | ||
| commands.sort((a, b) => a.path.localeCompare(b.path)); | ||
| return { commands }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.