fix(mcp): return fixed client message on sampling model access denial

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yassin 2026-09-16 01:51:29 +00:00
parent 15f2e25e8a
commit b085a3c151
2 changed files with 10 additions and 9 deletions

View file

@ -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}"),

View file

@ -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()