Skip to main content
Glama
HeshamFS

MCP Materials Server

by HeshamFS

get_structure

Retrieve crystal structure data from Materials Project by material ID. Supports CIF, POSCAR, and JSON formats for materials science analysis.

Instructions

Get the crystal structure for a material from Materials Project.

Args:
    material_id: Materials Project ID (e.g., "mp-149" for Silicon)
    format: Output format - "cif", "poscar", or "json" (default: "cif")

Returns:
    Crystal structure in the requested format

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
material_idYes
formatNocif

Implementation Reference

  • The main handler function for the 'get_structure' tool, decorated with @mcp.tool() for automatic registration and schema generation from type hints and docstring. Fetches crystal structure data from Materials Project API and returns in CIF, POSCAR, or JSON format.
    @mcp.tool()
    def get_structure(
        material_id: str,
        format: str = "cif",
    ) -> str:
        """
        Get the crystal structure for a material from Materials Project.
    
        Args:
            material_id: Materials Project ID (e.g., "mp-149" for Silicon)
            format: Output format - "cif", "poscar", or "json" (default: "cif")
    
        Returns:
            Crystal structure in the requested format
        """
        has_key, key_or_error = check_api_key()
        if not has_key:
            return json.dumps({"error": key_or_error})
    
        try:
            from mp_api.client import MPRester
    
            with MPRester(key_or_error) as mpr:
                structure = mpr.get_structure_by_material_id(material_id)
    
                if format.lower() == "cif":
                    from pymatgen.io.cif import CifWriter
                    return CifWriter(structure).__str__()
                elif format.lower() == "poscar":
                    from pymatgen.io.vasp import Poscar
                    return Poscar(structure).get_str()
                elif format.lower() == "json":
                    return structure.to_json()
                else:
                    return json.dumps({"error": f"Unknown format: {format}. Use 'cif', 'poscar', or 'json'"})
    
        except ImportError:
            return json.dumps({"error": "mp-api or pymatgen not installed"})
        except Exception as e:
            return json.dumps({"error": str(e)})
  • The @mcp.tool() decorator registers the get_structure function as an MCP tool.
    @mcp.tool()
  • Input schema defined by function parameters (material_id: str, format: str = 'cif') and comprehensive docstring describing args and return value. Output is str (structure file content or JSON error).
    def get_structure(
        material_id: str,
        format: str = "cif",
    ) -> str:
        """
        Get the crystal structure for a material from Materials Project.
    
        Args:
            material_id: Materials Project ID (e.g., "mp-149" for Silicon)
            format: Output format - "cif", "poscar", or "json" (default: "cif")
    
        Returns:
            Crystal structure in the requested format
        """

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/HeshamFS/mcp-materials-server'

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