@@ -63,6 +63,7 @@ import {
6363 type ProviderUserInputAnswers ,
6464 type ProviderThreadId ,
6565 type ThreadId ,
66+ type ToolActivitySource ,
6667} from "@t3tools/contracts" ;
6768
6869import * as Cause from "effect/Cause" ;
@@ -2016,6 +2017,66 @@ function claudeToolUseBlocksFromAssistantMessage(
20162017 return message . message . content . filter ( isClaudeToolUseContentBlock ) ;
20172018}
20182019
2020+ interface ClaudeToolPresentation {
2021+ readonly title : string ;
2022+ readonly toolSource ?: ToolActivitySource ;
2023+ }
2024+
2025+ function boundedMetaText ( value : unknown , maxLength : number ) : string | undefined {
2026+ if ( typeof value !== "string" ) return undefined ;
2027+ const text = value . trim ( ) . replace ( / \s + / gu, " " ) ;
2028+ return text . length > 0 && text . length <= maxLength ? text : undefined ;
2029+ }
2030+
2031+ function metaHttpUrl ( value : unknown ) : string | undefined {
2032+ if ( typeof value !== "string" || value . length > 4096 ) return undefined ;
2033+ try {
2034+ const url = new URL ( value ) ;
2035+ return ( url . protocol === "https:" || url . protocol === "http:" ) && url . href . length <= 4096
2036+ ? url . href
2037+ : undefined ;
2038+ } catch {
2039+ return undefined ;
2040+ }
2041+ }
2042+
2043+ /**
2044+ * Claude Code's own label for each tool use (the MCP tool's title, or its
2045+ * humanized name) plus the server's name and icon. The CLI sends these as
2046+ * `tool_use_meta` on assistant frames; the SDK types do not declare it yet.
2047+ */
2048+ function claudeToolPresentationsFromAssistantMessage (
2049+ message : SDKMessage ,
2050+ ) : ReadonlyMap < string , ClaudeToolPresentation > {
2051+ const presentations = new Map < string , ClaudeToolPresentation > ( ) ;
2052+ const meta = message . type === "assistant" ? Reflect . get ( message , "tool_use_meta" ) : undefined ;
2053+ if ( ! Array . isArray ( meta ) ) return presentations ;
2054+ for ( const entry of meta ) {
2055+ if ( typeof entry !== "object" || entry === null ) continue ;
2056+ const id = boundedMetaText ( Reflect . get ( entry , "id" ) , 512 ) ;
2057+ const title = boundedMetaText ( Reflect . get ( entry , "display_name" ) , 160 ) ;
2058+ if ( id === undefined || title === undefined ) continue ;
2059+ const serverName = boundedMetaText ( Reflect . get ( entry , "server_display_name" ) , 160 ) ;
2060+ const iconUrl = metaHttpUrl ( Reflect . get ( entry , "icon_url" ) ) ;
2061+ presentations . set ( id , {
2062+ title,
2063+ ...( serverName === undefined
2064+ ? { }
2065+ : {
2066+ toolSource : {
2067+ key : `mcp:${ serverName . toLowerCase ( ) } ` ,
2068+ name : serverName ,
2069+ kind : "integration" ,
2070+ ...( iconUrl === undefined
2071+ ? { }
2072+ : { icon : { _tag : "themed-logo" , logoUrl : iconUrl } as const } ) ,
2073+ } ,
2074+ } ) ,
2075+ } ) ;
2076+ }
2077+ return presentations ;
2078+ }
2079+
20192080function claudeToolResultBlocksFromAssistantMessage (
20202081 message : SDKMessage ,
20212082) : ReadonlyArray < ClaudeToolResultContentBlock > {
@@ -2619,6 +2680,7 @@ interface ActiveClaudeToolCall {
26192680 readonly parentNodeId : OrchestrationV2ExecutionNode [ "id" ] ;
26202681 readonly ordinal : number ;
26212682 readonly startedAt : DateTime . Utc ;
2683+ readonly presentation ?: ClaudeToolPresentation ;
26222684}
26232685
26242686// What is known about a subagent before its task_started registers it,
@@ -3588,6 +3650,7 @@ export function makeClaudeAdapterV2(
35883650 > ;
35893651 readonly startedAt : DateTime . Utc ;
35903652 readonly updatedAt : DateTime . Utc ;
3653+ readonly presentation : ClaudeToolPresentation | undefined ;
35913654 } ) => {
35923655 const completedAt = input . status === "running" ? null : input . updatedAt ;
35933656 const nodeId = idAllocator . derive . nodeFromProviderItem ( {
@@ -3645,7 +3708,10 @@ export function makeClaudeAdapterV2(
36453708 parentItemId : null ,
36463709 ordinal : input . ordinal ,
36473710 status : input . status ,
3648- title : readPath !== undefined ? formatReadToolLabel ( readPath ) : ( searchTitle ?? null ) ,
3711+ title :
3712+ readPath !== undefined
3713+ ? formatReadToolLabel ( readPath )
3714+ : ( searchTitle ?? input . presentation ?. title ?? null ) ,
36493715 startedAt : input . startedAt ,
36503716 completedAt,
36513717 updatedAt : input . updatedAt ,
@@ -3708,6 +3774,9 @@ export function makeClaudeAdapterV2(
37083774 : {
37093775 ...itemBase ,
37103776 type : "dynamic_tool" ,
3777+ ...( input . presentation ?. toolSource === undefined
3778+ ? { }
3779+ : { toolSource : input . presentation . toolSource } ) ,
37113780 toolName : input . toolName ,
37123781 ...( viewedImagePath === undefined ? { } : { viewedImagePath } ) ,
37133782 input : claudeNativeToolInputValue ( input . toolInput ) ,
@@ -4378,10 +4447,38 @@ export function makeClaudeAdapterV2(
43784447 readonly toolName : string ;
43794448 readonly toolInput : ClaudeNativeToolInput ;
43804449 readonly parentToolUseId : string | null ;
4450+ readonly presentation ?: ClaudeToolPresentation | undefined ;
43814451 } ) {
43824452 const existing = input . context . toolCalls . get ( input . nativeItemId ) ;
43834453 if ( existing !== undefined ) {
4384- return existing ;
4454+ // The permission callback can start a call before its assistant
4455+ // frame arrives with the tool's display name and icon.
4456+ if ( input . presentation === undefined || existing . presentation !== undefined ) {
4457+ return existing ;
4458+ }
4459+ const presented = { ...existing , presentation : input . presentation } ;
4460+ input . context . toolCalls . set ( input . nativeItemId , presented ) ;
4461+ const updatedAt = yield * DateTime . now ;
4462+ yield * emitToolCallArtifacts (
4463+ buildToolCallArtifacts ( {
4464+ context : input . context ,
4465+ nativeItemId : presented . nativeItemId ,
4466+ toolName : presented . toolName ,
4467+ classification : presented . classification ,
4468+ toolInput : presented . input ,
4469+ threadId : presented . threadId ,
4470+ runId : presented . runId ,
4471+ rootNodeId : presented . rootNodeId ,
4472+ parentNodeId : presented . parentNodeId ,
4473+ ordinal : presented . ordinal ,
4474+ output : NO_CLAUDE_NATIVE_TOOL_OUTPUT ,
4475+ status : "running" ,
4476+ startedAt : presented . startedAt ,
4477+ updatedAt,
4478+ presentation : input . presentation ,
4479+ } ) ,
4480+ ) ;
4481+ return presented ;
43854482 }
43864483 const startedAt = yield * DateTime . now ;
43874484 const classification = classifyClaudeNativeTool ( input . toolName ) ;
@@ -4408,6 +4505,7 @@ export function makeClaudeAdapterV2(
44084505 parentNodeId,
44094506 ordinal,
44104507 startedAt,
4508+ ...( input . presentation === undefined ? { } : { presentation : input . presentation } ) ,
44114509 } ;
44124510 input . context . toolCalls . set ( input . nativeItemId , toolCall ) ;
44134511 yield * emitToolCallArtifacts (
@@ -4426,6 +4524,7 @@ export function makeClaudeAdapterV2(
44264524 status : "running" ,
44274525 startedAt,
44284526 updatedAt : startedAt ,
4527+ presentation : input . presentation ,
44294528 } ) ,
44304529 ) ;
44314530 return toolCall ;
@@ -4614,6 +4713,7 @@ export function makeClaudeAdapterV2(
46144713 status : "failed" ,
46154714 startedAt : toolCall . startedAt ,
46164715 updatedAt : input . completedAt ,
4716+ presentation : toolCall . presentation ,
46174717 } ) ;
46184718 yield * emitToolCallArtifacts ( artifacts ) ;
46194719 }
@@ -5816,6 +5916,7 @@ export function makeClaudeAdapterV2(
58165916 }
58175917 }
58185918
5919+ const toolPresentations = claudeToolPresentationsFromAssistantMessage ( message ) ;
58195920 for ( const toolUse of claudeToolUseBlocksFromAssistantMessage ( message ) ) {
58205921 const nativeToolInput = claudeNativeToolInputFromUnknown ( toolUse . input ) ;
58215922 if ( toolUse . name === "Agent" ) {
@@ -5841,6 +5942,7 @@ export function makeClaudeAdapterV2(
58415942 toolName : toolUse . name ,
58425943 toolInput : nativeToolInput ,
58435944 parentToolUseId : parentToolUseIdFromSdkMessage ( message ) ,
5945+ presentation : toolPresentations . get ( toolUse . id ) ,
58445946 } ) ;
58455947 const heldPlan = heldProposedPlansByToolUseId . get ( toolUse . id ) ;
58465948 if ( heldPlan !== undefined ) {
@@ -5904,6 +6006,7 @@ export function makeClaudeAdapterV2(
59046006 status : isClaudeToolResultError ( toolResult ) ? "failed" : "completed" ,
59056007 startedAt : toolCall . startedAt ,
59066008 updatedAt : completedAt ,
6009+ presentation : toolCall . presentation ,
59076010 } ) ;
59086011 yield * emitToolCallArtifacts ( artifacts ) ;
59096012 context . toolCalls . delete ( toolCall . nativeItemId ) ;
0 commit comments