|
| 1 | +// Health checks for MCP connections, liveness-only: a connection's credential is |
| 2 | +// probed by dialing the server and listing tools (the same path tool discovery |
| 3 | +// uses). A live token reads healthy; a revoked/wrong token reads expired. MCP |
| 4 | +// has no usable identity source, so there is no identity field and no operation |
| 5 | +// picker - only the alive/expired signal. The connect-modal "Validate key" path |
| 6 | +// (connections.validate) runs the same probe on an unsaved credential. |
| 7 | +// |
| 8 | +// The upstream is a real in-process MCP server (the plugin's own test helper) |
| 9 | +// gated on a bearer token, so revoking the token mid-scenario reproduces the |
| 10 | +// "dev token expired" transition on a saved connection. |
| 11 | +import { randomBytes } from "node:crypto"; |
| 12 | + |
| 13 | +import { Effect } from "effect"; |
| 14 | +import { expect } from "@effect/vitest"; |
| 15 | +import type { HttpApiClient } from "effect/unstable/httpapi"; |
| 16 | +import { composePluginApi } from "@executor-js/api/server"; |
| 17 | +import { mcpHttpPlugin } from "@executor-js/plugin-mcp/api"; |
| 18 | +import { makeEchoMcpServer, serveMcpServer } from "@executor-js/plugin-mcp/testing"; |
| 19 | +import { variable } from "@executor-js/sdk/http-auth"; |
| 20 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 21 | + |
| 22 | +import { scenario } from "../src/scenario"; |
| 23 | +import { Api, Target } from "../src/services"; |
| 24 | + |
| 25 | +const api = composePluginApi([mcpHttpPlugin()] as const); |
| 26 | +type Client = HttpApiClient.ForApi<typeof api>; |
| 27 | + |
| 28 | +const newSlug = (prefix: string) => |
| 29 | + IntegrationSlug.make(`${prefix}-${randomBytes(4).toString("hex")}`); |
| 30 | + |
| 31 | +scenario( |
| 32 | + "Health checks · MCP liveness reports healthy, then expired when the token is revoked", |
| 33 | + {}, |
| 34 | + Effect.scoped( |
| 35 | + Effect.gen(function* () { |
| 36 | + const target = yield* Target; |
| 37 | + const { client: makeClient } = yield* Api; |
| 38 | + const identity = yield* target.newIdentity(); |
| 39 | + const client: Client = yield* makeClient(api, identity); |
| 40 | + const goodToken = `mcp_${randomBytes(8).toString("hex")}`; |
| 41 | + const slug = newSlug("hc-mcp"); |
| 42 | + const name = ConnectionName.make("main"); |
| 43 | + |
| 44 | + // A real MCP server gated on the bearer token. `live` flips off to |
| 45 | + // reproduce a revoked credential against an already-saved connection. |
| 46 | + let live = true; |
| 47 | + const server = yield* serveMcpServer(() => makeEchoMcpServer({ name: "liveness-mcp" }), { |
| 48 | + auth: { |
| 49 | + validateAuthorization: (authorization) => |
| 50 | + Effect.succeed(live && authorization === `Bearer ${goodToken}`), |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + yield* Effect.ensuring( |
| 55 | + Effect.gen(function* () { |
| 56 | + yield* client.mcp.addServer({ |
| 57 | + payload: { |
| 58 | + transport: "remote", |
| 59 | + name: "Liveness MCP", |
| 60 | + endpoint: server.url, |
| 61 | + slug: String(slug), |
| 62 | + // Pin streamable-http so the probe's failure is the server's 401 |
| 63 | + // (no auto SSE fallback to muddy the classification). |
| 64 | + remoteTransport: "streamable-http", |
| 65 | + authenticationTemplate: [ |
| 66 | + { |
| 67 | + slug: "bearer", |
| 68 | + type: "apiKey", |
| 69 | + headers: { Authorization: ["Bearer ", variable("token")] }, |
| 70 | + }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + yield* client.connections.create({ |
| 76 | + payload: { |
| 77 | + owner: "org", |
| 78 | + name, |
| 79 | + integration: slug, |
| 80 | + template: AuthTemplateSlug.make("bearer"), |
| 81 | + value: goodToken, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + // Saved connection with the live token: alive. |
| 86 | + const healthy = yield* client.connections.checkHealth({ |
| 87 | + params: { owner: "org", integration: slug, name }, |
| 88 | + }); |
| 89 | + expect(healthy.status, "a live MCP credential is healthy").toBe("healthy"); |
| 90 | + |
| 91 | + // Key-first validate (unsaved credential) runs the same probe. |
| 92 | + const validated = yield* client.connections.validate({ |
| 93 | + payload: { |
| 94 | + owner: "org", |
| 95 | + integration: slug, |
| 96 | + template: AuthTemplateSlug.make("bearer"), |
| 97 | + value: goodToken, |
| 98 | + }, |
| 99 | + }); |
| 100 | + expect(validated.status, "validating a live key is healthy").toBe("healthy"); |
| 101 | + const rejected = yield* client.connections.validate({ |
| 102 | + payload: { |
| 103 | + owner: "org", |
| 104 | + integration: slug, |
| 105 | + template: AuthTemplateSlug.make("bearer"), |
| 106 | + value: "wrong-token", |
| 107 | + }, |
| 108 | + }); |
| 109 | + expect(rejected.status, "validating a rejected key is expired").toBe("expired"); |
| 110 | + |
| 111 | + // The upstream revokes the saved token: the same connection now expired. |
| 112 | + live = false; |
| 113 | + const expired = yield* client.connections.checkHealth({ |
| 114 | + params: { owner: "org", integration: slug, name }, |
| 115 | + }); |
| 116 | + expect(expired.status, "a revoked MCP credential reads expired").toBe("expired"); |
| 117 | + // No identity is ever derived for MCP (manual label only). |
| 118 | + expect(expired.identity, "MCP surfaces no derived identity").toBeUndefined(); |
| 119 | + }), |
| 120 | + Effect.gen(function* () { |
| 121 | + yield* client.connections |
| 122 | + .remove({ params: { owner: "org", integration: slug, name } }) |
| 123 | + .pipe(Effect.ignore); |
| 124 | + yield* client.mcp.removeServer({ params: { slug } }).pipe(Effect.ignore); |
| 125 | + }), |
| 126 | + ); |
| 127 | + }), |
| 128 | + ), |
| 129 | +); |
0 commit comments