use crate::adapters::caps::{
Caps, DisableMech, EnvRefSyntax, FieldSet, ProjectDisable, ProjectSemantics, Source,
TransportSet,
};
use crate::adapters::detect::Probe;
use crate::adapters::location::Location;
use crate::adapters::mapper::{
Canonical, EntryMapper, Launch, MapError, RenderedServer, ServerDef, WriteCx, carry_over,
check_name, check_transport, drop_default_enabled, drop_empty, existing_entry, read_disabled,
read_tools, shape, split_rest, string_field, string_map, string_map_field, unsupported,
write_enabled, write_passthrough, write_tools,
};
use crate::adapters::spec::{AdapterSpec, ContainerPath, McpSpec, Tier, exact_names};
use crate::env::Os;
use crate::formats::{Format, NativeMap, NativeValue};
use crate::model::clients::passthrough_of;
use crate::model::server::{ServerFields, Transport};
pub const ID: &str = "opencode";
const TYPE: &str = "type";
const LOCAL: &str = "local";
const REMOTE: &str = "remote";
const COMMAND: &str = "command";
const ENVIRONMENT: &str = "environment";
const ENABLED: &str = "enabled";
pub const SKELETON: &str = "{\n \"$schema\": \"https://opencode.ai/config.json\"\n}\n";
const CAPS: Caps = Caps {
transports: TransportSet::ALL,
env_ref: Some(EnvRefSyntax::BraceEnv),
env_ref_fields: FieldSet::COMMAND
.union(FieldSet::ARGS)
.union(FieldSet::ENV)
.union(FieldSet::URL)
.union(FieldSet::HEADERS),
command_ref: None,
native_disable: Some(DisableMech::EnabledFalse),
project_disable: Some(ProjectDisable::EnabledFalse { partial: false }),
project_semantics: ProjectSemantics::DeepMerge,
precedence: &[Source::Project, Source::User],
compat_reads: &[],
trust_gate: None,
rewrites_file_at_runtime: false,
restart_required: false,
tool_filter: None,
windows_shell_shim: false,
timeouts: &[],
passthrough_allowlist: passthrough_of(ID),
};
const MODELED_KEYS: [&str; 6] = [TYPE, COMMAND, ENVIRONMENT, "url", "headers", ENABLED];
fn is_modeled(key: &str) -> bool {
MODELED_KEYS.contains(&key) || CAPS.passthrough_allowlist.contains(&key)
}
fn valid_name(name: &str) -> bool {
!name.is_empty()
}
pub fn is_sse_url(url: &str) -> bool {
let end = url.find(['?', '#']).unwrap_or(url.len());
let path = url[..end].trim_end_matches('/');
let path = path.split_once("://").map_or(path, |(_, rest)| {
rest.find('/').map_or("", |at| &rest[at..])
});
path.ends_with("/sse")
}
fn inferred_type(entry: &NativeMap) -> Option<&'static str> {
if entry.contains_key(COMMAND) {
Some(LOCAL)
} else if entry.contains_key("url") {
Some(REMOTE)
} else {
None
}
}
#[derive(Debug)]
pub struct OpenCodeJson;
impl EntryMapper for OpenCodeJson {
fn to_native(
&self,
server: &RenderedServer,
cx: &WriteCx<'_>,
) -> Result<NativeValue, MapError> {
check_name(valid_name, &server.name)?;
check_transport(&CAPS, &server.launch)?;
let existing = existing_entry(cx);
let keep_empty = |key: &str| existing.is_some_and(|entry| entry.contains_key(key));
let mut entry = NativeMap::new();
match &server.launch {
Launch::Stdio(stdio) => {
if !stdio.forward_env.is_empty() {
return unsupported("env_vars");
}
if stdio.cwd.is_some() {
return unsupported("cwd");
}
entry.insert(TYPE, LOCAL.into());
let command = std::iter::once(&stdio.command)
.chain(&stdio.args)
.map(|part| NativeValue::from(part.as_str()))
.collect();
entry.insert(COMMAND, NativeValue::Array(command));
if !stdio.env.is_empty() || keep_empty(ENVIRONMENT) {
entry.insert(ENVIRONMENT, string_map(&stdio.env));
}
}
Launch::Http(remote) | Launch::Sse(remote) => {
if remote.headers_helper.is_some() {
return unsupported("headersHelper");
}
if remote.bearer_env.is_some() {
return unsupported("bearer_token_env_var");
}
if !remote.headers_env.is_empty() {
return unsupported("env_http_headers");
}
entry.insert(TYPE, REMOTE.into());
entry.insert("url", remote.url.as_str().into());
if !remote.headers.is_empty() || keep_empty("headers") {
entry.insert("headers", string_map(&remote.headers));
}
}
}
write_enabled(&mut entry, &CAPS, server, existing)?;
write_tools(&mut entry, &CAPS, &server.tools)?;
write_passthrough(&mut entry, &CAPS, server)?;
carry_over(&mut entry, existing, is_modeled);
Ok(NativeValue::Object(entry))
}
fn from_native(&self, key: &str, value: &NativeValue) -> Result<ServerDef, MapError> {
let entry = value.as_object().ok_or_else(|| MapError::UnexpectedShape {
field: key.to_owned(),
expected: "an object",
})?;
let kind = match entry.get(TYPE) {
Some(NativeValue::String(kind)) => kind.as_str(),
Some(_) => return shape(TYPE, "a string"),
None => match inferred_type(entry) {
Some(kind) => kind,
None => return shape(TYPE, "`local` or `remote`"),
},
};
let fields = match kind {
LOCAL => {
let (command, args) = command_array(entry)?;
ServerFields {
transport: Some(Transport::Stdio),
command: Some(command),
args: Some(args),
env: string_map_field(entry, ENVIRONMENT)?,
..ServerFields::default()
}
}
REMOTE => {
let url = string_field(entry, "url")?;
let transport = match &url {
Some(url) if is_sse_url(url) => Transport::Sse,
_ => Transport::Http,
};
ServerFields {
transport: Some(transport),
url,
headers: string_map_field(entry, "headers")?,
..ServerFields::default()
}
}
other => {
return Err(MapError::UnsupportedTransport {
transport: other.to_owned(),
});
}
};
let mut def = ServerDef {
fields,
disabled: read_disabled(entry, &CAPS)?,
tools: read_tools(entry, &CAPS)?,
..ServerDef::default()
};
split_rest(&mut def, entry, &CAPS, is_modeled);
Ok(def)
}
fn canonical(&self, value: &NativeValue) -> Canonical {
let Some(entry) = value.as_object() else {
return Canonical::of(value);
};
let mut entry = entry.clone();
drop_empty(&mut entry, &[ENVIRONMENT, "headers"]);
drop_default_enabled(&mut entry, &CAPS);
Canonical::of(&NativeValue::Object(entry))
}
fn skipped_by_client(&self, value: &NativeValue) -> Option<&'static str> {
let entry = value.as_object()?;
(!entry.contains_key(TYPE) && inferred_type(entry).is_some())
.then_some("has no `type` (`local` or `remote`), so the client rejects it")
}
}
fn command_array(entry: &NativeMap) -> Result<(String, Vec<String>), MapError> {
const EXPECTED: &str = "a non-empty array of strings (the command, then its arguments)";
let parts = entry
.get(COMMAND)
.and_then(NativeValue::as_array)
.and_then(|items| {
items
.iter()
.map(|item| item.as_str().map(str::to_owned))
.collect::<Option<Vec<String>>>()
});
match parts {
Some(mut parts) if !parts.is_empty() => {
let command = parts.remove(0);
Ok((command, parts))
}
_ => shape(COMMAND, EXPECTED),
}
}
static MAPPER: OpenCodeJson = OpenCodeJson;
pub static SPEC: AdapterSpec = AdapterSpec {
id: ID,
display_name: "OpenCode",
tier: Tier::C,
detect: &[
Probe::Path(Location::XdgConfigAllOs("opencode")),
Probe::Executable("opencode"),
],
mcp: Some(McpSpec {
oses: &[Os::Windows, Os::Linux, Os::MacOs],
read_only: false,
user: &[
Location::XdgConfigAllOs("opencode/opencode.json"),
Location::XdgConfigAllOs("opencode/opencode.jsonc"),
],
user_files: None,
user_mirrors: &[],
profiles: None,
project: &[
Location::Project("opencode.json"),
Location::Project("opencode.jsonc"),
Location::Project(".opencode/opencode.json"),
Location::Project(".opencode/opencode.jsonc"),
],
project_local: &[],
format: Format::Jsonc,
skeleton: SKELETON,
container: ContainerPath(&["mcp"]),
project_local_container: ContainerPath(&["mcp"]),
legacy_containers: &[],
same_name: exact_names,
replaced_by: &[],
mapper: &MAPPER,
caps: CAPS,
sign_in: crate::adapters::sign_in::OPENCODE,
}),
};