Skip to content

Commit 9702895

Browse files
fix(server): Claude can show the user mid-turn text in full
Claude 5.1+ returns text written before a tool call as a progress-update thinking block, and the API returns only a summary of it. A draft written before a question card reached the user as a one-line paraphrase (#32877). Following Anthropic's guidance, T3's MCP server gains `send_to_user`, loaded up front in Claude Code, and Claude's appended instructions say when to use it. Web and mobile render its message as the agent's own text, kept visible through the turn fold. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e94db17 commit 9702895

20 files changed

Lines changed: 281 additions & 9 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,24 @@ function renderFeedEntry(
17381738
);
17391739
}
17401740

1741+
if (entry.type === "sent-message") {
1742+
return (
1743+
<View className="mb-1 px-1">
1744+
<MarkdownImageAvailableWidthContext value={props.markdownContentWidth}>
1745+
<AssistantMarkdownContent
1746+
markdown={entry.text}
1747+
environmentId={props.environmentId}
1748+
markdownStyles={markdownStyles.assistant}
1749+
linkHandlers={props.markdownLinkHandlers}
1750+
onUseArtifactTemplate={props.onUseArtifactTemplate}
1751+
renderImage={props.renderMarkdownImage}
1752+
skills={props.skills}
1753+
/>
1754+
</MarkdownImageAvailableWidthContext>
1755+
</View>
1756+
);
1757+
}
1758+
17411759
if (entry.type === "viewed-images") {
17421760
const thumbnailWidth = Math.floor((props.contentWidth - VIEWED_IMAGE_GAP) / 2);
17431761
return (

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import type { McpAppReference } from "@t3tools/shared/mcpApp";
7373
import {
7474
compactDynamicToolOutput,
7575
htmlRenderFromToolItem,
76+
sentMessageFromToolItem,
7677
mcpAppFromToolItem,
7778
} from "@t3tools/shared/toolOutput";
7879
import * as DateTime from "effect/DateTime";
@@ -182,6 +183,13 @@ type RawThreadFeedEntry =
182183
readonly runId: RunId | null;
183184
readonly render: HtmlRenderReference;
184185
}
186+
| {
187+
readonly type: "sent-message";
188+
readonly id: string;
189+
readonly createdAt: string;
190+
readonly runId: RunId | null;
191+
readonly text: string;
192+
}
185193
| {
186194
readonly type: "mcp-app";
187195
readonly id: string;
@@ -200,7 +208,7 @@ export type ThreadFeedEntry = ThreadFeedEntryContent & {
200208
};
201209

202210
type ThreadFeedEntryContent =
203-
| Extract<RawThreadFeedEntry, { type: "message" | "html-render" | "mcp-app" }>
211+
| Extract<RawThreadFeedEntry, { type: "message" | "html-render" | "sent-message" | "mcp-app" }>
204212
| {
205213
readonly type: "activity-group";
206214
readonly id: string;
@@ -1098,7 +1106,10 @@ function feedEntryFoldRunId(entry: ThreadFeedEntry, runlessKey: RunId | null): R
10981106
if (entry.type === "message") {
10991107
return entry.message.role === "assistant" ? (entry.message.runId ?? runlessKey) : null;
11001108
}
1101-
return entry.type === "activity-group" || entry.type === "html-render" || entry.type === "mcp-app"
1109+
return entry.type === "activity-group" ||
1110+
entry.type === "html-render" ||
1111+
entry.type === "sent-message" ||
1112+
entry.type === "mcp-app"
11021113
? (entry.runId ?? runlessKey)
11031114
: null;
11041115
}
@@ -1227,6 +1238,7 @@ function deriveThreadFeedRunFolds(
12271238
entry.id !== terminalAssistantId &&
12281239
!(entry.type === "message" && retainedAnswerIds.has(entry.message.id)) &&
12291240
entry.type !== "html-render" &&
1241+
entry.type !== "sent-message" &&
12301242
entry.type !== "mcp-app" &&
12311243
entry.id !== visibleCompactionId &&
12321244
!(
@@ -2429,6 +2441,23 @@ export function buildThreadFeed(
24292441
entryRows.push(index);
24302442
continue;
24312443
}
2444+
const sentMessage =
2445+
item.type === "dynamic_tool" && item.status === "completed"
2446+
? sentMessageFromToolItem(item)
2447+
: undefined;
2448+
if (sentMessage !== undefined) {
2449+
const entry: RawThreadFeedEntry = {
2450+
type: "sent-message",
2451+
id: `sent-message:${row.visibility}:${row.sourceThreadId}:${row.sourceItemId}`,
2452+
createdAt,
2453+
runId: item.runId,
2454+
text: sentMessage,
2455+
};
2456+
projectedEntriesCache.set(row, { attemptId, entries: [entry] });
2457+
entries.push(entry);
2458+
entryRows.push(index);
2459+
continue;
2460+
}
24322461
if (item.type === "user_message" || item.type === "assistant_message") {
24332462
const updatedAt = DateTime.formatIso(item.updatedAt);
24342463
const entry: RawThreadFeedEntry = {

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ import {
8484
DeviceStandardToolkit,
8585
} from "./toolkits/device/tools.ts";
8686
import * as HtmlHandlers from "./toolkits/html/handlers.ts";
87+
import { MessageToolkit } from "./toolkits/message/tools.ts";
88+
import * as MessageHandlers from "./toolkits/message/handlers.ts";
8789
import { HtmlPreviewTool, HtmlPreviewToolkit, HtmlRenderToolkit } from "./toolkits/html/tools.ts";
8890
import { requestUrl } from "../requestUrl.ts";
8991

@@ -1239,6 +1241,8 @@ export const layerOrchestratorToolkit = toolkitRegistration(
12391241

12401242
export const layerThreadToolkit = toolkitRegistration(ThreadToolkit, ThreadHandlers.layer);
12411243

1244+
const layerMessageToolkit = toolkitRegistration(MessageToolkit, MessageHandlers.layer);
1245+
12421246
const layerWorktreeToolkitRegistration = toolkitRegistration(
12431247
WorktreeToolkit,
12441248
WorktreeHandlers.layer,
@@ -1324,6 +1328,7 @@ export const layerTools = Layer.mergeAll(
13241328
layerPullRequestsToolkit,
13251329
layerDeviceToolkit,
13261330
layerHtmlToolkit,
1331+
layerMessageToolkit,
13271332
);
13281333

13291334
const layerToolExposure = Layer.effect(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as Effect from "effect/Effect";
2+
3+
import * as McpInvocationContext from "../../McpInvocationContext.ts";
4+
import * as McpToolAccess from "../../McpToolAccess.ts";
5+
import { MessageToolkit } from "./tools.ts";
6+
7+
const handlers = {
8+
send_to_user: McpToolAccess.actsAsCaller((input) =>
9+
Effect.gen(function* () {
10+
const scope = yield* McpInvocationContext.McpInvocationContext;
11+
yield* McpInvocationContext.requireThreadScope(scope, "send_to_user");
12+
return {
13+
sentMessage: input.message,
14+
message: "Shown to the user. Don't repeat it.",
15+
};
16+
}),
17+
),
18+
} satisfies McpToolAccess.Handlers<typeof MessageToolkit.tools>;
19+
20+
export const layer = McpToolAccess.toLayer(MessageToolkit, handlers);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { OrchestratorMcpFailure } from "@t3tools/contracts";
2+
import { SEND_TO_USER_MAX_LENGTH, SEND_TO_USER_TOOL_NAME } from "@t3tools/shared/toolOutput";
3+
import * as Schema from "effect/Schema";
4+
import { Tool, Toolkit } from "effect/ai";
5+
6+
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
7+
import * as McpInvocationContext from "../../McpInvocationContext.ts";
8+
9+
// Claude 5.1+ returns text written before a tool call only as a short summary; tool input arrives intact.
10+
// It affects only the caller's thread, so read-only sandboxes and plan mode may use it.
11+
const SendToUserTool = Tool.make(SEND_TO_USER_TOOL_NAME, {
12+
description:
13+
"Show the user a Markdown message in this thread, exactly as written, before your turn ends. Use it for content the user must read in full mid-turn, such as a draft, a code block, or what a question you are about to ask refers to. Don't repeat it in your reply.",
14+
parameters: Schema.Struct({
15+
message: Schema.String.check(
16+
Schema.isMinLength(1),
17+
Schema.isMaxLength(SEND_TO_USER_MAX_LENGTH),
18+
).annotate({ description: "Markdown, shown verbatim." }),
19+
}),
20+
success: Schema.Struct({
21+
sentMessage: Schema.String,
22+
message: Schema.String,
23+
}),
24+
failure: OrchestratorMcpFailure,
25+
failureMode: "return",
26+
dependencies: [
27+
McpInvocationContext.McpInvocationContext,
28+
ThreadManagementService.ThreadManagementService,
29+
],
30+
})
31+
.annotate(Tool.Title, "Send a message to the user")
32+
.annotate(Tool.Readonly, true)
33+
.annotate(Tool.Destructive, false)
34+
.annotate(Tool.Idempotent, false)
35+
.annotate(Tool.OpenWorld, false)
36+
// Claude Code defers MCP tools behind ToolSearch; Anthropic says to declare this from the first request.
37+
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true });
38+
39+
export const MessageToolkit = Toolkit.make(SendToUserTool);

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import { T3_CODE_DEVICE_TOOL_INSTRUCTIONS_WITH_TOOL_SEARCH } from "@t3tools/prov
9090
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
9191
import { PreviewControlsToolkit } from "../../mcp/toolkits/previewControls/tools.ts";
9292
import { HtmlToolkit } from "../../mcp/toolkits/html/tools.ts";
93+
import { MessageToolkit } from "../../mcp/toolkits/message/tools.ts";
9394
import { EnvironmentToolkit } from "../../mcp/toolkits/environment/tools.ts";
9495
import { ProjectToolkit } from "../../mcp/toolkits/project/tools.ts";
9596
import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts";
@@ -663,6 +664,7 @@ describe("ClaudeAdapterV2 MCP query overrides", () => {
663664
...Object.values(EnvironmentToolkit.tools),
664665
...Object.values(PreviewControlsToolkit.tools),
665666
...Object.values(HtmlToolkit.tools),
667+
...Object.values(MessageToolkit.tools),
666668
]
667669
.filter((tool) => Context.get(tool.annotations, Tool.Readonly))
668670
.map((tool) => `mcp__t3-code__${tool.name}`)

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,14 @@ const CLAUDE_BACKGROUND_WAIT_INSTRUCTIONS = `
19541954
To wait for a background command or Monitor you started (tests, a build), call \`t3_background_wait\` with its task ID before you end your turn. Do not mark work you leave running, or PRs. T3 MCP tools are deferred: load one with ToolSearch before you call it.
19551955
`;
19561956

1957+
// Claude 5.1+ returns text written before a tool call as a progress update, which the API
1958+
// returns only as a summary.
1959+
const CLAUDE_SEND_TO_USER_INSTRUCTIONS = `
1960+
### Text the user must read mid-turn
1961+
1962+
Text you write before a tool call may reach the user only as a one-line summary. When the user must read something in full before your turn ends, such as a draft, a code block, or what a question you're about to ask refers to, send it with \`send_to_user\`. Put everything else in your final reply.
1963+
`;
1964+
19571965
// Native subagents start from their task, not this session's system prompt.
19581966
const subagentDeviceInstructions: HookCallback = () =>
19591967
Promise.resolve({
@@ -2132,6 +2140,7 @@ export function makeClaudeQueryOptions(input: {
21322140
? ""
21332141
: T3_CODE_ORCHESTRATION_INSTRUCTIONS +
21342142
CLAUDE_BACKGROUND_WAIT_INSTRUCTIONS +
2143+
CLAUDE_SEND_TO_USER_INSTRUCTIONS +
21352144
(deviceInstructions
21362145
? `\n${T3_CODE_DEVICE_TOOL_INSTRUCTIONS_WITH_TOOL_SEARCH}\n`
21372146
: "")),
@@ -2252,6 +2261,7 @@ export const CLAUDE_READ_ONLY_T3_MCP_ALLOWED_TOOLS: ReadonlyArray<string> = [
22522261
"mcp__t3-code__t3_environment_links",
22532262
"mcp__t3-code__html_preview",
22542263
"mcp__t3-code__html_render",
2264+
"mcp__t3-code__send_to_user",
22552265
];
22562266

22572267
// T3 waits can block for an hour; exceed Claude's default 60 s timeout.

‎apps/server/src/provider/AgentContextBudget.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,16 @@ const layerAdvertisedTools = McpHttpServer.layerTools.pipe(
116116
);
117117

118118
// Claude Code loads these tools up front instead of behind ToolSearch, outside this budget.
119-
it.effect("only request_secret asks Claude Code to load it up front", () =>
119+
it.effect("only request_secret and send_to_user ask Claude Code to load them up front", () =>
120120
Effect.gen(function* () {
121121
const mcp = yield* McpServer.McpServer;
122122
const alwaysLoad = mcp.tools
123123
.filter(({ tool }) => tool._meta?.["anthropic/alwaysLoad"] !== undefined)
124124
.map(({ tool }) => [tool.name, tool._meta]);
125-
expect(alwaysLoad).toEqual([["request_secret", { "anthropic/alwaysLoad": true }]]);
125+
expect(alwaysLoad).toEqual([
126+
["request_secret", { "anthropic/alwaysLoad": true }],
127+
["send_to_user", { "anthropic/alwaysLoad": true }],
128+
]);
126129
}).pipe(Effect.provide(layerAdvertisedTools)),
127130
);
128131

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"claudeSystemAppendBytes": 5594,
2+
"claudeSystemAppendBytes": 5930,
33
"codexAdditionalContextBytes": 7324,
4-
"mcpToolListJsonBytes": 48094
4+
"mcpToolListJsonBytes": 48566
55
}

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export function timelineEntryRunId(entry: TimelineEntry): RunId | null {
8282
if (entry.kind === "proposed-plan") {
8383
return entry.proposedPlan.runId;
8484
}
85-
if (entry.kind === "html-render" || entry.kind === "mcp-app") return entry.runId;
85+
if (entry.kind === "html-render" || entry.kind === "mcp-app" || entry.kind === "sent-message") {
86+
return entry.runId;
87+
}
8688
return entry.kind === "work" ? (entry.entry.runId ?? null) : null;
8789
}
8890

@@ -634,6 +636,12 @@ type MessagesTimelineRowContent =
634636
createdAt: string;
635637
htmlRender: HtmlRenderReference;
636638
}
639+
| {
640+
kind: "sent-message";
641+
id: string;
642+
createdAt: string;
643+
text: string;
644+
}
637645
| {
638646
kind: "mcp-app";
639647
id: string;
@@ -817,6 +825,7 @@ function deriveSupersededAttemptFolds(
817825
(entry.kind === "event" && entry.projectedItem.item.type === "handoff") ||
818826
entry.kind === "html-render" ||
819827
entry.kind === "mcp-app" ||
828+
entry.kind === "sent-message" ||
820829
timelineEntryIsPersistentResourceCard(entry) ||
821830
liveSubagentEntryIds.has(entry.id) ||
822831
(entry.kind === "work" && entry.entry.itemType === "system_notice")
@@ -2126,6 +2135,16 @@ export function deriveMessagesTimelineRows(input: {
21262135
continue;
21272136
}
21282137

2138+
if (timelineEntry.kind === "sent-message") {
2139+
nextRows.push({
2140+
kind: "sent-message",
2141+
id: timelineEntry.id,
2142+
createdAt: timelineEntry.createdAt,
2143+
text: timelineEntry.text,
2144+
});
2145+
continue;
2146+
}
2147+
21292148
if (timelineEntry.kind === "mcp-app") {
21302149
nextRows.push({
21312150
kind: "mcp-app",
@@ -2720,6 +2739,11 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
27202739
return a.createdAt === bh.createdAt && htmlRenderReferencesEqual(a.htmlRender, bh.htmlRender);
27212740
}
27222741

2742+
case "sent-message": {
2743+
const bs = b as typeof a;
2744+
return a.createdAt === bs.createdAt && a.text === bs.text;
2745+
}
2746+
27232747
case "mcp-app": {
27242748
const bm = b as typeof a;
27252749
return (

0 commit comments

Comments
 (0)