@@ -233,3 +233,53 @@ def test_configured_server_applies_no_cache_to_served_oauth_discovery_routes(
233233 # Ensure we did not create a shadow route at the wrong path.
234234 wrong_path = client .get ("/.well-known/oauth-protected-resource" )
235235 assert wrong_path .status_code == 404
236+
237+
238+ def test_external_oauth_metadata_matches_mcp_resource_and_challenge (monkeypatch ):
239+ monkeypatch .setenv ("MCP_ENABLE_OAUTH21" , "true" )
240+ monkeypatch .setenv ("GOOGLE_OAUTH_CLIENT_ID" , "dummy-client" )
241+ monkeypatch .setenv ("GOOGLE_OAUTH_CLIENT_SECRET" , "dummy-secret" )
242+ monkeypatch .setenv ("WORKSPACE_MCP_BASE_URI" , "http://localhost" )
243+ monkeypatch .setenv ("WORKSPACE_MCP_PORT" , "8000" )
244+ monkeypatch .setenv ("WORKSPACE_EXTERNAL_URL" , "https://workspace.example.com" )
245+ monkeypatch .setenv ("EXTERNAL_OAUTH21_PROVIDER" , "true" )
246+ monkeypatch .setenv ("WORKSPACE_MCP_STATELESS_MODE" , "true" )
247+
248+ import core .server as core_server
249+ from auth .oauth_config import reload_oauth_config
250+
251+ reload_oauth_config ()
252+ core_server = importlib .reload (core_server )
253+ core_server .set_transport_mode ("streamable-http" )
254+ core_server .configure_server_for_http ()
255+
256+ app = core_server .server .http_app (transport = "streamable-http" , path = "/mcp" )
257+ client = TestClient (app )
258+
259+ protected_resource = client .get ("/.well-known/oauth-protected-resource/mcp" )
260+ assert protected_resource .status_code == 200
261+ assert protected_resource .json ()["resource" ] == "https://workspace.example.com/mcp"
262+
263+ wrong_path = client .get ("/.well-known/oauth-protected-resource" )
264+ assert wrong_path .status_code == 404
265+
266+ challenge = client .post (
267+ "/mcp" ,
268+ headers = {"Accept" : "application/json, text/event-stream" },
269+ json = {
270+ "jsonrpc" : "2.0" ,
271+ "id" : 1 ,
272+ "method" : "initialize" ,
273+ "params" : {
274+ "protocolVersion" : "2025-06-18" ,
275+ "capabilities" : {},
276+ "clientInfo" : {"name" : "test" , "version" : "1" },
277+ },
278+ },
279+ )
280+ assert challenge .status_code == 401
281+ assert (
282+ 'resource_metadata="https://workspace.example.com/'
283+ '.well-known/oauth-protected-resource/mcp"'
284+ in challenge .headers ["www-authenticate" ]
285+ )
0 commit comments