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:
experimental.chat.messages.transform- Validates user prompts before sending to AItool.execute.before- Validates tool calls (MCP and built-in) before executiontool.execute.after- Logs tool responses for audit trail
File Structure
Key Files:
akto-guardrails-plugin.js: Main plugin that registers hooks with OpenCodeakto-validate-*.py: Python handlers for prompt/tool/response validationakto-mcp-request.py: MCP tool request handler — converts to JSON-RPC format and sends to/mcpendpointakto-mcp-response.py: MCP tool response handler — logs responses for auditakto_machine_id.py: Generates unique device identifiers for Akto dashboardsettings.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
Obtain Plugin Files
Clone the Akto repository or download the plugin files:
Alternatively, download individual files from GitHub:
Copy Plugin to OpenCode
Create the plugins directory and copy all files:
Verify installation:
Configure Akto Server URL ⚠️ CRITICAL STEP
Set your Akto instance URL as an environment variable:
Verify the URL is set:
This environment variable is REQUIRED. Without it, the plugin will run in fail-open mode (allows all execution but won't send data to Akto).
(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.
Start OpenCode
Kill any existing instance and start fresh:
Verify plugin is loaded:
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
Support: [email protected]
Community: https://www.akto.io/community
Last updated