@@ -43,6 +43,81 @@ export async function serverInstructions(): Promise<string> {
4343 }
4444}
4545
46+ // altimate_change start — lineage tools answer with structured content as well as text
47+ // Lineage tools also return their metadata as structured content, so a client that draws lineage
48+ // (the Claude Code plugin's lineage pane) reads edges and downstream lists without parsing text.
49+ // Claude Code shows structured content to the model in place of the text, so `report` carries the
50+ // text (for dbt_lineage only its header: the mappings and edges travel as data), only the lineage
51+ // fields are passed, and the whole is capped.
52+ const STRUCTURED_FIELDS : Record < string , readonly string [ ] > = {
53+ dbt_lineage : [ "model_name" , "model_unique_id" , "confidence" , "confidence_factors" , "upstream" , "columns" , "edges" , "error" ] ,
54+ impact_analysis : [
55+ "model" ,
56+ "column" ,
57+ "change_type" ,
58+ "severity" ,
59+ "direct_count" ,
60+ "transitive_count" ,
61+ "test_count" ,
62+ "downstream" ,
63+ "exposures" ,
64+ "column_reach" ,
65+ "column_reach_error" ,
66+ "exposures_error" ,
67+ "error" ,
68+ ] ,
69+ }
70+ export const STRUCTURED_TOOLS : ReadonlySet < string > = new Set ( Object . keys ( STRUCTURED_FIELDS ) )
71+ const STRUCTURED_OUTPUT_SCHEMA = {
72+ type : "object" as const ,
73+ properties : {
74+ report : { type : "string" , description : "The tool's text report" } ,
75+ truncated : { type : "boolean" , description : "True when the lineage fields were left out for size; the report is kept, shortened if it is too large on its own" } ,
76+ } ,
77+ required : [ "report" ] ,
78+ additionalProperties : true ,
79+ }
80+ // The structured result reaches the model in Claude Code, so it stays within the same order of
81+ // size as a text result: past this, only the report is sent. Claude Code caps an MCP result at
82+ // 25k tokens by default and JSON of ids and edges runs ~3-4 characters a token, so this stays under that.
83+ const STRUCTURED_MAX_BYTES = 60_000
84+
85+ export function structuredResult ( tool : string , metadata : Record < string , unknown > , report : string ) : Record < string , unknown > {
86+ // A tool that builds its own structured result (dbt_lineage from the dbt integration) hands it
87+ // over whole; otherwise the lineage fields of its metadata are passed.
88+ const own = metadata . structuredContent
89+ if ( own && typeof own === "object" && ! Array . isArray ( own ) ) {
90+ // The text repeats every edge that `columnLineage` already carries: only its header goes with them.
91+ const head = report . split ( / \n (?: C o l u m n e d g e s \( | T r a c e o f ) / ) [ 0 ] ! . trim ( )
92+ const full = { ...( own as Record < string , unknown > ) , report : head }
93+ return Buffer . byteLength ( JSON . stringify ( full ) ) <= STRUCTURED_MAX_BYTES ? full : capReport ( report , true )
94+ }
95+ const fields = Object . fromEntries ( ( STRUCTURED_FIELDS [ tool ] ?? [ ] ) . filter ( ( key ) => key in metadata ) . map ( ( key ) => [ key , metadata [ key ] ] ) )
96+ // dbt_lineage's text prints the same mappings and edges as `columns` and `edges`: keep its header.
97+ const head = tool === "dbt_lineage" && "columns" in fields ? report . split ( / \n (?: C o l u m n M a p p i n g s | L i n e a g e E d g e s ) : / ) [ 0 ] ! . trim ( ) : report
98+ const full = { ...fields , report : head }
99+ return Buffer . byteLength ( JSON . stringify ( full ) ) <= STRUCTURED_MAX_BYTES ? full : capReport ( report , true )
100+ }
101+ /** An error report held to the same cap as a result, so a huge exception message cannot exceed it. */
102+ export function structuredError ( message : string ) : Record < string , unknown > {
103+ return capReport ( message , false )
104+ }
105+ // `dropped`: the result's other fields were left out, so it is marked truncated even when the
106+ // report itself fits.
107+ function capReport ( message : string , dropped : boolean ) : Record < string , unknown > {
108+ // Measured as sent: JSON escaping can grow the text (a newline is two bytes, a control
109+ // character six), so the cap applies to the serialized result, shortening until it fits.
110+ const size = ( value : Record < string , unknown > ) => Buffer . byteLength ( JSON . stringify ( value ) )
111+ const whole = dropped ? { report : message , truncated : true } : { report : message }
112+ if ( size ( whole ) <= STRUCTURED_MAX_BYTES ) return whole
113+ let report = message
114+ while ( report . length > 0 && size ( { report, truncated : true } ) > STRUCTURED_MAX_BYTES ) {
115+ report = report . slice ( 0 , Math . floor ( report . length * 0.9 ) )
116+ }
117+ return { report, truncated : true }
118+ }
119+ // altimate_change end
120+
46121/** Spilled results: a resource_link next to the text, which keeps the preview, row count and path. */
47122function resultLinks ( metadata : Record < string , unknown > ) {
48123 // A batched sql_execute call reports one saved file per spilled query.
@@ -97,6 +172,7 @@ export async function createServer(options: ServeOptions = {}) {
97172 name,
98173 description : descriptions [ name ] ?? tool . description ,
99174 inputSchema : tool . jsonSchema as McpTool [ "inputSchema" ] ,
175+ ...( STRUCTURED_TOOLS . has ( name ) && { outputSchema : STRUCTURED_OUTPUT_SCHEMA } ) ,
100176 } ) ) , ...( workspace ?. tools ?? [ ] ) ] ,
101177 } ) )
102178 // Ask the user once per session when the model weighed two readings and the client can ask; any
@@ -149,11 +225,16 @@ export async function createServer(options: ServeOptions = {}) {
149225 return {
150226 content : [ { type : "text" as const , text : result . output } , ...resultLinks ( result . metadata ) ] ,
151227 isError,
228+ ...( STRUCTURED_TOOLS . has ( request . params . name ) && {
229+ structuredContent : structuredResult ( request . params . name , result . metadata , result . output ) ,
230+ } ) ,
152231 }
153232 } catch ( error ) {
233+ const message = error instanceof Error ? error . message : String ( error )
154234 return {
155- content : [ { type : "text" as const , text : error instanceof Error ? error . message : String ( error ) } ] ,
235+ content : [ { type : "text" as const , text : message } ] ,
156236 isError : true ,
237+ ...( STRUCTURED_TOOLS . has ( request . params . name ) && { structuredContent : structuredError ( message ) } ) ,
157238 }
158239 }
159240 }
@@ -193,8 +274,13 @@ export async function serveStdio(options: ServeOptions = {}): Promise<void> {
193274 const onclose = server . onclose
194275 server . onclose = async ( ) => {
195276 try {
196- await onclose ?.( )
197- await Registry . closeAll ( )
277+ try {
278+ await onclose ?.( )
279+ await Registry . closeAll ( )
280+ } finally {
281+ // The dbt adapter's Python bridge is stopped even when closing the rest fails.
282+ await ( await import ( "../native/connections/register" ) ) . disposeDbtAdapter ( )
283+ }
198284 closed . resolve ( )
199285 } catch ( error ) {
200286 closed . reject ( error )
0 commit comments