Connect OpenCode to Capsule Security for visibility into AI coding agent activity, including prompts, tool execution, agent replies and subagents - with inline blocking on policy-violating prompts and tool calls.
This integration uses OpenCode's plugin system to capture and govern AI coding agent activity. The Capsule plugin runs in-process inside OpenCode, forwards events to your Capsule tenant over HTTPS, and applies policy verdicts to prompts before they reach the model and to tool calls before they execute.
The plugin targets OpenCode 1.x (the opencode-ai npm package). OpenCode 2.x has a different plugin API and is not supported yet.
The following events are captured:
| Event | Type | Description |
|---|---|---|
| chat.message | Blocking | The prompt the developer submitted - can drop it before it reaches the model |
| tool.execute.before | Blocking | Tool execution requests, including MCP tools and skill loads - can stop the call before it runs |
| tool.execute.after | Observation | Tool output after execution |
| tool.execute.error | Observation | A tool call that failed, including one Capsule blocked |
| assistant.message | Observation | Each finished block of agent reply text |
| session.created | Observation | A new session, including the child session a subagent runs in |
When Capsule blocks a tool call, the tool does not run and the model receives the policy reason as the tool's error. When Capsule blocks a prompt, it is not saved or sent to the model, and the OpenCode TUI shows the policy reason as a session error. opencode run reports a generic server error for a blocked prompt instead.
Subagents run in child sessions, so their prompts and tool calls go through the same checks. Text OpenCode adds to a prompt itself, such as the contents of an attached file, is not part of the policy check on that prompt.
Before you begin, ensure you have:
- OpenCode 1.x installed
- A Capsule Security account with admin access
- Network access from your developer machines to your Capsule agentsecurity endpoint (e.g.
https://agents.capsule.security)
Log in to the Capsule Security portal
Navigate to Integrations and locate OpenCode
Click Install - Capsule generates a JWT scoped to your tenant and OpenCode environment. The token contains the tenant and environment claims required by the agentsecurity endpoint; treat it as a secret.
Copy the generated token. You will reference it from your Capsule plugin configuration in Step 3.
OpenCode installs npm plugins listed in the plugin array of its config. Add the Capsule package to your global config, ~/.config/opencode/opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["@capsulesecurity/opencode-capsule"]
}OpenCode installs the package on next start. To pin a version, use "@capsulesecurity/opencode-capsule@1.0.0".
To roll the plugin out to every user of a machine, add the same entry to the system-wide config instead. OpenCode reads it on every start, alongside each user's own config:
| OS | System-wide config |
|---|---|
| macOS | /Library/Application Support/opencode/opencode.json |
| Linux | /etc/opencode/opencode.json |
| Windows | %ProgramData%\opencode\opencode.json |
The Capsule plugin reads its settings from ~/.config/opencode/capsule.json ($XDG_CONFIG_HOME/opencode/capsule.json when XDG_CONFIG_HOME is set):
{
"endpoint": "https://agents.capsule.security",
"token": "<token-from-step-1>",
"blockOnRisk": true,
"failOpen": true,
"timeoutMs": 5000
}Every option can also be supplied as an environment variable.
endpoint and token come from the environment when both are set there, otherwise from capsule.json, and the remaining settings come from that same source. Environment variables still override the other options. Plugin options in opencode.json and anything in a project directory are never read, so cloning an untrusted repository cannot redirect your prompts and tool calls to someone else's server or turn enforcement off.
| Option | Env var | Type | Default | Description |
|---|---|---|---|---|
endpoint | CAPSULE_ENDPOINT | string | (required) | Capsule agentsecurity endpoint, e.g. https://agents.capsule.security |
token | CAPSULE_TOKEN | string | (required) | JWT generated in Step 1, scoped to your tenant and environment |
blockOnRisk | CAPSULE_BLOCK_ON_RISK | boolean | true | Apply server block decisions inline. Set to false to run in observe-only mode |
failOpen | CAPSULE_FAIL_OPEN | boolean | true | When Capsule is unreachable or returns an error, allow the prompt or tool call. Set to false to fail closed |
timeoutMs | CAPSULE_TIMEOUT_MS | number | 5000 | Per-request timeout in milliseconds. OpenCode does not time out plugins itself, so this bounds every blocking check |
user | CAPSULE_USER | string | OS user name | User identifier sent on every event |
Without endpoint and token the plugin writes a warning to the OpenCode log and stays inactive. It never breaks the agent loop.
Never commit the JWT to version control. Recommended approaches:
- Keep the token in
~/.config/opencode/capsule.jsonoutside any repository - Set
CAPSULE_TOKENfrom your secret manager (1Password, AWS Secrets Manager, HashiCorp Vault) at shell startup - For MDM-managed deployments, deliver it through the same channel that distributes the OpenCode configuration
For the plugin to take effect, restart any running OpenCode sessions:
- Exit OpenCode
- Restart OpenCode so it installs the package and loads the plugin
Start a new OpenCode session and execute a simple task to generate activity:
Create a new file called test.txt with the content "Hello World"Log in to the Capsule Security portal
Navigate to Inventory > Agents and confirm your OpenCode agent appears. OpenCode has no persistent agent identifier, so an agent is identified by the working directory and the user running it.
Click on the agent and review the audit logs to verify events are captured:
- Session start event
- The prompt you submitted
- Tool execution (write tool)
- The agent reply
If events are not appearing:
Verify the endpoint is reachable from the host running OpenCode:
curl -sS -o /dev/null -w "%{http_code}\n" https://agents.capsule.security/healthVerify the configuration is found - the plugin writes a warning to the OpenCode log and stays inactive when
endpointortokenis missing. Check that~/.config/opencode/capsule.jsonis valid JSON. Runopencode --print-logsto see the log on stderr.Check that plugins are enabled - OpenCode skips npm plugins when
OPENCODE_PURE=1is set.Check for timeouts - if your network has high latency to the Capsule endpoint, increase
timeoutMsor confirmfailOpenis set appropriately.Contact Capsule Security support if issues persist.
The Capsule plugin runs in-process inside OpenCode with full system permissions and observes every prompt, tool call and agent reply. Before deploying:
- Protect the JWT - anyone with the token can post events on behalf of your tenant. Rotate it through the portal if it is exposed.
- Choose
failOpendeliberately -failOpen: true(the default) prioritizes developer productivity;failOpen: falseprioritizes policy enforcement and will block prompts and tool execution when Capsule is unreachable. - Pin a plugin version in production, using
"@capsulesecurity/opencode-capsule@<version>", and review changelogs before upgrading. - Use TLS-only endpoints. The plugin sends a Bearer token on every request - never configure an
http://endpoint. - Understand the coverage boundary - shell commands a developer runs directly with
!in the OpenCode prompt do not go through the tool hooks, so they are not checked.
For help with this integration:
- Email: support@capsule.security
- Include: Your organization ID, integration status, plugin version, and any error messages