Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PeerMcpClient.test.ts

177 lines6.4 KB

1import { NodeHttpServer } from "@effect/platform-node";
2import { expect, it } from "@effect/vitest";
3import * as Context from "effect/Context";
4import * as DateTime from "effect/DateTime";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Layer from "effect/Layer";
9import * as Ref from "effect/Ref";
10import * as Schema from "effect/Schema";
11import { Tool, Toolkit } from "effect/ai";
12import { HttpClient, HttpClientResponse } from "effect/http";
13​
14import * as McpHttpServer from "../mcp/McpHttpServer.ts";
15import * as McpInvocationContext from "../mcp/McpInvocationContext.ts";
16import * as McpToolAccess from "../mcp/McpToolAccess.ts";
17import * as PeerLinks from "./PeerLinks.ts";
18import * as PeerMcpClient from "./PeerMcpClient.ts";
19import { descriptorOf, layerLinkingEnvironment, linkTo, servePeer } from "./PeerLinks.testkit.ts";
20​
21const Result = Schema.Struct({ modes: Schema.String });
22const toolkit = Toolkit.make(
23 Tool.make("probe", {
24 description: "Returns this request's mode limits.",
25 success: Result,
26 dependencies: [McpInvocationContext.McpInvocationContext],
27 }),
28 Tool.make("hold", {
29 description: "Holds a tool request open.",
30 success: Result,
31 dependencies: [McpInvocationContext.McpInvocationContext],
32 }),
33);
34​
35it.effect.each([false, true])(
36 "reuses an initialized session before its first call finishes (cancelled: %s)",
37 (cancelFirst) =>
38 Effect.scoped(
39 Effect.gen(function* () {
40 const entered = yield* Deferred.make<void>();
41 const release = yield* Deferred.make<void>();
42 const modes = McpInvocationContext.McpInvocationContext.pipe(
43 Effect.map((scope) => {
44 const ceiling = McpInvocationContext.clientModeCeiling(scope.client);
45 return { modes: `${ceiling.runtimeMode}/${ceiling.interactionMode}` };
46 }),
47 );
48 const handlers = McpToolAccess.toLayer(toolkit, {
49 probe: McpToolAccess.reads(() => modes),
50 hold: McpToolAccess.reads(() =>
51 Deferred.succeed(entered, undefined).pipe(
52 Effect.andThen(Deferred.await(release)),
53 Effect.andThen(modes),
54 ),
55 ),
56 });
57 const box = descriptorOf("environment-box", "Box");
58 const b = yield* servePeer(box, McpHttpServer.toolkitRegistration(toolkit, handlers));
59 const context = yield* Layer.build(
60 layerLinkingEnvironment(descriptorOf("environment-a", "A")),
61 );
62 const links = Context.get(context, PeerLinks.PeerLinks);
63 const client = Context.get(context, PeerMcpClient.PeerMcpClient);
64 yield* linkTo(links, b);
65 const held = yield* client
66 .call({
67 environmentId: box.environmentId,
68 tool: "hold",
69 arguments: {},
70 limits: { runtimeMode: "full-access", interactionMode: "default" },
71 success: Result,
72 })
73 .pipe(Effect.forkScoped);
74 yield* Deferred.await(entered);
75 if (cancelFirst) yield* Fiber.interrupt(held);
76 const before = (yield* Ref.get(b.bearers)).length;
77 expect(
78 yield* client.call({
79 environmentId: box.environmentId,
80 tool: "probe",
81 arguments: {},
82 limits: { runtimeMode: "approval-required", interactionMode: "plan" },
83 success: Result,
84 }),
85 ).toEqual({ modes: "approval-required/plan" });
86 // One tool POST; no second initialize or initialized notification.
87 expect((yield* Ref.get(b.bearers)).length - before).toBe(1);
88 yield* Deferred.succeed(release, undefined);
89 if (!cancelFirst) expect(yield* Fiber.join(held)).toEqual({ modes: "auto/default" });
90 }),
91 ).pipe(Effect.provide(NodeHttpServer.layerTest)),
92);
93​
94it.effect("renews a missing peer session once and reuses its replacement", () =>
95 Effect.gen(function* () {
96 const sessionHeaders: Array<string | undefined> = [];
97 const statuses = [200, 202, 404, 200, 202, 200, 200];
98 const http = HttpClient.make((request) =>
99 Effect.sync(() => {
100 const index = sessionHeaders.length;
101 sessionHeaders.push(request.headers["mcp-session-id"]);
102 const status = statuses[index];
103 if (status === undefined) throw new Error("Unexpected retry");
104 return HttpClientResponse.fromWeb(
105 request,
106 new Response(
107 index >= 5
108 ? JSON.stringify({
109 result: {
110 content: [{ type: "text", text: '{"modes":"approval-required/plan"}' }],
111 },
112 })
113 : "",
114 {
115 status,
116 headers:
117 index === 0
118 ? { "mcp-session-id": "old" }
119 : index === 3
120 ? { "mcp-session-id": "renewed" }
121 : {},
122 },
123 ),
124 );
125 }),
126 );
127 const box = descriptorOf("environment-box", "Box");
128 const context = yield* Layer.build(
129 PeerMcpClient.layer.pipe(
130 Layer.provide(
131 Layer.mergeAll(
132 Layer.succeed(HttpClient.HttpClient, http),
133 Layer.mock(PeerLinks.PeerLinks)({
134 resolve: () =>
135 Effect.succeed({
136 link: {
137 environmentId: box.environmentId,
138 label: box.label,
139 urls: ["https://peer.example"],
140 access: "auto",
141 linkedAt: DateTime.makeUnsafe(0),
142 expiresAt: DateTime.makeUnsafe(1),
143 lastReachedAt: null,
144 lastError: null,
145 },
146 url: "https://peer.example",
147 token: "fixture",
148 }),
149 recordOutcome: () => Effect.void,
150 }),
151 ),
152 ),
153 ),
154 );
155 const client = Context.get(context, PeerMcpClient.PeerMcpClient);
156 for (let call = 0; call < 2; call++) {
157 expect(
158 yield* client.call({
159 environmentId: box.environmentId,
160 tool: "probe",
161 arguments: {},
162 limits: { runtimeMode: "approval-required", interactionMode: "plan" },
163 success: Result,
164 }),
165 ).toEqual({ modes: "approval-required/plan" });
166 }
167 expect(sessionHeaders).toEqual([
168 undefined,
169 "old",
170 "old",
171 undefined,
172 "renewed",
173 "renewed",
174 "renewed",
175 ]);
176 }).pipe(Effect.scoped),
177);