OpenCode Hooks

Akto Guardrails for OpenCode provides security validation for AI coding agent interactions. It intercepts prompts and tool calls before execution, validates against security policies, blocks risky behavior, and reports all events to your Akto dashboard.

Key Features

  • Built-in & MCP Tool Support - Validates both OpenCode built-in tools (read, glob) and MCP server tools

  • Real-time Protection - Validates every prompt and tool call before execution

  • JSON-RPC Compliance - Full JSON-RPC 2.0 support for MCP protocol

  • Centralized Monitoring - All events reported to Akto dashboard

  • Configurable Behavior - Blocking or observation modes

How It Works

OpenCode plugin hooks into three critical points in the AI coding agent lifecycle:

3 Hook Points:

  1. experimental.chat.messages.transform - Validates user prompts before sending to AI

  2. tool.execute.before - Validates tool calls (MCP and built-in) before execution

  3. tool.execute.after - Logs tool responses for audit trail

File Structure

Key Files:

  • akto-guardrails-plugin.js: Main plugin that registers hooks with OpenCode

  • akto-validate-*.py: Python handlers for prompt/tool/response validation

  • akto-mcp-request.py: MCP tool request handler — converts to JSON-RPC format and sends to /mcp endpoint

  • akto-mcp-response.py: MCP tool response handler — logs responses for audit

  • akto_machine_id.py: Generates unique device identifiers for Akto dashboard

  • settings.json: Plugin metadata (name, version, hook descriptions) — OpenCode uses this for plugin discovery

Setup Guide

Prerequisites

  • OpenCode installed on your system

  • Python 3.6+ available

  • Akto instance with guardrails API endpoint

  • Network access to your Akto server

  • macOS, Linux, or Windows with bash/zsh

Installation Steps

1

Obtain Plugin Files

Clone the Akto repository or download the plugin files:

Alternatively, download individual files from GitHub:

2

Copy Plugin to OpenCode

Create the plugins directory and copy all files:

Verify installation:

3

Configure Akto Server URL ⚠️ CRITICAL STEP

Set your Akto instance URL as an environment variable:

Verify the URL is set:

circle-exclamation
4

(Optional) Configure MCP Servers

If you use MCP servers with OpenCode, add them to ~/.config/opencode/opencode.json:

The plugin will automatically detect MCP tools (format: server_tool) and route them to the /mcp endpoint.

5

Start OpenCode

Kill any existing instance and start fresh:

Verify plugin is loaded:

6

Verify Installation

Check all components are working:

Configuration Reference

Environment Variables

Set these to customize the plugin behavior:

OpenCode Plugin Settings

Edit ~/.opencode/plugins/settings.json to customize (optional):

Monitoring & Logs

View Real-time Activity

Log Format

Each log entry includes:

  • Timestamp: When the event occurred

  • Hook: Which hook point was triggered (PLUGIN_INIT, TOOL_EXECUTE_BEFORE, MCP_TOOL_DETECTED, etc.)

  • Details: JSON object with relevant context (tool name, args, API responses)

Enable Debug Logging

For verbose logging with full payloads:

Troubleshooting

Plugin Not Loading

Symptom: No logs appear in ~/.config/opencode/akto/logs/

Solution:

Akto Server Unreachable

Symptom: Logs show "API CALL FAILED"

Solution:

MCP Tools Not Detected

Symptom: MCP tool doesn't appear as server_tool format

Solution:

Python Script Errors

Symptom: Errors in akto-mcp-request.log

Solution:

No Events in Dashboard

Symptom: Plugin runs but events don't appear in Akto dashboard

Solution:

Quick Setup Summary

Data Flow

Built-in Tools (read, glob, etc.)

MCP Tools (calculator_add, git_status, etc.)

Resources

Last updated