dbt-get-model
Retrieve a single dbt model including refs, sources, columns with catalog types, attached tests, and raw/compiled SQL.
Instructions
Get a single dbt model: refs, sources, columns (with catalog types if available), attached tests, raw/compiled SQL
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| uniqueId | No | dbt unique_id (e.g. 'model.us_dbt.users_dim') | |
| name | No | Model name (resolved if uniqueId not provided) | |
| includeCompiledSql | No | Include compiled_code in response |
Implementation Reference
- src/tools/dbt-models.ts:66-128 (handler)The main handler function for the 'dbt-get-model' tool. Loads a dbt model by uniqueId or name from the manifest, resolves refs, sources, columns (with catalog types), attached tests, and raw/compiled SQL.
export async function dbtGetModel(args: z.infer<typeof dbtGetModelSchema>): Promise<unknown> { const manifest = loadManifest(); const catalog = loadCatalog(); let node: DbtNode | undefined; if (args.uniqueId) { node = manifest.nodes[args.uniqueId]; } else if (args.name) { node = Object.values(manifest.nodes).find((n) => isModel(n) && n.name === args.name); } else { throw new Error("Provide uniqueId or name"); } if (!node) throw new Error(`Model not found: ${args.uniqueId ?? args.name}`); const refs = (node.refs ?? []).map(refKey); const sources = (node.sources ?? []).map((s) => s.join(".")); const dependsOn = node.depends_on?.nodes ?? []; const catalogEntry = catalog?.nodes[node.unique_id]; const columns = node.columns ? Object.values(node.columns).map((c) => ({ name: c.name, dataType: catalogEntry?.columns?.[c.name]?.type ?? c.data_type ?? null, description: c.description, tags: c.tags ?? [], })) : []; // Tests attached to this model const tests: Array<{ uniqueId: string; name: string; column?: string; severity?: string }> = []; for (const t of Object.values(manifest.nodes)) { if (t.resource_type !== "test") continue; const dependsNodes = t.depends_on?.nodes ?? []; if (dependsNodes.includes(node.unique_id) || t.attached_node === node.unique_id) { tests.push({ uniqueId: t.unique_id, name: t.name, column: t.column_name, severity: t.severity ?? t.config?.meta?.severity as string | undefined, }); } } return { uniqueId: node.unique_id, name: node.name, package: node.package_name, schema: node.schema, database: node.database, alias: node.alias, materialized: node.config?.materialized, tags: node.tags ?? node.config?.tags ?? [], path: node.original_file_path, description: node.description, refs, sources, dependsOn, columns, tests, rawCode: node.raw_code, compiledCode: args.includeCompiledSql ? node.compiled_code : undefined, }; } - src/tools/dbt-models.ts:60-64 (schema)Zod schema defining input parameters for dbt-get-model: uniqueId (optional), name (optional), and includeCompiledSql (boolean, default false).
export const dbtGetModelSchema = z.object({ uniqueId: z.string().optional().describe("dbt unique_id (e.g. 'model.us_dbt.users_dim')"), name: z.string().optional().describe("Model name (resolved if uniqueId not provided)"), includeCompiledSql: z.boolean().default(false).describe("Include compiled_code in response"), }); - src/index.ts:78-78 (registration)Registration of the 'dbt-get-model' tool with the MCP server using the schema and wrapped handler.
tool("dbt-get-model", "Get a single dbt model: refs, sources, columns (with catalog types if available), attached tests, raw/compiled SQL", dbtGetModelSchema.shape, wrapToolHandler(dbtGetModel)); - src/tools/dbt-models.ts:13-16 (helper)Helper function 'refKey' used to extract the name from a ref entry (supports both array and object formats).
function refKey(ref: string[] | { name: string; package?: string | null }): string { if (Array.isArray(ref)) return ref[ref.length - 1]!; return ref.name; } - src/tools/dbt-models.ts:9-11 (helper)Helper function 'isModel' used to check if a node is a model resource type.
function isModel(node: DbtNode): boolean { return node.resource_type === "model"; }