# Introduction 
The IFC-MCP Service is a toolkit for extracting and aggregating data from Industry Foundation Classes (IFC) models used in Building Information Modeling (BIM). It provides an MCP with tools that can be used by an LLM. It enables users to access, filter, and analyze building elements and their properties in a structured way. This service is designed to support developers and analysts working with BIM data, making it easier to perform data-driven tasks on complex building models.

<div align="center">
<img src="_docu/product/assets/business_case/business_case_statement.jpg" alt="Business Case Statement" height="256" />
</div>

Architects and other building professionals often need to retrieve specific information from construction plans to support their work. For example, an architect may want to know how many windows, doors, chimneys, or ramps a building has; a glass cleaning company might need details about the largest window to select the right equipment; construction workers may be interested in the number of walls above a certain height to determine which ladders to bring; and painters require the total wall area to estimate the amount of paint needed. Currently, this kind of information can only be obtained by manually analyzing complex and non-human-readable IFC files or by using a combination of IFC viewer tools, which often do not provide all the desired data in one place. This makes the process time-consuming and inefficient for experts in the building industry.

<div align="center">
<img src="_docu/product/assets/business_case/problem_statement.jpg" alt="Problem Statement" height="256" />
</div>
<div align="center">
<img src="_docu/product/assets/business_case/problem_statement_2.jpg" alt="Problem Statement 2" height="256" />
</div>

The proposed solution is to implement an MCP that integrates seamlessly with a large language model (LLM) and is accessible to users through OpenWebUI. This approach allows users to ask any question about the building site in natural language, without needing technical expertise or familiarity with specialized tools. The system provides easy access and delivers answers to a wide range of queries, automatically handling the information retrieval and presenting results in a clear, human-readable format.

<div align="center">
<img src="_docu/product/assets/business_case/proposed_solution.jpg" alt="Proposed Solution" height="256" />
</div>

At this point, queries can include questions about the overall building inventory, quantities of specific entity types, filtering based on properties, and aggregations such as total sums or maximal and minimal values of properties.

<div align="center">
<img src="_docu/product/assets/business_case/features.jpg" alt="Features" height="256" />
</div>

 
# Getting Started

To get started with the IFC-MCP Service, follow these steps:

## Software Dependencies
Ensure you have the following installed:
- Node.js (v16 or higher)
- npm (v7 or higher)
- Docker (optional, for containerized deployment)
- Local development: pip & uvicorn

## Running the Project Locally
- Ensure you run a virtual environment for Python
  - VS code: 
    - Create virtual environment: `python -m venv .venv`
    - Activate virtual environment `.venv\Scripts\acitvate` (on Windows)
- Install dependencies:
  ```
  pip install -r requirements.txt
  ```
- Run: 
    ```
    python .\app\main.py
    ```

- Update requirements.txt
  ```
  pip freeze > requirements.txt 
  ```

## Running with Python Debugger in VS Code

Add the following configuration to the ``launch.json`` file in ``.vscode``:

```json
 {
     "name": "Python: Run FastMCP",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/app/main.py",
      "console": "integratedTerminal",
      "env": {
        "PYTHONPATH": "${workspaceFolder}/app"
      }
    }
```

## Use VS Code Copilot as Client

Create a ``mcp.json`` file in the ``.vscode`` folder. And add the following content:

```json
{
  "servers": {
    "ifc-mcp": {
      "url": "http://0.0.0.0:8000/mcp/"
    }
  }
}
```

Run the server with the Python debugger or

```bash
fastmcp run app/main.py:mcp
```

Start the MCP by selecting **Agent** mode in VS Code Copilot and make sure to select the ``ifc-mcp`` tool by clicking the Tools-Icon. 


# MCP Tools

- `get_entity_types`  
  Retrieves all entity types present in an IFC file, allowing users to see what kinds of elements are available for analysis.

- `get_entities`  
  Retrieves entities of a specific type from an IFC file, with optional filtering by attributes, enabling targeted queries for elements like walls, slabs, or furniture.

- `get_entity_info`  
  Provides detailed information about a specific entity in an IFC file, supporting both basic and recursive data extraction for a given element.

- `get_property_sets`  
  Returns property sets or a subset of specified properties for a specific entity, helping users access detailed property data for individual elements.

- `get_property_names`  
  Lists the property names available for a specific entity type, making it easier to understand what data can be queried or filtered.

- `get_entities_with_properties`  
  Retrieves entities of a specific type, optionally filtered by attributes and properties, and includes specified property data and statistics for advanced analysis.

- `analyze_building_inventory`  
  Generates a comprehensive inventory of all building elements in the IFC file, providing counts and basic statistics for each entity type to support

# Testing
## Test Data

Retrieve test data from: [buildingSMART: Sample-Test-Files](https://github.com/buildingSMART/Sample-Test-Files/tree/main/IFC%204.0.2.1%20(IFC%204)/PCERT-Sample-Scene)

##  Unit Testing

- run in Service directory in virtual environment.
```
pytest .\tests\
```
  - `-s`: disables this output capturing, allowing you to see print statements and other output directly in the console during the test run.
  - `-v`: increases the verbosity of pytest's output, providing more detailed information about each test, including the names of the tests and their outcome

## Test Case Builder

To verify the results of the MCP, you can compute any results of a given IFC file and given instructions by:

1. Add an IFC file to the ``ifcs`` folder
2. Add an instruction to ``instructions.json``
3. Run the script by:
   1. Ensuring PYTHONPATH=/app: `$env:PYTHONPATH="{path_to_project}\IfcMCP\app"`
   2. ``python .\tests\build_test_cases.py``  or
   3. using Python debugger by adding the configuration

```json
{
      "name": "Python Debugger: Current File",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/tests/build_test_cases.py",
      "console": "integratedTerminal",
      "env": {
        "PYTHONPATH": "${workspaceFolder}/app"
      }
    },
```


