feat(opencode): add env variable format translation for MCP config#1667
Merged
dyoshikawa merged 4 commits intoMay 23, 2026
Merged
Conversation
9d16a1d to
a99d931
Compare
OpenCode uses {env:VAR} format for environment variable references,
while rulesync canonical format uses \${VAR}. Add bidirectional
translation in convertToOpencodeFormat and convertFromOpencodeFormat,
covering both environment (local servers) and headers (remote servers).
This mirrors the existing env var translation implemented for Cursor
(\${env:VAR} <-> \${VAR}) but with OpenCode's distinct format
({env:VAR}, no \$ prefix).
Assisted-by: OpenCode
a99d931 to
c7f06c6
Compare
Extract duplicated env var format conversion logic from opencode-mcp.ts and cursor-mcp.ts into a shared mcp-env-var-format.ts utility. This eliminates the DRY violation where both files had structurally identical regex-based record transformation patterns. Also fixes: - Cursor now translates env var refs in headers (was env-only) - Tightened OpenCode import regex capture from [^}]+ to [^}:]+ for consistency with the export regex - Added edge case tests: cross-tool interop, server-without-env, and Cursor headers env var conversion Assisted-by: Claude Code Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add mcp-env-var-format.test.ts with 10 tests covering both exported
functions independently using a generic {tool:VAR} pattern, ensuring
the utility works correctly for any future tool integration.
Assisted-by: Claude Code
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove 4 tests now covered by the shared mcp-env-var-format.test.ts: - "multiple env variables embedded in strings" from both cursor and opencode - "server without env field" from cursor - "server without env or headers fields" from opencode Assisted-by: Claude Code Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
dyoshikawa-claw
approved these changes
May 22, 2026
Collaborator
dyoshikawa-claw
left a comment
There was a problem hiding this comment.
LGTM. All CI checks pass and all findings from code/security review are low severity. Well-designed shared utility extraction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OpenCode uses
{env:VAR}format for environment variable references in its MCP config (docs, while rulesync's canonical format uses${VAR}. This PR adds bidirectional translation and extracts a shared env var conversion utility used by both OpenCode and Cursor.I ran into an issue while configuring SourceBot with API key, where this config didn't get translated into working MCP config for OpenCode.
Changes
src/features/mcp/mcp-env-var-format.ts(new)convertEnvVarRefsFromToolFormat()andconvertEnvVarRefsToToolFormat()envandheadersfields across all MCP serversCANONICAL_ENV_VAR_PATTERNregex avoids double-converting values already in${env:}formsrc/features/mcp/mcp-env-var-format.test.ts(new){tool:VAR}pattern, independent of any specific toolsrc/features/mcp/opencode-mcp.tsOPENCODE_ENV_VAR_PATTERNregex for matching{env:VAR}(with negative lookbehind to avoid matching Cursor's${env:VAR})fromRulesyncMcp()(export:${VAR}->{env:VAR}) andtoRulesyncMcp()(import:{env:VAR}->${VAR})src/features/mcp/cursor-mcp.tsheaders(wasenv-only before)Design Decisions
mcp-env-var-format.tsto eliminate duplication between OpenCode and Cursor. Each tool only provides its tool-specific regex pattern and replacement string.headers, since remote MCP servers use env var references in headers (e.g.,Authorization: Bearer {env:API_KEY}).(?<!\$)\{env:...}avoids matching Cursor's${env:VAR}format, preventing cross-tool interference.[^}:]+to exclude colons, ensuring round-trip consistency.How it was tested
pnpm cicheckpasses locally (format, lint, typecheck)/review-prwith parallel code-review and security-review agents — all mid/high findings fixed, final iteration has only low-severity findings remaining