Skip to content

Service HTTP and config logic is duplicated across ~14 tools and has drifted #103

Closed

Description

@TMHSDigital

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 / 22 mapping is repeated in 12 files
  • HOMELAB_COMPOSE_DIR and its default are copied into 4 compose tools; HOMELAB_BACKUP_REPO and 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 produces http://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:

  1. Add utils/config.ts with getters: composeDir(), backupRepo(), and getServicePort(envVar, default) that validates the port.
  2. Add utils/service-http.ts with a shared curl helper and curlErrorResponse(err, serviceName, portEnv).
  3. Add utils/npm-auth.ts with getNpmToken(node), used by npmCerts, npmProxyHosts, and certList.
  4. 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

Activity

  1. added
    help wanted
    tech-debt
    severity: medium
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedseverity: mediumtech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions