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(
14        &self,
15        path_params: Path<(String,)>,
16        State(ctx): State<AppContext>,
17        request: Request<Body>,
18    ) -> Response<Body> {
19        let Path((service_name,)) = path_params;
20        let target = ProxyTarget {
21            service_name: &service_name,
22            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_mcp_request_with_path(
32        &self,
33        path_params: Path<(String, String)>,
34        State(ctx): State<AppContext>,
35        request: Request<Body>,
36    ) -> Response<Body> {
37        let Path((service_name, path)) = path_params;
38        let target = ProxyTarget {
39            service_name: &service_name,
40            path: &path,
41            kind: ProxyKind::Mcp,
42        };
43        match self.proxy_request(target, request, ctx).await {
44            Ok(response) => response,
45            Err(e) => e.into_response(),
46        }
47    }
48
49    pub async fn handle_agent_request(
50        &self,
51        path_params: Path<(String,)>,
52        State(ctx): State<AppContext>,
53        request: Request<Body>,
54    ) -> Result<Response<Body>, StatusCode> {
55        let Path((service_name,)) = path_params;
56        let target = ProxyTarget {
57            service_name: &service_name,
58            path: "",
59            kind: ProxyKind::Agent,
60        };
61        self.proxy_request(target, request, ctx)
62            .await
63            .map_err(|e| e.to_status_code())
64    }
65
66    pub async fn handle_agent_request_with_path(
67        &self,
68        path_params: Path<(String, String)>,
69        State(ctx): State<AppContext>,
70        request: Request<Body>,
71    ) -> Result<Response<Body>, StatusCode> {
72        let Path((service_name, path)) = path_params;
73        let target = ProxyTarget {
74            service_name: &service_name,
75            path: &path,
76            kind: ProxyKind::Agent,
77        };
78        self.proxy_request(target, request, ctx)
79            .await
80            .map_err(|e| e.to_status_code())
81    }
82}