Skip to content

feat: add {file:path} config substitution for reading values from files - #259

Open
RalphSu2 wants to merge 1 commit into
philschmid:mainfrom
RalphSu2:feature/file-reference-substitution
Open

feat: add {file:path} config substitution for reading values from files#259
RalphSu2 wants to merge 1 commit into
philschmid:mainfrom
RalphSu2:feature/file-reference-substitution

Conversation

@RalphSu2

@RalphSu2 RalphSu2 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Extends the config substitution system to support {file:path} syntax in addition to the existing ${VAR_NAME} env var substitution. This allows users to reference file contents directly in config values, e.g. for auth tokens:

{
  "mcpServers": {
    "my-server": {
      "url": "https://example.com/mcp",
      "headers": {
        "Authorization": "Bearer {file:~/.ssh/my_token}"
      }
    }
  }
}

Changes

  • src/config.ts: Added a second regex pass (/\{file:([^}]+)\}/g) in substituteEnvVars() that reads file contents, trims whitespace, and expands ~ to home directory. Same strict/non-strict error handling as env vars — new MISSING_FILE error type in strict mode, warning + empty string in non-strict mode.
  • tests/config.test.ts: 6 new tests covering file substitution, content trimming, tilde expansion, coexistence with ${VAR}, and error handling in both strict and non-strict modes.
  • README.md: Updated substitution docs with a syntax table and {file:...} example.

Design decisions

  • Synchronous readFileSync — config loads once at startup; consistent with existing existsSync usage. Avoids cascading async refactor.
  • Trim file contents to avoid trailing newline issues common with token files.
  • ~ expansion via homedir() (already imported).
  • No collision between ${...} and {file:...} regexes.
  • Order: env vars first, then file refs — allows env var substitution inside file paths.

Test results

bun test tests/config.test.ts
21 pass, 0 fail

Extend the config substitution system to support {file:path} syntax in
addition to the existing  env var substitution. This allows
users to reference file contents directly in config, e.g. for auth tokens:

  "Authorization": "Bearer {file:~/.ssh/my_token}"

- {file:...} reads file contents and trims whitespace
- ~ prefix expands to home directory
- Same strict/non-strict error handling as env vars (MISSING_FILE)
- 6 new tests covering substitution, trimming, tilde expansion, coexistence
  with env vars, and error handling in both modes
@RalphSu2

RalphSu2 commented Aug 7, 2026

Copy link
Copy Markdown
Author

fixs #260

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant