fix(mcp): allow browser MCP clients through CORS preflight (#179)

The OPTIONS preflight hardcoded Access-Control-Allow-Headers to just
Content-Type. Any browser MCP client sending mcp-protocol-version (per
the MCP spec) or Authorization / X-API-Key (typical for hosted
deployments) failed the preflight and the browser blocked the actual
request.

Add those three to the allow-list and a 24h Max-Age so browsers don't
re-preflight every call. Adds an integration test that drives the
server with a real preflight request.

Closes #175.
This commit is contained in:
SGavrl authored and GitHub committed 2026-05-24 20:14:01 +00:00
1 parent a3b21bbb70
commit d4d749d850
2 files changed
+92 -1

No files matched your search

+6 -1
View File
@@ -83,10 +83,15 @@ async fn handle_connection(
match method {
"OPTIONS" => {
// mcp-protocol-version is part of the MCP spec, Authorization /
// X-API-Key are common for hosted deployments. Without these
// listed the browser preflight check fails and blocks the actual
// request.
let hdr = "HTTP/1.1 204 No Content\r\n\
Access-Control-Allow-Origin: *\r\n\
Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n\
Access-Control-Allow-Headers: Content-Type\r\n\
Access-Control-Allow-Headers: Content-Type, Authorization, X-API-Key, mcp-protocol-version\r\n\
Access-Control-Max-Age: 86400\r\n\
\r\n";
writer.write_all(hdr.as_bytes()).await?;
}