Skip to content

Commit abf9a62

Browse files
ngxsonggerganovallozaur
authored
server: wrap headers for mcp proxy (#21072)
* server: wrap headers for mcp proxy * Update tools/server/server-cors-proxy.h Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> * fix build * chore: update webui build output * chore: update webui build output --------- Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com>
1 parent 7c20367 commit abf9a62

6 files changed

Lines changed: 35 additions & 5 deletions

File tree

tools/server/public/index.html.gz

10 Bytes
Binary file not shown.

tools/server/server-cors-proxy.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ static server_http_res_ptr proxy_request(const server_http_req & req, std::strin
3232

3333
SRV_INF("proxying %s request to %s://%s:%i%s\n", method.c_str(), parsed_url.scheme.c_str(), parsed_url.host.c_str(), parsed_url.port, parsed_url.path.c_str());
3434

35+
std::map<std::string, std::string> headers;
36+
for (auto [key, value] : req.headers) {
37+
auto new_key = key;
38+
if (string_starts_with(new_key, "X-Proxy-Header-")) {
39+
string_replace_all(new_key, "X-Proxy-Header-", "");
40+
}
41+
headers[new_key] = value;
42+
}
43+
3544
auto proxy = std::make_unique<server_http_proxy>(
3645
method,
3746
parsed_url.scheme,
3847
parsed_url.host,
3948
parsed_url.port,
4049
parsed_url.path,
41-
req.headers,
50+
headers,
4251
req.body,
4352
req.should_stop,
4453
600, // timeout_read (default to 10 minutes)

tools/server/server-http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ using server_http_res_ptr = std::unique_ptr<server_http_res>;
3535

3636
struct server_http_req {
3737
std::map<std::string, std::string> params; // path_params + query_params
38-
std::map<std::string, std::string> headers; // reserved for future use
38+
std::map<std::string, std::string> headers; // used by MCP proxy
3939
std::string path;
4040
std::string query_string; // query parameters string (e.g. "action=save")
4141
std::string body;

tools/server/webui/src/lib/services/mcp.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ import type {
3939
MCPResourceContent,
4040
MCPReadResourceResult
4141
} from '$lib/types';
42-
import { buildProxiedUrl, throwIfAborted, isAbortError, createBase64DataUrl } from '$lib/utils';
42+
import {
43+
buildProxiedUrl,
44+
buildProxiedHeaders,
45+
throwIfAborted,
46+
isAbortError,
47+
createBase64DataUrl
48+
} from '$lib/utils';
4349

4450
interface ToolResultContentItem {
4551
type: string;
@@ -118,7 +124,7 @@ export class MCPService {
118124
const requestInit: RequestInit = {};
119125

120126
if (config.headers) {
121-
requestInit.headers = config.headers;
127+
requestInit.headers = buildProxiedHeaders(config.headers);
122128
}
123129

124130
if (config.credentials) {

tools/server/webui/src/lib/utils/cors-proxy.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ export function buildProxiedUrl(targetUrl: string): URL {
1919
return proxyUrl;
2020
}
2121

22+
/**
23+
* Wrap original headers for proxying through the CORS proxy. This avoids issues with duplicated llama.cpp-specific and target headers when using the CORS proxy.
24+
* @param headers - The original headers to be proxied to target
25+
* @returns List of "wrapped" headers to be sent to the CORS proxy
26+
*/
27+
export function buildProxiedHeaders(headers: Record<string, string>): Record<string, string> {
28+
const proxiedHeaders: Record<string, string> = {};
29+
30+
for (const [key, value] of Object.entries(headers)) {
31+
proxiedHeaders[`X-Proxy-Header-${key}`] = value;
32+
}
33+
34+
return proxiedHeaders;
35+
}
36+
2237
/**
2338
* Get a proxied URL string for use in fetch requests.
2439
* @param targetUrl - The original URL to proxy

tools/server/webui/src/lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export { highlightCode, detectIncompleteCodeBlock, type IncompleteCodeBlock } fr
3838
export { setConfigValue, getConfigValue, configToParameterRecord } from './config-helpers';
3939

4040
// CORS Proxy
41-
export { buildProxiedUrl, getProxiedUrlString } from './cors-proxy';
41+
export { buildProxiedUrl, getProxiedUrlString, buildProxiedHeaders } from './cors-proxy';
4242

4343
// Conversation utilities
4444
export { createMessageCountMap, getMessageCount } from './conversation-utils';

0 commit comments

Comments
 (0)