Skip to main content
Glama

metadata_raster

Extract metadata from raster datasets by providing either a local file path or HTTPS URL to analyze geospatial data properties.

Instructions

Open a raster dataset in read-only mode and return metadata.

This tool supports two modes based on the provided string:

  1. A local filesystem path (e.g., "D:\Data\my_raster.tif").

  2. An HTTPS URL (e.g., "https://example.com/my_raster.tif").

The input must be a single string that is either a valid file path on the local machine or a valid HTTPS URL pointing to a raster.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
path_or_urlYes

Implementation Reference

  • Core handler function for 'metadata_raster' tool. Accepts path_or_url (local path or HTTPS URL), opens with rasterio, returns structured metadata (dimensions, CRS, bounds, nodata, transform, band dtypes). Handles remote URLs directly and validates local files.
    @gis_mcp.tool()
    def metadata_raster(path_or_url: str) -> Dict[str, Any]:
        """
        Open a raster dataset in read-only mode and return metadata.
        
        This tool supports two modes based on the provided string:
        1. A local filesystem path (e.g., "D:\\Data\\my_raster.tif").
        2. An HTTPS URL (e.g., "https://example.com/my_raster.tif").
        
        The input must be a single string that is either a valid file path
        on the local machine or a valid HTTPS URL pointing to a raster.
        """
        try:
            # Import numpy first to ensure NumPy's C-API is initialized
            import numpy as np
            import rasterio
    
            # Remove any backticks (`) if the client wrapped the path_or_url in them
            cleaned = path_or_url.replace("`", "")
    
            # Determine if the string is an HTTPS URL or a local file path
            if cleaned.lower().startswith("https://"):
                # For HTTPS URLs, let Rasterio/GDAL handle remote access directly
                dataset = rasterio.open(cleaned)
            else:
                # Treat as local filesystem path
                local_path = os.path.expanduser(cleaned)
    
                # Verify that the file exists on disk
                if not os.path.isfile(local_path):
                    raise FileNotFoundError(f"Raster file not found at '{local_path}'.")
    
                # Open the local file in read-only mode
                dataset = rasterio.open(local_path)
    
            # Build a mapping from band index to its data type (dtype)
            band_dtypes = {i: dtype for i, dtype in zip(dataset.indexes, dataset.dtypes)}
    
            # Collect core metadata fields in simple Python types
            meta: Dict[str, Any] = {
                "name": dataset.name,                                       # Full URI or filesystem path
                "mode": dataset.mode,                                       # Mode should be 'r' for read                                 
                "driver": dataset.driver,                                   # GDAL driver, e.g. "GTiff"
                "width": dataset.width,                                     # Number of columns
                "height": dataset.height,                                   # Number of rows
                "count": dataset.count,                                     # Number of bands
                "bounds": dataset.bounds,                                   # Show bounding box
                "band_dtypes": band_dtypes,                                 # { band_index: dtype_string }
                "no_data": dataset.nodatavals,                              # Number of NoData values in each band
                "crs": dataset.crs.to_string() if dataset.crs else None,    # CRS as EPSG string or None
                "transform": list(dataset.transform),                       # Affine transform coefficients (6 floats)
            }
    
            # Return a success status along with metadata
            return {
                "status": "success",
                "metadata": meta,
                "message": f"Raster dataset opened successfully from '{cleaned}'."
            }
    
        except Exception as e:
            # Log the error for debugging purposes, then raise ValueError so MCP can relay it
            logger.error(f"Error opening raster '{path_or_url}': {str(e)}")
            raise ValueError(f"Failed to open raster '{path_or_url}': {str(e)}")
  • MCP resource endpoint listing 'metadata_raster' among available rasterio operations, enabling client discovery of tools.
    @gis_mcp.resource("gis://operation/rasterio")
    def get_rasterio_operations() -> Dict[str, List[str]]:
        """List available rasterio operations."""
        return {
            "operations": [
                "metadata_raster",
                "get_raster_crs",
                "clip_raster_with_shapefile",
                "resample_raster",
                "reproject_raster",
                "weighted_band_sum",
                "concat_bands",
                "raster_algebra",
                "compute_ndvi",
                "raster_histogram",
                "tile_raster",
                "raster_band_statistics",
                "extract_band",
                "zonal_statistics",
                "reclassify_raster",
                "focal_statistics",
                "hillshade",
                "write_raster"
            ]
        }
  • Imports rasterio_functions.py in main server entrypoint, executing @gis_mcp.tool() decorators to register 'metadata_raster' with the FastMCP server instance.
    from . import (
        geopandas_functions,
        shapely_functions,
        rasterio_functions,
        pyproj_functions,
        pysal_functions,
    )
  • src/gis_mcp/mcp.py:5-6 (registration)
    Defines the FastMCP server instance 'gis_mcp' used by decorators (@gis_mcp.tool(), @gis_mcp.resource()) to register tools and resources.
    gis_mcp = FastMCP("GIS MCP")

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mahdin75/gis-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server