|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { homedir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
2 | 4 | import { buildProxyDescription, resolveDirectTools } from "../direct-tools.js"; |
3 | 5 | import { computeServerHash, type MetadataCache } from "../metadata-cache.js"; |
4 | 6 | import { buildToolMetadata } from "../tool-metadata.js"; |
5 | 7 | import type { McpConfig } from "../types.js"; |
6 | 8 | import { reconstructToolMetadata } from "../metadata-cache.js"; |
7 | 9 |
|
| 10 | +const originalHashEnv = { |
| 11 | + MCP_HASH_CWD: process.env.MCP_HASH_CWD, |
| 12 | + MCP_HASH_ENV: process.env.MCP_HASH_ENV, |
| 13 | + MCP_HASH_HEADER: process.env.MCP_HASH_HEADER, |
| 14 | + MCP_HASH_TOKEN: process.env.MCP_HASH_TOKEN, |
| 15 | +}; |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + for (const [key, value] of Object.entries(originalHashEnv)) { |
| 19 | + if (value === undefined) { |
| 20 | + delete process.env[key]; |
| 21 | + } else { |
| 22 | + process.env[key] = value; |
| 23 | + } |
| 24 | + } |
| 25 | +}); |
| 26 | + |
8 | 27 | describe("buildProxyDescription", () => { |
9 | 28 | it("documents the ui-messages action", () => { |
10 | 29 | const config: McpConfig = { |
@@ -79,6 +98,66 @@ describe("buildProxyDescription", () => { |
79 | 98 | }); |
80 | 99 | }); |
81 | 100 |
|
| 101 | +describe("metadata cache hashing", () => { |
| 102 | + it("hashes interpolated cwd", () => { |
| 103 | + process.env.MCP_HASH_CWD = "/tmp/mcp-one"; |
| 104 | + const first = computeServerHash({ command: "node", cwd: "${MCP_HASH_CWD}/server" }); |
| 105 | + |
| 106 | + process.env.MCP_HASH_CWD = "/tmp/mcp-two"; |
| 107 | + const second = computeServerHash({ command: "node", cwd: "${MCP_HASH_CWD}/server" }); |
| 108 | + |
| 109 | + expect(first).not.toBe(second); |
| 110 | + expect(computeServerHash({ command: "node", cwd: "${MCP_HASH_CWD}/server" })).toBe( |
| 111 | + computeServerHash({ command: "node", cwd: "/tmp/mcp-two/server" }), |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("hashes interpolated env values", () => { |
| 116 | + process.env.MCP_HASH_ENV = "/tmp/data-one"; |
| 117 | + const first = computeServerHash({ command: "node", env: { DATA_DIR: "${MCP_HASH_ENV}" } }); |
| 118 | + |
| 119 | + process.env.MCP_HASH_ENV = "/tmp/data-two"; |
| 120 | + const second = computeServerHash({ command: "node", env: { DATA_DIR: "${MCP_HASH_ENV}" } }); |
| 121 | + |
| 122 | + expect(first).not.toBe(second); |
| 123 | + expect(computeServerHash({ command: "node", env: { DATA_DIR: "${MCP_HASH_ENV}" } })).toBe( |
| 124 | + computeServerHash({ command: "node", env: { DATA_DIR: "/tmp/data-two" } }), |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it("hashes interpolated header values", () => { |
| 129 | + process.env.MCP_HASH_HEADER = "header-one"; |
| 130 | + const first = computeServerHash({ url: "https://example.test/mcp", headers: { "x-root": "$env:MCP_HASH_HEADER" } }); |
| 131 | + |
| 132 | + process.env.MCP_HASH_HEADER = "header-two"; |
| 133 | + const second = computeServerHash({ url: "https://example.test/mcp", headers: { "x-root": "$env:MCP_HASH_HEADER" } }); |
| 134 | + |
| 135 | + expect(first).not.toBe(second); |
| 136 | + expect(computeServerHash({ url: "https://example.test/mcp", headers: { "x-root": "$env:MCP_HASH_HEADER" } })).toBe( |
| 137 | + computeServerHash({ url: "https://example.test/mcp", headers: { "x-root": "header-two" } }), |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it("hashes tilde cwd as the home directory", () => { |
| 142 | + expect(computeServerHash({ command: "node", cwd: "~/server" })).toBe( |
| 143 | + computeServerHash({ command: "node", cwd: join(homedir(), "server") }), |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it("hashes the effective bearerTokenEnv value", () => { |
| 148 | + process.env.MCP_HASH_TOKEN = "token-one"; |
| 149 | + const first = computeServerHash({ url: "https://example.test/mcp", auth: "bearer", bearerTokenEnv: "MCP_HASH_TOKEN" }); |
| 150 | + |
| 151 | + process.env.MCP_HASH_TOKEN = "token-two"; |
| 152 | + const second = computeServerHash({ url: "https://example.test/mcp", auth: "bearer", bearerTokenEnv: "MCP_HASH_TOKEN" }); |
| 153 | + |
| 154 | + expect(first).not.toBe(second); |
| 155 | + expect(computeServerHash({ url: "https://example.test/mcp", auth: "bearer", bearerTokenEnv: "MCP_HASH_TOKEN" })).toBe( |
| 156 | + computeServerHash({ url: "https://example.test/mcp", auth: "bearer", bearerToken: "token-two", bearerTokenEnv: "MCP_HASH_TOKEN" }), |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
82 | 161 | describe("excludeTools filtering", () => { |
83 | 162 | it("filters excluded tools from live and cached metadata", () => { |
84 | 163 | const definition = { |
|
0 commit comments