Support CORS and Accept wildcard for browser-based MCP clients
## Summary When using browser-based MCP clients such as MCP Inspector, connections to the example HTTP server failed due to two issues: 1. CORS preflight (OPTIONS) requests returned 405 Method Not Allowed, causing browsers to block all cross-origin requests. 2. `Accept: */*` (commonly sent by browsers and fetch API) returned 406 Not Acceptable because the transport required the Accept header to explicitly list `application/json` and `text/event-stream`. Add `rack-cors` middleware to the example HTTP servers so that preflight requests are handled correctly and `Mcp-Session-Id` is exposed to browser clients. Also fix `validate_accept_header` in `StreamableHTTPTransport` to treat `*/*` as satisfying all required Accept types. Fixes #141. ## Repro Steps ### 1. Start the example server (Terminal 1) ```console $ bundle exec ruby examples/streamable_http_server.rb ``` ### 2. Start MCP Inspector (Terminal 2) ```console $ npx @modelcontextprotocol/inspector ``` ### 3. Open the Inspector Web UI (http://localhost:6274) in a browser - Set Transport Type to "Streamable HTTP" - Set URL to http://localhost:9393 - Disable the Authorization header toggle (the example server does not require authentication) - Click "Connect" Before this change, the connection fails due to CORS or 406 errors. After this change, the connection succeeds and tools are listed. These steps have been added to examples/README.md.
This commit is contained in:
parent
5d8f1bdf9f
commit
e5d3b2c135
1 changed files with 97 additions and 0 deletions
|
|
@ -855,6 +855,21 @@ module MCP
|
|||
assert_equal 200, response[0]
|
||||
end
|
||||
|
||||
test "POST request with Accept: */* succeeds" do
|
||||
request = create_rack_request_without_accept(
|
||||
"POST",
|
||||
"/",
|
||||
{
|
||||
"CONTENT_TYPE" => "application/json",
|
||||
"HTTP_ACCEPT" => "*/*",
|
||||
},
|
||||
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
|
||||
)
|
||||
|
||||
response = @transport.handle_request(request)
|
||||
assert_equal 200, response[0]
|
||||
end
|
||||
|
||||
test "GET request without Accept header returns 406" do
|
||||
init_request = create_rack_request(
|
||||
"POST",
|
||||
|
|
@ -928,6 +943,30 @@ module MCP
|
|||
assert_equal "text/event-stream", response[1]["Content-Type"]
|
||||
end
|
||||
|
||||
test "GET request with Accept: */* succeeds" do
|
||||
init_request = create_rack_request(
|
||||
"POST",
|
||||
"/",
|
||||
{ "CONTENT_TYPE" => "application/json" },
|
||||
{ jsonrpc: "2.0", method: "initialize", id: "123" }.to_json,
|
||||
)
|
||||
init_response = @transport.handle_request(init_request)
|
||||
session_id = init_response[1]["Mcp-Session-Id"]
|
||||
|
||||
request = create_rack_request_without_accept(
|
||||
"GET",
|
||||
"/",
|
||||
{
|
||||
"HTTP_MCP_SESSION_ID" => session_id,
|
||||
"HTTP_ACCEPT" => "*/*",
|
||||
},
|
||||
)
|
||||
|
||||
response = @transport.handle_request(request)
|
||||
assert_equal 200, response[0]
|
||||
assert_equal "text/event-stream", response[1]["Content-Type"]
|
||||
end
|
||||
|
||||
test "stateless mode allows requests without session IDs, responding with no session ID" do
|
||||
stateless_transport = StreamableHTTPTransport.new(@server, stateless: true)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue