@atlassian-dc-mcp/common 0.16.0 → 0.17.1
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.
- package/CHANGELOG.md +23 -0
- package/build/__tests__/runtime-config.test.js +65 -14
- package/build/__tests__/runtime-config.test.js.map +1 -1
- package/build/__tests__/setup-cli.test.d.ts +2 -0
- package/build/__tests__/setup-cli.test.d.ts.map +1 -0
- package/build/__tests__/setup-cli.test.js +166 -0
- package/build/__tests__/setup-cli.test.js.map +1 -0
- package/build/config/__tests__/registry.test.d.ts +2 -0
- package/build/config/__tests__/registry.test.d.ts.map +1 -0
- package/build/config/__tests__/registry.test.js +105 -0
- package/build/config/__tests__/registry.test.js.map +1 -0
- package/build/config/__tests__/resolve-base.test.d.ts +2 -0
- package/build/config/__tests__/resolve-base.test.d.ts.map +1 -0
- package/build/config/__tests__/resolve-base.test.js +89 -0
- package/build/config/__tests__/resolve-base.test.js.map +1 -0
- package/build/config/__tests__/sources/env-file.test.d.ts +2 -0
- package/build/config/__tests__/sources/env-file.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/env-file.test.js +93 -0
- package/build/config/__tests__/sources/env-file.test.js.map +1 -0
- package/build/config/__tests__/sources/home-file.test.d.ts +2 -0
- package/build/config/__tests__/sources/home-file.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/home-file.test.js +114 -0
- package/build/config/__tests__/sources/home-file.test.js.map +1 -0
- package/build/config/__tests__/sources/macos-keychain.test.d.ts +2 -0
- package/build/config/__tests__/sources/macos-keychain.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/macos-keychain.test.js +109 -0
- package/build/config/__tests__/sources/macos-keychain.test.js.map +1 -0
- package/build/config/__tests__/sources/process-env.test.d.ts +2 -0
- package/build/config/__tests__/sources/process-env.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/process-env.test.js +51 -0
- package/build/config/__tests__/sources/process-env.test.js.map +1 -0
- package/build/config/index.d.ts +9 -0
- package/build/config/index.d.ts.map +1 -0
- package/build/config/index.js +9 -0
- package/build/config/index.js.map +1 -0
- package/build/config/registry.d.ts +26 -0
- package/build/config/registry.d.ts.map +1 -0
- package/build/config/registry.js +51 -0
- package/build/config/registry.js.map +1 -0
- package/build/config/resolve-base.d.ts +8 -0
- package/build/config/resolve-base.d.ts.map +1 -0
- package/build/config/resolve-base.js +31 -0
- package/build/config/resolve-base.js.map +1 -0
- package/build/config/runtime-config.d.ts +14 -0
- package/build/config/runtime-config.d.ts.map +1 -0
- package/build/config/runtime-config.js +41 -0
- package/build/config/runtime-config.js.map +1 -0
- package/build/config/source.d.ts +28 -0
- package/build/config/source.d.ts.map +1 -0
- package/build/config/source.js +11 -0
- package/build/config/source.js.map +1 -0
- package/build/config/sources/dotenv-file-cache.d.ts +12 -0
- package/build/config/sources/dotenv-file-cache.d.ts.map +1 -0
- package/build/config/sources/dotenv-file-cache.js +41 -0
- package/build/config/sources/dotenv-file-cache.js.map +1 -0
- package/build/config/sources/env-file.d.ts +16 -0
- package/build/config/sources/env-file.d.ts.map +1 -0
- package/build/config/sources/env-file.js +48 -0
- package/build/config/sources/env-file.js.map +1 -0
- package/build/config/sources/home-file.d.ts +19 -0
- package/build/config/sources/home-file.d.ts.map +1 -0
- package/build/config/sources/home-file.js +102 -0
- package/build/config/sources/home-file.js.map +1 -0
- package/build/config/sources/macos-keychain.d.ts +26 -0
- package/build/config/sources/macos-keychain.d.ts.map +1 -0
- package/build/config/sources/macos-keychain.js +88 -0
- package/build/config/sources/macos-keychain.js.map +1 -0
- package/build/config/sources/process-env.d.ts +8 -0
- package/build/config/sources/process-env.d.ts.map +1 -0
- package/build/config/sources/process-env.js +12 -0
- package/build/config/sources/process-env.js.map +1 -0
- package/build/index.d.ts +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build/setup-cli.d.ts +25 -0
- package/build/setup-cli.d.ts.map +1 -0
- package/build/setup-cli.js +215 -0
- package/build/setup-cli.js.map +1 -0
- package/package.json +5 -4
- package/src/__tests__/runtime-config.test.ts +75 -27
- package/src/__tests__/setup-cli.test.ts +175 -0
- package/src/config/__tests__/registry.test.ts +118 -0
- package/src/config/__tests__/resolve-base.test.ts +101 -0
- package/src/config/__tests__/sources/env-file.test.ts +113 -0
- package/src/config/__tests__/sources/home-file.test.ts +129 -0
- package/src/config/__tests__/sources/macos-keychain.test.ts +136 -0
- package/src/config/__tests__/sources/process-env.test.ts +59 -0
- package/src/config/index.ts +8 -0
- package/src/config/registry.ts +79 -0
- package/src/config/resolve-base.ts +43 -0
- package/src/config/runtime-config.ts +60 -0
- package/src/config/source.ts +37 -0
- package/src/config/sources/dotenv-file-cache.ts +54 -0
- package/src/config/sources/env-file.ts +61 -0
- package/src/config/sources/home-file.ts +121 -0
- package/src/config/sources/macos-keychain.ts +117 -0
- package/src/config/sources/process-env.ts +15 -0
- package/src/index.ts +2 -1
- package/src/setup-cli.ts +308 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/runtime-config.d.ts +0 -14
- package/build/runtime-config.d.ts.map +0 -1
- package/build/runtime-config.js +0 -123
- package/build/runtime-config.js.map +0 -1
- package/src/runtime-config.ts +0 -176
|
@@ -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,43 @@
|
|
|
1
|
+
export type ResolveOpenApiBaseOptions = {
|
|
2
|
+
host?: string;
|
|
3
|
+
apiBasePath?: string;
|
|
4
|
+
defaultBasePath: string;
|
|
5
|
+
strippableSuffixes?: readonly string[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function normalizeHost(host: string): string {
|
|
9
|
+
const withScheme = /^https?:\/\//i.test(host) ? host : `https://${host}`;
|
|
10
|
+
return withScheme.replace(/\/+$/, '');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function stripGeneratedSuffix(path: string, suffixes: readonly string[]): string {
|
|
14
|
+
for (const suffix of suffixes) {
|
|
15
|
+
const escaped = suffix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
16
|
+
const re = new RegExp(`${escaped}\\/?$`, 'i');
|
|
17
|
+
if (re.test(path)) {
|
|
18
|
+
return path.replace(re, '');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return path;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function normalizeBasePath(path: string, suffixes: readonly string[]): string {
|
|
25
|
+
const stripped = stripGeneratedSuffix(path, suffixes).replace(/\/+$/, '');
|
|
26
|
+
return stripped === '/' ? '' : stripped;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function resolveOpenApiBase(options: ResolveOpenApiBaseOptions): string {
|
|
30
|
+
const { host, apiBasePath, defaultBasePath, strippableSuffixes = [] } = options;
|
|
31
|
+
|
|
32
|
+
if (apiBasePath && /^https?:\/\//i.test(apiBasePath)) {
|
|
33
|
+
const url = new URL(apiBasePath);
|
|
34
|
+
return `${url.origin}${normalizeBasePath(url.pathname, strippableSuffixes)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!host) {
|
|
38
|
+
throw new Error('host or apiBasePath must be provided');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const basePath = apiBasePath ? normalizeBasePath(apiBasePath, strippableSuffixes) : defaultBasePath;
|
|
42
|
+
return `${normalizeHost(host)}${basePath}`;
|
|
43
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import type { ConfigKey, ProductDefinition, SourceId, WritableSource } from '../source.js';
|
|
6
|
+
import { getNonEmptyValue } from '../source.js';
|
|
7
|
+
import {
|
|
8
|
+
DotenvFileCache,
|
|
9
|
+
DotenvFileNotFoundError,
|
|
10
|
+
type ParsedEnvironment,
|
|
11
|
+
} from './dotenv-file-cache.js';
|
|
12
|
+
|
|
13
|
+
const HOME_DIR_NAME = '.atlassian-dc-mcp';
|
|
14
|
+
|
|
15
|
+
function getHomeFilePath(product: ProductDefinition | string): string {
|
|
16
|
+
const id = typeof product === 'string' ? product : product.id;
|
|
17
|
+
return path.join(os.homedir(), HOME_DIR_NAME, `${id}.env`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function needsQuoting(value: string): boolean {
|
|
21
|
+
return /[\s#'"]/.test(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function serializeValue(value: string): string {
|
|
25
|
+
if (!needsQuoting(value)) {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function serialize(values: ParsedEnvironment): string {
|
|
32
|
+
const lines: string[] = [];
|
|
33
|
+
for (const [key, raw] of Object.entries(values)) {
|
|
34
|
+
lines.push(`${key}=${serializeValue(raw)}`);
|
|
35
|
+
}
|
|
36
|
+
return lines.length > 0 ? `${lines.join('\n')}\n` : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class HomeFileSource implements WritableSource {
|
|
40
|
+
readonly id: SourceId = 'home-file';
|
|
41
|
+
readonly priority = 60;
|
|
42
|
+
|
|
43
|
+
private cache = new DotenvFileCache();
|
|
44
|
+
|
|
45
|
+
isAvailable(): boolean {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
read(product: ProductDefinition, key: ConfigKey): string | undefined {
|
|
50
|
+
const values = this.loadFile(product);
|
|
51
|
+
return getNonEmptyValue(values[product.envVars[key]]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe(): string {
|
|
55
|
+
return `home-file (~/${HOME_DIR_NAME}/<product>.env)`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describeForProduct(product: ProductDefinition): string {
|
|
59
|
+
return `home-file (${getHomeFilePath(product)})`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
write(product: ProductDefinition, key: ConfigKey, value: string): void {
|
|
63
|
+
const envVar = product.envVars[key];
|
|
64
|
+
const existing = this.readFileIgnoringErrors(product);
|
|
65
|
+
existing[envVar] = value;
|
|
66
|
+
this.persist(product, existing);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
clear(product: ProductDefinition, key: ConfigKey): void {
|
|
70
|
+
const envVar = product.envVars[key];
|
|
71
|
+
const existing = this.readFileIgnoringErrors(product);
|
|
72
|
+
if (!(envVar in existing)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
delete existing[envVar];
|
|
76
|
+
this.persist(product, existing);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private loadFile(product: ProductDefinition): ParsedEnvironment {
|
|
80
|
+
const filePath = getHomeFilePath(product);
|
|
81
|
+
try {
|
|
82
|
+
return this.cache.load(filePath);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof DotenvFileNotFoundError) {
|
|
85
|
+
return {};
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private readFileIgnoringErrors(product: ProductDefinition): ParsedEnvironment {
|
|
92
|
+
try {
|
|
93
|
+
return { ...this.loadFile(product) };
|
|
94
|
+
} catch {
|
|
95
|
+
return {};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private persist(product: ProductDefinition, values: ParsedEnvironment): void {
|
|
100
|
+
const filePath = getHomeFilePath(product);
|
|
101
|
+
const dir = path.dirname(filePath);
|
|
102
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
103
|
+
|
|
104
|
+
const tmpPath = `${filePath}.tmp-${process.pid}-${crypto.randomBytes(4).toString('hex')}`;
|
|
105
|
+
try {
|
|
106
|
+
fs.writeFileSync(tmpPath, serialize(values), { mode: 0o600 });
|
|
107
|
+
fs.renameSync(tmpPath, filePath);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
try {
|
|
110
|
+
fs.unlinkSync(tmpPath);
|
|
111
|
+
} catch {
|
|
112
|
+
// best-effort cleanup
|
|
113
|
+
}
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.cache.invalidate();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { getHomeFilePath, HOME_DIR_NAME };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { execFileSync as nodeExecFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync as nodeExistsSync } from 'node:fs';
|
|
3
|
+
import type { ConfigKey, ProductDefinition, SourceId, WritableSource } from '../source.js';
|
|
4
|
+
|
|
5
|
+
// We pass the token via `-w <value>` on write. `security add-generic-password`
|
|
6
|
+
// has no stdin-password mode, so the token is visible in argv to same-user
|
|
7
|
+
// processes. A same-user attacker already has keychain access via `security`
|
|
8
|
+
// itself; this does not widen the attack surface.
|
|
9
|
+
|
|
10
|
+
export const SECURITY_BINARY = '/usr/bin/security';
|
|
11
|
+
export const KEYCHAIN_SERVICE = 'atlassian-dc-mcp';
|
|
12
|
+
const KEYCHAIN_TIMEOUT_MS = 5000;
|
|
13
|
+
export const NOT_FOUND_STATUS = 44;
|
|
14
|
+
|
|
15
|
+
export type KeychainDeps = {
|
|
16
|
+
execFileSync: typeof nodeExecFileSync;
|
|
17
|
+
existsSync: typeof nodeExistsSync;
|
|
18
|
+
getPlatform: () => NodeJS.Platform;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function accountFor(product: ProductDefinition): string {
|
|
22
|
+
return `${product.id}-token`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DEFAULT_DEPS: KeychainDeps = {
|
|
26
|
+
execFileSync: nodeExecFileSync,
|
|
27
|
+
existsSync: nodeExistsSync,
|
|
28
|
+
getPlatform: () => process.platform,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export class MacosKeychainSource implements WritableSource {
|
|
32
|
+
readonly id: SourceId = 'macos-keychain';
|
|
33
|
+
readonly priority = 40;
|
|
34
|
+
|
|
35
|
+
private cache = new Map<string, string | undefined>();
|
|
36
|
+
private cacheWarmed = new Set<string>();
|
|
37
|
+
private readonly deps: KeychainDeps;
|
|
38
|
+
|
|
39
|
+
constructor(deps: Partial<KeychainDeps> = {}) {
|
|
40
|
+
this.deps = { ...DEFAULT_DEPS, ...deps };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
isAvailable(): boolean {
|
|
44
|
+
return this.deps.getPlatform() === 'darwin' && this.deps.existsSync(SECURITY_BINARY);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
read(product: ProductDefinition, key: ConfigKey): string | undefined {
|
|
48
|
+
if (key !== 'token' || !this.isAvailable()) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
if (this.cacheWarmed.has(product.id)) {
|
|
52
|
+
return this.cache.get(product.id);
|
|
53
|
+
}
|
|
54
|
+
const value = this.findPassword(product);
|
|
55
|
+
this.cache.set(product.id, value);
|
|
56
|
+
this.cacheWarmed.add(product.id);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe(): string {
|
|
61
|
+
return 'macOS Keychain';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
write(product: ProductDefinition, key: ConfigKey, value: string): void {
|
|
65
|
+
if (key !== 'token') {
|
|
66
|
+
throw new Error('macOS Keychain only stores the token key');
|
|
67
|
+
}
|
|
68
|
+
if (!this.isAvailable()) {
|
|
69
|
+
throw new Error('macOS Keychain is not available on this platform');
|
|
70
|
+
}
|
|
71
|
+
this.deps.execFileSync(
|
|
72
|
+
SECURITY_BINARY,
|
|
73
|
+
[
|
|
74
|
+
'add-generic-password',
|
|
75
|
+
'-U',
|
|
76
|
+
'-s', KEYCHAIN_SERVICE,
|
|
77
|
+
'-a', accountFor(product),
|
|
78
|
+
'-w', value,
|
|
79
|
+
],
|
|
80
|
+
{ stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', timeout: KEYCHAIN_TIMEOUT_MS },
|
|
81
|
+
);
|
|
82
|
+
this.cache.set(product.id, value);
|
|
83
|
+
this.cacheWarmed.add(product.id);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
clear(product: ProductDefinition, key: ConfigKey): void {
|
|
87
|
+
if (key !== 'token' || !this.isAvailable()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
this.deps.execFileSync(
|
|
92
|
+
SECURITY_BINARY,
|
|
93
|
+
['delete-generic-password', '-s', KEYCHAIN_SERVICE, '-a', accountFor(product)],
|
|
94
|
+
{ stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', timeout: KEYCHAIN_TIMEOUT_MS },
|
|
95
|
+
);
|
|
96
|
+
} catch {
|
|
97
|
+
// absent / already cleared — non-fatal
|
|
98
|
+
}
|
|
99
|
+
this.cache.set(product.id, undefined);
|
|
100
|
+
this.cacheWarmed.add(product.id);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private findPassword(product: ProductDefinition): string | undefined {
|
|
104
|
+
try {
|
|
105
|
+
const stdout = this.deps.execFileSync(
|
|
106
|
+
SECURITY_BINARY,
|
|
107
|
+
['find-generic-password', '-s', KEYCHAIN_SERVICE, '-a', accountFor(product), '-w'],
|
|
108
|
+
{ stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', timeout: KEYCHAIN_TIMEOUT_MS },
|
|
109
|
+
);
|
|
110
|
+
return String(stdout).replace(/\n$/, '');
|
|
111
|
+
} catch {
|
|
112
|
+
// exit 44 = not found; any other failure (locked keychain, etc.) is
|
|
113
|
+
// treated as absent so the resolver continues down the chain.
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ConfigKey, ProductDefinition, ReadableSource, SourceId } from '../source.js';
|
|
2
|
+
import { getNonEmptyValue } from '../source.js';
|
|
3
|
+
|
|
4
|
+
export class ProcessEnvSource implements ReadableSource {
|
|
5
|
+
readonly id: SourceId = 'process-env';
|
|
6
|
+
readonly priority = 100;
|
|
7
|
+
|
|
8
|
+
read(product: ProductDefinition, key: ConfigKey): string | undefined {
|
|
9
|
+
return getNonEmptyValue(process.env[product.envVars[key]]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe(): string {
|
|
13
|
+
return 'process env';
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
2
2
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
3
3
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
4
|
export * from './api-error-handler.js'
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './config/index.js';
|
|
6
|
+
export { runSetup } from './setup-cli.js';
|
|
6
7
|
|
|
7
8
|
// Helper function to format tool responses
|
|
8
9
|
export const formatToolResponse = (result: unknown) => ({
|