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, serde::Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(rename = "mcp.CliResponse")]
pub enum CliResponse {
Ack { id: String },
Item {
id: String,
item: crate::cli::command::ResponseItem,
},
Error { id: String, error: String },
Done { id: String },
}
#[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: crate::cli::command::ResponseItem::Python(
serde_json::json!({"ok": true}),
),
};
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"));
}
}