What is MCP?
The Model Context Protocol (MCP) is a standardized way for AI assistants to interact with external tools and services. It provides:- Standardized interface for tool discovery and execution
- Multiple transport types (stdio, SSE)
- Dynamic tool registration at runtime
- Consistent permission model across all tools
OpenCode uses the mcp-go library to implement MCP client functionality.
How MCP works in OpenCode
Server configuration
You configure MCP servers in
.opencode.json with connection details and authenticationInitialization
When OpenCode starts, it connects to configured MCP servers and discovers available tools
Tool registration
MCP tools are registered alongside built-in tools with a
{server-name}_{tool-name} naming patternConnection types
OpenCode supports two MCP transport types:- Stdio
- SSE
Standard Input/Output transportCommunicate with tools via stdin/stdout, perfect for local executables.Configuration fields:Best for:
Must be
"stdio"Executable command to run (e.g.,
npx, python, /usr/local/bin/mcp-server)Command-line arguments to pass to the executable
Environment variables to set (format:
["KEY=value"])- Local tools and scripts
- Python/Node.js MCP servers
- Command-line utilities
- File system access
Configuration
MCP servers are configured in.opencode.json under the mcpServers key:
{server-name}_{tool-name}
Complete configuration example
Available MCP servers
The MCP ecosystem includes many pre-built servers:Filesystem
@modelcontextprotocol/server-filesystem
- Read/write file operations
- Directory listing
- File search
- Safe path restrictions
GitHub
@modelcontextprotocol/server-github
- Repository operations
- Issue management
- Pull request workflows
- Code search
GITHUB_TOKENPostgreSQL
@modelcontextprotocol/server-postgres
- Query execution
- Schema inspection
- Table operations
- Safe query validation
Web Search
@modelcontextprotocol/server-brave-search
- Web search capabilities
- News search
- Safe search filtering
Slack
@modelcontextprotocol/server-slack
- Send messages
- Channel management
- User lookup
Google Drive
@modelcontextprotocol/server-gdrive
- File access
- Document reading
- Upload/download
Find more MCP servers at the MCP Servers Registry
Tool naming and discovery
Tool name format
MCP tools are automatically named:{server-name}_{tool-name}
Example: If you configure a server named github that provides a tool called create_issue, the tool becomes available as github_create_issue.
Tool discovery
Registration
Each discovered tool is registered with schema:
- Tool name (prefixed with server name)
- Description
- Input parameters and types
- Required fields
Example: filesystem server
If thefilesystem server provides these tools:
read_filewrite_filelist_directory
filesystem_read_filefilesystem_write_filefilesystem_list_directory
Permission model
MCP tools follow the same permission system as built-in tools:Per-execution permissions
Per-execution permissions
Each MCP tool execution requires permission approval:
Session-level permissions
Session-level permissions
Grant permission for entire session:
- Press
Ain permission dialog - Permission persists for all future calls
- Scoped to specific tool and session
Security considerations
Security considerations
- Tool parameters are visible in permission prompt
- Sensitive data (tokens, passwords) shown in permissions
- Review all parameters before approval
- Session permissions don’t persist across restarts
Real-world examples
Example 1: Filesystem operations
Configuration:filesystem_read_file, filesystem_write_file, etc., within the /Users/username/projects directory.
Example 2: GitHub integration
Configuration:Example 3: Database queries
Configuration:Example 4: Multi-server workflow
Configuration:Building custom MCP servers
You can create custom MCP servers for specialized functionality:- Python
- Node.js
Troubleshooting
Server not connecting
Server not connecting
Check:
- Command path is correct and executable
- Required dependencies are installed (
npxmay need to download packages) - Environment variables are set correctly
- Check OpenCode debug logs:
opencode -d
Tools not appearing
Tools not appearing
Verify:
- Server successfully initialized (check logs)
- Server implements
ListToolscorrectly - Tool schemas are valid
- Restart OpenCode after config changes
Permission denied errors
Permission denied errors
Ensure:
- Authentication tokens are valid
- File paths are within allowed directories
- Service accounts have necessary permissions
- Check MCP server logs for auth errors
Tool execution fails
Tool execution fails
Debug:
- Check tool parameters match schema
- Verify required fields are provided
- Review MCP server error logs
- Test tool directly via MCP server CLI
- Enable debug mode:
"debug": truein config
Best practices
Name servers clearly
Use descriptive names that indicate purpose:
githubnotghdatabase-prodnotdbfilesystem-safefor restricted access
Scope permissions
Configure minimal necessary access:
- Filesystem servers: specific directories only
- Database servers: read-only when possible
- API servers: scope tokens appropriately
Use environment variables
Store sensitive data securely:
- Never commit tokens to config files
- Use environment variables for secrets
- Consider using secret management tools
Test independently
Verify MCP servers work before integration:
- Test with MCP CLI tools first
- Verify authentication separately
- Check tool schemas are valid
Handle errors gracefully
Design tools for robustness:
- Validate inputs thoroughly
- Provide helpful error messages
- Include fallback behavior
Document tool usage
Help AI use tools correctly:
- Write clear tool descriptions
- Document parameter formats
- Include usage examples
Security considerations
- Principle of least privilege: Grant minimum necessary permissions
- Network isolation: Use stdio for local tools when possible
- Token rotation: Regularly rotate API keys and tokens
- Audit logs: Monitor tool usage and review permission grants
- Input validation: MCP servers should validate all inputs
- Safe defaults: Configure restrictive defaults, expand as needed
Next steps
MCP Specification
Learn about the Model Context Protocol specification
MCP Servers
Browse available MCP server implementations
Configuration
Complete configuration reference
AI Tools
Explore OpenCode’s built-in tools