Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Fix failing tests and type checking errors
claude[bot]98aecf6701a6
diff --git a/browser_use/mcp/client.py b/browser_use/mcp/client.pyindex cfd93ee4d..b58138462 100644--- a/browser_use/mcp/client.py+++ b/browser_use/mcp/client.py@@ -27,7 +27,7 @@ import loggingimport timefrom typing import Any-from pydantic import BaseModel, Field, create_model, ConfigDict+from pydantic import BaseModel, ConfigDict, Field, create_modelfrom browser_use.agent.views import ActionResultfrom browser_use.controller.registry.service import Registry@@ -286,7 +286,7 @@ class MCPClient:# Create a BaseModel class with proper configurationclass ConfiguredBaseModel(BaseModel):model_config = ConfigDict(extra='forbid', validate_by_name=True, validate_by_alias=True)-+param_model = create_model(f'{action_name}_Params', __base__=ConfiguredBaseModel, **param_fields)else:# No parameters - create empty model@@ -313,7 +313,7 @@ class MCPClient:return ActionResult(error=f"MCP server '{self.server_name}' not connected", success=False)# Convert pydantic model to dict for MCP call- tool_params = params.model_dump()+ tool_params = params.model_dump(exclude_none=True)logger.debug(f"🔧 Calling MCP tool '{tool.name}' with params: {tool_params}")@@ -480,11 +480,11 @@ class MCPClient:# Create nested pydantic model for objects with propertiesnested_fields = {}required_fields = set(schema.get('required', []))-+for prop_name, prop_schema in properties.items():# Recursively process nested propertiesprop_type = self._json_schema_to_python_type(prop_schema, f'{model_name}_{prop_name}')-+# Determine if field is required and handle defaultsif prop_name in required_fields:default = ... # Required field@@ -495,24 +495,24 @@ class MCPClient:default = prop_schema['default']else:default = None-+# Add field with description if availablefield_kwargs = {}if 'description' in prop_schema:field_kwargs['description'] = prop_schema['description']-+nested_fields[prop_name] = (prop_type, Field(default, **field_kwargs))-+# Create a BaseModel class with proper configurationclass ConfiguredBaseModel(BaseModel):model_config = ConfigDict(extra='forbid', validate_by_name=True, validate_by_alias=True)-+try:# Create and return nested pydantic modelreturn create_model(model_name, __base__=ConfiguredBaseModel, **nested_fields)except Exception as e:- logger.error(f"Failed to create nested model {model_name}: {e}")- logger.debug(f"Fields: {nested_fields}")+ logger.error(f'Failed to create nested model {model_name}: {e}')+ logger.debug(f'Fields: {nested_fields}')# Fallback to basic dict if model creation failsreturn dictelse:diff --git a/tests/ci/test_mcp_client.py b/tests/ci/test_mcp_client.pyindex 59c3f430e..3c8af5eef 100644--- a/tests/ci/test_mcp_client.py+++ b/tests/ci/test_mcp_client.py@@ -122,11 +122,11 @@ class MockMCPServer:assert arguments is not Nonetrace = arguments.get('trace', {})window_size = arguments.get('window_size', 10)-+recent_actions = trace.get('recent_actions', [])current_context = trace.get('current_context', 'unknown')goal = trace.get('goal', 'no goal')-+result = f'Processed trace update: {len(recent_actions)} actions, goal: {goal}, context: {current_context}, window: {window_size}'else:@@ -617,12 +617,14 @@ async def test_mcp_nested_object_parameters(test_mcp_server_script):# Verify the trace parameter has nested structuretrace_field = param_model.model_fields['trace']trace_annotation = trace_field.annotation-+# Should be a nested pydantic model, not just dictassert trace_annotation is not dict-+ assert trace_annotation is not None+# Verify nested model has the expected fieldsnested_model = trace_annotation+ assert hasattr(nested_model, 'model_fields')nested_fields = nested_model.model_fieldsassert 'recent_actions' in nested_fieldsassert 'current_context' in nested_fields@@ -634,26 +636,26 @@ async def test_mcp_nested_object_parameters(test_mcp_server_script):assert not nested_fields['current_context'].is_required()# Test creating an instance with nested parameters- trace_data = nested_model(- recent_actions=['click', 'type', 'navigate'],- current_context='web page',- goal='complete form'- )+ assert callable(nested_model)+ trace_data = nested_model(recent_actions=['click', 'type', 'navigate'], current_context='web page', goal='complete form')# Test the full parameter model+ assert callable(param_model)params = param_model(trace=trace_data, window_size=5)-+# Verify the parameter structure- assert params.trace.recent_actions == ['click', 'type', 'navigate']- assert params.trace.current_context == 'web page'- assert params.trace.goal == 'complete form'- assert params.window_size == 5+ assert hasattr(params, 'trace') and hasattr(params, 'window_size')+ trace_obj = getattr(params, 'trace')+ assert trace_obj.recent_actions == ['click', 'type', 'navigate']+ assert trace_obj.current_context == 'web page'+ assert trace_obj.goal == 'complete form'+ assert getattr(params, 'window_size') == 5# Test calling the tool with nested parametersresult = await trace_action.function(params=params)# Verify the result contains the nested data- assert result.success+ assert result.success is not Falseassert 'Processed trace update' in result.extracted_contentassert '3 actions' in result.extracted_contentassert 'goal: complete form' in result.extracted_content@@ -666,10 +668,7 @@ async def test_mcp_nested_object_parameters(test_mcp_server_script):assert 'window: 10' in result_default.extracted_content# Test with minimal required parameters- minimal_trace = nested_model(- recent_actions=['action1', 'action2'],- goal='test goal'- )+ minimal_trace = nested_model(recent_actions=['action1', 'action2'], goal='test goal')params_minimal = param_model(trace=minimal_trace)result_minimal = await trace_action.function(params=params_minimal)assert 'goal: test goal' in result_minimal.extracted_content