Skip to main content
Glama
HeshamFS

MCP Materials Server

by HeshamFS

search_materials

Find materials by chemical formula in the Materials Project database to retrieve material IDs, formulas, and key properties for research and analysis.

Instructions

Search for materials by chemical formula in the Materials Project database.

Args:
    formula: Chemical formula (e.g., "Fe2O3", "LiFePO4", "Si")
    max_results: Maximum number of results to return (default: 10)

Returns:
    JSON with matching materials including material_id, formula, and key properties

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
formulaYes
max_resultsNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Full implementation of the 'search_materials' tool handler. Decorated with @mcp.tool() for automatic registration in the FastMCP server. Performs API key check, queries Materials Project via MPRester for materials matching the formula, extracts key properties (formula, energy above hull, band gap, etc.), formats as JSON.
    @mcp.tool()
    def search_materials(
        formula: str,
        max_results: int = 10,
    ) -> str:
        """
        Search for materials by chemical formula in the Materials Project database.
    
        Args:
            formula: Chemical formula (e.g., "Fe2O3", "LiFePO4", "Si")
            max_results: Maximum number of results to return (default: 10)
    
        Returns:
            JSON with matching materials including material_id, formula, and key properties
        """
        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:
                docs = mpr.materials.summary.search(
                    formula=formula,
                    fields=[
                        "material_id",
                        "formula_pretty",
                        "energy_above_hull",
                        "band_gap",
                        "formation_energy_per_atom",
                        "density",
                        "symmetry",
                        "is_stable",
                    ],
                    num_chunks=1,
                    chunk_size=max_results,
                )
    
                results = []
                for doc in docs[:max_results]:
                    crystal_system = None
                    if doc.symmetry and doc.symmetry.crystal_system:
                        crystal_system = str(doc.symmetry.crystal_system.value) if hasattr(doc.symmetry.crystal_system, 'value') else str(doc.symmetry.crystal_system)
                    results.append({
                        "material_id": str(doc.material_id),
                        "formula": doc.formula_pretty,
                        "energy_above_hull_eV": doc.energy_above_hull,
                        "band_gap_eV": doc.band_gap,
                        "formation_energy_eV_atom": doc.formation_energy_per_atom,
                        "density_g_cm3": doc.density,
                        "crystal_system": crystal_system,
                        "space_group": doc.symmetry.symbol if doc.symmetry else None,
                        "is_stable": doc.is_stable,
                    })
    
                return json.dumps({
                    "count": len(results),
                    "materials": results,
                }, indent=2)
    
        except ImportError:
            return json.dumps({"error": "mp-api package not installed. Run: pip install mp-api"})
        except Exception as e:
            return json.dumps({"error": str(e)})
  • Input schema defined by function parameters with type hints: formula (str), max_results (int, default 10). Output: str (JSON). Detailed Arg/Return docstring provides validation guidance.
    def search_materials(
        formula: str,
        max_results: int = 10,
    ) -> str:
        """
        Search for materials by chemical formula in the Materials Project database.
    
        Args:
            formula: Chemical formula (e.g., "Fe2O3", "LiFePO4", "Si")
            max_results: Maximum number of results to return (default: 10)
    
        Returns:
            JSON with matching materials including material_id, formula, and key properties
        """
  • The @mcp.tool() decorator registers the search_materials function as an MCP tool on the FastMCP instance.
    @mcp.tool()
  • Helper functions for API key management: get_mp_api_key retrieves the key from env var, check_api_key validates it and returns error message if missing. Used by search_materials.
    def get_mp_api_key() -> str | None:
        """Get Materials Project API key from environment."""
        return os.environ.get("MP_API_KEY")
    
    
    def check_api_key() -> tuple[bool, str]:
        """Check if API key is configured."""
        key = get_mp_api_key()
        if not key:
            return False, "MP_API_KEY environment variable not set. Get your key at https://materialsproject.org/api"
        return True, key
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions the database source and return format, but doesn't cover important aspects like rate limits, authentication requirements, pagination behavior, or error conditions. The description adds some context but leaves significant gaps for a search tool.

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

Conciseness5/5

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

The description is efficiently structured with clear sections: purpose statement, parameter documentation, and return format. Every sentence adds value without redundancy. The formatting with 'Args:' and 'Returns:' sections enhances readability while maintaining brevity.

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

Completeness3/5

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

Given the tool has an output schema (which handles return values) and only 2 parameters with good semantic coverage in the description, the description is reasonably complete. However, as a search tool with no annotations, it should ideally mention more behavioral aspects like result ordering, pagination, or common use cases to be fully comprehensive.

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?

The description adds meaningful semantic information beyond the input schema. While schema description coverage is 0%, the description provides concrete examples for the 'formula' parameter ('Fe2O3', 'LiFePO4', 'Si') and clarifies the default value and purpose of 'max_results'. This compensates well for the lack of schema descriptions.

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

Purpose4/5

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

The description clearly states the tool's purpose: 'Search for materials by chemical formula in the Materials Project database.' It specifies the verb ('search'), resource ('materials'), and scope ('Materials Project database'), but doesn't explicitly differentiate from sibling tools like 'search_by_band_gap' or 'search_by_elements'.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'search_by_band_gap' or 'search_by_elements', nor does it specify prerequisites or exclusions. Usage is implied by the purpose statement alone.

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

Install Server

Other Tools

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