MCPcopy Create free account
hub / github.com/YougLin-dev/amp-server / handle_proxy_request

Method handle_proxy_request

api/src/proxy/service.rs:120–189  ·  view source on GitHub ↗
(
        config: EndpointConfig,
        req: Request,
    )

Source from the content-addressed store, hash-verified

118 }
119
120 async fn handle_proxy_request(
121 config: EndpointConfig,
122 req: Request,
123 ) -> Result<Response, (StatusCode, String)> {
124 let target_url = config.target_url.clone();
125 info!("Forwarding request: {} -> {}", config.path, target_url);
126
127 let client = Client::new();
128 let (parts, body) = req.into_parts();
129
130 // Read request body
131 let body_bytes = match axum::body::to_bytes(body, usize::MAX).await {
132 Ok(bytes) => bytes,
133 Err(e) => {
134 error!("Failed to read request body: {}", e);
135 return Err((StatusCode::BAD_REQUEST, "Unable to read request body".to_string()));
136 }
137 };
138
139 // Build request
140 let method = Method::from_bytes(config.method.as_bytes())
141 .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid HTTP method".to_string()))?;
142
143 let mut req_builder = client
144 .request(method, &target_url)
145 .body(body_bytes);
146
147 // Add forwarded request headers
148 for header_name in &config.forward_request_headers {
149 if let Some(header_value) = parts.headers.get(header_name) {
150 req_builder = req_builder.header(header_name, header_value);
151 }
152 }
153
154 // Add custom request headers
155 for (name, value) in &config.custom_headers {
156 req_builder = req_builder.header(name, value);
157 }
158
159 // Special handling: add auth header for LLM proxy
160 if config.path.contains("llm-proxy") {
161 req_builder = req_builder.header("authorization", format!("Bearer {}", get_amp_api_key()));
162 }
163
164 // Special handling: add Google API key header when proxying Google endpoints
165 if config.path.contains("/api/provider/google/") {
166 if let Some(key) = get_google_api_key() {
167 req_builder = req_builder.header("x-goog-api-key", key);
168 } else {
169 warn!("GOOGLE_API_KEY not set; skipping x-goog-api-key injection for {}", config.path);
170 }
171 }
172
173 // Send request
174 let response = match req_builder.send().await {
175 Ok(resp) => resp,
176 Err(e) => {
177 error!("Failed to forward request: {}", e);

Callers

nothing calls this directly

Calls 1

get_google_api_keyFunction · 0.85

Tested by

no test coverage detected