@lightdash-tools/mcp 0.14.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/README.md +43 -14
  2. package/dist/auth/oauth-broker/as-metadata.d.ts +6 -2
  3. package/dist/auth/oauth-broker/as-metadata.js +8 -4
  4. package/dist/auth/oauth-broker/invoke-origins.d.ts +16 -0
  5. package/dist/auth/oauth-broker/invoke-origins.js +112 -0
  6. package/dist/auth/oauth-broker/invoke-origins.test.d.ts +1 -0
  7. package/dist/auth/oauth-broker/invoke-origins.test.js +82 -0
  8. package/dist/auth/oauth-broker/lightdash-token.d.ts +6 -5
  9. package/dist/auth/oauth-broker/lightdash-token.js +6 -10
  10. package/dist/auth/oauth-broker/mcp-access-token.d.ts +31 -0
  11. package/dist/auth/oauth-broker/mcp-access-token.js +109 -0
  12. package/dist/auth/oauth-broker/mcp-access-token.test.d.ts +1 -0
  13. package/dist/auth/oauth-broker/mcp-access-token.test.js +61 -0
  14. package/dist/auth/oauth-broker/pending-store.d.ts +9 -3
  15. package/dist/auth/oauth-broker/pending-store.js +5 -3
  16. package/dist/auth/oauth-broker/pending-store.test.js +28 -44
  17. package/dist/auth/oauth-broker/routes.d.ts +2 -1
  18. package/dist/auth/oauth-broker/routes.js +124 -48
  19. package/dist/auth/oauth-broker/routes.test.js +191 -16
  20. package/dist/auth/resource-server/lightdash-oauth-middleware.d.ts +3 -1
  21. package/dist/auth/resource-server/lightdash-oauth-middleware.js +65 -41
  22. package/dist/auth/resource-server/lightdash-oauth-middleware.test.js +95 -59
  23. package/dist/auth/resource-server/oauth-protected-resource.d.ts +2 -2
  24. package/dist/auth/resource-server/oauth-protected-resource.js +8 -11
  25. package/dist/auth/resource-server/www-authenticate.test.js +3 -3
  26. package/dist/bin.js +33 -7
  27. package/dist/config/env.d.ts +10 -0
  28. package/dist/config/env.js +11 -1
  29. package/dist/config/load-mcp-config.d.ts +14 -1
  30. package/dist/config/load-mcp-config.js +35 -8
  31. package/dist/config/load-mcp-config.test.js +49 -0
  32. package/dist/config/prompt-context-policy.d.ts +18 -0
  33. package/dist/config/prompt-context-policy.js +40 -0
  34. package/dist/config/prompt-context-policy.test.d.ts +4 -0
  35. package/dist/config/prompt-context-policy.test.js +35 -0
  36. package/dist/config/test-mcp-http-config.js +1 -1
  37. package/dist/governance/mcp-post-factory-context.d.ts +2 -0
  38. package/dist/http.d.ts +7 -1
  39. package/dist/http.integration.helpers.d.ts +50 -0
  40. package/dist/http.integration.helpers.js +190 -0
  41. package/dist/http.integration.test.js +81 -187
  42. package/dist/http.invoke-origin.integration.test.d.ts +1 -0
  43. package/dist/http.invoke-origin.integration.test.js +108 -0
  44. package/dist/http.js +10 -1
  45. package/dist/index.d.ts +6 -1
  46. package/dist/index.js +6 -3
  47. package/dist/policy/content-developer.d.ts +5 -0
  48. package/dist/policy/content-developer.js +25 -1
  49. package/dist/policy/content-developer.test.js +29 -0
  50. package/dist/profiles/ai-agent-ops/v1/invariants.d.ts +27 -0
  51. package/dist/profiles/ai-agent-ops/v1/invariants.js +39 -0
  52. package/dist/profiles/ai-agent-ops/v1/prompts.d.ts +3 -2
  53. package/dist/profiles/ai-agent-ops/v1/prompts.js +67 -41
  54. package/dist/profiles/ai-agent-ops/v1/resources/playbooks.d.ts +2 -1
  55. package/dist/profiles/ai-agent-ops/v1/resources/playbooks.js +10 -3
  56. package/dist/profiles/content-developer/v1/invariants.d.ts +69 -0
  57. package/dist/profiles/content-developer/v1/invariants.js +95 -0
  58. package/dist/profiles/content-developer/v1/prompt-specs.d.ts +32 -0
  59. package/dist/profiles/content-developer/v1/prompt-specs.js +56 -0
  60. package/dist/profiles/content-developer/v1/prompts.d.ts +4 -3
  61. package/dist/profiles/content-developer/v1/prompts.js +65 -71
  62. package/dist/profiles/content-developer/v1/prompts.test.js +61 -7
  63. package/dist/profiles/content-developer/v1/resources/playbooks/core.md +1 -0
  64. package/dist/profiles/content-developer/v1/resources/playbooks/index.md +11 -8
  65. package/dist/profiles/content-developer/v1/resources/playbooks/recovery/dashboard-diff.md +15 -0
  66. package/dist/profiles/content-developer/v1/resources/playbooks/recovery/preview-required.md +14 -0
  67. package/dist/profiles/content-developer/v1/resources/playbooks/recovery/preview-stale.md +21 -0
  68. package/dist/profiles/content-developer/v1/resources/playbooks.d.ts +3 -2
  69. package/dist/profiles/content-developer/v1/resources/playbooks.js +35 -22
  70. package/dist/profiles/content-governance/v1/invariants.d.ts +27 -0
  71. package/dist/profiles/content-governance/v1/invariants.js +39 -0
  72. package/dist/profiles/content-governance/v1/prompts.d.ts +4 -2
  73. package/dist/profiles/content-governance/v1/prompts.js +31 -19
  74. package/dist/profiles/content-governance/v1/resources/playbooks.d.ts +2 -1
  75. package/dist/profiles/content-governance/v1/resources/playbooks.js +8 -9
  76. package/dist/profiles/content-reader/v1/invariants.d.ts +33 -0
  77. package/dist/profiles/content-reader/v1/invariants.js +47 -0
  78. package/dist/profiles/content-reader/v1/prompt-specs.d.ts +18 -0
  79. package/dist/profiles/content-reader/v1/prompt-specs.js +33 -0
  80. package/dist/profiles/content-reader/v1/prompts.d.ts +3 -2
  81. package/dist/profiles/content-reader/v1/prompts.js +69 -76
  82. package/dist/profiles/content-reader/v1/resources/playbooks.d.ts +2 -1
  83. package/dist/profiles/content-reader/v1/resources/playbooks.js +10 -11
  84. package/dist/profiles/data-analyst/v1/invariants.d.ts +21 -0
  85. package/dist/profiles/data-analyst/v1/invariants.js +31 -0
  86. package/dist/profiles/data-analyst/v1/prompts.d.ts +3 -2
  87. package/dist/profiles/data-analyst/v1/prompts.js +17 -9
  88. package/dist/profiles/data-analyst/v1/resources/playbooks.d.ts +2 -1
  89. package/dist/profiles/data-analyst/v1/resources/playbooks.js +6 -7
  90. package/dist/profiles/lib/playbook-resources.d.ts +16 -34
  91. package/dist/profiles/lib/playbook-resources.js +55 -76
  92. package/dist/profiles/lib/prompt-context.d.ts +71 -0
  93. package/dist/profiles/lib/prompt-context.js +205 -0
  94. package/dist/profiles/lib/prompt-context.test.d.ts +4 -0
  95. package/dist/profiles/lib/prompt-context.test.js +154 -0
  96. package/dist/profiles/lib/prompt-invariants.d.ts +20 -0
  97. package/dist/profiles/lib/prompt-invariants.js +54 -0
  98. package/dist/profiles/organization-audit/v1/invariants.d.ts +24 -0
  99. package/dist/profiles/organization-audit/v1/invariants.js +35 -0
  100. package/dist/profiles/organization-audit/v1/prompts.d.ts +3 -2
  101. package/dist/profiles/organization-audit/v1/prompts.js +44 -28
  102. package/dist/profiles/organization-audit/v1/resources/playbooks.d.ts +2 -1
  103. package/dist/profiles/organization-audit/v1/resources/playbooks.js +10 -3
  104. package/dist/profiles/semantic-layer/v1/invariants.d.ts +27 -0
  105. package/dist/profiles/semantic-layer/v1/invariants.js +39 -0
  106. package/dist/profiles/semantic-layer/v1/prompts.d.ts +3 -2
  107. package/dist/profiles/semantic-layer/v1/prompts.js +37 -26
  108. package/dist/profiles/semantic-layer/v1/resources/playbooks.d.ts +2 -4
  109. package/dist/profiles/semantic-layer/v1/resources/playbooks.js +8 -7
  110. package/dist/profiles/test-support/prompt-context-budget.test.d.ts +5 -0
  111. package/dist/profiles/test-support/prompt-context-budget.test.js +72 -0
  112. package/dist/profiles/types.d.ts +6 -1
  113. package/dist/server/capabilities.d.ts +3 -0
  114. package/dist/server/capabilities.js +4 -2
  115. package/dist/tools/lib/schema-fields.d.ts +4 -0
  116. package/dist/tools/lib/schema-fields.js +8 -1
  117. package/dist/tools/project/developer-content-preview.js +1 -1
  118. package/dist/tools/project/developer-content.js +2 -2
  119. package/dist/tools/query/reader-tool-helpers.d.ts +2 -2
  120. package/dist/tools/query/reader-tool-helpers.js +2 -2
  121. package/dist/tools/query/reader-tool-helpers.test.js +1 -0
  122. package/dist/tools/shared.d.ts +6 -1
  123. package/dist/tools/shared.js +12 -2
  124. package/dist/transports/streamable-http.js +13 -4
  125. package/dist/transports/streamable-http.test.js +8 -3
  126. package/package.json +3 -3
@@ -33,6 +33,26 @@ function setOAuthCreds() {
33
33
  vitest_1.vi.restoreAllMocks();
34
34
  });
35
35
  (0, vitest_1.describe)('loadMcpHttpConfig', () => {
36
+ (0, vitest_1.it)('defaults promptContextPolicy to compact and accepts overrides', () => {
37
+ process.env.LIGHTDASH_URL = 'https://app.lightdash.cloud';
38
+ process.env.NODE_ENV = 'development';
39
+ (0, vitest_1.expect)((0, load_mcp_config_js_1.loadMcpHttpConfig)().promptContextPolicy).toBe('compact');
40
+ process.env.LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT = 'embedded';
41
+ (0, vitest_1.expect)((0, load_mcp_config_js_1.loadMcpHttpConfig)().promptContextPolicy).toBe('embedded');
42
+ });
43
+ (0, vitest_1.it)('fails closed on invalid LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT', () => {
44
+ process.env.LIGHTDASH_URL = 'https://app.lightdash.cloud';
45
+ process.env.NODE_ENV = 'development';
46
+ process.env.LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT = 'uri-only';
47
+ (0, vitest_1.expect)(() => (0, load_mcp_config_js_1.loadMcpHttpConfig)()).toThrow(/LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT/);
48
+ });
49
+ (0, vitest_1.it)('uses explicit promptContextPolicy and skips invalid env resolve', () => {
50
+ process.env.LIGHTDASH_URL = 'https://app.lightdash.cloud';
51
+ process.env.NODE_ENV = 'development';
52
+ process.env.LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT = 'uri-only';
53
+ const config = (0, load_mcp_config_js_1.loadMcpHttpConfig)(process.env, { promptContextPolicy: 'compact' });
54
+ (0, vitest_1.expect)(config.promptContextPolicy).toBe('compact');
55
+ });
36
56
  (0, vitest_1.it)('prefers LIGHTDASH_TOOLS_MCP_HTTP_PORT over MCP_HTTP_PORT', () => {
37
57
  process.env.LIGHTDASH_URL = 'https://app.lightdash.cloud';
38
58
  process.env.LIGHTDASH_TOOLS_MCP_HTTP_PORT = '3200';
@@ -178,6 +198,35 @@ function setOAuthCreds() {
178
198
  const config = (0, load_mcp_config_js_1.loadMcpHttpConfig)();
179
199
  (0, vitest_1.expect)(config.publicUrl).toBe('http://127.0.0.1:3100');
180
200
  });
201
+ (0, vitest_1.it)('parses extra invoke origins in OAuth mode including non-loopback http', () => {
202
+ setOAuthCreds();
203
+ process.env[env_js_1.ENV_LIGHTDASH_TOOLS_MCP_INVOKE_ORIGINS] =
204
+ 'http://mcp.ilb.internal, https://mcp.example.com';
205
+ const config = (0, load_mcp_config_js_1.loadMcpHttpConfig)();
206
+ (0, vitest_1.expect)(config.invokeOrigins.map((origin) => origin.origin)).toEqual([
207
+ 'http://mcp.ilb.internal',
208
+ ]);
209
+ });
210
+ (0, vitest_1.it)('rejects invalid invoke origins in OAuth mode', () => {
211
+ setOAuthCreds();
212
+ process.env[env_js_1.ENV_LIGHTDASH_TOOLS_MCP_INVOKE_ORIGINS] = 'not-a-url';
213
+ (0, vitest_1.expect)(() => (0, load_mcp_config_js_1.loadMcpHttpConfig)()).toThrow(env_js_1.ENV_LIGHTDASH_TOOLS_MCP_INVOKE_ORIGINS);
214
+ });
215
+ (0, vitest_1.it)('ignores invoke origins outside OAuth mode', () => {
216
+ process.env.LIGHTDASH_URL = 'https://app.lightdash.cloud';
217
+ process.env.NODE_ENV = 'development';
218
+ process.env[env_js_1.ENV_LIGHTDASH_TOOLS_MCP_INVOKE_ORIGINS] = 'not-a-url';
219
+ const config = (0, load_mcp_config_js_1.loadMcpHttpConfig)();
220
+ (0, vitest_1.expect)(config.authMode).toBe('none');
221
+ (0, vitest_1.expect)(config.invokeOrigins).toEqual([]);
222
+ });
223
+ (0, vitest_1.it)('warns for non-loopback http invoke origins', () => {
224
+ setOAuthCreds();
225
+ process.env[env_js_1.ENV_LIGHTDASH_TOOLS_MCP_INVOKE_ORIGINS] = 'http://mcp.ilb.internal';
226
+ const config = (0, load_mcp_config_js_1.loadMcpHttpConfig)();
227
+ (0, load_mcp_config_js_1.emitMcpHttpSecurityWarnings)(config);
228
+ (0, vitest_1.expect)(vitest_1.vi.mocked(console.warn).mock.calls.flat().join('\n')).toMatch(/non-loopback http origins/);
229
+ });
181
230
  (0, vitest_1.it)('rejects VALIDATE_TOKEN=false in production', () => {
182
231
  setOAuthCreds();
183
232
  process.env[env_js_1.ENV_LIGHTDASH_TOOLS_MCP_VALIDATE_TOKEN] = 'false';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Prompt-context policy for progressive-disclosure playbook embedding (RFC).
3
+ *
4
+ * Canonical: `LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT`.
5
+ * CLI `--prompt-context` overrides env. Unset → package default (`compact`).
6
+ * Invalid values fail closed.
7
+ */
8
+ export declare const PROMPT_CONTEXT_POLICIES: readonly ["compact", "compatible", "embedded"];
9
+ export type PromptContextPolicy = (typeof PROMPT_CONTEXT_POLICIES)[number];
10
+ /** Package default after progressive-disclosure migration. */
11
+ export declare const DEFAULT_PROMPT_CONTEXT_POLICY: PromptContextPolicy;
12
+ export declare function isPromptContextPolicy(value: string): value is PromptContextPolicy;
13
+ export declare function parsePromptContextPolicy(raw: string | undefined): PromptContextPolicy;
14
+ /** CLI flag wins when present (non-empty). */
15
+ export declare function resolvePromptContextPolicy(opts: {
16
+ cli?: string;
17
+ env?: NodeJS.ProcessEnv;
18
+ }): PromptContextPolicy;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /**
3
+ * Prompt-context policy for progressive-disclosure playbook embedding (RFC).
4
+ *
5
+ * Canonical: `LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT`.
6
+ * CLI `--prompt-context` overrides env. Unset → package default (`compact`).
7
+ * Invalid values fail closed.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.DEFAULT_PROMPT_CONTEXT_POLICY = exports.PROMPT_CONTEXT_POLICIES = void 0;
11
+ exports.isPromptContextPolicy = isPromptContextPolicy;
12
+ exports.parsePromptContextPolicy = parsePromptContextPolicy;
13
+ exports.resolvePromptContextPolicy = resolvePromptContextPolicy;
14
+ const env_js_1 = require("./env.js");
15
+ const read_env_js_1 = require("./read-env.js");
16
+ exports.PROMPT_CONTEXT_POLICIES = ['compact', 'compatible', 'embedded'];
17
+ /** Package default after progressive-disclosure migration. */
18
+ exports.DEFAULT_PROMPT_CONTEXT_POLICY = 'compact';
19
+ function isPromptContextPolicy(value) {
20
+ return exports.PROMPT_CONTEXT_POLICIES.includes(value);
21
+ }
22
+ function parsePromptContextPolicy(raw) {
23
+ if (raw === undefined || raw.trim() === '') {
24
+ return exports.DEFAULT_PROMPT_CONTEXT_POLICY;
25
+ }
26
+ const value = raw.trim();
27
+ if (!isPromptContextPolicy(value)) {
28
+ throw new Error(`${env_js_1.ENV_LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT} must be one of: ${exports.PROMPT_CONTEXT_POLICIES.join(', ')} (got '${value}')`);
29
+ }
30
+ return value;
31
+ }
32
+ /** CLI flag wins when present (non-empty). */
33
+ function resolvePromptContextPolicy(opts) {
34
+ var _a, _b;
35
+ const fromCli = (_a = opts.cli) === null || _a === void 0 ? void 0 : _a.trim();
36
+ if (fromCli) {
37
+ return parsePromptContextPolicy(fromCli);
38
+ }
39
+ return parsePromptContextPolicy((0, read_env_js_1.readEnv)(env_js_1.ENV_LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT, (_b = opts.env) !== null && _b !== void 0 ? _b : process.env));
40
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Prompt-context policy unit tests.
3
+ */
4
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ * Prompt-context policy unit tests.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const vitest_1 = require("vitest");
7
+ const prompt_context_policy_js_1 = require("./prompt-context-policy.js");
8
+ (0, vitest_1.describe)('parsePromptContextPolicy', () => {
9
+ (0, vitest_1.it)('defaults to compact when unset or empty', () => {
10
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)(undefined)).toBe(prompt_context_policy_js_1.DEFAULT_PROMPT_CONTEXT_POLICY);
11
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)('')).toBe('compact');
12
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)(' ')).toBe('compact');
13
+ });
14
+ (0, vitest_1.it)('accepts allowed values', () => {
15
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)('compact')).toBe('compact');
16
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)('compatible')).toBe('compatible');
17
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.parsePromptContextPolicy)('embedded')).toBe('embedded');
18
+ });
19
+ (0, vitest_1.it)('fails closed on unknown values', () => {
20
+ (0, vitest_1.expect)(() => (0, prompt_context_policy_js_1.parsePromptContextPolicy)('uri-only')).toThrow(/LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT/);
21
+ });
22
+ });
23
+ (0, vitest_1.describe)('resolvePromptContextPolicy', () => {
24
+ (0, vitest_1.it)('prefers CLI over env', () => {
25
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.resolvePromptContextPolicy)({
26
+ cli: 'embedded',
27
+ env: { LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT: 'compatible' },
28
+ })).toBe('embedded');
29
+ });
30
+ (0, vitest_1.it)('uses env when CLI omitted', () => {
31
+ (0, vitest_1.expect)((0, prompt_context_policy_js_1.resolvePromptContextPolicy)({
32
+ env: { LIGHTDASH_TOOLS_MCP_PROMPT_CONTEXT: 'compatible' },
33
+ })).toBe('compatible');
34
+ });
35
+ });
@@ -6,5 +6,5 @@ const enabled_profiles_js_1 = require("./enabled-profiles.js");
6
6
  function makeTestMcpHttpConfig(overrides) {
7
7
  var _a;
8
8
  const enabledProfiles = (_a = overrides === null || overrides === void 0 ? void 0 : overrides.enabledProfiles) !== null && _a !== void 0 ? _a : enabled_profiles_js_1.UNRESTRICTED_ENABLED_PROFILES;
9
- return Object.assign({ lightdashUrl: 'https://app.lightdash.cloud', host: '127.0.0.1', port: 3100, publicUrl: 'https://mcp.example.com', mcpPath: (0, enabled_profiles_js_1.resolveRootMcpPath)(enabledProfiles), enabledProfiles, authMode: 'lightdash-oauth', allowedOrigins: [], maxBodyBytes: 1024, requiredScopes: ['mcp:read'], scopesSupported: ['mcp:read', 'mcp:write'], validateToken: true, tokenValidationCacheTtlMs: 30000 }, overrides);
9
+ return Object.assign({ lightdashUrl: 'https://app.lightdash.cloud', host: '127.0.0.1', port: 3100, publicUrl: 'https://mcp.example.com', invokeOrigins: [], mcpPath: (0, enabled_profiles_js_1.resolveRootMcpPath)(enabledProfiles), enabledProfiles, promptContextPolicy: 'compact', authMode: 'lightdash-oauth', allowedOrigins: [], maxBodyBytes: 1024, requiredScopes: ['mcp:read'], scopesSupported: ['mcp:read', 'mcp:write'], validateToken: true, tokenValidationCacheTtlMs: 30000 }, overrides);
10
10
  }
@@ -3,11 +3,13 @@
3
3
  * Auth / Origin / pin stay outside the handler; the factory only needs
4
4
  * contextProvider + profile (ADR-0019).
5
5
  */
6
+ import type { PromptContextPolicy } from '../config/prompt-context-policy.js';
6
7
  import type { ProfileDefinition } from '../profiles/types.js';
7
8
  import type { McpContextProvider } from '../server/request-context.js';
8
9
  export type McpPostFactoryStore = {
9
10
  contextProvider: McpContextProvider;
10
11
  profile: ProfileDefinition;
12
+ promptContextPolicy?: PromptContextPolicy;
11
13
  };
12
14
  export declare function getMcpPostFactoryStore(): McpPostFactoryStore | undefined;
13
15
  export declare function runWithMcpPostFactoryAsync<T>(store: McpPostFactoryStore, fn: () => Promise<T>): Promise<T>;
package/dist/http.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  /**
2
2
  * MCP server entrypoint (Streamable HTTP).
3
+ * Prefer `lightdash-mcp http` (bin) which resolves prompt-context before start.
3
4
  */
4
- export {};
5
+ import type { PromptContextPolicy } from './config/prompt-context-policy.js';
6
+ export type StartHttpOptions = {
7
+ promptContextPolicy?: PromptContextPolicy;
8
+ };
9
+ /** Start Streamable HTTP MCP (optional resolved prompt-context policy from CLI). */
10
+ export declare function startHttp(options?: StartHttpOptions): Promise<void>;
@@ -0,0 +1,50 @@
1
+ import { type IncomingHttpHeaders, type Server } from 'node:http';
2
+ import type { McpHttpConfig } from './config/load-mcp-config.js';
3
+ export declare const TOKEN_A: string;
4
+ export declare const TOKEN_B: string;
5
+ export declare const TOKEN_READ_ONLY: string;
6
+ export declare const OPAQUE_TOKEN_A = "opaque-token-user-a";
7
+ export declare const INITIALIZE_BODY: {
8
+ jsonrpc: string;
9
+ method: string;
10
+ params: {
11
+ protocolVersion: string;
12
+ capabilities: {};
13
+ clientInfo: {
14
+ name: string;
15
+ version: string;
16
+ };
17
+ };
18
+ id: number;
19
+ };
20
+ export interface MockLightdashServer {
21
+ baseUrl: string;
22
+ authorizationHeaders: string[];
23
+ close: () => Promise<void>;
24
+ }
25
+ export interface McpHttpServer {
26
+ baseUrl: string;
27
+ close: () => Promise<void>;
28
+ }
29
+ export declare function listen(server: Server, port: number, host: string): Promise<void>;
30
+ export declare function startMockLightdashServer(): Promise<MockLightdashServer>;
31
+ export declare function baseOAuthConfig(lightdashUrl: string, overrides?: Partial<McpHttpConfig>): McpHttpConfig;
32
+ export declare function brokerAccessToken(mcpBaseUrl: string, lightdashAccessToken: string, options?: {
33
+ path?: string;
34
+ scope?: string;
35
+ }): string;
36
+ export declare function nodeRequest(options: {
37
+ baseUrl: string;
38
+ path: string;
39
+ method?: string;
40
+ headers?: Record<string, string>;
41
+ body?: string;
42
+ }): Promise<{
43
+ status: number;
44
+ headers: IncomingHttpHeaders;
45
+ json: unknown;
46
+ }>;
47
+ export declare function postMcp(baseUrl: string, body: unknown, options?: {
48
+ token?: string;
49
+ path?: string;
50
+ }): Promise<Response>;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INITIALIZE_BODY = exports.OPAQUE_TOKEN_A = exports.TOKEN_READ_ONLY = exports.TOKEN_B = exports.TOKEN_A = void 0;
4
+ exports.listen = listen;
5
+ exports.startMockLightdashServer = startMockLightdashServer;
6
+ exports.baseOAuthConfig = baseOAuthConfig;
7
+ exports.brokerAccessToken = brokerAccessToken;
8
+ exports.nodeRequest = nodeRequest;
9
+ exports.postMcp = postMcp;
10
+ const node_http_1 = require("node:http");
11
+ const client_1 = require("@lightdash-tools/client");
12
+ const mcp_access_token_js_1 = require("./auth/oauth-broker/mcp-access-token.js");
13
+ const test_mcp_http_config_js_1 = require("./config/test-mcp-http-config.js");
14
+ // These constants model downstream Lightdash credentials only. MCP clients never send
15
+ // them directly after ADR-0026; tests wrap them in broker-issued MCP access tokens.
16
+ exports.TOKEN_A = scopedAccessToken('mcp:read mcp:write', 'token-a');
17
+ exports.TOKEN_B = scopedAccessToken('mcp:read mcp:write', 'token-b');
18
+ exports.TOKEN_READ_ONLY = scopedAccessToken('mcp:read', 'token-read-only');
19
+ exports.OPAQUE_TOKEN_A = 'opaque-token-user-a';
20
+ const USER_A = { userUuid: 'user-a-uuid', email: 'a@example.com', organizationUuid: 'org-a-uuid' };
21
+ const USER_B = { userUuid: 'user-b-uuid', email: 'b@example.com', organizationUuid: 'org-b-uuid' };
22
+ function scopedAccessToken(scope, subject) {
23
+ const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url');
24
+ const payload = Buffer.from(JSON.stringify({ scope, sub: subject })).toString('base64url');
25
+ return `${header}.${payload}.signature`;
26
+ }
27
+ exports.INITIALIZE_BODY = {
28
+ jsonrpc: '2.0',
29
+ method: 'initialize',
30
+ params: {
31
+ protocolVersion: '2024-11-05',
32
+ capabilities: {},
33
+ clientInfo: { name: 'integration-test', version: '1.0.0' },
34
+ },
35
+ id: 1,
36
+ };
37
+ function listen(server, port, host) {
38
+ return new Promise((resolve, reject) => {
39
+ server.once('error', reject);
40
+ server.listen(port, host, () => {
41
+ server.removeListener('error', reject);
42
+ resolve();
43
+ });
44
+ });
45
+ }
46
+ function writeJson(res, status, body) {
47
+ res.writeHead(status, { 'Content-Type': 'application/json' }).end(JSON.stringify(body));
48
+ }
49
+ function writeUnauthorized(res) {
50
+ writeJson(res, 401, {
51
+ status: 'error',
52
+ error: { statusCode: 401, name: 'Unauthorized', message: 'Invalid token' },
53
+ });
54
+ }
55
+ function bearerFromAuth(auth) {
56
+ return auth.startsWith('Bearer ') ? auth.slice('Bearer '.length).trim() : '';
57
+ }
58
+ function userForAuth(auth, users) {
59
+ return users.get(bearerFromAuth(auth));
60
+ }
61
+ async function startMockLightdashServer() {
62
+ const authorizationHeaders = [];
63
+ const users = new Map([
64
+ [exports.TOKEN_A, USER_A],
65
+ [exports.TOKEN_B, USER_B],
66
+ [exports.TOKEN_READ_ONLY, USER_A],
67
+ [exports.OPAQUE_TOKEN_A, USER_A],
68
+ ]);
69
+ let issuerBaseUrl = '';
70
+ const server = (0, node_http_1.createServer)((req, res) => {
71
+ var _a;
72
+ const authHeader = req.headers.authorization;
73
+ const auth = typeof authHeader === 'string' ? authHeader : ((_a = authHeader === null || authHeader === void 0 ? void 0 : authHeader[0]) !== null && _a !== void 0 ? _a : '');
74
+ authorizationHeaders.push(auth);
75
+ if (req.method === 'GET' && req.url === '/.well-known/oauth-authorization-server') {
76
+ writeJson(res, 200, {
77
+ issuer: issuerBaseUrl,
78
+ authorization_endpoint: `${issuerBaseUrl}/api/v1/oauth/authorize`,
79
+ token_endpoint: `${issuerBaseUrl}/api/v1/oauth/token`,
80
+ revocation_endpoint: `${issuerBaseUrl}/api/v1/oauth/revoke`,
81
+ registration_endpoint: `${issuerBaseUrl}/api/v1/oauth/register`,
82
+ userinfo_endpoint: `${issuerBaseUrl}/api/v1/oauth/userinfo`,
83
+ });
84
+ return;
85
+ }
86
+ if (req.method === 'GET' && req.url === '/api/v1/user') {
87
+ const user = userForAuth(auth, users);
88
+ if (user === undefined) {
89
+ writeUnauthorized(res);
90
+ return;
91
+ }
92
+ writeJson(res, 200, { status: 'ok', results: user });
93
+ return;
94
+ }
95
+ if (req.method === 'GET' && req.url === '/api/v1/org/projects') {
96
+ if (userForAuth(auth, users) === undefined) {
97
+ writeUnauthorized(res);
98
+ return;
99
+ }
100
+ writeJson(res, 200, {
101
+ status: 'ok',
102
+ results: [{ projectUuid: 'project-a-uuid', name: 'Project A', type: 'DEFAULT' }],
103
+ });
104
+ return;
105
+ }
106
+ res.writeHead(404).end();
107
+ });
108
+ await listen(server, 0, '127.0.0.1');
109
+ const address = server.address();
110
+ issuerBaseUrl = `http://127.0.0.1:${address.port}`;
111
+ return {
112
+ baseUrl: issuerBaseUrl,
113
+ authorizationHeaders,
114
+ close: () => new Promise((resolve, reject) => {
115
+ server.close((err) => {
116
+ if (err)
117
+ reject(err);
118
+ else
119
+ resolve();
120
+ });
121
+ }),
122
+ };
123
+ }
124
+ function baseOAuthConfig(lightdashUrl, overrides) {
125
+ return (0, test_mcp_http_config_js_1.makeTestMcpHttpConfig)(Object.assign({ lightdashUrl, port: 0, publicUrl: 'http://127.0.0.1:0', oauthClientId: 'test-client-id', oauthClientSecret: new client_1.SecretString('test-client-secret'), maxBodyBytes: 1024 * 1024, requiredScopes: [], scopesSupported: [] }, overrides));
126
+ }
127
+ function brokerAccessToken(mcpBaseUrl, lightdashAccessToken, options = {}) {
128
+ var _a;
129
+ const path = (_a = options.path) !== null && _a !== void 0 ? _a : '/semantic-layer/v1/mcp';
130
+ return (0, mcp_access_token_js_1.mintMcpAccessToken)(baseOAuthConfig('https://lightdash.invalid'), {
131
+ lightdashAccessToken,
132
+ clientId: 'integration-test-client',
133
+ resource: `${mcpBaseUrl}${path}`,
134
+ scope: options.scope,
135
+ expiresAtMs: Date.now() + 60000,
136
+ }).accessToken;
137
+ }
138
+ function nodeRequest(options) {
139
+ const url = new URL(options.path, options.baseUrl);
140
+ return new Promise((resolve, reject) => {
141
+ var _a;
142
+ const req = (0, node_http_1.request)({
143
+ hostname: url.hostname,
144
+ port: url.port,
145
+ path: `${url.pathname}${url.search}`,
146
+ method: (_a = options.method) !== null && _a !== void 0 ? _a : 'GET',
147
+ headers: options.headers,
148
+ }, (res) => {
149
+ const chunks = [];
150
+ res.on('data', (chunk) => chunks.push(chunk));
151
+ res.on('end', () => {
152
+ var _a;
153
+ const text = Buffer.concat(chunks).toString('utf8');
154
+ let json = text;
155
+ try {
156
+ json = JSON.parse(text);
157
+ }
158
+ catch (_b) {
159
+ // keep text
160
+ }
161
+ resolve({
162
+ status: (_a = res.statusCode) !== null && _a !== void 0 ? _a : 0,
163
+ headers: res.headers,
164
+ json,
165
+ });
166
+ });
167
+ });
168
+ req.on('error', reject);
169
+ if (options.body !== undefined) {
170
+ req.write(options.body);
171
+ }
172
+ req.end();
173
+ });
174
+ }
175
+ async function postMcp(baseUrl, body, options) {
176
+ var _a;
177
+ const headers = {
178
+ 'Content-Type': 'application/json',
179
+ Accept: 'application/json, text/event-stream',
180
+ };
181
+ if (options === null || options === void 0 ? void 0 : options.token) {
182
+ headers.Authorization = `Bearer ${options.token}`;
183
+ }
184
+ const path = (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : '/semantic-layer/v1/mcp';
185
+ return fetch(`${baseUrl}${path}`, {
186
+ method: 'POST',
187
+ headers,
188
+ body: JSON.stringify(body),
189
+ });
190
+ }