use crate::config::{InvocationMode, McpConfig};
use rustapi_core::{Request, RequestDispatcher, Response as CoreResponse};
use std::sync::Arc;
#[derive(Clone)]
pub struct RequestInvoker {
dispatcher: Arc<RequestDispatcher>,
config: Arc<McpConfig>,
}
impl std::fmt::Debug for RequestInvoker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RequestInvoker")
.field("mode", &self.config.invocation_mode)
.finish_non_exhaustive()
}
}
impl RequestInvoker {
pub(crate) fn new(dispatcher: RequestDispatcher, config: Arc<McpConfig>) -> Self {
Self {
dispatcher: Arc::new(dispatcher),
config,
}
}
pub async fn invoke(&self, req: Request) -> CoreResponse {
self.dispatcher.dispatch(req).await
}
pub fn should_use(&self) -> bool {
match self.config.invocation_mode {
InvocationMode::Proxy => false,
InvocationMode::InProcess | InvocationMode::Auto => true,
}
}
pub fn state_ref(&self) -> Arc<http::Extensions> {
self.dispatcher.state_ref()
}
}