Skip to main content

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

1

Get Credits

Purchase credits at leanmcp.com
2

Create API Key

Create an API key at leanmcp.com/api-keys with SDK permissions
3

Install OpenCode

Configuration

Configuration File

Create or update opencode.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:
Since the model is defined in opencode.json as leanmcp/claude-sonnet-4-5, Opencode will automatically use it. To view available models:
Select leanmcp/claude-sonnet-4-5 if it’s not already selected.

Configuration Options

Setting Token Limits

For proper context management, specify model limits:

Environment Variables

Alternatively, set credentials via environment variables:
Then reference in config:

Multiple Providers via LeanMCP

Route all your AI providers through LeanMCP:

Verifying Setup

  1. Run OpenCode: opencode
  2. Select a LeanMCP model: /models
  3. Ask a question
  4. 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

  • Verify opencode.json is in your project root or ~/.config/opencode/
  • Check JSON syntax is valid
  • Restart OpenCode after config changes
  • Run /connect and re-enter your API key
  • Verify the API key has SDK permissions
  • Check you have credits in your account
  • 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