test_mcp_server / tools /place_tool.py
SrikanthNagelli's picture
initial commit
b0979b9
"""
Place/hotel search tool for MCP server.
"""
from smolagents import Tool
from typing import Optional, Dict, Any
from services.place_service import place_service
class PlaceSearchTool(Tool):
"""Tool for searching hotels and accommodations using enhanced place service."""
def __init__(self):
self.name = "place_search"
self.description = "Search for hotels and accommodations in a location"
self.input_type = "object"
self.output_type = "object"
self.inputs = {
"location": {
"type": "string",
"description": "Location to search for places"
},
"max_distance": {
"type": "integer",
"description": "Maximum distance in miles",
"optional": True,
"nullable": True
}
}
self.outputs = {
"location": {
"type": "string",
"description": "Search location"
},
"total_found": {
"type": "integer",
"description": "Total number of places found"
},
"places": {
"type": "array",
"description": "List of places found"
}
}
self.required_inputs = ["location"]
self.is_initialized = True
def forward(self, location: str, max_distance: Optional[int] = None) -> dict:
"""Search for places using enhanced place service."""
try:
# Convert max_distance to float for service compatibility
distance = float(max_distance) if max_distance is not None else None
# Use the enhanced place service
results = place_service.search_places(location, distance)
# Handle error cases
if "error" in results:
return {
"location": location,
"total_found": 0,
"places": [],
"error": results["error"]
}
# Convert service results to tool output format
formatted_places = []
for place in results.get('top_places', []):
formatted_places.append({
"name": place.get('name', 'Unnamed Place'),
"type": place.get('type', 'Accommodation'),
"address": place.get('address', 'Address not available'),
"rating": place.get('rating', 'Not rated'),
"distance": place.get('distance', 'Distance not available'),
"description": place.get('description', 'Quality accommodation')
})
return {
"location": results.get('location', location),
"search_radius": results.get('search_radius', 'Not specified'),
"total_found": results.get('total_found', len(formatted_places)),
"places": formatted_places
}
except Exception as e:
return {
"location": location,
"total_found": 0,
"places": [],
"error": f"Unexpected error: {str(e)}"
}