|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Layer, Option, Schema } from "effect"; |
| 3 | +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; |
| 4 | + |
| 5 | +import { |
| 6 | + AuthTemplateSlug, |
| 7 | + ConnectionName, |
| 8 | + IntegrationSlug, |
| 9 | + ToolAddress, |
| 10 | + createExecutor, |
| 11 | +} from "@executor-js/sdk"; |
| 12 | +import { makeTestConfig, memoryCredentialsPlugin } from "@executor-js/sdk/testing"; |
| 13 | + |
| 14 | +import { mcpPlugin } from "./plugin"; |
| 15 | + |
| 16 | +const FILE_ID = "F012ABC3456"; |
| 17 | +const IMAGE_BYTES = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]); |
| 18 | +const seenRpcMethods: string[] = []; |
| 19 | + |
| 20 | +const JsonRpcRequest = Schema.Struct({ |
| 21 | + id: Schema.optional(Schema.Union([Schema.String, Schema.Number, Schema.Null])), |
| 22 | + method: Schema.String, |
| 23 | +}); |
| 24 | +const decodeJsonRpcRequest = Schema.decodeUnknownOption(Schema.fromJsonString(JsonRpcRequest)); |
| 25 | + |
| 26 | +const jsonRpcResponse = (request: typeof JsonRpcRequest.Type, result: unknown): Response => |
| 27 | + Response.json({ jsonrpc: "2.0", id: request.id ?? null, result }); |
| 28 | + |
| 29 | +const slackFallbackHttpClientLayer = Layer.succeed(HttpClient.HttpClient)( |
| 30 | + HttpClient.make((request: HttpClientRequest.HttpClientRequest) => |
| 31 | + Effect.gen(function* () { |
| 32 | + const webRequest = yield* HttpClientRequest.toWeb(request).pipe(Effect.orDie); |
| 33 | + const url = new URL(webRequest.url); |
| 34 | + |
| 35 | + if (url.hostname === "slack.com" && url.pathname === "/api/files.info") { |
| 36 | + return HttpClientResponse.fromWeb( |
| 37 | + request, |
| 38 | + Response.json({ |
| 39 | + ok: true, |
| 40 | + file: { |
| 41 | + id: FILE_ID, |
| 42 | + name: "external-screenshot.png", |
| 43 | + mimetype: "image/png", |
| 44 | + size: IMAGE_BYTES.byteLength, |
| 45 | + url_private_download: `https://files.slack.com/files-pri/T000-${FILE_ID}/download/external-screenshot.png`, |
| 46 | + }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (url.hostname === "files.slack.com") { |
| 52 | + return HttpClientResponse.fromWeb( |
| 53 | + request, |
| 54 | + new Response(IMAGE_BYTES, { status: 200, headers: { "content-type": "image/png" } }), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (url.hostname !== "mcp.slack.com") { |
| 59 | + return HttpClientResponse.fromWeb( |
| 60 | + request, |
| 61 | + new Response("unexpected host", { status: 500 }), |
| 62 | + ); |
| 63 | + } |
| 64 | + if (webRequest.method === "GET") { |
| 65 | + return HttpClientResponse.fromWeb(request, new Response("SSE disabled", { status: 405 })); |
| 66 | + } |
| 67 | + |
| 68 | + const rpc = Option.getOrUndefined( |
| 69 | + decodeJsonRpcRequest(yield* Effect.promise(() => webRequest.text())), |
| 70 | + ); |
| 71 | + if (rpc === undefined) { |
| 72 | + return HttpClientResponse.fromWeb( |
| 73 | + request, |
| 74 | + new Response("invalid JSON-RPC", { status: 400 }), |
| 75 | + ); |
| 76 | + } |
| 77 | + seenRpcMethods.push(rpc.method); |
| 78 | + if (rpc.method === "initialize") { |
| 79 | + return HttpClientResponse.fromWeb( |
| 80 | + request, |
| 81 | + jsonRpcResponse(rpc, { |
| 82 | + protocolVersion: "2025-06-18", |
| 83 | + capabilities: { tools: {} }, |
| 84 | + serverInfo: { name: "Slack", version: "1.0.0" }, |
| 85 | + }), |
| 86 | + ); |
| 87 | + } |
| 88 | + if (rpc.method === "notifications/initialized") { |
| 89 | + return HttpClientResponse.fromWeb(request, new Response("", { status: 202 })); |
| 90 | + } |
| 91 | + if (rpc.method === "tools/list") { |
| 92 | + return HttpClientResponse.fromWeb( |
| 93 | + request, |
| 94 | + jsonRpcResponse(rpc, { |
| 95 | + tools: [ |
| 96 | + { |
| 97 | + name: "slack_read_file", |
| 98 | + inputSchema: { |
| 99 | + type: "object", |
| 100 | + properties: { file_id: { type: "string" } }, |
| 101 | + required: ["file_id"], |
| 102 | + }, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }), |
| 106 | + ); |
| 107 | + } |
| 108 | + if (rpc.method === "tools/call") { |
| 109 | + return HttpClientResponse.fromWeb( |
| 110 | + request, |
| 111 | + jsonRpcResponse(rpc, { |
| 112 | + isError: true, |
| 113 | + content: [{ type: "text", text: "execution_failed: file_not_found" }], |
| 114 | + }), |
| 115 | + ); |
| 116 | + } |
| 117 | + return HttpClientResponse.fromWeb( |
| 118 | + request, |
| 119 | + new Response("unexpected method", { status: 400 }), |
| 120 | + ); |
| 121 | + }), |
| 122 | + ), |
| 123 | +); |
| 124 | + |
| 125 | +describe("Slack Connect file fallback", () => { |
| 126 | + it.effect("recovers the image through the caller-visible MCP tool", () => |
| 127 | + Effect.scoped( |
| 128 | + Effect.gen(function* () { |
| 129 | + const config = { |
| 130 | + ...makeTestConfig({ |
| 131 | + plugins: [ |
| 132 | + memoryCredentialsPlugin(), |
| 133 | + mcpPlugin({ httpClientLayer: slackFallbackHttpClientLayer }), |
| 134 | + ] as const, |
| 135 | + }), |
| 136 | + httpClientLayer: slackFallbackHttpClientLayer, |
| 137 | + }; |
| 138 | + const executor = yield* Effect.acquireRelease(createExecutor(config), (executor) => |
| 139 | + Effect.gen(function* () { |
| 140 | + yield* executor.close().pipe(Effect.ignore); |
| 141 | + yield* Effect.promise(() => config.testDb.close()).pipe(Effect.ignore); |
| 142 | + }), |
| 143 | + ); |
| 144 | + |
| 145 | + yield* executor.mcp.addServer({ |
| 146 | + name: "Slack", |
| 147 | + endpoint: "https://mcp.slack.com/mcp", |
| 148 | + slug: "slack_connect_fixture", |
| 149 | + remoteTransport: "streamable-http", |
| 150 | + auth: { kind: "oauth2" }, |
| 151 | + }); |
| 152 | + yield* executor.connections.create({ |
| 153 | + owner: "org", |
| 154 | + name: ConnectionName.make("main"), |
| 155 | + integration: IntegrationSlug.make("slack_connect_fixture"), |
| 156 | + template: AuthTemplateSlug.make("oauth2"), |
| 157 | + value: "xoxp-test-token", |
| 158 | + }); |
| 159 | + |
| 160 | + const toolAddresses = (yield* executor.tools.list()).map((tool) => String(tool.address)); |
| 161 | + expect(seenRpcMethods).toContain("tools/list"); |
| 162 | + expect(toolAddresses).toContain("tools.slack_connect_fixture.org.main.slack_read_file"); |
| 163 | + |
| 164 | + const result = yield* executor.execute( |
| 165 | + ToolAddress.make("tools.slack_connect_fixture.org.main.slack_read_file"), |
| 166 | + { file_id: FILE_ID }, |
| 167 | + { onElicitation: "accept-all" }, |
| 168 | + ); |
| 169 | + |
| 170 | + expect(result).toMatchObject({ |
| 171 | + ok: true, |
| 172 | + data: { |
| 173 | + content: [ |
| 174 | + { type: "text", text: expect.stringContaining(FILE_ID) }, |
| 175 | + { type: "image", mimeType: "image/png" }, |
| 176 | + ], |
| 177 | + }, |
| 178 | + }); |
| 179 | + }), |
| 180 | + ), |
| 181 | + ); |
| 182 | +}); |
0 commit comments