Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mail_mcp (1.0.0)
mail_mcp (1.0.1)
aws-sdk-s3 (~> 1.0)
base64 (~> 0.2)
json-jwt (~> 1.16)
Expand Down
12 changes: 8 additions & 4 deletions lib/mail_mcp/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class App < Sinatra::Base

def resolve_mcp_context
auth = request.env["HTTP_AUTHORIZATION"]
return [nil, nil] unless auth&.start_with?("Bearer ")
return [nil, unauthorized_response("missing bearer token")] unless auth&.start_with?("Bearer ")

creds = JwtService.verify(auth[7..])
context = CredentialContext.new(
Expand All @@ -161,12 +161,16 @@ def resolve_mcp_context
)
[context, nil]
rescue JwtService::Error => e
error = [
[nil, unauthorized_response(e.message)]
end

# 401 challenge that tells an MCP client where to run the OAuth flow.
def unauthorized_response(description)
[
401,
{ "Content-Type" => "application/json", "WWW-Authenticate" => mcp_www_authenticate },
[JSON.generate({ error: "invalid_token", error_description: e.message })]
[JSON.generate({ error: "invalid_token", error_description: description })]
]
[nil, error]
end

def exchange_code(params, client_id)
Expand Down
10 changes: 10 additions & 0 deletions spec/mail_mcp/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ def mcp_request(method, params = {}, id: 1)
body = JSON.parse(last_response.body)
expect(body["error"]).to eq("invalid_token")
end

it "returns a 401 challenge when the Authorization header is missing" do
post "/mcp", mcp_request("tools/list"), mcp_headers.except("HTTP_AUTHORIZATION")

expect(last_response.status).to eq(401)
expect(last_response.headers["WWW-Authenticate"]).to include("resource_metadata=")
body = JSON.parse(last_response.body)
expect(body["error"]).to eq("invalid_token")
expect(body["error_description"]).to eq("missing bearer token")
end
end

describe "GET /health" do
Expand Down
Loading