Fix unsupported MCP tools param (#8610)

This commit is contained in:
Engel Nyst 2025-05-21 16:41:01 +02:00 committed by GitHub
parent 37e9933092
commit c26ef180f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 23 deletions

View File

@ -10,6 +10,7 @@ if TYPE_CHECKING:
from openhands.events.action import Action
from openhands.llm.llm import ModelResponse
from openhands.llm.llm_utils import check_tools
import openhands.agenthub.codeact_agent.function_calling as codeact_function_calling
from openhands.agenthub.codeact_agent.tools.bash import create_cmd_run_tool
from openhands.agenthub.codeact_agent.tools.browser import BrowserTool
@ -185,26 +186,7 @@ class CodeActAgent(Agent):
params: dict = {
'messages': self.llm.format_messages_for_llm(messages),
}
params['tools'] = self.tools
# Special handling for Gemini model which doesn't support default fields
if self.llm.config.model == 'gemini-2.5-pro-preview-03-25':
logger.info(
f'Removing the default fields from tools for {self.llm.config.model} '
"since it doesn't support them and the request would crash."
)
# prevent mutation of input tools
params['tools'] = copy.deepcopy(params['tools'])
# Strip off default fields that cause errors with gemini-preview
for tool in params['tools']:
if 'function' in tool and 'parameters' in tool['function']:
if 'properties' in tool['function']['parameters']:
for prop_name, prop in tool['function']['parameters'][
'properties'
].items():
if 'default' in prop:
del prop['default']
# log to litellm proxy if possible
params['tools'] = check_tools(self.tools, self.llm.config)
params['extra_body'] = {'metadata': state.to_llm_metadata(agent_name=self.name)}
response = self.llm.completion(**params)
logger.debug(f'Response from LLM: {response}')

View File

@ -279,9 +279,6 @@ class LLM(RetryMixin, DebugMixin):
# Record start time for latency measurement
start_time = time.time()
# we don't support streaming here, thus we get a ModelResponse
logger.debug(
f'LLM: calling litellm completion with model: {self.config.model}, base_url: {self.config.base_url}, args: {args}, kwargs: {kwargs}'
)
resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)
# Calculate and record latency

View File

@ -0,0 +1,44 @@
import copy
from typing import TYPE_CHECKING
from openhands.core.config import LLMConfig
from openhands.core.logger import openhands_logger as logger
if TYPE_CHECKING:
from litellm import ChatCompletionToolParam
def check_tools(
tools: list['ChatCompletionToolParam'], llm_config: LLMConfig
) -> list['ChatCompletionToolParam']:
"""Checks and modifies tools for compatibility with the current LLM."""
# Special handling for Gemini models which don't support default fields and have limited format support
if 'gemini' in llm_config.model.lower():
logger.info(
f'Removing default fields and unsupported formats from tools for Gemini model {llm_config.model} '
"since Gemini models have limited format support (only 'enum' and 'date-time' for STRING types)."
)
# prevent mutation of input tools
checked_tools = copy.deepcopy(tools)
# Strip off default fields and unsupported formats that cause errors with gemini-preview
for tool in checked_tools:
if 'function' in tool and 'parameters' in tool['function']:
if 'properties' in tool['function']['parameters']:
for prop_name, prop in tool['function']['parameters'][
'properties'
].items():
# Remove default fields
if 'default' in prop:
del prop['default']
# Remove format fields for STRING type parameters if the format is unsupported
# Gemini only supports 'enum' and 'date-time' formats for STRING type
if prop.get('type') == 'string' and 'format' in prop:
supported_formats = ['enum', 'date-time']
if prop['format'] not in supported_formats:
logger.info(
f'Removing unsupported format "{prop["format"]}" for STRING parameter "{prop_name}"'
)
del prop['format']
return checked_tools
return tools