Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Fix failing tests and type checking errors

claude[bot]98aecf6701a6
diff --git a/browser_use/mcp/client.py b/browser_use/mcp/client.py
index cfd93ee4d..b58138462 100644
--- a/browser_use/mcp/client.py
+++ b/browser_use/mcp/client.py
@@ -27,7 +27,7 @@ import logging
import time
from typing import Any
-from pydantic import BaseModel, Field, create_model, ConfigDict
+from pydantic import BaseModel, ConfigDict, Field, create_model
from browser_use.agent.views import ActionResult
from browser_use.controller.registry.service import Registry
@@ -286,7 +286,7 @@ class MCPClient:
# Create a BaseModel class with proper configuration
class 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 properties
nested_fields = {}
required_fields = set(schema.get('required', []))
-
+
for prop_name, prop_schema in properties.items():
# Recursively process nested properties
prop_type = self._json_schema_to_python_type(prop_schema, f'{model_name}_{prop_name}')
-
+
# Determine if field is required and handle defaults
if 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 available
field_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 configuration
class ConfiguredBaseModel(BaseModel):
model_config = ConfigDict(extra='forbid', validate_by_name=True, validate_by_alias=True)
-
+
try:
# Create and return nested pydantic model
return 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 fails
return dict
else:
diff --git a/tests/ci/test_mcp_client.py b/tests/ci/test_mcp_client.py
index 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 None
trace = 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 structure
trace_field = param_model.model_fields['trace']
trace_annotation = trace_field.annotation
-
+
# Should be a nested pydantic model, not just dict
assert trace_annotation is not dict
-
+ assert trace_annotation is not None
+
# Verify nested model has the expected fields
nested_model = trace_annotation
+ assert hasattr(nested_model, 'model_fields')
nested_fields = nested_model.model_fields
assert 'recent_actions' in nested_fields
assert '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 parameters
result = await trace_action.function(params=params)
# Verify the result contains the nested data
- assert result.success
+ assert result.success is not False
assert 'Processed trace update' in result.extracted_content
assert '3 actions' in result.extracted_content
assert '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