Skip to main content

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

1//! `agents mcp 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.mcp.resources.read.Request")]
10pub struct Request {
11    pub path_type: Path,
12    /// Objectiveai response id of the live agent to address. `None` ⇒
13    /// resolved from the caller's contextual agent arguments
14    /// (`OBJECTIVEAI_RESPONSE_ID`); an error if absent there too.
15    #[serde(default, skip_serializing_if = "Option::is_none")]
16    #[schemars(extend("omitempty" = true))]
17    pub response_id: Option<String>,
18    pub params: crate::mcp::resource::ReadResourceRequestParams,
19    #[serde(flatten)]
20    pub base: crate::cli::command::RequestBase,
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
24#[schemars(rename = "cli.command.agents.mcp.resources.read.Path")]
25pub enum Path {
26    #[serde(rename = "agents/mcp/resources/read")]
27    AgentsMcpResourcesRead,
28}
29
30impl CommandRequest for Request {
31    fn request_base(&self) -> &crate::cli::command::RequestBase {
32        &self.base
33    }
34
35    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
36        Some(&mut self.base)
37    }
38}
39
40pub type Response = crate::mcp::resource::ReadResourceResult;
41
42#[derive(clap::Args)]
43#[command(group(clap::ArgGroup::new("params_required").required(true).args(["params"])))]
44pub struct Args {
45    /// Objectiveai response id of the live agent whose MCP aggregation
46    /// to query (the socket at `<state>/socks/<response_id>.sock`).
47    /// Omit to use the invoking agent's own response id (from the
48    /// contextual agent arguments).
49    #[arg(long)]
50    pub response_id: Option<String>,
51    /// MCP `ReadResourceRequestParams` as a JSON string, e.g.
52    /// `{"uri":"..."}`.
53    #[arg(long)]
54    pub params: Option<String>,
55    #[command(flatten)]
56    pub base: crate::cli::command::RequestBaseArgs,
57}
58
59#[derive(clap::Args)]
60#[command(args_conflicts_with_subcommands = true)]
61pub struct Command {
62    #[command(flatten)]
63    pub args: Args,
64    #[command(subcommand)]
65    pub schema: Option<Schema>,
66}
67
68#[derive(clap::Subcommand)]
69pub enum Schema {
70    /// Emit the JSON Schema for this leaf's `Request` type and exit.
71    RequestSchema(request_schema::Args),
72    /// Emit the JSON Schema for this leaf's `Response` type and exit.
73    ResponseSchema(response_schema::Args),
74}
75
76impl TryFrom<Args> for Request {
77    type Error = crate::cli::command::FromArgsError;
78    fn try_from(args: Args) -> Result<Self, Self::Error> {
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::AgentsMcpResourcesRead,
95            response_id: args.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;
133
134/// One `/listen` broadcast run of `agents mcp resources read`: the actual
135/// [`Request`], the producer's
136/// [`AgentArguments`](crate::cli::command::AgentArguments), and the
137/// unary response future. See [`crate::cli::broadcast_listener`].
138#[cfg(feature = "cli-listener")]
139pub struct ListenerExecution {
140    pub request: Request,
141    pub agent_arguments: crate::cli::command::AgentArguments,
142    pub response: crate::cli::broadcast_listener::UnaryResponse<Response>,
143}