Skip to main content

objectiveai_sdk/cli/command/agents/resources/read/
mod.rs

1//! `agents resources read` — run `resources/read` against the
2//! per-`response_id` MCP listener socket and return the MCP
3//! `ReadResourceResult`. `--params` is the MCP `ReadResourceRequestParams`,
4//! supplied as a JSON string (e.g. `{"uri":"..."}`).
5
6use crate::cli::command::CommandRequest;
7
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
9#[schemars(rename = "cli.command.agents.resources.read.Request")]
10pub struct Request {
11    pub path_type: Path,
12    pub response_id: String,
13    pub params: crate::mcp::resource::ReadResourceRequestParams,
14    #[serde(flatten)]
15    pub base: crate::cli::command::RequestBase,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
19#[schemars(rename = "cli.command.agents.resources.read.Path")]
20pub enum Path {
21    #[serde(rename = "agents/resources/read")]
22    AgentsResourcesRead,
23}
24
25impl CommandRequest for Request {
26    fn request_base(&self) -> &crate::cli::command::RequestBase {
27        &self.base
28    }
29
30    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
31        Some(&mut self.base)
32    }
33}
34
35pub type Response = crate::mcp::resource::ReadResourceResult;
36
37#[derive(clap::Args)]
38#[command(group(clap::ArgGroup::new("response_id_required").required(true).args(["response_id"])))]
39#[command(group(clap::ArgGroup::new("params_required").required(true).args(["params"])))]
40pub struct Args {
41    /// Objectiveai response id of the live agent whose MCP aggregation
42    /// to query (the socket at `<state>/socks/<response_id>.sock`).
43    #[arg(long)]
44    pub response_id: Option<String>,
45    /// MCP `ReadResourceRequestParams` as a JSON string, e.g.
46    /// `{"uri":"..."}`.
47    #[arg(long)]
48    pub params: Option<String>,
49    #[command(flatten)]
50    pub base: crate::cli::command::RequestBaseArgs,
51}
52
53#[derive(clap::Args)]
54#[command(args_conflicts_with_subcommands = true)]
55pub struct Command {
56    #[command(flatten)]
57    pub args: Args,
58    #[command(subcommand)]
59    pub schema: Option<Schema>,
60}
61
62#[derive(clap::Subcommand)]
63pub enum Schema {
64    /// Emit the JSON Schema for this leaf's `Request` type and exit.
65    RequestSchema(request_schema::Args),
66    /// Emit the JSON Schema for this leaf's `Response` type and exit.
67    ResponseSchema(response_schema::Args),
68}
69
70impl TryFrom<Args> for Request {
71    type Error = crate::cli::command::FromArgsError;
72    fn try_from(args: Args) -> Result<Self, Self::Error> {
73        let response_id = args.response_id.ok_or_else(|| {
74            crate::cli::command::FromArgsError::path_parse(
75                "response_id",
76                "--response-id is required".to_string(),
77            )
78        })?;
79        let params = {
80            let s = args.params.ok_or_else(|| {
81                crate::cli::command::FromArgsError::path_parse(
82                    "params",
83                    "--params is required".to_string(),
84                )
85            })?;
86            let mut de = serde_json::Deserializer::from_str(&s);
87            serde_path_to_error::deserialize(&mut de)
88                .map_err(|source| crate::cli::command::FromArgsError {
89                    field: "params",
90                    source: source.into(),
91                })?
92        };
93        Ok(Self {
94            path_type: Path::AgentsResourcesRead,
95            response_id,
96            params,
97            base: args.base.into(),
98        })
99    }
100}
101
102#[cfg(feature = "cli-executor")]
103pub async fn execute<E: crate::cli::command::CommandExecutor>(
104    executor: &E,
105    mut request: Request,
106    agent_arguments: Option<&crate::cli::command::AgentArguments>,
107) -> Result<Response, E::Error> {
108    request.base.clear_transform();
109    executor.execute_one(request, agent_arguments).await
110}
111
112#[cfg(feature = "cli-executor")]
113pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
114    executor: &E,
115    mut request: Request,
116    transform: crate::cli::command::Transform,
117    agent_arguments: Option<&crate::cli::command::AgentArguments>,
118) -> Result<serde_json::Value, E::Error> {
119    request.base.set_transform(transform);
120    executor.execute_one(request, agent_arguments).await
121}
122
123#[cfg(feature = "mcp")]
124impl crate::cli::command::CommandResponse for Response {
125    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
126        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
127    }
128}
129
130pub mod request_schema;
131
132pub mod response_schema;