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
6 changes: 6 additions & 0 deletions apps/server/src/mcp/McpToolAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export const reads = <P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>)
() => Effect.void,
);

export const refuses = (failure: OrchestratorMcpFailure) =>
declare(
() => Effect.fail(failure),
() => Effect.fail(failure),
);

/** Apply the caller check before local execution or forwarding loses the child identity. */
export const withoutSubagents = <P, A, E, R>(
declaration: Declaration<(params: P) => Effect.Effect<A, E, R>>,
Expand Down
174 changes: 99 additions & 75 deletions apps/server/src/mcp/toolkits/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CommandId,
EnvironmentId,
NodeId,
makeThreadInbox,
OrchestratorMcpThreadReadResult,
ProjectId,
ProviderDriverKind,
Expand Down Expand Up @@ -153,21 +154,21 @@ it.effect("an answered question reports already answered instead of not found",
const projection = McpToolAccessTestkit.idleThreadProjection(shell);
return Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const respond = (id: string) =>
const read = (id: string) =>
server
.callTool({
name: "t3_pending_request",
arguments: { action: "respond", requestId: id, answers: { size: "large" } },
arguments: { action: "read", requestId: id },
})
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
);
expect(declaredFailure(yield* respond(requestId))).toMatchObject({
expect(declaredFailure(yield* read(requestId))).toMatchObject({
code: "invalid_request",
message: "The user-input request was already answered.",
});
expect(declaredFailure(yield* respond("missing-question"))).toMatchObject({
expect(declaredFailure(yield* read("missing-question"))).toMatchObject({
code: "invalid_request",
message: "The pending user-input request was not found.",
});
Expand Down Expand Up @@ -226,79 +227,102 @@ it.effect("an answered question reports already answered instead of not found",
);
});

it.effect("a pending question read keeps option previews", () => {
const shell = McpToolAccessTestkit.liveThreadShell(threadId);
const requestId = RuntimeRequestId.make("previewed-question");
const projection = McpToolAccessTestkit.idleThreadProjection(shell);
const questions = [
{
id: "post",
header: "Review",
question: "Post it?",
options: [{ label: "Post", description: "Publish the draft", preview: "Full proposed text" }],
},
];
return Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const result = yield* server
.callTool({ name: "t3_pending_request", arguments: { action: "read", requestId } })
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
);
expect(result.structuredContent).toEqual({ requestId, questions });
}).pipe(
Effect.provide(
layerThreadToolkit.pipe(
Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
Layer.provide(NodeCrypto.layer),
Layer.provide(
Layer.mock(ThreadManagement.ThreadManagementService)({
getThreadShell: () => Effect.succeed(shell),
getProjectThreadRecords: () =>
Effect.succeed({
...projection,
runtimeRequests: [
{
id: requestId,
nodeId: NodeId.make("root"),
providerTurnId: null,
nativeRequestRef: null,
kind: "user_input",
status: "pending",
responseCapability: { type: "message" },
createdAt: shell.createdAt,
resolvedAt: null,
},
],
turnItems: [
{
id: TurnItemId.make("previewed-question-item"),
threadId,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 0,
status: "running",
title: null,
startedAt: shell.createdAt,
completedAt: null,
updatedAt: shell.updatedAt,
type: "user_input_request",
requestId,
questions,
},
],
}),
}),
it.effect.each(["shared", "agent-only"] as const)(
"a question in a %s thread stays visible after an agent is denied",
(audience) => {
const shell = {
...McpToolAccessTestkit.liveThreadShell(threadId),
inbox: makeThreadInbox({ actor: { kind: "agent", id: "test-agent" }, routing: { audience } }),
};
const requestId = RuntimeRequestId.make("previewed-question");
const projection = McpToolAccessTestkit.idleThreadProjection(shell);
const questions = [
{
id: "post",
header: "Review",
question: "Post it?",
options: [
{ label: "Post", description: "Publish the draft", preview: "Full proposed text" },
],
},
];
return Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const call = (args: Record<string, unknown>) =>
server
.callTool({ name: "t3_pending_request", arguments: args })
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
);
const respond = { action: "respond", requestId, answers: { post: "Post" } };
for (const target of [{}, { environmentId: "linked-test-environment" }]) {
expect(declaredFailure(yield* call({ ...respond, ...target }))).toMatchObject({
code: "capability_denied",
message: expect.stringContaining("needs a human answer"),
});
}
expect((yield* call({ action: "list" })).structuredContent).toEqual({
requestIds: [requestId],
});
expect((yield* call({ action: "read", requestId })).structuredContent).toEqual({
requestId,
questions,
});
}).pipe(
Effect.provide(
layerThreadToolkit.pipe(
Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
Layer.provide(NodeCrypto.layer),
Layer.provide(
Layer.mock(ThreadManagement.ThreadManagementService)({
dispatch: () => Effect.die("An agent's answer must never dispatch or emit events"),
getThreadShell: () => Effect.succeed(shell),
getProjectThreadRecords: () =>
Effect.succeed({
...projection,
runtimeRequests: [
{
id: requestId,
nodeId: NodeId.make("root"),
providerTurnId: null,
nativeRequestRef: null,
kind: "user_input",
status: "pending",
responseCapability: { type: "message" },
createdAt: shell.createdAt,
resolvedAt: null,
},
],
turnItems: [
{
id: TurnItemId.make("previewed-question-item"),
threadId,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 0,
status: "running",
title: null,
startedAt: shell.createdAt,
completedAt: null,
updatedAt: shell.updatedAt,
type: "user_input_request",
requestId,
questions,
},
],
}),
}),
),
),
),
),
);
});
);
},
);

it("bounds public command rejections and redacts internal dispatch causes", () => {
const command = { commandId: CommandId.make("mcp-core-command"), commandType: "thread.settle" };
Expand Down
24 changes: 4 additions & 20 deletions apps/server/src/mcp/toolkits/thread/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,26 +286,10 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
return { requestId, questions: item.questions };
}),
),
writesThread((input) =>
Effect.gen(function* () {
const requestId = yield* required(input.requestId, "requestId");
const answers = yield* required(input.answers, "answers");
const { threads, projection } = yield* readQuestion({
threadId: input.threadId,
requestId,
});
const result = yield* threads
.dispatch({
type: "runtime-request.respond",
threadId: projection.thread.id,
commandId: yield* newCommandId(),
requestId,
answers,
// An agent's answer is an agent's message, never a person's.
createdBy: "agent",
})
.pipe(Effect.mapError(dispatchFailure));
return { sequence: result.sequence };
McpToolAccess.refuses(
new OrchestratorMcpFailure({
code: "capability_denied",
message: "This user-input card needs a human answer. Ask a person to answer it in T3 Code.",
}),
),
),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/mcp/toolkits/thread/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const question = Schema.Struct({
const PendingRequestTool = Tool.make("t3_pending_request", {
...commandTool,
description:
"User questions only (default: this thread). List IDs, read by `requestId`, respond with `answers`. No permission approvals.",
"User questions only (default: this thread). List IDs or read by `requestId`. A person answers these cards in T3 Code.",
parameters: Schema.Struct({
action: Schema.Literals(["list", "read", "respond"]),
environmentId: OrchestratorMcpEnvironmentTarget,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 5930,
"codexAdditionalContextBytes": 7324,
"mcpToolListJsonBytes": 55827
"mcpToolListJsonBytes": 55821
}
Loading