|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Predicate } from "effect"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +import { createMcpConnector, type StdioConnectorInput } from "./connection"; |
| 6 | + |
| 7 | +const fixture = fileURLToPath(new URL("./stdio-negotiation-test-server.ts", import.meta.url)); |
| 8 | + |
| 9 | +const stdioInput = ( |
| 10 | + overrides: Partial<Omit<StdioConnectorInput, "transport" | "command">> & { |
| 11 | + readonly args: readonly string[]; |
| 12 | + }, |
| 13 | +): StdioConnectorInput => ({ |
| 14 | + transport: "stdio", |
| 15 | + command: "bun", |
| 16 | + ...overrides, |
| 17 | +}); |
| 18 | + |
| 19 | +const withConnection = (input: StdioConnectorInput) => |
| 20 | + Effect.acquireRelease(createMcpConnector(input).pipe(Effect.orDie), (connection) => |
| 21 | + Effect.promise(connection.close), |
| 22 | + ); |
| 23 | + |
| 24 | +describe("stdio version negotiation", () => { |
| 25 | + it.effect("auto negotiation connects modern to a server with legacy support disabled", () => |
| 26 | + Effect.scoped( |
| 27 | + Effect.gen(function* () { |
| 28 | + const connection = yield* withConnection( |
| 29 | + stdioInput({ args: ["run", fixture, "--legacy-reject"], versionNegotiation: "auto" }), |
| 30 | + ); |
| 31 | + |
| 32 | + expect(connection.client.getProtocolEra()).toBe("modern"); |
| 33 | + const tools = yield* Effect.promise(() => connection.client.listTools()); |
| 34 | + expect(tools.tools.map(({ name }) => name)).toContain("add"); |
| 35 | + const result = yield* Effect.promise(() => |
| 36 | + connection.client.callTool({ name: "add", arguments: { a: 2, b: 2 } }), |
| 37 | + ); |
| 38 | + expect(result.content).toEqual([{ type: "text", text: "4" }]); |
| 39 | + }), |
| 40 | + ), |
| 41 | + ); |
| 42 | + |
| 43 | + it.effect("the default handshake surfaces a connection error on that same server", () => |
| 44 | + Effect.gen(function* () { |
| 45 | + const error = yield* createMcpConnector( |
| 46 | + stdioInput({ args: ["run", fixture, "--legacy-reject"] }), |
| 47 | + ).pipe(Effect.flip); |
| 48 | + |
| 49 | + expect(Predicate.isTagged(error, "McpConnectionError")).toBe(true); |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + it.effect("absent config keeps the legacy handshake against a both-era server", () => |
| 54 | + Effect.scoped( |
| 55 | + Effect.gen(function* () { |
| 56 | + const connection = yield* withConnection(stdioInput({ args: ["run", fixture] })); |
| 57 | + |
| 58 | + expect(connection.client.getProtocolEra()).toBe("legacy"); |
| 59 | + const tools = yield* Effect.promise(() => connection.client.listTools()); |
| 60 | + expect(tools.tools.map(({ name }) => name)).toContain("add"); |
| 61 | + }), |
| 62 | + ), |
| 63 | + ); |
| 64 | +}); |
0 commit comments