Skip to content
Merged
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
66 changes: 61 additions & 5 deletions apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ import { advertisedJsonSchema } from "./advertisedJsonSchema.ts";
import * as McpInvocationContext from "./McpInvocationContext.ts";
import * as McpToolAccess from "./McpToolAccess.ts";
import * as McpToolExposure from "./McpToolExposure.ts";
import {
failureFingerprintFromExit,
loopGuardScopeKey,
loopGuardTtlMs,
makeToolCallLoopGuard,
toolCallLoopBlockedResult,
toolCallLoopKey,
} from "./McpToolLoopGuard.ts";
import * as McpWebhookTransport from "./McpWebhookTransport.ts";
import { HTML_RENDER_TOOL_NAME } from "@t3tools/shared/htmlRender";
import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput";
Expand Down Expand Up @@ -1305,6 +1313,52 @@ const withToolResultCapture = Effect.fn("McpHttpServer.withToolResultCapture")(f
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, capturingServer));
});

const withToolLoopGuard = Effect.fn("McpHttpServer.withToolLoopGuard")(function* <A, E, R>(
registration: Effect.Effect<A, E, R>,
) {
const server = yield* McpServer.McpServer;
const mcpSessions = yield* Effect.serviceOption(McpProviderSessions.McpProviderSessions);
const guard = yield* makeToolCallLoopGuard();
const guardedServer = McpServer.McpServer.of({
...server,
addTool: Effect.fn("McpServer.addTool")(function (options) {
const toolName = options.tool.name;
return server.addTool({
...options,
handle: Effect.fnUntraced(function* (payload) {
const scope = Option.getOrUndefined(
yield* Effect.serviceOption(McpInvocationContext.McpInvocationContext),
);
const thread = scope?.thread;
const epoch =
thread === undefined || Option.isNone(mcpSessions)
? undefined
: yield* mcpSessions.value.toolResultEpoch(thread.threadId);
const scopeKey = loopGuardScopeKey(scope, epoch);
if (scopeKey === undefined) return yield* options.handle(payload);
const ttlMs = loopGuardTtlMs(scope);
const key = toolCallLoopKey(scopeKey, toolName, payload);
const decision = yield* guard.check(key, ttlMs);
if (decision.blocked) {
yield* Effect.logWarning("mcp.tool.identical-failure-loop-blocked", {
tool: toolName,
priorFailures: decision.priorFailures,
...(thread === undefined ? {} : { threadId: thread.threadId }),
});
return toolCallLoopBlockedResult(toolName, decision.priorFailures);
}
return yield* options
.handle(payload)
.pipe(
Effect.onExit((exit) => guard.record(key, failureFingerprintFromExit(exit), ttlMs)),
);
}),
});
}),
});
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, guardedServer));
});

/** 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 @@ -1388,8 +1442,10 @@ export const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX,
Layer.effectDiscard(
withToolAnalytics(
withToolResultCapture(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
withToolLoopGuard(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
),
),
),
),
Expand All @@ -1407,9 +1463,9 @@ 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(withToolResultCapture(withToolSpans(register)))).pipe(
Layer.provide(McpToolAccess.HandlersLayer.layer(handlers)),
);
Layer.effectDiscard(
withToolAnalytics(withToolResultCapture(withToolLoopGuard(withToolSpans(register)))),
).pipe(Layer.provide(McpToolAccess.HandlersLayer.layer(handlers)));

export const layerHtmlToolkit = Layer.mergeAll(
toolkitRegistration(HtmlRenderToolkit, HtmlHandlers.layerRender),
Expand Down
Loading
Loading