Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/late/mcp/middleware/tool_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from typing import Any

import sentry_sdk
from fastmcp.exceptions import ValidationError as InvalidArgumentsError
from fastmcp.server.auth import AccessToken
from fastmcp.server.dependencies import get_access_token
from fastmcp.server.middleware import CallNext, Middleware, MiddlewareContext
from mcp.types import CallToolRequestParams, TextContent
from pydantic import ValidationError as PydanticValidationError

from late.client.exceptions import LateAPIError, LateRateLimitError
from late.mcp.auth import token_cache_key
Expand Down Expand Up @@ -90,7 +92,29 @@ def _result_chars(result: Any) -> int:
return sum(len(block.text) for block in content if isinstance(block, TextContent))


def _invalid_fields(error: PydanticValidationError) -> str:
# Field paths and error types only: argument values can carry customer content.
return ",".join(
f"{'.'.join(str(part) for part in detail['loc'])}:{detail['type']}"
for detail in error.errors(include_input=False)
)


def _pydantic_cause(exc: BaseException) -> PydanticValidationError | None:
while exc is not None and not isinstance(exc, PydanticValidationError):
exc = exc.__cause__
return exc


def _error_fields(exc: Exception) -> dict[str, Any]:
validation = _pydantic_cause(exc)
if validation is not None:
return {
"error_type": "InvalidArguments"
if isinstance(exc, InvalidArgumentsError)
else "ValidationError",
"invalid_fields": _invalid_fields(validation),
}
cause = exc.__cause__ if isinstance(exc.__cause__, Exception) else exc
fields: dict[str, Any] = {"error_type": type(cause).__name__}
if isinstance(cause, LateAPIError):
Expand Down
45 changes: 44 additions & 1 deletion tests/test_mcp_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from datetime import datetime
from types import SimpleNamespace
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal

import httpx
import pytest
Expand Down Expand Up @@ -247,6 +247,49 @@ def rejects_response() -> str:
assert issue["exception"]["values"][0]["type"] == "ValidationError"


async def test_rejected_arguments_log_which_fields_failed_without_their_values(
caplog,
) -> None:
caplog.set_level(logging.INFO, logger="late.mcp.tools")
server = _server()

@server.tool
def list_campaigns(status: Literal["active", "paused"], limit: int = 20) -> str:
return f"{status} {limit}"

async with Client(server) as client:
await client.call_tool(
"list_campaigns",
{"status": "secret-customer-text", "limit": "many"},
raise_on_error=False,
)

[line] = _logged(caplog)
assert line["outcome"] == "error"
assert line["error_type"] == "InvalidArguments"
assert line["invalid_fields"] == "status:literal_error,limit:int_parsing"
assert "secret-customer-text" not in json.dumps(line)


async def test_a_response_rejected_by_an_sdk_model_stays_a_validation_error(
caplog,
) -> None:
caplog.set_level(logging.INFO, logger="late.mcp.tools")
server = _server()

@server.tool
def rejects_response() -> str:
AccountsListResponse.model_validate({"accounts": "nope"})
return "unreachable"

async with Client(server) as client:
await client.call_tool("rejects_response", {}, raise_on_error=False)

[line] = _logged(caplog)
assert line["error_type"] == "ValidationError"
assert line["invalid_fields"].startswith("accounts:")


def _proxied_server() -> FastMCP:
server = FastMCP("t", transforms=[BM25SearchTransform(max_results=1)])

Expand Down
Loading