use schemars::JsonSchema;
pub const OBJECTIVEAI_CAPABILITY: &str = "objectiveai";
pub const CLI_COMMAND_ENDPOINT_SUFFIX: &str = "/objectiveai/command";
pub trait McpClientCommandExecutor: Send + Sync + 'static {
type Error: std::fmt::Display + Send + 'static;
type Stream: futures_util::Stream<
Item = Result<crate::cli::command::ResponseItem, Self::Error>,
> + Send
+ 'static;
fn execute(
&self,
request: crate::cli::command::Request,
) -> impl Future<Output = Result<Self::Stream, Self::Error>> + Send;
}
#[derive(Debug, Clone, serde::Serialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(rename = "mcp.CliResponse")]
pub enum CliResponse {
Ack { id: String },
Item {
id: String,
#[schemars(with = "serde_json::Value")]
item: Box<serde_json::value::RawValue>,
},
Error { id: String, error: String },
Done { id: String },
}
impl<'de> serde::Deserialize<'de> for CliResponse {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
use serde::de::{self, MapAccess, Visitor};
struct FrameVisitor;
impl<'de> Visitor<'de> for FrameVisitor {
type Value = CliResponse;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a cli response frame")
}
fn visit_map<A: MapAccess<'de>>(
self,
mut map: A,
) -> Result<CliResponse, A::Error> {
let mut kind: Option<String> = None;
let mut id: Option<String> = None;
let mut item: Option<Box<serde_json::value::RawValue>> = None;
let mut error: Option<String> = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"type" => kind = Some(map.next_value()?),
"id" => id = Some(map.next_value()?),
"item" => item = Some(map.next_value()?),
"error" => error = Some(map.next_value()?),
_ => {
map.next_value::<de::IgnoredAny>()?;
}
}
}
let kind = kind.ok_or_else(|| de::Error::missing_field("type"))?;
let id = || id.ok_or_else(|| de::Error::missing_field("id"));
match kind.as_str() {
"ack" => Ok(CliResponse::Ack { id: id()? }),
"item" => Ok(CliResponse::Item {
id: id()?,
item: item
.ok_or_else(|| de::Error::missing_field("item"))?,
}),
"error" => Ok(CliResponse::Error {
id: id()?,
error: error
.ok_or_else(|| de::Error::missing_field("error"))?,
}),
"done" => Ok(CliResponse::Done { id: id()? }),
other => Err(de::Error::unknown_variant(
other,
&["ack", "item", "error", "done"],
)),
}
}
}
deserializer.deserialize_map(FrameVisitor)
}
}
#[derive(Debug, thiserror::Error)]
#[error("CLI command execution is not supported by this MCP client")]
pub struct CommandExecutionNotSupported;
#[derive(Debug, Clone, Copy, Default)]
pub struct NotSupportedMcpClientCommandExecutor;
impl McpClientCommandExecutor for NotSupportedMcpClientCommandExecutor {
type Error = CommandExecutionNotSupported;
type Stream = futures_util::stream::Empty<
Result<crate::cli::command::ResponseItem, Self::Error>,
>;
async fn execute(
&self,
_request: crate::cli::command::Request,
) -> Result<Self::Stream, Self::Error> {
Err(CommandExecutionNotSupported)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn not_supported_executor_errors() {
let executor = NotSupportedMcpClientCommandExecutor;
let request = crate::cli::command::Request::Update(
crate::cli::command::update::Request {
path_type: crate::cli::command::update::Path::Update,
base: crate::cli::command::RequestBase {
jq: None,
python: None,
timeout_seconds: None,
max_tokens: None,
},
},
);
let err = executor.execute(request).await.unwrap_err();
assert_eq!(
err.to_string(),
"CLI command execution is not supported by this MCP client",
);
}
#[test]
fn cli_response_wire_shapes() {
let ack = CliResponse::Ack {
id: "7".to_string(),
};
assert_eq!(
serde_json::to_value(&ack).unwrap(),
serde_json::json!({"type": "ack", "id": "7"}),
);
let error = CliResponse::Error {
id: "7".to_string(),
error: "boom".to_string(),
};
assert_eq!(
serde_json::to_value(&error).unwrap(),
serde_json::json!({
"type": "error",
"id": "7",
"error": "boom",
}),
);
let done = CliResponse::Done {
id: "7".to_string(),
};
assert_eq!(
serde_json::to_value(&done).unwrap(),
serde_json::json!({"type": "done", "id": "7"}),
);
let item = CliResponse::Item {
id: "7".to_string(),
item: serde_json::value::to_raw_value(
&serde_json::json!({"ok": true}),
)
.unwrap(),
};
let value = serde_json::to_value(&item).unwrap();
assert_eq!(value["type"], "item");
assert_eq!(value["id"], "7");
assert_eq!(value["item"], serde_json::json!({"ok": true}));
let back: CliResponse =
serde_json::from_str(&value.to_string()).unwrap();
assert!(matches!(back, CliResponse::Item { id, .. } if id == "7"));
}
}