Skip to content

Commit d7831b4

Browse files
committed
feat(mcp): enhance POST request handling with URL normalization
1 parent b3fda77 commit d7831b4

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

cloudflare-mcp/src/index.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,49 @@ export default {
2727
});
2828
}
2929

30-
const newRequest = new Request(
31-
request.url.replace("/mcp", "") || "/",
32-
request,
33-
);
30+
const newUrl = request.url.replace("/mcp", "") || "/";
31+
let newRequest = request;
32+
33+
if (request.method === "POST") {
34+
try {
35+
const body = await request.json();
36+
37+
const normalizeUrl = (url) => {
38+
let normalized = url;
39+
try {
40+
normalized = new URL(url).href;
41+
} catch {
42+
// Already a relative path; leave as-is.
43+
}
44+
return normalized.replace(/\/+$/, "");
45+
};
46+
47+
// Top-level url field (e.g. some direct requests)
48+
if (body?.url && typeof body.url === "string") {
49+
body.url = normalizeUrl(body.url);
50+
}
51+
52+
// Nested url inside tools/call → docs_fetch arguments
53+
if (
54+
body?.method === "tools/call" &&
55+
body?.params?.arguments?.url &&
56+
typeof body.params.arguments.url === "string"
57+
) {
58+
body.params.arguments.url = normalizeUrl(body.params.arguments.url);
59+
}
60+
61+
newRequest = new Request(newUrl, {
62+
method: "POST",
63+
headers: request.headers,
64+
body: JSON.stringify(body),
65+
});
66+
} catch {
67+
// Non-JSON payloads should continue to pass through unchanged.
68+
newRequest = new Request(newUrl, request);
69+
}
70+
} else {
71+
newRequest = new Request(newUrl, request);
72+
}
3473

3574
return cachedHandler(newRequest);
3675
},

0 commit comments

Comments
 (0)