Skip to content

OpenCode Integration

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.

Overview

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:

EventTypeDescription
chat.messageBlockingThe prompt the developer submitted - can drop it before it reaches the model
tool.execute.beforeBlockingTool execution requests, including MCP tools and skill loads - can stop the call before it runs
tool.execute.afterObservationTool output after execution
tool.execute.errorObservationA tool call that failed, including one Capsule blocked
assistant.messageObservationEach finished block of agent reply text
session.createdObservationA 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.

Prerequisites

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)

Step 1: Generate a Plugin Token

  1. Log in to the Capsule Security portal

  2. Navigate to Integrations and locate OpenCode

  3. 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.

  4. Copy the generated token. You will reference it from your Capsule plugin configuration in Step 3.


Step 2: Register the Plugin

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:

OSSystem-wide config
macOS/Library/Application Support/opencode/opencode.json
Linux/etc/opencode/opencode.json
Windows%ProgramData%\opencode\opencode.json

Step 3: Configure the Plugin

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
}

Configuration Options

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.

OptionEnv varTypeDefaultDescription
endpointCAPSULE_ENDPOINTstring(required)Capsule agentsecurity endpoint, e.g. https://agents.capsule.security
tokenCAPSULE_TOKENstring(required)JWT generated in Step 1, scoped to your tenant and environment
blockOnRiskCAPSULE_BLOCK_ON_RISKbooleantrueApply server block decisions inline. Set to false to run in observe-only mode
failOpenCAPSULE_FAIL_OPENbooleantrueWhen Capsule is unreachable or returns an error, allow the prompt or tool call. Set to false to fail closed
timeoutMsCAPSULE_TIMEOUT_MSnumber5000Per-request timeout in milliseconds. OpenCode does not time out plugins itself, so this bounds every blocking check
userCAPSULE_USERstringOS user nameUser 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.

Token Storage

Never commit the JWT to version control. Recommended approaches:

  • Keep the token in ~/.config/opencode/capsule.json outside any repository
  • Set CAPSULE_TOKEN from 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

Step 4: Restart OpenCode

For the plugin to take effect, restart any running OpenCode sessions:

  1. Exit OpenCode
  2. Restart OpenCode so it installs the package and loads the plugin

Step 5: Verify the Installation

  1. 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"
  2. Log in to the Capsule Security portal

  3. 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.

  4. 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

Troubleshooting

If events are not appearing:

  1. Verify the endpoint is reachable from the host running OpenCode:

    curl -sS -o /dev/null -w "%{http_code}\n" https://agents.capsule.security/health
  2. Verify the configuration is found - the plugin writes a warning to the OpenCode log and stays inactive when endpoint or token is missing. Check that ~/.config/opencode/capsule.json is valid JSON. Run opencode --print-logs to see the log on stderr.

  3. Check that plugins are enabled - OpenCode skips npm plugins when OPENCODE_PURE=1 is set.

  4. Check for timeouts - if your network has high latency to the Capsule endpoint, increase timeoutMs or confirm failOpen is set appropriately.

  5. Contact Capsule Security support if issues persist.


Security Considerations

The Capsule plugin runs in-process inside OpenCode with full system permissions and observes every prompt, tool call and agent reply. Before deploying:

  1. Protect the JWT - anyone with the token can post events on behalf of your tenant. Rotate it through the portal if it is exposed.
  2. Choose failOpen deliberately - failOpen: true (the default) prioritizes developer productivity; failOpen: false prioritizes policy enforcement and will block prompts and tool execution when Capsule is unreachable.
  3. Pin a plugin version in production, using "@capsulesecurity/opencode-capsule@&lt;version&gt;", and review changelogs before upgrading.
  4. Use TLS-only endpoints. The plugin sends a Bearer token on every request - never configure an http:// endpoint.
  5. 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.

Support

For help with this integration:

  • Email: support@capsule.security
  • Include: Your organization ID, integration status, plugin version, and any error messages

References