Skip to main content

objectiveai_sdk/cli/command/mcp/config/
mod.rs

1pub mod address;
2pub mod get;
3pub mod port;
4
5#[derive(clap::Subcommand)]
6pub enum Command {
7    Get(get::Command),
8    Address {
9        #[command(subcommand)]
10        command: address::Command,
11    },
12    Port {
13        #[command(subcommand)]
14        command: port::Command,
15    },
16}
17
18#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
19#[serde(untagged)]
20#[schemars(rename = "cli.command.mcp.config.Request")]
21pub enum Request {
22    #[schemars(title = "Get")]
23    Get(get::Request),
24    #[schemars(title = "GetRequestSchema")]
25    GetRequestSchema(get::request_schema::Request),
26    #[schemars(title = "GetResponseSchema")]
27    GetResponseSchema(get::response_schema::Request),
28    #[schemars(title = "Address")]
29    Address(address::Request),
30    #[schemars(title = "Port")]
31    Port(port::Request),
32}
33
34// Exempt from json-schema coverage: tier aggregate (see the root
35// `ResponseItem` in command.rs - TS7056).
36#[objectiveai_sdk_macros::json_schema_ignore]
37#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
38#[schemars(rename = "cli.command.mcp.config.Response")]
39#[serde(untagged)]
40pub enum Response {
41    #[schemars(title = "Get")]
42    Get(get::Response),
43    #[schemars(title = "GetRequestSchema")]
44    GetRequestSchema(get::request_schema::Response),
45    #[schemars(title = "GetResponseSchema")]
46    GetResponseSchema(get::response_schema::Response),
47    #[schemars(title = "Address")]
48    Address(address::Response),
49    #[schemars(title = "Port")]
50    Port(port::Response),
51}
52
53#[cfg(feature = "mcp")]
54impl crate::cli::command::CommandResponse for Response {
55    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
56        match self {
57            Response::Get(v) => v.into_mcp(),
58            Response::GetRequestSchema(v) => v.into_mcp(),
59            Response::GetResponseSchema(v) => v.into_mcp(),
60            Response::Address(v) => v.into_mcp(),
61            Response::Port(v) => v.into_mcp(),
62        }
63    }
64}
65
66impl TryFrom<Command> for Request {
67    type Error = crate::cli::command::FromArgsError;
68    fn try_from(command: Command) -> Result<Self, Self::Error> {
69        match command {
70            Command::Get(cmd) => match cmd.schema {
71                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
72                Some(get::Schema::RequestSchema(args)) =>
73                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
74                Some(get::Schema::ResponseSchema(args)) =>
75                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
76            },
77            Command::Address { command } =>
78                Ok(Request::Address(address::Request::try_from(command)?)),
79            Command::Port { command } =>
80                Ok(Request::Port(port::Request::try_from(command)?)),
81        }
82    }
83}
84
85impl crate::cli::command::CommandRequest for Request {
86    fn request_base(&self) -> &crate::cli::command::RequestBase {
87        match self {
88            Request::Get(inner) => inner.request_base(),
89            Request::GetRequestSchema(inner) => inner.request_base(),
90            Request::GetResponseSchema(inner) => inner.request_base(),
91            Request::Address(inner) => inner.request_base(),
92            Request::Port(inner) => inner.request_base(),
93        }
94    }
95
96    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
97        match self {
98            Request::Get(inner) => inner.request_base_mut(),
99            Request::GetRequestSchema(inner) => inner.request_base_mut(),
100            Request::GetResponseSchema(inner) => inner.request_base_mut(),
101            Request::Address(inner) => inner.request_base_mut(),
102            Request::Port(inner) => inner.request_base_mut(),
103        }
104    }
105}
106
107#[cfg(feature = "cli-executor")]
108pub async fn execute<E: crate::cli::command::CommandExecutor>(
109    executor: &E,
110    request: Request,
111
112        agent_arguments: Option<&crate::cli::command::AgentArguments>,
113    ) -> Result<
114    std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>>,
115    E::Error,
116> {
117    use futures::StreamExt;
118    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>> =
119        match request {
120            Request::Get(req) => {
121                let value = get::execute(executor, req, agent_arguments).await?;
122                Box::pin(crate::cli::command::StreamOnce::new(Ok(
123                    Response::Get(value),
124                )))
125            }
126            Request::GetRequestSchema(req) => {
127                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
128                Box::pin(crate::cli::command::StreamOnce::new(Ok(
129                    Response::GetRequestSchema(value),
130                )))
131            }
132            Request::GetResponseSchema(req) => {
133                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
134                Box::pin(crate::cli::command::StreamOnce::new(Ok(
135                    Response::GetResponseSchema(value),
136                )))
137            }
138            Request::Address(req) => {
139                let inner = address::execute(executor, req, agent_arguments).await?;
140                Box::pin(inner.map(|r| r.map(Response::Address)))
141            }
142            Request::Port(req) => {
143                let inner = port::execute(executor, req, agent_arguments).await?;
144                Box::pin(inner.map(|r| r.map(Response::Port)))
145            }
146        };
147    Ok(stream)
148}
149
150#[cfg(feature = "cli-executor")]
151pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
152    executor: &E,
153    request: Request,
154    transform: crate::cli::command::Transform,
155
156        agent_arguments: Option<&crate::cli::command::AgentArguments>,
157    ) -> Result<
158    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
159    E::Error,
160> {
161    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
162        match request {
163            Request::Get(req) => {
164                let value = get::execute_transform(executor, req, transform, agent_arguments).await?;
165                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
166            }
167            Request::GetRequestSchema(req) => {
168                let value = get::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
169                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
170            }
171            Request::GetResponseSchema(req) => {
172                let value = get::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
173                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
174            }
175            Request::Address(req) => {
176                let inner = address::execute_transform(executor, req, transform, agent_arguments).await?;
177                Box::pin(inner)
178            }
179            Request::Port(req) => {
180                let inner = port::execute_transform(executor, req, transform, agent_arguments).await?;
181                Box::pin(inner)
182            }
183        };
184    Ok(stream)
185}