From f539d776b88f2ae4fc0f5862402651224eed38af Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:54:57 -0700 Subject: [PATCH] fix(token_counter): count the MCP tool listing block Anthropic returns with MCP tool calls --- litellm/litellm_core_utils/token_counter.py | 1 + .../litellm_core_utils/test_token_counter.py | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index fa679b67e0e..e6185d06852 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -1686,6 +1686,7 @@ _SERVER_TOOL_BLOCK_TYPES: Final = frozenset( "text_editor_code_execution_tool_result", "tool_search_tool_result", "mcp_tool_result", + "mcp_tool_listing", "advisor_tool_result", "container_upload", "connector_text", diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index 872d97fab15..6c31c56d413 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -2242,6 +2242,21 @@ _MCP_TOOL_RESULT: Final = { "is_error": False, "content": [{"type": "text", "text": "Issue 42 is still open."}], } +_MCP_TOOL_LISTING: Final = { + "type": "mcp_tool_listing", + "mcp_server_name": "tracker", + "tools": [ + { + "name": "search_issues", + "description": "Search the issue tracker.", + "input_schema": { + "type": "object", + "properties": {"query": {"type": "string", "description": "Words to search for."}}, + "required": ["query"], + }, + } + ], +} _ADVISOR_TOOL_RESULT: Final = { "type": "advisor_tool_result", "tool_use_id": "srvtoolu_01Wq8GYYv1dHrKzZx4k8Aa2m", @@ -2325,6 +2340,11 @@ _CONNECTOR_TEXT: Final = { ), pytest.param(_tool_search_tool_result(), ("get_weather",), id="tool_search_tool_result"), pytest.param(_MCP_TOOL_RESULT, ("Issue 42 is still open.",), id="mcp_tool_result"), + pytest.param( + _MCP_TOOL_LISTING, + ("tracker", "search_issues", "Search the issue tracker.", "Words to search for.", "query"), + id="mcp_tool_listing", + ), pytest.param(_ADVISOR_TOOL_RESULT, ("Check the encrypted fields first.",), id="advisor_tool_result"), pytest.param(_CONTAINER_UPLOAD, (), id="container_upload"), pytest.param(_CONNECTOR_TEXT, ("Ticket 42 was closed on Monday.",), id="connector_text"),