Create README.md
Browse files
README.md
CHANGED
|
@@ -1,14 +1,155 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
emoji: π
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: pink
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 6.20.0
|
| 8 |
-
python_version: '3.13'
|
| 9 |
-
app_file: app.py
|
| 10 |
-
pinned: false
|
| 11 |
-
license: mit
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
colorTo: green
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
| 4 |
|
| 5 |
+
# π§οΈ SWMM Analysis MCP Server
|
| 6 |
+
|
| 7 |
+
A dual-surface **MCP + REST** server for deterministic EPA-SWMM stormwater analysis,
|
| 8 |
+
with a **built-in multi-provider agent**. Wraps the SWMM6 GIS Tool engine (Rev 23.2):
|
| 9 |
+
crash-isolated OpenSWMM simulation, result summaries, Calgary-style criteria
|
| 10 |
+
screening, deterministic QA/QC findings, engine-report (`.rpt`) reconciliation,
|
| 11 |
+
controlled scenario comparison, and Calgary SWMR draft report generation.
|
| 12 |
+
|
| 13 |
+
**All outputs are preliminary engineering screening β not professional
|
| 14 |
+
determinations.** Screening thresholds require confirmation by the responsible
|
| 15 |
+
engineer.
|
| 16 |
+
|
| 17 |
+
## Surfaces
|
| 18 |
+
|
| 19 |
+
| Surface | Endpoint | For |
|
| 20 |
+
|---|---|---|
|
| 21 |
+
| **MCP (Streamable HTTP)** | `https://<space-url>/mcp` | Claude Desktop / claude.ai, ChatGPT connectors, Gemini, Codex CLI, LangChain, n8n MCP node, Flowise, Langflow, local MCP clients |
|
| 22 |
+
| **REST / OpenAPI** | `POST /api/tool/{name}` Β· catalog `/api/tools` Β· schema `/openapi.json` | Custom GPT Actions, n8n HTTP nodes, webhooks, anything speaking OpenAPI |
|
| 23 |
+
| **Built-in agent** | `POST /api/agent` and MCP tool `agent_analyze` | One natural-language call; the server's own LLM loop plans and runs the tools |
|
| 24 |
+
| **Files** | `GET /files/{session_id}/{filename}` | Generated SWMR docx / audit-zip downloads |
|
| 25 |
+
|
| 26 |
+
## Tools (16)
|
| 27 |
+
|
| 28 |
+
`upload_model` β `run_simulation` β then: `get_node_results`, `get_link_results`,
|
| 29 |
+
`get_subcatchment_results`, `get_timeseries`, `query_results` (validated JSON plans
|
| 30 |
+
against a 34-table SQLite store incl. the complete tokenized INP), `get_table_catalog`,
|
| 31 |
+
`calgary_screening`, `preliminary_design_review` (deterministic findings register incl.
|
| 32 |
+
RPT-### reconciliation findings), `get_reconciliation`, `run_scenario` (base never
|
| 33 |
+
mutated; deterministic comparison), `generate_report` (SWMR docx + audit zip),
|
| 34 |
+
`list_sessions`, `close_session`, `agent_analyze`.
|
| 35 |
+
|
| 36 |
+
Typical flow: **upload_model β run_simulation β analysis tools**, reusing the
|
| 37 |
+
returned `session_id`. Sessions expire after 6 h of inactivity.
|
| 38 |
+
|
| 39 |
+
## Connecting from each platform
|
| 40 |
+
|
| 41 |
+
Replace `SPACE` with this Space's direct URL, e.g.
|
| 42 |
+
`https://username-swmm-mcp.hf.space` (use the *direct* subdomain, not the
|
| 43 |
+
huggingface.co page URL).
|
| 44 |
+
|
| 45 |
+
### Claude (claude.ai web / desktop, remote connector)
|
| 46 |
+
Settings β Connectors β Add custom connector β URL: `SPACE/mcp`.
|
| 47 |
+
|
| 48 |
+
### Claude Desktop (mcp-remote bridge, or any stdio-only client)
|
| 49 |
+
```json
|
| 50 |
+
{
|
| 51 |
+
"mcpServers": {
|
| 52 |
+
"swmm-analysis": {
|
| 53 |
+
"command": "npx",
|
| 54 |
+
"args": ["-y", "mcp-remote", "SPACE/mcp"]
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### ChatGPT
|
| 61 |
+
Settings β Connectors β Create β MCP server URL: `SPACE/mcp` (Developer mode).
|
| 62 |
+
Alternatively build a **Custom GPT** with Actions: import the schema from
|
| 63 |
+
`SPACE/openapi.json` β the REST surface is designed for this.
|
| 64 |
+
|
| 65 |
+
### Gemini CLI
|
| 66 |
+
```json
|
| 67 |
+
// ~/.gemini/settings.json
|
| 68 |
+
{ "mcpServers": { "swmm-analysis": { "httpUrl": "SPACE/mcp" } } }
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### Codex CLI
|
| 72 |
+
```toml
|
| 73 |
+
# ~/.codex/config.toml
|
| 74 |
+
[mcp_servers.swmm-analysis]
|
| 75 |
+
url = "SPACE/mcp"
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
### n8n
|
| 79 |
+
- **MCP Client Tool node**: endpoint `SPACE/mcp`, transport *HTTP Streamable*.
|
| 80 |
+
- Or plain **HTTP Request** nodes against `POST SPACE/api/tool/{name}`.
|
| 81 |
+
- Or one-shot: `POST SPACE/api/agent` with `{"question": "...", "provider": "gemini"}`.
|
| 82 |
+
|
| 83 |
+
### Flowise / Langflow
|
| 84 |
+
Add the **Custom MCP** (Flowise) / **MCP Tools** (Langflow) component with a
|
| 85 |
+
Streamable-HTTP config pointing at `SPACE/mcp`; the tool list auto-populates.
|
| 86 |
+
|
| 87 |
+
### LangChain / LangGraph
|
| 88 |
+
```python
|
| 89 |
+
from langchain_mcp_adapters.client import MultiServerMCPClient
|
| 90 |
+
client = MultiServerMCPClient({
|
| 91 |
+
"swmm": {"url": "SPACE/mcp", "transport": "streamable_http"}
|
| 92 |
+
})
|
| 93 |
+
tools = await client.get_tools() # bind to any model
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
### Local LLMs (Ollama, LM Studio, vLLM)
|
| 97 |
+
Two options:
|
| 98 |
+
1. Point any local MCP-capable client (e.g. LM Studio, mcp-use) at `SPACE/mcp`.
|
| 99 |
+
2. Reverse: let the **server's agent** use your local model β
|
| 100 |
+
`POST /api/agent` with `{"provider": "local", "base_url": "http://host:11434/v1", "model": "llama3.1", "question": "..."}`.
|
| 101 |
+
|
| 102 |
+
## Built-in agent
|
| 103 |
+
|
| 104 |
+
`POST /api/agent` body:
|
| 105 |
+
```json
|
| 106 |
+
{
|
| 107 |
+
"question": "Upload this model, run it, and screen velocities against Calgary criteria.",
|
| 108 |
+
"provider": "anthropic",
|
| 109 |
+
"inp_content": "<.inp text or base64>",
|
| 110 |
+
"session_id": "optional-existing-session",
|
| 111 |
+
"allow_report": false
|
| 112 |
+
}
|
| 113 |
+
```
|
| 114 |
+
Providers: `anthropic`, `openai`, `gemini`, `groq`, `mistral`, `local`
|
| 115 |
+
(OpenAI-compatible `base_url`). Keys come from **Space secrets**
|
| 116 |
+
(`ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY`, `GROQ_API_KEY`,
|
| 117 |
+
`MISTRAL_API_KEY`) or a per-request `api_key`. Every response includes the
|
| 118 |
+
complete `tool_trace` audit trail (tool, arguments, elapsed time, result
|
| 119 |
+
preview) β the agent may only report numbers that came from tools.
|
| 120 |
+
|
| 121 |
+
> If your platform is already an MCP client, prefer driving the tools directly:
|
| 122 |
+
> your model is the agent, and it sees full (not previewed) tool results.
|
| 123 |
+
|
| 124 |
+
## Architecture notes
|
| 125 |
+
|
| 126 |
+
- **Crash isolation**: the native OpenSWMM engine runs only in a one-shot
|
| 127 |
+
subprocess in its own venv (`/opt/swmm-venv`); a segfaulting model cannot
|
| 128 |
+
take down the server.
|
| 129 |
+
- **Single source of truth**: MCP, REST, and the agent all dispatch to the
|
| 130 |
+
same typed registry (`tools.py`), so behaviour is identical per platform.
|
| 131 |
+
- **Result integrity**: every simulation is cross-checked against the
|
| 132 |
+
engine's own `.rpt` (velocity/flow/depth/continuity); disagreements surface
|
| 133 |
+
as `RPT-###` findings and the verdict is embedded in report manifests.
|
| 134 |
+
- **HF Spaces specifics baked in**: DNS-rebinding protection disabled for the
|
| 135 |
+
Spaces proxy, MCP session-manager lifespan wired into FastAPI, stateless
|
| 136 |
+
HTTP + JSON responses for maximum connector compatibility.
|
| 137 |
+
- **Sessions**: process-local with TTL sweep; Space restarts clear state
|
| 138 |
+
(persistent storage is not required for the workflow).
|
| 139 |
+
|
| 140 |
+
## Security
|
| 141 |
+
|
| 142 |
+
No authentication is enabled by default β anyone with the URL can run models
|
| 143 |
+
and (if Space secrets are set) spend your LLM keys via `/api/agent`. For
|
| 144 |
+
non-demo use, set the Space to private, or front it with an auth proxy, and
|
| 145 |
+
prefer per-request `api_key` over Space secrets.
|
| 146 |
+
|
| 147 |
+
## Local development
|
| 148 |
+
|
| 149 |
+
```bash
|
| 150 |
+
pip install -r requirements.txt
|
| 151 |
+
python -m venv /opt/swmm-venv && /opt/swmm-venv/bin/pip install -r worker-requirements.txt
|
| 152 |
+
SWMM_WORKER_PYTHON=/opt/swmm-venv/bin/python uvicorn server:app --port 7860
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
Free and non-commercial, for the water-engineering community.
|