Skip to main content
Glama
jagan-shanmugam

OpenStreetMap MCP Server

analyze_neighborhood

Evaluate neighborhood livability by analyzing amenities, transportation, green spaces, and services to support real estate decisions and relocation planning.

Instructions

Generate a comprehensive neighborhood analysis focused on livability factors.

This advanced analysis tool evaluates a neighborhood based on multiple livability factors, including amenities, transportation options, green spaces, and services. Results include counts and proximity scores for various categories, helping to assess the overall quality and convenience of a residential area. Invaluable for real estate decisions, relocation planning, and neighborhood comparisons.

Args: latitude: Center point latitude (decimal degrees) longitude: Center point longitude (decimal degrees) radius: Analysis radius in meters (defaults to 1000m/1km)

Returns: Comprehensive neighborhood profile including: - Overall neighborhood score - Walkability assessment - Public transportation access - Nearby amenities (shops, restaurants, services) - Green spaces and recreation - Education and healthcare facilities - Detailed counts and distance metrics for each category

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
latitudeYes
longitudeYes
radiusNo

Implementation Reference

  • The primary handler function decorated with @mcp.tool() that implements the analyze_neighborhood tool. It performs detailed neighborhood analysis by querying OSM Overpass API for various amenity categories (groceries, restaurants, healthcare, etc.), calculates distances, scores livability factors, and computes overall and walkability scores.
    async def analyze_neighborhood(
        latitude: float,
        longitude: float,
        ctx: Context,
        radius: float = 1000
    ) -> Dict[str, Any]:
        """
        Generate a comprehensive neighborhood analysis focused on livability factors.
        
        This advanced analysis tool evaluates a neighborhood based on multiple livability factors,
        including amenities, transportation options, green spaces, and services. Results include
        counts and proximity scores for various categories, helping to assess the overall quality
        and convenience of a residential area. Invaluable for real estate decisions, relocation
        planning, and neighborhood comparisons.
        
        Args:
            latitude: Center point latitude (decimal degrees)
            longitude: Center point longitude (decimal degrees)
            radius: Analysis radius in meters (defaults to 1000m/1km)
            
        Returns:
            Comprehensive neighborhood profile including:
            - Overall neighborhood score
            - Walkability assessment
            - Public transportation access
            - Nearby amenities (shops, restaurants, services)
            - Green spaces and recreation
            - Education and healthcare facilities
            - Detailed counts and distance metrics for each category
        """
        osm_client = ctx.request_context.lifespan_context.osm_client
        
        # Get address information for the center point
        address_info = await osm_client.reverse_geocode(latitude, longitude)
        
        # Categories to analyze for neighborhood quality
        categories = [
            # Essential services
            {"name": "groceries", "tags": ["shop=supermarket", "shop=convenience", "shop=grocery"]},
            {"name": "restaurants", "tags": ["amenity=restaurant", "amenity=cafe", "amenity=fast_food"]},
            {"name": "healthcare", "tags": ["amenity=hospital", "amenity=doctors", "amenity=pharmacy"]},
            {"name": "education", "tags": ["amenity=school", "amenity=kindergarten", "amenity=university"]},
            
            # Transportation
            {"name": "public_transport", "tags": ["public_transport=stop_position", "railway=station", "amenity=bus_station"]},
            
            # Recreation
            {"name": "parks", "tags": ["leisure=park", "leisure=garden", "leisure=playground"]},
            {"name": "sports", "tags": ["leisure=sports_centre", "leisure=fitness_centre", "leisure=swimming_pool"]},
            
            # Culture and entertainment
            {"name": "entertainment", "tags": ["amenity=theatre", "amenity=cinema", "amenity=arts_centre"]},
            
            # Other amenities
            {"name": "shopping", "tags": ["shop=mall", "shop=department_store", "shop=clothes"]},
            {"name": "services", "tags": ["amenity=bank", "amenity=post_office", "amenity=atm"]}
        ]
        
        # Build overpass queries and collect results
        results = {}
        scores = {}
        
        for i, category in enumerate(categories):
            await ctx.report_progress(i, len(categories))
            ctx.info(f"Analyzing {category['name']} in neighborhood...")
            
            # Convert radius to bounding box
            lat_delta = radius / 111000
            lon_delta = radius / (111000 * math.cos(math.radians(latitude)))
            
            bbox = (
                longitude - lon_delta,
                latitude - lat_delta,
                longitude + lon_delta,
                latitude + lat_delta
            )
            
            # Build Overpass query
            overpass_url = "https://overpass-api.de/api/interpreter"
            
            # Create query for category tags
            tag_filters = []
            for tag in category["tags"]:
                key, value = tag.split("=")
                tag_filters.append(f'node["{key}"="{value}"]({{bbox}});')
                tag_filters.append(f'way["{key}"="{value}"]({{bbox}});')
            
            query = f"""
            [out:json];
            (
                {" ".join(tag_filters)}
            );
            out body;
            """
            
            query = query.replace("{bbox}", f"{bbox[1]},{bbox[0]},{bbox[3]},{bbox[2]}")
            
            try:
                async with aiohttp.ClientSession() as session:
                    async with session.post(overpass_url, data={"data": query}) as response:
                        if response.status == 200:
                            data = await response.json()
                            features = data.get("elements", [])
                        else:
                            ctx.warning(f"Failed to analyze {category['name']}: {response.status}")
                            features = []
                            
                # Process and calculate metrics
                feature_list = []
                distances = []
                
                for feature in features:
                    tags = feature.get("tags", {})
                    
                    # Get coordinates based on feature type
                    coords = {}
                    if feature.get("type") == "node":
                        coords = {
                            "latitude": feature.get("lat"),
                            "longitude": feature.get("lon")
                        }
                    elif "center" in feature:
                        coords = {
                            "latitude": feature.get("center", {}).get("lat"),
                            "longitude": feature.get("center", {}).get("lon")
                        }
                    
                    # Skip if no valid coordinates
                    if not coords:
                        continue
                    
                    # Calculate distance from center point
                    from math import radians, sin, cos, sqrt, asin
                    
                    def haversine(lat1, lon1, lat2, lon2):
                        R = 6371000  # Earth radius in meters
                        dLat = radians(lat2 - lat1)
                        dLon = radians(lon2 - lon1)
                        a = sin(dLat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dLon/2)**2
                        c = 2 * asin(sqrt(a))
                        return R * c
                    
                    distance = haversine(latitude, longitude, coords["latitude"], coords["longitude"])
                    distances.append(distance)
                    
                    feature_list.append({
                        "id": feature.get("id"),
                        "name": tags.get("name", "Unnamed"),
                        "type": feature.get("type"),
                        "coordinates": coords,
                        "distance": round(distance, 1),
                        "tags": tags
                    })
                
                # Sort by distance
                feature_list.sort(key=lambda x: x["distance"])
                
                # Calculate metrics
                count = len(feature_list)
                avg_distance = sum(distances) / count if count > 0 else None
                min_distance = min(distances) if count > 0 else None
                
                # Score this category (0-10)
                # Higher score for more amenities and closer proximity
                if count == 0:
                    category_score = 0
                else:
                    # Base score on count and proximity
                    count_score = min(count / 5, 1) * 5  # Up to 5 points for count
                    proximity_score = 5 - min(min_distance / radius, 1) * 5  # Up to 5 points for proximity
                    category_score = count_score + proximity_score
                
                # Store results
                results[category["name"]] = {
                    "count": count,
                    "features": feature_list[:10],  # Limit to top 10
                    "metrics": {
                        "total_count": count,
                        "avg_distance": round(avg_distance, 1) if avg_distance else None,
                        "min_distance": round(min_distance, 1) if min_distance else None
                    }
                }
                
                scores[category["name"]] = category_score
                
            except Exception as e:
                ctx.warning(f"Error analyzing {category['name']}: {str(e)}")
                results[category["name"]] = {"error": str(e)}
                scores[category["name"]] = 0
        
        # Calculate overall neighborhood score
        if scores:
            overall_score = sum(scores.values()) / len(scores)
        else:
            overall_score = 0
        
        # Calculate walkability score based on amenities within walking distance (500m)
        walkable_amenities = 0
        walkable_categories = 0
        
        for category_name, category_data in results.items():
            if "metrics" in category_data:
                # Count amenities within walking distance
                walking_count = sum(1 for feature in category_data.get("features", []) 
                                   if feature.get("distance", float("inf")) <= 500)
                
                if walking_count > 0:
                    walkable_amenities += walking_count
                    walkable_categories += 1
        
        walkability_score = min(walkable_amenities + walkable_categories, 10)
        
        # Report completion
        await ctx.report_progress(len(categories), len(categories))
        
        return {
            "location": {
                "coordinates": {
                    "latitude": latitude,
                    "longitude": longitude
                },
                "address": address_info.get("display_name", "Unknown location")
            },
            "scores": {
                "overall": round(overall_score, 1),
                "walkability": walkability_score,
                "categories": {k: round(v, 1) for k, v in scores.items()}
            },
            "categories": results,
            "analysis_radius": radius,
            "timestamp": datetime.now().isoformat()
        }
  • The @mcp.tool() decorator registers the analyze_neighborhood function as an MCP tool.
    async def analyze_neighborhood(

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

A4.3/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full transparency burden. It explains what the tool computes (counts, proximity scores, walkability, transport access) and what it returns (a structured profile with scores and distance metrics), going beyond a simple action statement. It does not disclose data sources, error cases, or auth requirements, but for a benign read-only analysis tool the behavior is well characterized.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with a one-line summary, a supporting paragraph, an Args section, and a Returns section, keeping the most important information front-loaded. It is slightly redundant—categories like amenities and green spaces appear in both the prose and the Returns list—but every sentence adds some value and the overall length is appropriate.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With no output schema and no annotations, the description must explain both parameters and return values, and it does so thoroughly: it lists seven output categories and documents all three parameters with units. For a tool of moderate complexity (3 simple params, no nested objects), this is nearly complete, though it omits edge-case behavior like invalid coordinates or radius limitations.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Although the context signal reports 0% schema description coverage, the description compensates by adding meaning beyond the bare schema: latitude and longitude are labeled 'Center point latitude (decimal degrees)' and radius is clarified as 'Analysis radius in meters (defaults to 1000m/1km)'. This adds units, role, and default interpretation that the schema properties lack, though it does not explain radius limits or coordinate validity.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description opens with a specific verb+resource: 'Generate a comprehensive neighborhood analysis focused on livability factors' and elaborates with concrete categories (amenities, transportation, green spaces, services). This clearly distinguishes it from sibling tools like find_nearby_places, find_schools_nearby, and analyze_commute, which focus on narrower, single-category lookups rather than a holistic livability score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear use cases: 'Invaluable for real estate decisions, relocation planning, and neighborhood comparisons,' which tells the agent when to select this tool. However, it does not explicitly name alternatives or mention when not to use it, such as when a single category like schools or commute alone is needed.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.