BTWIN
README.md
# BTWIN MCP
[](https://github.com/angelomassafra/btwin_mcp/actions/workflows/tests.yml)
[](LICENSE)
BTWIN (Building Twin Interface) is a [Model Context Protocol](https://modelcontextprotocol.io)
(MCP) server that exposes a Python API for building digital twins to large language
model (LLM) applications such as Claude, ChatGPT, OpenAI Codex, GitHub Copilot or
local models. Through it, an LLM can
create, query and analyse semantic building models based on the BOT, Brick, IFC-OWL,
SOSA and EKO ontologies, link them to time-series observations and utility bills,
compute energy KPIs, and serialise the results as JSON-LD, RDF/Turtle, SQLite databases
and graph images.
The server follows the code-execution pattern for MCP: instead of one tool per API
function, it exposes two tools, one to search the API and one to run Python scripts
against it (see [Architecture](#architecture)).
---
## Installation
BTWIN is a local MCP server (stdio transport). It can be used with any MCP client
that supports local servers, independently of the underlying LLM. Supported clients
include:
| Client | LLM provider | Registered automatically by the installer |
|---|---|---|
| [Claude Desktop](https://claude.ai/download) | Anthropic | yes |
| [Claude Code](https://claude.com/claude-code) | Anthropic | yes |
| [ChatGPT desktop app](https://openai.com/chatgpt/download/) | OpenAI | yes (shared Codex configuration) |
| [OpenAI Codex](https://developers.openai.com/codex) (CLI, IDE extension) | OpenAI | yes |
| VS Code (GitHub Copilot), Cursor, Gemini CLI, LM Studio, Cline, … | various, including local models | no, see [Manual configuration](#manual-configuration) |
The ChatGPT web and mobile apps only accept remote MCP servers reachable over HTTPS
and therefore cannot start BTWIN directly; use the ChatGPT desktop app instead.
Requirements: one of the clients above and an internet connection. Python does not need
to be installed beforehand: the installer provides it through
[uv](https://docs.astral.sh/uv/).
### Installation through an AI coding agent
In an agent that can run commands on your computer (e.g. Claude Code, OpenAI Codex,
GitHub Copilot agent mode, Gemini CLI), enter:
> *Install the BTWIN MCP server from https://github.com/angelomassafra/btwin_mcp*
The agent follows [`llms-install.md`](llms-install.md), runs the installer and registers
BTWIN in the clients found on the computer. After approving the proposed commands,
restart the client.
### Windows installer
1. Click the green **`<> Code`** button at the top of this page → **Download ZIP**.
2. Open the downloaded ZIP and **extract** it (right-click → *Extract All…*).
3. Open the extracted folder, go into **`install`**, and **double-click `install-windows.bat`**.
- If Windows shows *"Windows protected your PC"*, click **More info → Run anyway**.
4. Wait until you see **"Done! BTWIN is installed."** (the first time takes 1–3 minutes).
The installer lists the clients in which BTWIN was registered.
5. **Fully quit** those clients (right-click their icon near the clock → *Quit*) and open them again.
<details>
<summary>Alternative: one command in PowerShell</summary>
```powershell
irm https://raw.githubusercontent.com/angelomassafra/btwin_mcp/main/install/install-windows.ps1 | iex
```
</details>
### macOS installer
1. Click **`<> Code`** → **Download ZIP**, then double-click the ZIP to extract it.
2. Open the folder **`install`** and **double-click `install-macos.command`**.
- If macOS says it *"cannot be opened"*: right-click the file → **Open** → **Open**.
3. Wait for **"Done! BTWIN is installed."**, then quit the listed clients (**Cmd+Q**) and reopen them.
<details>
<summary>Alternative: one command in Terminal (macOS and Linux)</summary>
```bash
curl -LsSf https://raw.githubusercontent.com/angelomassafra/btwin_mcp/main/install/install.sh | sh
```
</details>
### Verification
The client should list a server named **btwin** with two tools (`btwin_exec`,
`btwin_help`): in Claude Desktop under the **"+" / tools** button, in Claude Code and
Codex with the `/mcp` command, in the ChatGPT desktop app under *Settings → MCP servers*.
Then ask:
> *Use BTWIN to check that everything works: build a small building with 2 floors and
> 3 rooms per floor, save it as `test.json` and draw its graph as `test.png`.*
Files created by BTWIN are saved in the **Documents → BTWIN** folder.
---
## What the installer does
The installer scripts are in [`install/`](install). They:
1. install [uv](https://docs.astral.sh/uv/) (Astral), which provides a suitable Python
version and the dependencies in an isolated environment;
2. download BTWIN and its dependencies (pandas, networkx, rdflib, matplotlib, plotly, openpyxl);
3. register a `btwin` server in each client found on the computer:
- Claude Desktop: `claude_desktop_config.json`;
- Claude Code: `claude mcp add --scope user`;
- ChatGPT desktop and OpenAI Codex: `~/.codex/config.toml` (or `$CODEX_HOME/config.toml`).
Existing settings are preserved and each modified file is first copied to a `.bak` backup.
**Update:** run the installer again. **Uninstall:** remove the `btwin` entry from the
client configuration (Claude Desktop: *Settings → Developer → Edit Config*; Claude Code:
`claude mcp remove btwin`; ChatGPT desktop / Codex: delete the `[mcp_servers.btwin]`
table from `~/.codex/config.toml`).
---
## Example prompts
- *"Create a site with one building, 3 storeys and 4 spaces per storey. Export it as JSON-LD and RDF Turtle."*
- *"Add an air handling unit per floor that feeds every room on that floor, and a temperature sensor in each room."*
- *"Here are 12 months of electricity readings in kWh: … Compute the EUI for a 500 m² building and store the readings in a SQLite database."*
- *"Open `office_building/observations.db` and find temperature readings more than 3 sigma from the mean."*
- *"Load `office_building/graph.json`, find the shortest path from space-001 to space-004 and list the rooms fed by ahu-1."*
The [`examples/`](examples) folder contains two synthetic datasets
(an office building with HVAC, sensors and two years of bills, and a home with water
bills) and further prompts. To use them, copy them into *Documents/BTWIN*.
---
## Architecture

Exposing each of the ~100 API functions as a separate MCP tool would place several
thousand tokens of tool definitions in the model's context. BTWIN instead follows the
[code execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp)
pattern and exposes two tools:
| Tool | What it does |
|------|--------------|
| `btwin_help` | Searches the BTWIN API by keyword (*space*, *equipment*, *kpi*, *graph*…) so the model only loads what it needs. |
| `btwin_exec` | Runs a short Python script in which all BTWIN classes are pre-loaded. Intermediate objects stay inside the server; only the final `result` goes back to the model. |
This reduces the tool definitions from about 7,800 to about 480 tokens (−94%) and allows
multi-step modelling operations to be executed in a single call. The full API (90 functions + helpers) is documented in
[`docs/API.md`](docs/API.md).
> **Security note.** `btwin_exec` runs Python code written by the LLM on your computer,
> with your user's permissions (like any local MCP server). Configure the client to ask
> for approval before each tool call (the default in Claude Desktop, ChatGPT and Codex): read the script if in doubt, and only work with files
> you trust.
---
## Manual configuration
All clients start the server with the same command, which requires only
[uv](https://docs.astral.sh/uv/getting-started/installation/):
```
uvx --from https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip btwin-mcp
```
If a client cannot find `uvx`, use its full path (Windows:
`C:\Users\<you>\.local\bin\uvx.exe`, macOS/Linux: `~/.local/bin/uvx`).
<details>
<summary>Claude Code / OpenAI Codex (one command)</summary>
```bash
claude mcp add --scope user btwin -- uvx --from https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip btwin-mcp
codex mcp add btwin -- uvx --from https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip btwin-mcp
```
</details>
<details>
<summary>ChatGPT desktop app and OpenAI Codex (<code>~/.codex/config.toml</code>)</summary>
```toml
[mcp_servers.btwin]
command = "uvx"
args = ["--from", "https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip", "btwin-mcp"]
startup_timeout_sec = 120
```
</details>
<details>
<summary>Claude Desktop, Cursor, Gemini CLI, LM Studio, Cline and other JSON-configured clients</summary>
Add to the client's MCP configuration file (Claude Desktop: `claude_desktop_config.json`;
Cursor: `~/.cursor/mcp.json`; Gemini CLI: `~/.gemini/settings.json`; LM Studio: `mcp.json`):
```json
{
"mcpServers": {
"btwin": {
"command": "uvx",
"args": ["--from", "https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip", "btwin-mcp"]
}
}
}
```
VS Code uses the same entry under a `"servers"` key in `.vscode/mcp.json`.
</details>
<details>
<summary>Plain Python / pip</summary>
```bash
pip install "btwin-mcp @ https://github.com/angelomassafra/btwin_mcp/archive/refs/heads/main.zip"
btwin-mcp # starts the server on stdio
```
or download [`btwin_mcp.py`](btwin_mcp.py), `pip install pandas openpyxl networkx rdflib matplotlib plotly`
and configure the client to run `python /absolute/path/to/btwin_mcp.py`.
</details>
BTWIN requires an LLM with tool-calling support that can write short Python scripts.
Small local models may need more attempts to produce correct scripts.
### Settings
| Environment variable | Default | Meaning |
|---|---|---|
| `BTWIN_WORKDIR` | `Documents/BTWIN` | Folder where relative file paths are read and saved. |
Add it to the `"env"` block of the server entry, e.g. `"env": {"BTWIN_WORKDIR": "D:\\Projects\\twin"}`.
---
## Troubleshooting
| Problem | Solution |
|---|---|
| BTWIN does not appear in the client | Make sure you **fully quit** the application (tray icon / Cmd+Q), not just closed the window, and that it was installed and opened at least once before running the installer. In Claude Desktop, *Settings → Developer* shows the server status and a link to the logs. |
| "Server disconnected" right after starting | Run the installer again: it downloads everything in advance. Check your internet connection / proxy. |
| Installer says the config is not valid JSON | The existing `claude_desktop_config.json` has a syntax error. Fix it (or delete it if you don't use other MCP servers) and run the installer again. |
| Windows blocks `install-windows.bat` | Click **More info → Run anyway**, or use the PowerShell one-liner above. |
| macOS blocks `install-macos.command` | Right-click → **Open**, or use the Terminal one-liner above. |
| The model cannot find a file | Put it in *Documents/BTWIN*, or give the full path. |
| `btwin_exec` returns `ok: false` | The `error` field contains the Python traceback; the model normally corrects the script by itself. |
| Script times out | Ask the model to use a larger `timeout` (default 30 s). |
Still stuck? [Open an issue](https://github.com/angelomassafra/btwin_mcp/issues) and attach the log
of the MCP client (Claude Desktop: *Settings → Developer → btwin → Open Logs Folder*).
---
## For developers
```bash
git clone https://github.com/angelomassafra/btwin_mcp && cd btwin_mcp
uv run --with pytest pytest # end-to-end test over stdio
uv run python examples/generate_examples.py # regenerate the synthetic examples
```
The whole server is a single file, [`btwin_mcp.py`](btwin_mcp.py), with no MCP SDK
dependency (plain JSON-RPC over stdio, protocol `2024-11-05`).
### Version history
| Version | Tools | Notes |
|---------|-------|-------|
| 1.0.0 | 8 | SpatialElement only |
| 2.0.0 | 36 | Point, Observation, PropertySet, Property, Scenario, Schema, Serialization |
| 3.0.0 | 82 | Document, Equipment, KPISet/KPI, Graph, Plot — single file |
| 4.0.0 | 109 | Hierarchy, graph query, JSON-LD ops, batch, CSV, analytics, adjacency |
| 5.0.0 | 2 | Code-execution pattern — `btwin_exec` + `btwin_help`, 94% fewer tokens upfront |
| 5.1.0 | 2 | Packaged for `uvx`/pip, installers for Claude and ChatGPT/Codex, default working folder, UTF-8 on Windows, protocol stream protected from stray output, `ping`, matplotlib ≥ 3.9 fix, synthetic examples |
---
## Citation
If you use BTWIN in research, please cite it (see [`CITATION.cff`](CITATION.cff); GitHub
shows a *"Cite this repository"* button).
## License
Copyright © 2025–2026 Angelo Massafra. Released under the
[GNU General Public License v3.0 or later](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues