Skip to main content

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

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