mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(mcp): log negotiated-version request rejections
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
351151fbac
commit
1c8bfd3cc0
3 changed files with 29 additions and 2 deletions
|
|
@ -17,6 +17,7 @@ import anyio
|
|||
from fastapi import HTTPException
|
||||
from mcp import MCPError, ReadResourceResult, Resource
|
||||
from mcp.types import (
|
||||
METHOD_NOT_FOUND,
|
||||
CallToolRequest,
|
||||
CallToolRequestParams,
|
||||
CallToolResult,
|
||||
|
|
@ -2817,7 +2818,7 @@ async def log_mcp_sdk_rejection(
|
|||
from litellm.proxy.proxy_server import proxy_logging_obj
|
||||
from litellm.utils import Rules, function_setup
|
||||
|
||||
message: Final = "Method not found" if error_code == -32601 else "Invalid request parameters"
|
||||
message: Final = "Method not found" if error_code == METHOD_NOT_FOUND else "Invalid request parameters"
|
||||
rejection: Final = {
|
||||
"method": "".join(char for char in method[:256] if char.isprintable()),
|
||||
"request_id": (
|
||||
|
|
|
|||
|
|
@ -549,6 +549,7 @@ if MCP_AVAILABLE:
|
|||
ListToolsRequest,
|
||||
ReadResourceRequest,
|
||||
)
|
||||
from mcp_types.methods import CLIENT_REQUESTS, SPEC_CLIENT_METHODS
|
||||
|
||||
from litellm.proxy.mcp_server import operations
|
||||
from litellm.proxy.mcp_server.contracts import OperationContext
|
||||
|
|
@ -660,7 +661,10 @@ if MCP_AVAILABLE:
|
|||
return METHOD_NOT_FOUND
|
||||
return None
|
||||
if not isinstance(error, ValidationError):
|
||||
return None
|
||||
unavailable_for_version: Final = (
|
||||
ctx.method in SPEC_CLIENT_METHODS and (ctx.method, ctx.protocol_version) not in CLIENT_REQUESTS
|
||||
)
|
||||
return METHOD_NOT_FOUND if error.code == METHOD_NOT_FOUND and unavailable_for_version else None
|
||||
try:
|
||||
entry.params_type.model_validate({} if ctx.params is None else ctx.params, by_name=False)
|
||||
except ValidationError:
|
||||
|
|
|
|||
|
|
@ -102,6 +102,8 @@ def sdk_rejection_logger() -> Generator[SDKRejectionLogger, None, None]:
|
|||
("logging/setLevel", {"level": 123}, -32602, "Invalid request parameters"),
|
||||
("logging/setLevel", {"level": "info"}, -32601, "Method not found"),
|
||||
("completion/complete", {}, -32602, "Invalid request parameters"),
|
||||
("server/discover", {}, -32601, "Method not found"),
|
||||
("subscriptions/listen", {}, -32601, "Method not found"),
|
||||
],
|
||||
)
|
||||
async def test_sdk_rejection_emits_one_redacted_zero_cost_failure(
|
||||
|
|
@ -201,6 +203,26 @@ async def test_sdk_rejection_sync_logging_allows_concurrent_ping(
|
|||
assert sdk_rejection_logger.pre_calls == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handler_method_not_found_does_not_emit_sdk_rejection(
|
||||
sdk_rejection_logger: SDKRejectionLogger,
|
||||
) -> None:
|
||||
context: Final = ServerRequestContext(
|
||||
session=MagicMock(spec=ServerSession),
|
||||
lifespan_context=None,
|
||||
protocol_version="2025-11-25",
|
||||
method="tools/list",
|
||||
params={},
|
||||
request_id="handler-error",
|
||||
)
|
||||
handler_error: Final = MCPError(code=-32601, message="Handler rejected the request")
|
||||
with pytest.raises(MCPError) as rejected:
|
||||
await _log_sdk_rejection(context, AsyncMock(side_effect=handler_error))
|
||||
assert rejected.value is handler_error
|
||||
assert sdk_rejection_logger.failures == []
|
||||
assert sdk_rejection_logger.post_failures == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sdk_initialization_errors_are_not_logged_as_tool_rejections(
|
||||
sdk_rejection_logger: SDKRejectionLogger,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue