Files
t3code/apps/server/src/mcp/McpInvocationContext.test.ts
T

99 lines
3.5 KiB
TypeScript

import { expect, it } from "@effect/vitest";
import {
EnvironmentId,
McpCapabilityUnavailableError,
PreviewAutomationUnavailableError,
ProviderInstanceId,
ThreadId,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as McpInvocationContext from "./McpInvocationContext.ts";
it.effect("reports the scoped credential context when preview capability is unavailable", () => {
const invocation: McpInvocationContext.McpInvocationScope = {
environmentId: EnvironmentId.make("environment-1"),
requestNamespace: "provider-session-1",
thread: {
threadId: ThreadId.make("thread-1"),
providerSessionId: "provider-session-1",
providerInstanceId: ProviderInstanceId.make("codex"),
},
client: undefined,
capabilities: new Set(),
issuedAt: 1,
};
return Effect.gen(function* () {
const error = yield* McpInvocationContext.requireMcpCapability("preview").pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
Effect.flip,
);
expect(error).toBeInstanceOf(PreviewAutomationUnavailableError);
expect(error).toMatchObject({
capability: "preview",
environmentId: invocation.environmentId,
threadId: invocation.thread?.threadId,
providerSessionId: invocation.thread?.providerSessionId,
providerInstanceId: invocation.thread?.providerInstanceId,
});
expect(error.message).toContain("MCP credential does not grant the preview capability");
expect(error.message).toContain("use a headless browser from the shell");
});
});
it.effect("reports other missing capabilities with the neutral error", () => {
const invocation: McpInvocationContext.McpInvocationScope = {
environmentId: EnvironmentId.make("environment-1"),
requestNamespace: "provider-session-1",
thread: {
threadId: ThreadId.make("thread-1"),
providerSessionId: "provider-session-1",
providerInstanceId: ProviderInstanceId.make("codex"),
},
client: undefined,
capabilities: new Set(["preview"]),
issuedAt: 1,
};
return Effect.gen(function* () {
const error = yield* McpInvocationContext.requireMcpCapability("pull-requests").pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
Effect.flip,
);
expect(error).toBeInstanceOf(McpCapabilityUnavailableError);
expect(error).toMatchObject({
capability: "pull-requests",
threadId: invocation.thread?.threadId,
});
const scope = yield* McpInvocationContext.requireMcpCapability("preview").pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
);
expect(scope).toBe(invocation);
});
});
it.effect("refuses thread-owned capabilities to a caller signed in from outside a thread", () => {
const invocation: McpInvocationContext.McpInvocationScope = {
environmentId: EnvironmentId.make("environment-1"),
requestNamespace: "client:session-1",
thread: undefined,
client: { sessionId: "session-1", label: "Claude Code", access: "auto" },
capabilities: new Set(["preview", "orchestration"]),
issuedAt: 1,
};
return Effect.gen(function* () {
const error = yield* McpInvocationContext.requireThreadMcpCapability("preview").pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
Effect.flip,
);
expect(error).toBeInstanceOf(PreviewAutomationUnavailableError);
expect(error).toMatchObject({ capability: "preview", environmentId: "environment-1" });
expect(error.threadId).toBeUndefined();
});
});