Repository navigation
Closed
Labels
help wantedExtra attention is neededseverity: mediumMedium severitytech-debtRefactoring and maintainability
Description
Summary
The logic for calling service HTTP APIs (port lookup, credentials, curl exit-code mapping, NPM token login) is copied across about 14 tool files. Config defaults are copied as well, and the copies have already drifted apart.
Where
- NPM token login written out three times:
npmCerts.ts:6-31,npmProxyHosts.ts:9-31,certList.ts:31-54 - A separate
getPort()in:adguardStats.ts,adguardFilters.ts,adguardQueryLog.ts,grafanaSnapshot.ts:16,prometheusQuery.ts:20,alertList.ts:19,speedtestResults.ts:22,uptimeKumaStatus.ts:10,ntfySend.ts:35,ntfyTopics.ts:24 - The
exitCode === 7/22mapping is repeated in 12 files HOMELAB_COMPOSE_DIRand its default are copied into 4 compose tools;HOMELAB_BACKUP_REPOand its default into 5 backup tools
Details
- The certList copy of the NPM login drops the exit-code mapping and the JSON error handling, and collapses every failure into one generic message.
parseInt(process.env.X_PORT)is never checked for NaN, so a bad value produceshttp://localhost:NaN/....- The compose tools read their env var once, at module load. The backup tools read theirs on every call. So the two families behave differently when env changes at runtime, for example in tests.
Impact
Any change to auth handling, error messages, or defaults has to be made in 4 to 14 places by hand, and a missed copy fails silently.
Suggested fix
Do this in incremental PRs, each leaving the code working:
- Add
utils/config.tswith getters:composeDir(),backupRepo(), andgetServicePort(envVar, default)that validates the port. - Add
utils/service-http.tswith a shared curl helper andcurlErrorResponse(err, serviceName, portEnv). - Add
utils/npm-auth.tswithgetNpmToken(node), used by npmCerts, npmProxyHosts, and certList. - Migrate one service family per PR: AdGuard, then NPM, then the rest.
Before starting, add mocked-execSSH tests that assert the generated command and the error mapping for exit codes 7 and 22, one tool per family.
Acceptance criteria
- No per-tool
getPort()or NPM login copies remain - Bad port values produce a clear error
- Existing behavior is covered by tests before the refactor
Getting started
- Relevant files: the files listed above,
mcp-server/src/utils/ - How to run tests:
cd mcp-server && npm test
Filed by repo-audit/full-audit at 9cfe5e3. Severity: medium. Effort: M. audit-key: tech-debt|mcp-server/src/tools/npmCerts.ts|getNpmToken
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededseverity: mediumMedium severitytech-debtRefactoring and maintainability
Activity