Back to Recipes

Local OpenCode Agent Workspace

Set up a local OpenCode agent workspace with custom MCP servers, tool permissions, and project-specific instructions.

Best for: Developers building custom AI agent workspaces

What You Get

  • -OpenCode workspace configuration
  • -MCP server setup with custom tools
  • -Tool permission management
  • -Project-specific system prompts
  • -Workspace sharing and versioning

Step by Step

1. Create the workspace directory

Create a directory structure: .opencode/ with subfolders for config, tools, and prompts. Initialize a git repo so the workspace is version-controlled and shareable.

2. Configure opencode.json

Create opencode.json with: agent name, model settings (provider, model name, temperature), allowed tools list (bash, read, write, edit, glob, grep), and reference to the system prompt file.

3. Add MCP servers

Configure MCP servers in the workspace: filesystem server for project file access, database server for querying the project DB, and web search server for research. Each server gets its own config block with command, args, and env vars.

4. Set tool permissions

Create a permissions config: allow read/write for project source files, allow bash for npm/bun/git/test commands only, block access to .env and node_modules directories, allow web search and database read-only queries.

5. Test the agent

Run OpenCode in the workspace and test: 'Read the project structure', 'Show me the dependencies', 'What is the main entry point?', 'Run the tests'. Verify MCP servers are connected and permissions work.

Stack

OpenCodeMCP SDKNode.jsTypeScriptVS Code / Cursor

Build This

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to build this recipe.

Build a local OpenCode agent workspace with custom MCP servers, tool permissions, and project-specific instructions. ROLE: You are a workspace configuration system that creates and manages OpenCode agent environments with MCP servers, tool permissions, and project-specific instructions. CONSTRAINTS: - Workspace must be self-contained in a single directory with no external dependencies beyond Node.js - Tool permissions must follow principle of least privilege: read by default, write requires explicit allow - MCP server configurations must include connection validation and error handling - System prompt must be under 500 words to avoid slow agent responses - Workspace must be portable: other team members can clone and use it without additional setup TOOL CALLING: - Use function calling for: create_workspace(project_name, template?), configure_mcp_server(server_name, config), set_permissions(tool_name, rules[]), validate_workspace(workspace_path), export_workspace(format?) - Each tool returns structured JSON with configuration data and validation results STRUCTURED OUTPUT: - Workspace config must return JSON: { workspace_id: string, project_name: string, mcp_servers: [{ name: string, status: 'connected' | 'disconnected' | 'error', config: Record<string, any> }], permissions: [{ tool: string, read: boolean, write: boolean, bash: boolean }], system_prompt_length: number, created_at: string } - Validation must return JSON: { workspace_path: string, valid: boolean, issues: [{ severity: 'error' | 'warning', message: string, fix?: string }], mcp_status: Record<string, string> } CHAIN OF THOUGHT: - Setup: determine project type → select appropriate MCP servers → configure permissions → generate system prompt - Validation: check directory structure → validate opencode.json → test MCP connections → verify permissions - Export: serialize workspace config → include README with instructions → package for distribution FEW-SHOT EXAMPLES: Workspace: { project_name: 'my-saas-app', mcp_servers: [{ name: 'filesystem', status: 'connected' }, { name: 'database', status: 'connected' }], permissions: [{ tool: 'read', read: true, write: false, bash: false }, { tool: 'bash', read: false, write: false, bash: true }], system_prompt_length: 287 } Validation: { valid: true, issues: [], mcp_status: { filesystem: 'ok', database: 'ok' } } EVALUATION CRITERIA: - Configuration accuracy: percentage of MCP servers that connect successfully on first attempt - Permission correctness: percentage of tool permissions that match intended security policy - Portability: percentage of team members who can use the workspace without additional setup - System prompt effectiveness: agent performance on project-specific tasks The system should: 1) Create a workspace directory with opencode.json config file, 2) Configure MCP servers for file system access, database queries, and web search, 3) Set tool permissions: allow read for all project files, allow bash for npm/bun/git commands, block access to .env and node_modules, 4) Add a project-specific system prompt describing the codebase and coding conventions, 5) Include quick-start instructions and a test command to verify the agent works, 6) The workspace should be portable so other team members can clone and use it.

Common Failure Modes

  • !MCP server connection errors
  • !Permission misconfiguration blocks needed tools
  • !System prompt too verbose causes slow responses
  • !LLM API key not configured

Implementation Notes

Start with a minimal config and add tools gradually. Test each MCP server individually before adding the next. Keep the system prompt under 500 words.

Ship local opencode agent workspace in production with 4M Labs

4M Labs designs and ships applied AI systems -- connected to your tools, secured for your team, deployed with monitoring.

  • Connected to your tools and data sources
  • Secured for your team with proper access controls
  • Deployed with monitoring and error handling
  • Documented for handoff and future maintenance
Work With 4M Labs

Frequently Asked Questions

Can I use this recipe in production?
Yes. Every recipe is production-tested with error handling, logging, and deployment guidance.
Which LLM providers are supported?
Recipes support OpenAI, Anthropic Claude, Google Gemini, and open-source models via a unified interface.
How do I customize these recipes?
Each recipe includes a configuration section. Override model selection, API keys, and parameters without changing core logic.