dbt-get-macro
Retrieve a dbt macro's signature and raw SQL, and find all nodes that call it.
Instructions
Get a dbt macro: signature, raw SQL, and reverse-lookup of nodes that call it
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| uniqueId | No | dbt unique_id (e.g. 'macro.proj.my_macro') | |
| name | No | Macro name (resolved if uniqueId not provided) |
Implementation Reference
- src/tools/dbt-macros.ts:35-63 (handler)The main handler function for the dbt-get-macro tool. It loads the manifest, finds the macro by uniqueId or name, performs a reverse-lookup of all nodes/models that depend on it (usages), and returns the macro with its signature, raw SQL, and usage info.
export async function dbtGetMacro(args: z.infer<typeof dbtGetMacroSchema>): Promise<unknown> { const manifest = loadManifest(); let macro = args.uniqueId ? manifest.macros[args.uniqueId] : undefined; if (!macro && args.name) { macro = Object.values(manifest.macros).find((m) => m.name === args.name); } if (!macro) throw new Error(`Macro not found: ${args.uniqueId ?? args.name}`); // Find usages: any node whose depends_on.macros includes this macro const usages: string[] = []; for (const n of [...Object.values(manifest.nodes), ...Object.values(manifest.macros)]) { if ((n.depends_on?.macros ?? []).includes(macro.unique_id)) { usages.push(n.unique_id); } } return { uniqueId: macro.unique_id, name: macro.name, package: macro.package_name, path: macro.original_file_path, arguments: macro.arguments ?? [], description: macro.description, rawCode: macro.raw_code, dependsOnMacros: macro.depends_on?.macros ?? [], usages, usageCount: usages.length, }; } - src/tools/dbt-macros.ts:30-33 (schema)Zod schema for dbt-get-macro input validation: accepts optional 'uniqueId' (e.g. macro.proj.my_macro) or optional 'name' (macro name, used as fallback if uniqueId not provided).
export const dbtGetMacroSchema = z.object({ uniqueId: z.string().optional().describe("dbt unique_id (e.g. 'macro.proj.my_macro')"), name: z.string().optional().describe("Macro name (resolved if uniqueId not provided)"), }); - src/index.ts:85-85 (registration)Registration of the dbt-get-macro tool with its name, description, schema, and handler via tool() call.
tool("dbt-get-macro", "Get a dbt macro: signature, raw SQL, and reverse-lookup of nodes that call it", dbtGetMacroSchema.shape, wrapToolHandler(dbtGetMacro)); - src/clients/dbt-artifacts.ts:167-169 (helper)The loadManifest() helper that reads and caches the dbt manifest.json file, which is used by the dbtGetMacro handler to look up macro definitions and their dependencies.
export function loadManifest(): DbtManifest { return readWithCache<DbtManifest>("manifest", targetPath("manifest.json")); }