|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import { McpSchema, McpServer, Tool } from "effect/unstable/ai"; |
| 7 | + |
| 8 | +import { OrchestratorProjectionError } from "../../orchestration-v2/Orchestrator.ts"; |
| 9 | +import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts"; |
| 10 | +import * as McpHttpServer from "../McpHttpServer.ts"; |
| 11 | +import { McpInvocationContext, type McpInvocationScope } from "../McpInvocationContext.ts"; |
| 12 | +import { OrchestratorToolkit } from "./orchestrator/tools.ts"; |
| 13 | +import { PreviewToolkit } from "./preview/tools.ts"; |
| 14 | +import { ThreadToolkit } from "./thread/tools.ts"; |
| 15 | +import { WorktreeToolkit } from "./worktree/tools.ts"; |
| 16 | + |
| 17 | +it("publishes unique tool names with object-root inputs", () => { |
| 18 | + const names = new Set<string>(); |
| 19 | + for (const toolkit of [OrchestratorToolkit, PreviewToolkit, WorktreeToolkit, ThreadToolkit]) { |
| 20 | + for (const tool of Object.values(toolkit.tools)) { |
| 21 | + expect(names.has(tool.name)).toBe(false); |
| 22 | + names.add(tool.name); |
| 23 | + expect(Tool.getJsonSchema(tool)).toMatchObject({ type: "object" }); |
| 24 | + } |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +const threadId = ThreadId.make("mcp-core-thread"); |
| 29 | +const scope: McpInvocationScope = { |
| 30 | + environmentId: EnvironmentId.make("mcp-core-environment"), |
| 31 | + threadId, |
| 32 | + providerSessionId: "mcp-core-session", |
| 33 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 34 | + issuedAt: 0, |
| 35 | + capabilities: new Set(["orchestration"]), |
| 36 | +}; |
| 37 | +const client = McpSchema.McpServerClient.of({ |
| 38 | + clientId: 1, |
| 39 | + protocolVersion: "2025-06-18", |
| 40 | + initializePayload: { |
| 41 | + protocolVersion: "2025-06-18", |
| 42 | + capabilities: {}, |
| 43 | + clientInfo: { name: "mcp-core", version: "1" }, |
| 44 | + }, |
| 45 | + getClient: Effect.die("unused"), |
| 46 | +}); |
| 47 | + |
| 48 | +it.effect("checks capability before accessing services through the production registration", () => |
| 49 | + Effect.gen(function* () { |
| 50 | + const server = yield* McpServer.McpServer; |
| 51 | + expect(server.tools.some(({ tool }) => tool.name === "t3_thread_organize")).toBe(true); |
| 52 | + const result = yield* server |
| 53 | + .callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }) |
| 54 | + .pipe( |
| 55 | + Effect.provideService(McpInvocationContext, { ...scope, capabilities: new Set<never>() }), |
| 56 | + Effect.provideService(McpSchema.McpServerClient, client), |
| 57 | + ); |
| 58 | + expect(result.structuredContent).toMatchObject({ code: "capability_denied" }); |
| 59 | + }).pipe( |
| 60 | + Effect.provide( |
| 61 | + McpHttpServer.ThreadToolkitRegistrationLive.pipe( |
| 62 | + Layer.provideMerge(McpServer.McpServer.layer), |
| 63 | + Layer.provide(NodeCrypto.layer), |
| 64 | + Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ), |
| 68 | +); |
| 69 | + |
| 70 | +it.effect("returns a bounded public failure without serializing storage causes", () => |
| 71 | + Effect.gen(function* () { |
| 72 | + const server = yield* McpServer.McpServer; |
| 73 | + const result = yield* server |
| 74 | + .callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }) |
| 75 | + .pipe( |
| 76 | + Effect.provideService(McpInvocationContext, scope), |
| 77 | + Effect.provideService(McpSchema.McpServerClient, client), |
| 78 | + ); |
| 79 | + expect(result.structuredContent).toEqual({ |
| 80 | + _tag: "OrchestratorMcpFailure", |
| 81 | + code: "orchestration_error", |
| 82 | + message: "The operation could not be completed.", |
| 83 | + }); |
| 84 | + }).pipe( |
| 85 | + Effect.provide( |
| 86 | + McpHttpServer.ThreadToolkitRegistrationLive.pipe( |
| 87 | + Layer.provideMerge(McpServer.McpServer.layer), |
| 88 | + Layer.provide(NodeCrypto.layer), |
| 89 | + Layer.provide( |
| 90 | + Layer.mock(ThreadManagement.ThreadManagementService)({ |
| 91 | + getThreadShell: () => |
| 92 | + Effect.fail( |
| 93 | + new OrchestratorProjectionError({ |
| 94 | + threadId, |
| 95 | + cause: new Error("private-storage-path"), |
| 96 | + }), |
| 97 | + ), |
| 98 | + }), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
| 102 | + ), |
| 103 | +); |
0 commit comments