Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ import * as McpInvocationContext from "./McpInvocationContext.ts";
import * as McpToolAccess from "./McpToolAccess.ts";
import * as McpToolExposure from "./McpToolExposure.ts";
import * as McpWebhookTransport from "./McpWebhookTransport.ts";
import { HTML_RENDER_TOOL_NAME } from "@t3tools/shared/htmlRender";
import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput";
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
import { MCP_TOOL_EXPOSURE_HEADER } from "@t3tools/provider-core/server/mcpSession";
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
import { PreviewControlsToolkit } from "./toolkits/previewControls/tools.ts";
Expand Down Expand Up @@ -1218,6 +1221,60 @@ const withToolAnalytics =
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, recordingServer));
});

/**
* ACP runtimes such as Antigravity and Kiro drop or replace MCP tool results on completion.
* Recording each thread-scoped call's compact result before returning lets adapters recover
* sent messages, IDs, counts, and error flags without waiting on provider output passthrough.
*/
const withToolResultCapture = Effect.fn("McpHttpServer.withToolResultCapture")(function* <A, E, R>(
registration: Effect.Effect<A, E, R>,
) {
const server = yield* McpServer.McpServer;
const mcpSessions = yield* Effect.serviceOption(McpProviderSessions.McpProviderSessions);
if (Option.isNone(mcpSessions)) {
return yield* registration;
}
const capturingServer = McpServer.McpServer.of({
...server,
addTool: Effect.fn("McpServer.addTool")(function (options) {
const toolName = options.tool.name;
// html_render tracks its own path/hash recovery with publish cleanup in HtmlRender.
if (toolName === HTML_RENDER_TOOL_NAME) {
return server.addTool(options);
}
return server.addTool({
...options,
handle: Effect.fnUntraced(function* (payload) {
return yield* options.handle(payload).pipe(
Effect.onExit((exit) => {
if (exit._tag === "Success" && exit.value instanceof McpSchema.InputRequired) {
return Effect.void;
}
if (exit._tag === "Failure" && Cause.hasInterruptsOnly(exit.cause)) {
return Effect.void;
}
return Effect.serviceOption(McpInvocationContext.McpInvocationContext).pipe(
Effect.flatMap((scope) => {
const threadId = Option.getOrUndefined(scope)?.thread?.threadId;
if (threadId === undefined) return Effect.void;
const output =
exit._tag === "Success"
? compactT3McpToolOutput(toolName, exit.value)
: { isError: true };
return output === undefined
? Effect.void
: mcpSessions.value.recordToolResult(threadId, toolName, payload, output);
}),
);
}),
);
}),
});
}),
});
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, capturingServer));
});

/** Each tool call gets a span named for its tool; arguments stay out, as they carry prompts and IDs. */
const withToolSpans = Effect.fn("McpHttpServer.withToolSpans")(function* <A, E, R>(
registration: Effect.Effect<A, E, R>,
Expand Down Expand Up @@ -1300,8 +1357,10 @@ export const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX,
) =>
Layer.effectDiscard(
withToolAnalytics(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
withToolResultCapture(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
),
),
),
).pipe(
Expand All @@ -1318,7 +1377,7 @@ const imageToolRegistration = <Tools extends Record<string, Tool.Any>, A, E, R,
register: Effect.Effect<A, E, R>,
handlers: McpToolAccess.HandlersLayer<Tools, EX, RX>,
) =>
Layer.effectDiscard(withToolAnalytics(withToolSpans(register))).pipe(
Layer.effectDiscard(withToolAnalytics(withToolResultCapture(withToolSpans(register)))).pipe(
Layer.provide(McpToolAccess.HandlersLayer.layer(handlers)),
);

Expand Down Expand Up @@ -1350,7 +1409,7 @@ export const layerOrchestratorToolkit = toolkitRegistration(
export const layerThreadToolkit = toolkitRegistration(ThreadToolkit, ThreadHandlers.layer);
const layerWorkflowToolkit = toolkitRegistration(WorkflowToolkit, WorkflowHandlers.layer);

const layerMessageToolkit = toolkitRegistration(MessageToolkit, MessageHandlers.layer);
export const layerMessageToolkit = toolkitRegistration(MessageToolkit, MessageHandlers.layer);

const layerWorktreeToolkitRegistration = toolkitRegistration(
WorktreeToolkit,
Expand Down
56 changes: 55 additions & 1 deletion apps/server/src/mcp/toolkits/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const declaredFailure = (result: McpSchema.CallToolResult) => {
const text = result.content[0];
return result.isError === true && text?.type === "text" ? JSON.parse(text.text) : undefined;
};
import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput";
import { htmlRenderFromToolItem, sentMessageFromToolItem } from "@t3tools/shared/toolOutput";

const decodeMcpAttachmentInput = Schema.decodeUnknownEffect(McpAttachmentInput);
const decodeThreadReadResult = Schema.decodeUnknownEffect(OrchestratorMcpThreadReadResult);
Expand Down Expand Up @@ -416,6 +416,60 @@ it.effect("returns an HTML render reference that Codex and Claude tool rows both
),
);

it.effect("captures send_to_user results for providers that drop MCP outputs", () =>
Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
const draft = "### Status\n\nAll checks passed.";
const result = yield* server
.callTool({ name: "send_to_user", arguments: { message: draft } })
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
);
expect(result.isError).not.toBe(true);
expect(
sentMessageFromToolItem({
toolName: "t3-code.send_to_user",
output: result.structuredContent,
}),
).toBe(draft);
expect(
sentMessageFromToolItem({ toolName: "mcp__t3-code__send_to_user", output: result.content }),
).toBe(draft);
// Antigravity adds toolSummary to ACP inputs without forwarding it to T3's MCP server.
expect(
yield* mcpSessions.takeToolResult(threadId, "send_to_user", {
message: draft,
toolSummary: "Send status summary",
}),
).toEqual({ sentMessage: draft });
expect(yield* mcpSessions.takeToolResult(threadId, "send_to_user", { message: draft })).toBe(
undefined,
);

const invalid = yield* server
.callTool({ name: "send_to_user", arguments: { message: "" } })
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
Effect.flip,
);
expect(invalid).toMatchObject({ _tag: "InvalidParams" });
expect(yield* mcpSessions.takeToolResult(threadId, "send_to_user", { message: "" })).toEqual({
isError: true,
});
}).pipe(
Effect.provide(
McpHttpServer.layerMessageToolkit.pipe(
Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
Layer.provideMerge(McpProviderSessions.layer),
Layer.provide(McpToolAccessTestkit.liveThreadsLayer),
),
),
),
);

it.effect("records only failed calls of tools that hand off no work", () => {
// Recording runs detached from the tool response, so the first event is awaited.
const recorded = Deferred.makeUnsafe<{ event: string; properties: object }>();
Expand Down
Loading
Loading