@@ -123,6 +123,114 @@ function summarizeToolTextOutput(value: string): string | null {
123123 return null ;
124124}
125125
126+ /**
127+ * Fields of an MCP tool-call item both clients render in the expanded
128+ * work-log row. Everything else — notably `result`, which carries the full
129+ * tool output and dominates wire size on MCP-heavy threads — is summarized
130+ * or dropped. Full payloads remain in persistence.
131+ */
132+ const MCP_ITEM_KEPT_FIELDS = [
133+ "type" ,
134+ "id" ,
135+ "tool" ,
136+ "server" ,
137+ "status" ,
138+ "arguments" ,
139+ "appContext" ,
140+ "error" ,
141+ "durationMs" ,
142+ ] as const ;
143+
144+ /**
145+ * Pulls renderable text out of an MCP tool result: either a Codex-style
146+ * `{content: [{type: "text", text}, ...]}` record or a raw Claude
147+ * `tool_result` block whose `content` is a string or block array.
148+ */
149+ function extractMcpResultText ( result : unknown ) : string | null {
150+ const record = asRecord ( result ) ;
151+ if ( ! record ) {
152+ return typeof result === "string" ? result : null ;
153+ }
154+ if ( typeof record . content === "string" ) {
155+ return record . content ;
156+ }
157+ if ( Array . isArray ( record . content ) ) {
158+ const texts : string [ ] = [ ] ;
159+ for ( const entry of record . content ) {
160+ const text = asRecord ( entry ) ?. text ;
161+ if ( typeof text === "string" && text . trim ( ) . length > 0 ) {
162+ texts . push ( text ) ;
163+ }
164+ }
165+ if ( texts . length > 0 ) {
166+ return texts . join ( "\n" ) ;
167+ }
168+ }
169+ return null ;
170+ }
171+
172+ function summarizeMcpResult ( result : unknown ) : Record < string , unknown > | undefined {
173+ if ( result === undefined || result === null ) {
174+ return undefined ;
175+ }
176+ const text = extractMcpResultText ( result ) ;
177+ const summary = text ? summarizeToolTextOutput ( text ) : null ;
178+ return summary ? { content : summary } : undefined ;
179+ }
180+
181+ /**
182+ * MCP tool calls carry full tool results (`data.item.result` on Codex,
183+ * `data.result` on Claude/OpenCode) that used to bypass slimming entirely to
184+ * keep the expanded-row UI working. Keep the fields the UI actually renders
185+ * and summarize the result like regular tool output.
186+ */
187+ function projectMcpToolCallData ( data : Record < string , unknown > ) : Record < string , unknown > {
188+ const projectedData : Record < string , unknown > = { } ;
189+
190+ const item = asRecord ( data . item ) ;
191+ if ( item ) {
192+ const projectedItem : Record < string , unknown > = { } ;
193+ for ( const key of MCP_ITEM_KEPT_FIELDS ) {
194+ if ( key in item ) {
195+ projectedItem [ key ] = item [ key ] ;
196+ }
197+ }
198+ const result = summarizeMcpResult ( item . result ) ;
199+ if ( result ) {
200+ projectedItem . result = result ;
201+ }
202+ projectedData . item = projectedItem ;
203+ }
204+
205+ if ( "toolName" in data ) {
206+ projectedData . toolName = data . toolName ;
207+ }
208+ if ( "input" in data ) {
209+ projectedData . input = data . input ;
210+ }
211+ if ( ! item ) {
212+ const result = summarizeMcpResult ( data . result ) ;
213+ if ( result ) {
214+ projectedData . result = result ;
215+ }
216+ }
217+
218+ if ( "toolCallId" in data ) {
219+ projectedData . toolCallId = data . toolCallId ;
220+ }
221+ if ( "kind" in data ) {
222+ projectedData . kind = data . kind ;
223+ }
224+
225+ const changedFiles : string [ ] = [ ] ;
226+ collectChangedFiles ( data , changedFiles , new Set < string > ( ) , 0 ) ;
227+ if ( changedFiles . length > 0 ) {
228+ projectedData . files = changedFiles . map ( ( path ) => ( { path } ) ) ;
229+ }
230+
231+ return projectedData ;
232+ }
233+
126234function projectRawOutput ( value : unknown ) : Record < string , unknown > | undefined {
127235 const rawOutput = asRecord ( value ) ;
128236 if ( ! rawOutput ) {
@@ -160,10 +268,20 @@ export function projectActivityPayload(
160268) : OrchestrationThreadActivity {
161269 const payload = asRecord ( activity . payload ) ;
162270 const data = asRecord ( payload ?. data ) ;
163- if ( ! payload || ! data || payload . itemType === "mcp_tool_call" ) {
271+ if ( ! payload || ! data ) {
164272 return activity ;
165273 }
166274
275+ if ( payload . itemType === "mcp_tool_call" ) {
276+ return {
277+ ...activity ,
278+ payload : {
279+ ...payload ,
280+ data : projectMcpToolCallData ( data ) ,
281+ } ,
282+ } ;
283+ }
284+
167285 const projectedData : Record < string , unknown > = { } ;
168286 const item = projectCommandData ( data ) ;
169287 if ( item ) {
0 commit comments