Problem
When running OpenCode from source via bun dev, non-interactive subcommands like mcp add, mcp auth, mcp list, etc. have no way to specify the target project directory. They all hardcode directory: process.cwd() in their handlers.
This is a problem when running from source because bun dev (and the underlying bun run --cwd packages/opencode ...) must be invoked from within the opencode repo. That means process.cwd() resolves to the opencode repo itself, not the user's actual project.
Context
The interactive TUI already has a [project] positional argument (bun dev <directory>), and the run command has a --dir flag. But none of the non-interactive subcommands support any equivalent.
This is related to #6780 and #6404 (missing docs for running from source), since those issues addressed the TUI and desktop cases but not non-interactive commands.
Motivating use case
I needed to trigger the OAuth flow for an MCP server (Notion) configured in my project's opencode.json. The mcp auth command needs to resolve the project config to find the MCP server definition, but when running from source it resolves the opencode repo's config instead.
The same problem would affect:
mcp add — writes config to the wrong opencode.json
mcp list / mcp debug — reads the wrong config
mcp logout — operates on the wrong project context
Other non-interactive commands (agent, models, pr, github, export, import, stats, etc.) share the same process.cwd() pattern, but in practice the pain is most acute for mcp subcommands since they depend heavily on project-specific config.
Steps to reproduce
- Clone the opencode repo and run
bun install
- Have a separate project with an
opencode.json that configures an MCP server
- Try to run
mcp auth from source against that project — there is no working approach
The only workaround I found was:
# Copy opencode.json into the opencode source tree
cp /path/to/my-project/opencode.json /path/to/opencode/packages/opencode/
# Run from within the opencode source tree
cd /path/to/opencode
bun run --conditions=browser --cwd packages/opencode src/index.ts mcp auth notion
This is awkward because:
- It pollutes the opencode source tree with project-specific config
- The project context (git root, config resolution) resolves to the opencode repo, not the target project
- Config written by
mcp add would end up in the wrong location
What bun run --cwd does vs what's needed
bun run --cwd packages/opencode tells bun which directory to resolve the script from — it does not affect process.cwd() as seen by the running Node/Bun process in the way that's needed here. The opencode CLI needs its own mechanism to set the logical project directory.
Proposed solution
Add a --dir (or --cwd) option to non-interactive subcommands, consistent with how run already supports --dir. The handler would pass args.dir ?? process.cwd() to Instance.provide({ directory: ... }).
Something like:
// In each non-interactive command's builder:
.option("dir", {
type: "string",
describe: "project directory to operate in",
})
// In each handler:
await Instance.provide({
directory: args.dir ?? process.cwd(),
async fn() { ... }
})
This would also be useful for the installed opencode binary (not just dev from source), for scripting and CI use cases.
Workaround
Copy opencode.json into the opencode source tree and run from there:
cd /path/to/opencode
cp /path/to/my-project/opencode.json packages/opencode/
bun run --conditions=browser --cwd packages/opencode src/index.ts mcp auth notion
Problem
When running OpenCode from source via
bun dev, non-interactive subcommands likemcp add,mcp auth,mcp list, etc. have no way to specify the target project directory. They all hardcodedirectory: process.cwd()in their handlers.This is a problem when running from source because
bun dev(and the underlyingbun run --cwd packages/opencode ...) must be invoked from within the opencode repo. That meansprocess.cwd()resolves to the opencode repo itself, not the user's actual project.Context
The interactive TUI already has a
[project]positional argument (bun dev <directory>), and theruncommand has a--dirflag. But none of the non-interactive subcommands support any equivalent.This is related to #6780 and #6404 (missing docs for running from source), since those issues addressed the TUI and desktop cases but not non-interactive commands.
Motivating use case
I needed to trigger the OAuth flow for an MCP server (Notion) configured in my project's
opencode.json. Themcp authcommand needs to resolve the project config to find the MCP server definition, but when running from source it resolves the opencode repo's config instead.The same problem would affect:
mcp add— writes config to the wrongopencode.jsonmcp list/mcp debug— reads the wrong configmcp logout— operates on the wrong project contextOther non-interactive commands (
agent,models,pr,github,export,import,stats, etc.) share the sameprocess.cwd()pattern, but in practice the pain is most acute formcpsubcommands since they depend heavily on project-specific config.Steps to reproduce
bun installopencode.jsonthat configures an MCP servermcp authfrom source against that project — there is no working approachThe only workaround I found was:
This is awkward because:
mcp addwould end up in the wrong locationWhat
bun run --cwddoes vs what's neededbun run --cwd packages/opencodetells bun which directory to resolve the script from — it does not affectprocess.cwd()as seen by the running Node/Bun process in the way that's needed here. The opencode CLI needs its own mechanism to set the logical project directory.Proposed solution
Add a
--dir(or--cwd) option to non-interactive subcommands, consistent with howrunalready supports--dir. The handler would passargs.dir ?? process.cwd()toInstance.provide({ directory: ... }).Something like:
This would also be useful for the installed
opencodebinary (not just dev from source), for scripting and CI use cases.Workaround
Copy
opencode.jsoninto the opencode source tree and run from there:cd /path/to/opencode cp /path/to/my-project/opencode.json packages/opencode/ bun run --conditions=browser --cwd packages/opencode src/index.ts mcp auth notion