OpenCode Integration
OpenCode is a powerful CLI tool for AI-assisted coding that supports 75+ LLM providers. You can configure it to use the LeanMCP AI Gateway as a custom provider.OpenCode uses the AI SDK and supports any OpenAI-compatible API, making it perfect for the LeanMCP AI Gateway.
Prerequisites
Get Credits
Purchase credits at ship.leanmcp.com
Create API Key
Create an API key at ship.leanmcp.com/api-keys with SDK permissions
Configuration
Configuration File
Create or updateopencode.json in your project directory (or ~/.config/opencode/opencode.json for global use):
Replace
leanmcp_your_api_key_here with your actual API key.Using LeanMCP in OpenCode
Once configured, you can simply run:model is defined in opencode.json as leanmcp/claude-sonnet-4-5, Opencode will automatically use it.
To view available models:
leanmcp/claude-sonnet-4-5 if it’s not already selected.
Configuration Options
| Option | Description |
|---|---|
npm | AI SDK package (@ai-sdk/openai-compatible for gateway) |
name | Display name in OpenCode UI |
options.baseURL | LeanMCP AI Gateway endpoint |
options.apiKey | Can be set here or via /connect |
options.headers | Custom headers for requests |
models | Map of available models |
Setting Token Limits
For proper context management, specify model limits:Environment Variables
Alternatively, set credentials via environment variables:Multiple Providers via LeanMCP
Route all your AI providers through LeanMCP:Verifying Setup
- Run OpenCode:
opencode - Select a LeanMCP model:
/models - Ask a question
- Check your LeanMCP Dashboard to see the logged request
Benefits
Unified Logging
All your OpenCode sessions logged in one place
Multi-Provider
Use OpenAI, Anthropic, xAI through one gateway
Cost Tracking
Track spending across all your coding sessions
Security
Detect if sensitive code is being sent
Troubleshooting
Provider not appearing in /models
Provider not appearing in /models
- Verify
opencode.jsonis in your project root or~/.config/opencode/ - Check JSON syntax is valid
- Restart OpenCode after config changes
Authentication errors
Authentication errors
- Run
/connectand re-enter your API key - Verify the API key has SDK permissions
- Check you have credits in your account
Model not working
Model not working
- Ensure the model name matches what LeanMCP supports
- Check the baseURL matches the provider (openai, anthropic, etc.)
- Try a different model to isolate the issue
Resources
Next Steps
View Logs
See all your OpenCode requests
SDK Integration
Use in your own applications