Skip to main content

objectiveai_cli/command/mcp/
mod.rs

1//! `mcp` tier dispatch. Mirrors
2//! `objectiveai-sdk-rs/src/cli/command/mcp/mod.rs` — matches on the
3//! SDK's tier `Request` variants and dispatches to local leaf
4//! `execute`s. All mcp leaves are unary, so every arm wraps a single
5//! value in a one-shot stream.
6
7use std::pin::Pin;
8
9use futures::{Stream, StreamExt};
10use objectiveai_sdk::cli::command::mcp::{Request, Response};
11
12use crate::context::Context;
13use crate::error::Error;
14
15pub mod config;
16pub mod kill;
17pub mod spawn;
18
19type ItemStream = Pin<Box<dyn Stream<Item = Result<Response, Error>> + Send>>;
20
21fn once<T: Send + 'static>(
22    item: Result<T, Error>,
23) -> Pin<Box<dyn Stream<Item = Result<T, Error>> + Send>> {
24    Box::pin(futures::stream::once(async move { item }))
25}
26
27pub async fn execute(ctx: &Context, request: Request) -> Result<ItemStream, Error> {
28    let stream: ItemStream = match request {
29        Request::Config(req) => {
30            let inner = config::execute(ctx, req).await?;
31            Box::pin(inner.map(|r| r.map(Response::Config)))
32        }
33        Request::Kill(req) => {
34            let value = kill::execute(ctx, req).await?;
35            once(Ok(Response::Kill(value)))
36        }
37        Request::KillRequestSchema(req) => {
38            let value = kill::request_schema::execute(ctx, req).await?;
39            once(Ok(Response::KillRequestSchema(value)))
40        }
41        Request::KillResponseSchema(req) => {
42            let value = kill::response_schema::execute(ctx, req).await?;
43            once(Ok(Response::KillResponseSchema(value)))
44        }
45        Request::Spawn(req) => {
46            let value = spawn::execute(ctx, req).await?;
47            once(Ok(Response::Spawn(value)))
48        }
49        Request::SpawnRequestSchema(req) => {
50            let value = spawn::request_schema::execute(ctx, req).await?;
51            once(Ok(Response::SpawnRequestSchema(value)))
52        }
53        Request::SpawnResponseSchema(req) => {
54            let value = spawn::response_schema::execute(ctx, req).await?;
55            once(Ok(Response::SpawnResponseSchema(value)))
56        }
57    };
58    Ok(stream)
59}