mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(mcp): log SSE protocol rejections
This commit is contained in:
parent
1b70439c41
commit
ac238fd9ff
2 changed files with 72 additions and 2 deletions
|
|
@ -3791,6 +3791,26 @@ if MCP_AVAILABLE:
|
|||
except Exception as log_exc: # noqa: BLE001 # best-effort: logging must never break the MCP response
|
||||
verbose_logger.debug("MCP protocol-error logging failed (continuing): %s", log_exc)
|
||||
|
||||
def _parse_jsonrpc_error_response_for_logging(
|
||||
raw_body: bytes,
|
||||
) -> _JSONRPCErrorEnvelopeForLogging | None:
|
||||
stripped_body: Final = raw_body.lstrip()
|
||||
if stripped_body.startswith(b"{"):
|
||||
json_body: Final = stripped_body
|
||||
else:
|
||||
data_lines: Final = tuple(
|
||||
line.removeprefix(b"data:").lstrip()
|
||||
for line in raw_body.splitlines()
|
||||
if line.startswith(b"data:")
|
||||
)
|
||||
if len(data_lines) != 1:
|
||||
return None
|
||||
json_body = data_lines[0]
|
||||
try:
|
||||
return _JSONRPC_ERROR_FOR_LOGGING_ADAPTER.validate_json(json_body)
|
||||
except ValidationError:
|
||||
return None
|
||||
|
||||
def _wrap_send_for_protocol_error_logging(
|
||||
inner_send: Send,
|
||||
request_method: str | None,
|
||||
|
|
@ -3834,7 +3854,9 @@ if MCP_AVAILABLE:
|
|||
return
|
||||
if b'"error"' not in raw_body or b'"result"' in raw_body:
|
||||
return
|
||||
payload: Final = _JSONRPC_ERROR_FOR_LOGGING_ADAPTER.validate_json(raw_body)
|
||||
payload: Final = _parse_jsonrpc_error_response_for_logging(raw_body)
|
||||
if payload is None:
|
||||
return
|
||||
already_logged["done"] = True
|
||||
await _log_mcp_protocol_rejection(
|
||||
request_method=request_method,
|
||||
|
|
@ -3845,7 +3867,7 @@ if MCP_AVAILABLE:
|
|||
raw_headers=raw_headers,
|
||||
start_time=start_time,
|
||||
)
|
||||
except (ValidationError, TypeError, ValueError):
|
||||
except (TypeError, ValueError):
|
||||
# Non-JSON / streaming chunk — nothing to log here.
|
||||
return
|
||||
except Exception as wrap_exc: # noqa: BLE001 # best-effort: never break the response on logging
|
||||
|
|
|
|||
|
|
@ -448,6 +448,7 @@ from datetime import datetime
|
|||
from litellm.proxy._experimental.mcp_server.server import (
|
||||
_jsonrpc_rejection_reason,
|
||||
_log_mcp_protocol_rejection,
|
||||
_parse_jsonrpc_error_response_for_logging,
|
||||
_parse_jsonrpc_request_for_logging,
|
||||
_wrap_send_for_protocol_error_logging,
|
||||
)
|
||||
|
|
@ -1029,6 +1030,53 @@ def test_parse_jsonrpc_request_for_logging_truncated_or_non_json():
|
|||
assert _parse_jsonrpc_request_for_logging(b'"a string"') == (None, {}, None)
|
||||
|
||||
|
||||
def test_parse_jsonrpc_error_response_for_logging_accepts_sse():
|
||||
payload = _parse_jsonrpc_error_response_for_logging(
|
||||
b'event: message\r\ndata: {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"bad"}}\r\n\r\n'
|
||||
)
|
||||
assert payload is not None
|
||||
assert payload["error"]["code"] == -32602
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mcp_protocol_sse_rejection_emits_slo():
|
||||
litellm.logging_callback_manager._reset_all_callbacks()
|
||||
capture = _ProtocolRejectionLogger()
|
||||
litellm.callbacks = [capture]
|
||||
|
||||
async def fake_send(message):
|
||||
pass
|
||||
|
||||
try:
|
||||
wrapped = _wrap_send_for_protocol_error_logging(
|
||||
fake_send,
|
||||
request_method="tools/not-real",
|
||||
params={},
|
||||
request_id=1,
|
||||
user_api_key_auth=UserAPIKeyAuth(api_key="test", user_id="u1"),
|
||||
raw_headers={},
|
||||
start_time=datetime.now(),
|
||||
)
|
||||
await wrapped(
|
||||
{
|
||||
"type": "http.response.body",
|
||||
"body": (
|
||||
b'event: message\r\ndata: {"jsonrpc":"2.0","id":1,'
|
||||
b'"error":{"code":-32602,"message":"Invalid request parameters"}}\r\n\r\n'
|
||||
),
|
||||
}
|
||||
)
|
||||
await asyncio.sleep(1)
|
||||
finally:
|
||||
litellm.callbacks = []
|
||||
|
||||
payloads = [payload for payload in capture.failure_payloads if isinstance(payload, dict)]
|
||||
assert len(payloads) == 1
|
||||
spend_meta = (payloads[0].get("metadata") or {}).get("spend_logs_metadata") or {}
|
||||
assert spend_meta.get("mcp_operation") == "tools/not-real"
|
||||
assert spend_meta.get("rejection_reason") == "malformed_params"
|
||||
|
||||
|
||||
_QUEUED_LOGGING_OUTLIVES_TEST = '''
|
||||
import time
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue