Skip to main content

systemprompt_api/services/proxy/engine/
handlers.rs

1//! Axum handler adapters for the MCP and agent proxy routes, each delegating
2//! to [`ProxyEngine::proxy_request`].
3
4use axum::body::Body;
5use axum::extract::{Path, Request, State};
6use axum::http::StatusCode;
7use axum::response::{IntoResponse, Response};
8use systemprompt_runtime::AppContext;
9
10use super::{ProxyEngine, ProxyKind, ProxyTarget};
11
12impl ProxyEngine {
13    pub async fn handle_mcp_request_with_path(
14        &self,
15        path_params: Path<(String, String)>,
16        State(ctx): State<AppContext>,
17        request: Request<Body>,
18    ) -> Response<Body> {
19        let Path((service_name, path)) = path_params;
20        let target = ProxyTarget {
21            service_name: &service_name,
22            path: &path,
23            kind: ProxyKind::Mcp,
24        };
25        match self.proxy_request(target, request, ctx).await {
26            Ok(response) => response,
27            Err(e) => e.into_response(),
28        }
29    }
30
31    pub async fn handle_agent_request(
32        &self,
33        path_params: Path<(String,)>,
34        State(ctx): State<AppContext>,
35        request: Request<Body>,
36    ) -> Result<Response<Body>, StatusCode> {
37        let Path((service_name,)) = path_params;
38        let target = ProxyTarget {
39            service_name: &service_name,
40            path: "",
41            kind: ProxyKind::Agent,
42        };
43        self.proxy_request(target, request, ctx)
44            .await
45            .map_err(|e| e.to_status_code())
46    }
47
48    pub async fn handle_agent_request_with_path(
49        &self,
50        path_params: Path<(String, String)>,
51        State(ctx): State<AppContext>,
52        request: Request<Body>,
53    ) -> Result<Response<Body>, StatusCode> {
54        let Path((service_name, path)) = path_params;
55        let target = ProxyTarget {
56            service_name: &service_name,
57            path: &path,
58            kind: ProxyKind::Agent,
59        };
60        self.proxy_request(target, request, ctx)
61            .await
62            .map_err(|e| e.to_status_code())
63    }
64}