@atlassian-dc-mcp/common 0.16.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 (96) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/build/__tests__/runtime-config.test.js +65 -14
  3. package/build/__tests__/runtime-config.test.js.map +1 -1
  4. package/build/__tests__/setup-cli.test.d.ts +2 -0
  5. package/build/__tests__/setup-cli.test.d.ts.map +1 -0
  6. package/build/__tests__/setup-cli.test.js +166 -0
  7. package/build/__tests__/setup-cli.test.js.map +1 -0
  8. package/build/config/__tests__/registry.test.d.ts +2 -0
  9. package/build/config/__tests__/registry.test.d.ts.map +1 -0
  10. package/build/config/__tests__/registry.test.js +105 -0
  11. package/build/config/__tests__/registry.test.js.map +1 -0
  12. package/build/config/__tests__/sources/env-file.test.d.ts +2 -0
  13. package/build/config/__tests__/sources/env-file.test.d.ts.map +1 -0
  14. package/build/config/__tests__/sources/env-file.test.js +93 -0
  15. package/build/config/__tests__/sources/env-file.test.js.map +1 -0
  16. package/build/config/__tests__/sources/home-file.test.d.ts +2 -0
  17. package/build/config/__tests__/sources/home-file.test.d.ts.map +1 -0
  18. package/build/config/__tests__/sources/home-file.test.js +114 -0
  19. package/build/config/__tests__/sources/home-file.test.js.map +1 -0
  20. package/build/config/__tests__/sources/macos-keychain.test.d.ts +2 -0
  21. package/build/config/__tests__/sources/macos-keychain.test.d.ts.map +1 -0
  22. package/build/config/__tests__/sources/macos-keychain.test.js +109 -0
  23. package/build/config/__tests__/sources/macos-keychain.test.js.map +1 -0
  24. package/build/config/__tests__/sources/process-env.test.d.ts +2 -0
  25. package/build/config/__tests__/sources/process-env.test.d.ts.map +1 -0
  26. package/build/config/__tests__/sources/process-env.test.js +51 -0
  27. package/build/config/__tests__/sources/process-env.test.js.map +1 -0
  28. package/build/config/index.d.ts +8 -0
  29. package/build/config/index.d.ts.map +1 -0
  30. package/build/config/index.js +8 -0
  31. package/build/config/index.js.map +1 -0
  32. package/build/config/registry.d.ts +26 -0
  33. package/build/config/registry.d.ts.map +1 -0
  34. package/build/config/registry.js +51 -0
  35. package/build/config/registry.js.map +1 -0
  36. package/build/config/runtime-config.d.ts +14 -0
  37. package/build/config/runtime-config.d.ts.map +1 -0
  38. package/build/config/runtime-config.js +41 -0
  39. package/build/config/runtime-config.js.map +1 -0
  40. package/build/config/source.d.ts +28 -0
  41. package/build/config/source.d.ts.map +1 -0
  42. package/build/config/source.js +11 -0
  43. package/build/config/source.js.map +1 -0
  44. package/build/config/sources/dotenv-file-cache.d.ts +12 -0
  45. package/build/config/sources/dotenv-file-cache.d.ts.map +1 -0
  46. package/build/config/sources/dotenv-file-cache.js +41 -0
  47. package/build/config/sources/dotenv-file-cache.js.map +1 -0
  48. package/build/config/sources/env-file.d.ts +16 -0
  49. package/build/config/sources/env-file.d.ts.map +1 -0
  50. package/build/config/sources/env-file.js +48 -0
  51. package/build/config/sources/env-file.js.map +1 -0
  52. package/build/config/sources/home-file.d.ts +19 -0
  53. package/build/config/sources/home-file.d.ts.map +1 -0
  54. package/build/config/sources/home-file.js +102 -0
  55. package/build/config/sources/home-file.js.map +1 -0
  56. package/build/config/sources/macos-keychain.d.ts +26 -0
  57. package/build/config/sources/macos-keychain.d.ts.map +1 -0
  58. package/build/config/sources/macos-keychain.js +88 -0
  59. package/build/config/sources/macos-keychain.js.map +1 -0
  60. package/build/config/sources/process-env.d.ts +8 -0
  61. package/build/config/sources/process-env.d.ts.map +1 -0
  62. package/build/config/sources/process-env.js +12 -0
  63. package/build/config/sources/process-env.js.map +1 -0
  64. package/build/index.d.ts +2 -1
  65. package/build/index.d.ts.map +1 -1
  66. package/build/index.js +2 -1
  67. package/build/index.js.map +1 -1
  68. package/build/setup-cli.d.ts +25 -0
  69. package/build/setup-cli.d.ts.map +1 -0
  70. package/build/setup-cli.js +215 -0
  71. package/build/setup-cli.js.map +1 -0
  72. package/package.json +5 -4
  73. package/src/__tests__/runtime-config.test.ts +75 -27
  74. package/src/__tests__/setup-cli.test.ts +175 -0
  75. package/src/config/__tests__/registry.test.ts +118 -0
  76. package/src/config/__tests__/sources/env-file.test.ts +113 -0
  77. package/src/config/__tests__/sources/home-file.test.ts +129 -0
  78. package/src/config/__tests__/sources/macos-keychain.test.ts +136 -0
  79. package/src/config/__tests__/sources/process-env.test.ts +59 -0
  80. package/src/config/index.ts +7 -0
  81. package/src/config/registry.ts +79 -0
  82. package/src/config/runtime-config.ts +60 -0
  83. package/src/config/source.ts +37 -0
  84. package/src/config/sources/dotenv-file-cache.ts +54 -0
  85. package/src/config/sources/env-file.ts +61 -0
  86. package/src/config/sources/home-file.ts +121 -0
  87. package/src/config/sources/macos-keychain.ts +117 -0
  88. package/src/config/sources/process-env.ts +15 -0
  89. package/src/index.ts +2 -1
  90. package/src/setup-cli.ts +308 -0
  91. package/tsconfig.tsbuildinfo +1 -1
  92. package/build/runtime-config.d.ts +0 -14
  93. package/build/runtime-config.d.ts.map +0 -1
  94. package/build/runtime-config.js +0 -123
  95. package/build/runtime-config.js.map +0 -1
  96. package/src/runtime-config.ts +0 -176
@@ -0,0 +1,129 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { HomeFileSource, getHomeFilePath, HOME_DIR_NAME } from '../../sources/home-file.js';
5
+ import type { ProductDefinition } from '../../source.js';
6
+
7
+ const JIRA: ProductDefinition = {
8
+ id: 'jira',
9
+ envVars: {
10
+ host: 'JIRA_HOST',
11
+ apiBasePath: 'JIRA_API_BASE_PATH',
12
+ token: 'JIRA_API_TOKEN',
13
+ defaultPageSize: 'JIRA_DEFAULT_PAGE_SIZE',
14
+ },
15
+ };
16
+
17
+ const CONFLUENCE: ProductDefinition = {
18
+ id: 'confluence',
19
+ envVars: {
20
+ host: 'CONFLUENCE_HOST',
21
+ apiBasePath: 'CONFLUENCE_API_BASE_PATH',
22
+ token: 'CONFLUENCE_API_TOKEN',
23
+ defaultPageSize: 'CONFLUENCE_DEFAULT_PAGE_SIZE',
24
+ },
25
+ };
26
+
27
+ describe('HomeFileSource', () => {
28
+ let tempHome: string;
29
+ let homedirSpy: jest.SpyInstance;
30
+
31
+ beforeEach(() => {
32
+ tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'home-file-src-'));
33
+ homedirSpy = jest.spyOn(os, 'homedir').mockReturnValue(tempHome);
34
+ });
35
+
36
+ afterEach(() => {
37
+ homedirSpy.mockRestore();
38
+ fs.rmSync(tempHome, { recursive: true, force: true });
39
+ });
40
+
41
+ it('returns undefined when the home file does not exist', () => {
42
+ expect(new HomeFileSource().read(JIRA, 'host')).toBeUndefined();
43
+ });
44
+
45
+ it('writes values and reads them back', () => {
46
+ const source = new HomeFileSource();
47
+ source.write(JIRA, 'host', 'j-host');
48
+ source.write(JIRA, 'token', 'j-token');
49
+ expect(source.read(JIRA, 'host')).toBe('j-host');
50
+ expect(source.read(JIRA, 'token')).toBe('j-token');
51
+ });
52
+
53
+ it('writes files with mode 0600 on POSIX', () => {
54
+ if (process.platform === 'win32') {
55
+ return;
56
+ }
57
+ const source = new HomeFileSource();
58
+ source.write(JIRA, 'host', 'j-host');
59
+ const stat = fs.statSync(getHomeFilePath(JIRA));
60
+ expect(stat.mode & 0o777).toBe(0o600);
61
+ });
62
+
63
+ it('merge preserves unrelated keys', () => {
64
+ const filePath = getHomeFilePath(JIRA);
65
+ fs.mkdirSync(path.join(tempHome, HOME_DIR_NAME), { recursive: true });
66
+ fs.writeFileSync(filePath, 'JIRA_HOST=old\nUNRELATED_KEY=keep-me\n', { mode: 0o600 });
67
+ const source = new HomeFileSource();
68
+ source.write(JIRA, 'host', 'new');
69
+ const contents = fs.readFileSync(filePath, 'utf8');
70
+ expect(contents).toContain('JIRA_HOST=new');
71
+ expect(contents).toContain('UNRELATED_KEY=keep-me');
72
+ });
73
+
74
+ it('clear removes only the specified key', () => {
75
+ const source = new HomeFileSource();
76
+ source.write(JIRA, 'host', 'j-host');
77
+ source.write(JIRA, 'token', 'j-token');
78
+ source.clear(JIRA, 'token');
79
+ expect(source.read(JIRA, 'host')).toBe('j-host');
80
+ expect(source.read(JIRA, 'token')).toBeUndefined();
81
+ });
82
+
83
+ it('quotes values that contain whitespace', () => {
84
+ const source = new HomeFileSource();
85
+ source.write(JIRA, 'host', 'value with spaces');
86
+ const contents = fs.readFileSync(getHomeFilePath(JIRA), 'utf8');
87
+ expect(contents).toMatch(/JIRA_HOST="value with spaces"/);
88
+ expect(source.read(JIRA, 'host')).toBe('value with spaces');
89
+ });
90
+
91
+ it('stores each product in its own file', () => {
92
+ const source = new HomeFileSource();
93
+ source.write(JIRA, 'host', 'j-host');
94
+ source.write(CONFLUENCE, 'host', 'c-host');
95
+ expect(fs.existsSync(getHomeFilePath(JIRA))).toBe(true);
96
+ expect(fs.existsSync(getHomeFilePath(CONFLUENCE))).toBe(true);
97
+ expect(source.read(JIRA, 'host')).toBe('j-host');
98
+ expect(source.read(CONFLUENCE, 'host')).toBe('c-host');
99
+ });
100
+
101
+ it('evicts cache when the home file is deleted externally so writes do not merge stale values', () => {
102
+ const source = new HomeFileSource();
103
+ source.write(JIRA, 'host', 'stale-host');
104
+ expect(source.read(JIRA, 'host')).toBe('stale-host');
105
+
106
+ fs.unlinkSync(getHomeFilePath(JIRA));
107
+ expect(source.read(JIRA, 'host')).toBeUndefined();
108
+
109
+ source.write(JIRA, 'token', 'fresh-token');
110
+ const contents = fs.readFileSync(getHomeFilePath(JIRA), 'utf8');
111
+ expect(contents).not.toContain('stale-host');
112
+ expect(contents).toContain('JIRA_API_TOKEN=fresh-token');
113
+ });
114
+
115
+ it('removes the temp file if rename fails', () => {
116
+ const source = new HomeFileSource();
117
+ const renameSpy = jest.spyOn(fs, 'renameSync').mockImplementation(() => {
118
+ throw new Error('boom');
119
+ });
120
+ try {
121
+ expect(() => source.write(JIRA, 'host', 'x')).toThrow('boom');
122
+ const dir = path.join(tempHome, HOME_DIR_NAME);
123
+ const entries = fs.existsSync(dir) ? fs.readdirSync(dir) : [];
124
+ expect(entries.filter((e) => e.includes('.tmp-'))).toEqual([]);
125
+ } finally {
126
+ renameSpy.mockRestore();
127
+ }
128
+ });
129
+ });
@@ -0,0 +1,136 @@
1
+ import { MacosKeychainSource, SECURITY_BINARY, type KeychainDeps } from '../../sources/macos-keychain.js';
2
+ import type { ProductDefinition } from '../../source.js';
3
+
4
+ const JIRA: ProductDefinition = {
5
+ id: 'jira',
6
+ envVars: {
7
+ host: 'JIRA_HOST',
8
+ apiBasePath: 'JIRA_API_BASE_PATH',
9
+ token: 'JIRA_API_TOKEN',
10
+ defaultPageSize: 'JIRA_DEFAULT_PAGE_SIZE',
11
+ },
12
+ };
13
+
14
+ function makeDeps(overrides: Partial<KeychainDeps> = {}) {
15
+ return {
16
+ execFileSync: jest.fn(),
17
+ existsSync: jest.fn(() => true),
18
+ getPlatform: () => 'darwin' as NodeJS.Platform,
19
+ ...overrides,
20
+ };
21
+ }
22
+
23
+ describe('MacosKeychainSource', () => {
24
+ it('isAvailable is true on darwin when binary exists', () => {
25
+ expect(new MacosKeychainSource(makeDeps()).isAvailable()).toBe(true);
26
+ });
27
+
28
+ it('isAvailable is false off darwin', () => {
29
+ const deps = makeDeps({ getPlatform: () => 'linux' as NodeJS.Platform });
30
+ expect(new MacosKeychainSource(deps).isAvailable()).toBe(false);
31
+ });
32
+
33
+ it('isAvailable is false when binary is missing', () => {
34
+ const deps = makeDeps({ existsSync: jest.fn(() => false) });
35
+ expect(new MacosKeychainSource(deps).isAvailable()).toBe(false);
36
+ });
37
+
38
+ it('read returns token on success and strips trailing newline', () => {
39
+ const execFileSync = jest.fn().mockReturnValueOnce('secret-token\n');
40
+ const deps = makeDeps({ execFileSync: execFileSync as unknown as KeychainDeps['execFileSync'] });
41
+ const source = new MacosKeychainSource(deps);
42
+ expect(source.read(JIRA, 'token')).toBe('secret-token');
43
+ expect(execFileSync).toHaveBeenCalledWith(
44
+ SECURITY_BINARY,
45
+ ['find-generic-password', '-s', 'atlassian-dc-mcp', '-a', 'jira-token', '-w'],
46
+ expect.objectContaining({ encoding: 'utf8', timeout: 5000 }),
47
+ );
48
+ });
49
+
50
+ it('read returns undefined when security exits non-zero (not found)', () => {
51
+ const execFileSync = jest.fn().mockImplementationOnce(() => {
52
+ const err: NodeJS.ErrnoException & { status?: number } = new Error('not found');
53
+ err.status = 44;
54
+ throw err;
55
+ });
56
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
57
+ expect(source.read(JIRA, 'token')).toBeUndefined();
58
+ });
59
+
60
+ it('read returns undefined when keychain is locked (non-44)', () => {
61
+ const execFileSync = jest.fn().mockImplementationOnce(() => {
62
+ const err: NodeJS.ErrnoException & { status?: number } = new Error('auth failed');
63
+ err.status = 128;
64
+ throw err;
65
+ });
66
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
67
+ expect(source.read(JIRA, 'token')).toBeUndefined();
68
+ });
69
+
70
+ it('read returns undefined for non-token keys without invoking security', () => {
71
+ const execFileSync = jest.fn();
72
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
73
+ expect(source.read(JIRA, 'host')).toBeUndefined();
74
+ expect(execFileSync).not.toHaveBeenCalled();
75
+ });
76
+
77
+ it('caches reads', () => {
78
+ const execFileSync = jest.fn().mockReturnValueOnce('cached\n');
79
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
80
+ source.read(JIRA, 'token');
81
+ source.read(JIRA, 'token');
82
+ source.read(JIRA, 'token');
83
+ expect(execFileSync).toHaveBeenCalledTimes(1);
84
+ });
85
+
86
+ it('write uses -U flag', () => {
87
+ const execFileSync = jest.fn().mockReturnValueOnce('');
88
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
89
+ source.write(JIRA, 'token', 'abc');
90
+ expect(execFileSync).toHaveBeenCalledWith(
91
+ SECURITY_BINARY,
92
+ ['add-generic-password', '-U', '-s', 'atlassian-dc-mcp', '-a', 'jira-token', '-w', 'abc'],
93
+ expect.objectContaining({ encoding: 'utf8' }),
94
+ );
95
+ });
96
+
97
+ it('write throws when the key is not token', () => {
98
+ const execFileSync = jest.fn();
99
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
100
+ expect(() => source.write(JIRA, 'host', 'x')).toThrow(/only stores the token/);
101
+ expect(execFileSync).not.toHaveBeenCalled();
102
+ });
103
+
104
+ it('round-trips a token containing shell metacharacters', () => {
105
+ const tricky = 'a\'b"c$d`e';
106
+ const execFileSync = jest
107
+ .fn()
108
+ .mockImplementationOnce(() => '')
109
+ .mockImplementationOnce(() => `${tricky}\n`);
110
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
111
+ source.write(JIRA, 'token', tricky);
112
+ expect(source.read(JIRA, 'token')).toBe(tricky);
113
+ const writeArgs = execFileSync.mock.calls[0][1] as string[];
114
+ expect(writeArgs[writeArgs.length - 1]).toBe(tricky);
115
+ });
116
+
117
+ it('clear invokes delete-generic-password and caches undefined', () => {
118
+ const execFileSync = jest.fn().mockImplementationOnce(() => '');
119
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
120
+ source.clear(JIRA, 'token');
121
+ expect(execFileSync).toHaveBeenCalledWith(
122
+ SECURITY_BINARY,
123
+ ['delete-generic-password', '-s', 'atlassian-dc-mcp', '-a', 'jira-token'],
124
+ expect.any(Object),
125
+ );
126
+ expect(source.read(JIRA, 'token')).toBeUndefined();
127
+ });
128
+
129
+ it('clear swallows errors', () => {
130
+ const execFileSync = jest.fn().mockImplementationOnce(() => {
131
+ throw new Error('not found');
132
+ });
133
+ const source = new MacosKeychainSource(makeDeps({ execFileSync: execFileSync as any }));
134
+ expect(() => source.clear(JIRA, 'token')).not.toThrow();
135
+ });
136
+ });
@@ -0,0 +1,59 @@
1
+ import { ProcessEnvSource } from '../../sources/process-env.js';
2
+ import type { ProductDefinition } from '../../source.js';
3
+
4
+ const JIRA: ProductDefinition = {
5
+ id: 'jira',
6
+ envVars: {
7
+ host: 'JIRA_HOST',
8
+ apiBasePath: 'JIRA_API_BASE_PATH',
9
+ token: 'JIRA_API_TOKEN',
10
+ defaultPageSize: 'JIRA_DEFAULT_PAGE_SIZE',
11
+ },
12
+ };
13
+
14
+ const CONFLUENCE: ProductDefinition = {
15
+ id: 'confluence',
16
+ envVars: {
17
+ host: 'CONFLUENCE_HOST',
18
+ apiBasePath: 'CONFLUENCE_API_BASE_PATH',
19
+ token: 'CONFLUENCE_API_TOKEN',
20
+ defaultPageSize: 'CONFLUENCE_DEFAULT_PAGE_SIZE',
21
+ },
22
+ };
23
+
24
+ describe('ProcessEnvSource', () => {
25
+ const originalEnv = process.env;
26
+
27
+ beforeEach(() => {
28
+ process.env = { ...originalEnv };
29
+ delete process.env.JIRA_HOST;
30
+ delete process.env.JIRA_API_TOKEN;
31
+ delete process.env.CONFLUENCE_HOST;
32
+ });
33
+
34
+ afterAll(() => {
35
+ process.env = originalEnv;
36
+ });
37
+
38
+ it('maps product+key to the correct env var', () => {
39
+ process.env.JIRA_HOST = 'j-host';
40
+ process.env.JIRA_API_TOKEN = 'j-token';
41
+ process.env.CONFLUENCE_HOST = 'c-host';
42
+ const source = new ProcessEnvSource();
43
+ expect(source.read(JIRA, 'host')).toBe('j-host');
44
+ expect(source.read(JIRA, 'token')).toBe('j-token');
45
+ expect(source.read(CONFLUENCE, 'host')).toBe('c-host');
46
+ });
47
+
48
+ it('returns undefined for absent or whitespace-only values', () => {
49
+ const source = new ProcessEnvSource();
50
+ expect(source.read(JIRA, 'host')).toBeUndefined();
51
+ process.env.JIRA_HOST = ' ';
52
+ expect(source.read(JIRA, 'host')).toBeUndefined();
53
+ });
54
+
55
+ it('trims surrounding whitespace', () => {
56
+ process.env.JIRA_HOST = ' j-host ';
57
+ expect(new ProcessEnvSource().read(JIRA, 'host')).toBe('j-host');
58
+ });
59
+ });
@@ -0,0 +1,7 @@
1
+ export * from './source.js';
2
+ export * from './registry.js';
3
+ export * from './runtime-config.js';
4
+ export { ProcessEnvSource } from './sources/process-env.js';
5
+ export { EnvFileSource, ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR } from './sources/env-file.js';
6
+ export { HomeFileSource, getHomeFilePath, HOME_DIR_NAME } from './sources/home-file.js';
7
+ export { MacosKeychainSource, KEYCHAIN_SERVICE } from './sources/macos-keychain.js';
@@ -0,0 +1,79 @@
1
+ import type {
2
+ ConfigKey,
3
+ ProductDefinition,
4
+ ReadableSource,
5
+ SourceLocation,
6
+ WritableSource,
7
+ } from './source.js';
8
+ import { isWritableSource } from './source.js';
9
+ import { ProcessEnvSource } from './sources/process-env.js';
10
+ import { EnvFileSource } from './sources/env-file.js';
11
+ import { HomeFileSource } from './sources/home-file.js';
12
+ import { MacosKeychainSource } from './sources/macos-keychain.js';
13
+
14
+ export interface ResolvedValue {
15
+ value: string | undefined;
16
+ from?: SourceLocation;
17
+ }
18
+
19
+ export interface ConfigRegistry {
20
+ readonly sources: readonly ReadableSource[];
21
+ initialize(options?: { cwd?: string }): void;
22
+ resolve(product: ProductDefinition, key: ConfigKey): ResolvedValue;
23
+ locate(product: ProductDefinition, key: ConfigKey): SourceLocation[];
24
+ getWritableSource<T extends WritableSource>(predicate: (s: WritableSource) => s is T): T | undefined;
25
+ }
26
+
27
+ export class DefaultConfigRegistry implements ConfigRegistry {
28
+ readonly sources: readonly ReadableSource[];
29
+
30
+ constructor(sources: ReadableSource[]) {
31
+ this.sources = [...sources].sort((a, b) => b.priority - a.priority);
32
+ }
33
+
34
+ initialize(options?: { cwd?: string }): void {
35
+ for (const source of this.sources) {
36
+ source.initialize?.(options);
37
+ }
38
+ }
39
+
40
+ resolve(product: ProductDefinition, key: ConfigKey): ResolvedValue {
41
+ for (const source of this.sources) {
42
+ const value = source.read(product, key);
43
+ if (value !== undefined) {
44
+ return { value, from: { sourceId: source.id, detail: source.describe() } };
45
+ }
46
+ }
47
+ return { value: undefined };
48
+ }
49
+
50
+ locate(product: ProductDefinition, key: ConfigKey): SourceLocation[] {
51
+ const locations: SourceLocation[] = [];
52
+ for (const source of this.sources) {
53
+ if (source.read(product, key) !== undefined) {
54
+ locations.push({ sourceId: source.id, detail: source.describe() });
55
+ }
56
+ }
57
+ return locations;
58
+ }
59
+
60
+ getWritableSource<T extends WritableSource>(
61
+ predicate: (s: WritableSource) => s is T,
62
+ ): T | undefined {
63
+ for (const source of this.sources) {
64
+ if (isWritableSource(source) && predicate(source)) {
65
+ return source;
66
+ }
67
+ }
68
+ return undefined;
69
+ }
70
+ }
71
+
72
+ export function buildDefaultRegistry(): DefaultConfigRegistry {
73
+ return new DefaultConfigRegistry([
74
+ new ProcessEnvSource(),
75
+ new EnvFileSource(),
76
+ new HomeFileSource(),
77
+ new MacosKeychainSource(),
78
+ ]);
79
+ }
@@ -0,0 +1,60 @@
1
+ import { buildDefaultRegistry, type ConfigRegistry } from './registry.js';
2
+ import type { ConfigKey, ProductDefinition } from './source.js';
3
+
4
+ const FALLBACK_PAGE_SIZE = 25;
5
+
6
+ export type ProductRuntimeConfig = {
7
+ host?: string;
8
+ apiBasePath?: string;
9
+ token?: string;
10
+ defaultPageSize: number;
11
+ };
12
+
13
+ export { ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR } from './sources/env-file.js';
14
+
15
+ let registry: ConfigRegistry = buildDefaultRegistry();
16
+
17
+ export function initializeRuntimeConfig(options?: { cwd?: string }): void {
18
+ registry.initialize(options);
19
+ }
20
+
21
+ function resolveString(product: ProductDefinition, key: ConfigKey): string | undefined {
22
+ return registry.resolve(product, key).value;
23
+ }
24
+
25
+ function parsePositiveInteger(value: string | undefined): number | undefined {
26
+ if (!value) {
27
+ return undefined;
28
+ }
29
+ const trimmed = value.trim();
30
+ if (!/^\d+$/.test(trimmed)) {
31
+ return undefined;
32
+ }
33
+ const parsed = Number.parseInt(trimmed, 10);
34
+ return parsed > 0 ? parsed : undefined;
35
+ }
36
+
37
+ export function getProductRuntimeConfig(product: ProductDefinition): ProductRuntimeConfig {
38
+ return {
39
+ host: resolveString(product, 'host'),
40
+ apiBasePath: resolveString(product, 'apiBasePath'),
41
+ token: resolveString(product, 'token'),
42
+ defaultPageSize:
43
+ parsePositiveInteger(resolveString(product, 'defaultPageSize')) ?? FALLBACK_PAGE_SIZE,
44
+ };
45
+ }
46
+
47
+ export function validateProductRuntimeConfig(product: ProductDefinition): string[] {
48
+ const config = getProductRuntimeConfig(product);
49
+ const missing: string[] = [];
50
+
51
+ if (!config.token) {
52
+ missing.push(product.envVars.token);
53
+ }
54
+
55
+ if (!config.host && !config.apiBasePath) {
56
+ missing.push(`${product.envVars.host} or ${product.envVars.apiBasePath}`);
57
+ }
58
+
59
+ return missing;
60
+ }
@@ -0,0 +1,37 @@
1
+ export type ConfigKey = 'host' | 'apiBasePath' | 'token' | 'defaultPageSize';
2
+
3
+ export type SourceId = 'process-env' | 'env-file' | 'home-file' | 'macos-keychain';
4
+
5
+ export type SourceLocation = { sourceId: SourceId; detail?: string };
6
+
7
+ export interface ProductDefinition {
8
+ readonly id: string;
9
+ readonly envVars: Record<ConfigKey, string>;
10
+ readonly defaultApiBasePath?: string;
11
+ }
12
+
13
+ export interface ReadableSource {
14
+ readonly id: SourceId;
15
+ readonly priority: number;
16
+ read(product: ProductDefinition, key: ConfigKey): string | undefined;
17
+ initialize?(options?: { cwd?: string }): void;
18
+ describe(): string;
19
+ }
20
+
21
+ export interface WritableSource extends ReadableSource {
22
+ isAvailable(): boolean;
23
+ write(product: ProductDefinition, key: ConfigKey, value: string): void;
24
+ clear(product: ProductDefinition, key: ConfigKey): void;
25
+ }
26
+
27
+ export function isWritableSource(source: ReadableSource): source is WritableSource {
28
+ return typeof (source as WritableSource).write === 'function';
29
+ }
30
+
31
+ export function getNonEmptyValue(value: string | undefined): string | undefined {
32
+ if (!value) {
33
+ return undefined;
34
+ }
35
+ const trimmed = value.trim();
36
+ return trimmed.length > 0 ? trimmed : undefined;
37
+ }
@@ -0,0 +1,54 @@
1
+ import fs from 'node:fs';
2
+ import dotenv from 'dotenv';
3
+
4
+ export type ParsedEnvironment = Record<string, string>;
5
+
6
+ type CacheEntry = {
7
+ filePath: string;
8
+ mtimeMs: number;
9
+ values: ParsedEnvironment;
10
+ };
11
+
12
+ export class DotenvFileNotFoundError extends Error {
13
+ readonly filePath: string;
14
+ constructor(filePath: string) {
15
+ super(`Dotenv file not found: ${filePath}`);
16
+ this.name = 'DotenvFileNotFoundError';
17
+ this.filePath = filePath;
18
+ }
19
+ }
20
+
21
+ export class DotenvFileCache {
22
+ private entry?: CacheEntry;
23
+
24
+ get filePath(): string | undefined {
25
+ return this.entry?.filePath;
26
+ }
27
+
28
+ load(filePath: string): ParsedEnvironment {
29
+ let stats: fs.Stats;
30
+ try {
31
+ stats = fs.statSync(filePath);
32
+ } catch (error) {
33
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
34
+ if (this.entry?.filePath === filePath) {
35
+ this.entry = undefined;
36
+ }
37
+ throw new DotenvFileNotFoundError(filePath);
38
+ }
39
+ throw error;
40
+ }
41
+
42
+ if (this.entry && this.entry.filePath === filePath && this.entry.mtimeMs === stats.mtimeMs) {
43
+ return this.entry.values;
44
+ }
45
+
46
+ const values = dotenv.parse(fs.readFileSync(filePath));
47
+ this.entry = { filePath, mtimeMs: stats.mtimeMs, values };
48
+ return values;
49
+ }
50
+
51
+ invalidate(): void {
52
+ this.entry = undefined;
53
+ }
54
+ }
@@ -0,0 +1,61 @@
1
+ import path from 'node:path';
2
+ import type { ConfigKey, ProductDefinition, ReadableSource, SourceId } from '../source.js';
3
+ import { getNonEmptyValue } from '../source.js';
4
+ import {
5
+ DotenvFileCache,
6
+ DotenvFileNotFoundError,
7
+ type ParsedEnvironment,
8
+ } from './dotenv-file-cache.js';
9
+
10
+ export const ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR = 'ATLASSIAN_DC_MCP_CONFIG_FILE';
11
+
12
+ export class EnvFileSource implements ReadableSource {
13
+ readonly id: SourceId = 'env-file';
14
+ readonly priority = 80;
15
+
16
+ private cwd: string = process.cwd();
17
+ private cache = new DotenvFileCache();
18
+
19
+ initialize(options?: { cwd?: string }): void {
20
+ this.cwd = options?.cwd ?? process.cwd();
21
+ this.loadFile();
22
+ }
23
+
24
+ read(product: ProductDefinition, key: ConfigKey): string | undefined {
25
+ const values = this.loadFile();
26
+ return getNonEmptyValue(values[product.envVars[key]]);
27
+ }
28
+
29
+ describe(): string {
30
+ return this.cache.filePath ? `env-file (${this.cache.filePath})` : 'env-file';
31
+ }
32
+
33
+ private getExplicitFilePath(): string | undefined {
34
+ const filePath = getNonEmptyValue(process.env[ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR]);
35
+ if (!filePath) {
36
+ return undefined;
37
+ }
38
+ if (!path.isAbsolute(filePath)) {
39
+ throw new Error(`${ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR} must be an absolute path: ${filePath}`);
40
+ }
41
+ return filePath;
42
+ }
43
+
44
+ private loadFile(): ParsedEnvironment {
45
+ const explicitFilePath = this.getExplicitFilePath();
46
+ const filePath = explicitFilePath ?? path.join(this.cwd, '.env');
47
+ try {
48
+ return this.cache.load(filePath);
49
+ } catch (error) {
50
+ if (error instanceof DotenvFileNotFoundError) {
51
+ if (explicitFilePath) {
52
+ throw new Error(
53
+ `${ATLASSIAN_DC_MCP_CONFIG_FILE_ENV_VAR} points to a missing file: ${error.filePath}`,
54
+ );
55
+ }
56
+ return {};
57
+ }
58
+ throw error;
59
+ }
60
+ }
61
+ }