Skip to content

test: mcp discover — env-var interpolation in external configs#695

Open
anandgupta42 wants to merge 1 commit intomainfrom
test/hourly-20260411-0133
Open

test: mcp discover — env-var interpolation in external configs#695
anandgupta42 wants to merge 1 commit intomainfrom
test/hourly-20260411-0133

Conversation

@anandgupta42
Copy link
Copy Markdown
Contributor

@anandgupta42 anandgupta42 commented Apr 11, 2026

What does this PR do?

1. discoverExternalMcp env-var interpolation — src/mcp/discover.ts (2 new tests)

The readJsonSafe function gained env-var interpolation support in commit f030bf8 (#656). When external MCP configs (.vscode/mcp.json, .mcp.json, .gemini/settings.json, etc.) contain ${VAR} or ${VAR:-default} patterns, these are now resolved via ConfigPaths.parseText before JSON parsing. Zero tests existed for this interpolation path — all 18 existing discover tests used hardcoded string values.

This matters because users migrating configs from Claude Code, VS Code, or docker-compose commonly use ${API_KEY} patterns in their MCP server configs. A regression in the interpolation path would silently produce broken MCP servers with literal ${VAR} strings instead of resolved values.

New coverage includes:

  • ${VAR} basic resolution: Sets a process env var, writes a .vscode/mcp.json referencing it, verifies the resolved value appears in the discovered server's command array
  • ${VAR:-default} fallback: Uses an unset env var with a :-fallback default, verifies the fallback value is used when the variable is not in the environment

Reconnaissance findings

During reconnaissance, I also examined and ruled out several other candidates:

  • File.search() (zero test coverage) — rejected by critic due to race condition: the internal file scanner populates cache asynchronously via ripgrep, and files() returns cache without waiting for scan completion, making integration tests inherently flaky
  • resolveEnvVars in mcp/index.ts — already well covered in test/mcp/env-var-interpolation.test.ts
  • altimate-backend defaultModel in provider.ts — already covered in test/provider/provider.test.ts
  • Trace.listTracesPaginated — already covered in test/altimate/tracing.test.ts

Type of change

  • New feature (non-breaking change which adds functionality)

Issue for this PR

N/A — proactive test coverage for recently added env-var interpolation in MCP discovery

How did you verify your code works?

bun test test/mcp/discover.test.ts       # 20 pass (18 existing + 2 new)

Marker guard check: bun run script/upstream/analyze.ts --markers --base main --strict → all clear.

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

https://claude.ai/code/session_01K8qyA8V76y6zrCYuEVVk3x


Summary by cubic

Add two tests to discoverExternalMcp that cover env-var interpolation in external MCP configs. Ensures ${VAR} and ${VAR:-default} resolve into the command array via the readJsonSafe interpolation path.

Written for commit 129e635. Summary will update on new commits.

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for environment-variable interpolation in external MCP configuration.
    • Tests validate correct variable placeholder resolution when environment variables are present.
    • Tests verify fallback mechanisms apply appropriate default values when variables are absent.
    • Tests confirm discovered servers maintain proper type classification with interpolated configurations.

Cover the readJsonSafe env-var resolution path added in f030bf8.
Users with ${VAR} or ${VAR:-default} in .vscode/mcp.json now get
their MCP servers resolved correctly; these tests guard against
regressions in that interpolation path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

📝 Walkthrough

Walkthrough

Two new test cases were added to discoverExternalMcp to validate environment-variable interpolation in external MCP configuration files using ${VAR} and ${VAR:-default} syntax. Both tests verify the discovered server is marked as type "local" with properly resolved command values.

Changes

Cohort / File(s) Summary
Test Suite Enhancements
packages/opencode/test/mcp/discover.test.ts
Added two test cases validating environment-variable interpolation: one with a runtime variable set and another with fallback default value handling.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Suggested labels

contributor

Poem

🐰 With ${VAR} and defaults so fine,
The tests hop and validate each line,
Environment whispers through config divine,
Interpolation works, and all will align!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is missing the required 'PINEAPPLE' marker at the top, which per the template is mandatory for AI-generated contributions. Add 'PINEAPPLE' as the first word in the PR description per the repository's template requirement for AI contributions.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding tests for environment-variable interpolation in MCP discovery external configs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/hourly-20260411-0133

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/opencode/test/mcp/discover.test.ts (1)

389-404: Use the shared tmpdir() fixture pattern in these new tests.

The new tests are good functionally, but they still depend on the file’s manual temp-dir lifecycle. Please migrate touched tests to await using ... = tmpdir() for consistent cleanup semantics with the project test standard.

As per coding guidelines, "Use the tmpdir function from fixture/fixture.ts to create temporary directories for tests with automatic cleanup in test files" and "Always use await using syntax with tmpdir() for automatic cleanup when the variable goes out of scope".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/opencode/test/mcp/discover.test.ts` around lines 389 - 404, The test
"resolves ${VAR:-default} with fallback when env var is unset" currently manages
a manual tempDir lifecycle; replace it with the shared tmpdir fixture by using
"await using temp = tmpdir()" (or whatever fixture alias is exported) to obtain
the temp directory and use temp.path (or the fixture's API) instead of the
manual tempDir variable, remove any manual cleanup, and update calls that
reference tempDir (mkdir, writeFile, and the call to discoverExternalMcp) to use
the fixture-provided path; ensure the test still deletes the env var key and
calls discoverExternalMcp(tempPath) so the assertions on result["default-test"]
remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/opencode/test/mcp/discover.test.ts`:
- Around line 389-404: The test "resolves ${VAR:-default} with fallback when env
var is unset" currently manages a manual tempDir lifecycle; replace it with the
shared tmpdir fixture by using "await using temp = tmpdir()" (or whatever
fixture alias is exported) to obtain the temp directory and use temp.path (or
the fixture's API) instead of the manual tempDir variable, remove any manual
cleanup, and update calls that reference tempDir (mkdir, writeFile, and the call
to discoverExternalMcp) to use the fixture-provided path; ensure the test still
deletes the env var key and calls discoverExternalMcp(tempPath) so the
assertions on result["default-test"] remain unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2388238e-97c3-4d6a-8e91-c3e76ddb13ec

📥 Commits

Reviewing files that changed from the base of the PR and between f030bf8 and 129e635.

📒 Files selected for processing (1)
  • packages/opencode/test/mcp/discover.test.ts

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants