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
| Name | Required | Description | Default |
|---|---|---|---|
| material_id | Yes | ||
| format | No | cif |
Implementation Reference
- src/mcp_materials/server.py:109-149 (handler)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)})
- src/mcp_materials/server.py:109-109 (registration)The @mcp.tool() decorator registers the get_structure function as an MCP tool.@mcp.tool()
- src/mcp_materials/server.py:110-123 (schema)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 """