diff --git a/litellm/proxy/_experimental/mcp_server/sampling_handler.py b/litellm/proxy/_experimental/mcp_server/sampling_handler.py index 6072e747bfe..fec2a1f9ee6 100644 --- a/litellm/proxy/_experimental/mcp_server/sampling_handler.py +++ b/litellm/proxy/_experimental/mcp_server/sampling_handler.py @@ -885,13 +885,14 @@ async def _check_model_access(model: str, user_api_key_auth: "UserAPIKeyAuth | N ) return None except Exception as access_err: - verbose_logger.warning( - "MCP sampling: model access denied for model=%s: %s", - model, - access_err.sanitized_internal_message() - if isinstance(access_err, ModelAccessDeniedProxyException) - else access_err, - ) + if isinstance(access_err, ModelAccessDeniedProxyException): + verbose_logger.warning( + "MCP sampling: model access denied for model=%s: %s", + model, + access_err.sanitized_internal_message(), + ) + return ErrorData(code=-1, message=access_err.message) + verbose_logger.warning("MCP sampling: model access denied for model=%s: %s", model, access_err) return ErrorData( code=-1, message=(f"Model access denied: the API key is not authorized to use model '{model}'. {access_err}"), diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py index 7eebf1eb436..7c5320ed4f4 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_sampling_model_access.py @@ -140,6 +140,7 @@ class TestCheckModelAccess: @pytest.mark.asyncio async def test_should_log_internal_denial_reason_and_hide_allowlist_from_client(self, caplog): from litellm.proxy._types import UserAPIKeyAuth + from litellm.proxy.auth.model_access_denied import model_access_denied_client_message auth = UserAPIKeyAuth(api_key="sk-test-key", models=["gpt-3.5-turbo"]) @@ -147,8 +148,7 @@ class TestCheckModelAccess: result = await _check_model_access("gpt-4o\r\nforged", user_api_key_auth=auth) assert result is not None - assert "gpt-4o\r\nforged" in result.message - assert "gpt-3.5-turbo" not in result.message + assert result.message == model_access_denied_client_message(model="gpt-4o\r\nforged") denial_records = [r for r in caplog.records if "gpt-3.5-turbo" in r.getMessage()] assert len(denial_records) == 1 assert "Tried to access gpt-4oforged" in denial_records[0].getMessage()