Spaces:
Running
Running
kabudadada
commited on
Commit
·
06f289c
1
Parent(s):
9d25df1
Update MCP service with core Foam-Agent functionality based on official repository
Browse files
Foam-Agent/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -2,7 +2,8 @@ import os
|
|
| 2 |
import sys
|
| 3 |
import tempfile
|
| 4 |
import subprocess
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 8 |
mcp_plugin_dir = os.path.join(project_root, "mcp_plugin")
|
|
@@ -19,6 +20,17 @@ mcp = FastMCP("FoamAgentService")
|
|
| 19 |
|
| 20 |
@mcp.tool(name="run_foam_agent", description="Run Foam-Agent workflow with natural language requirements")
|
| 21 |
def run_foam_agent(requirements: str, output_dir: str = "./output", custom_mesh: str = None) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
| 24 |
f.write(requirements)
|
|
@@ -46,6 +58,19 @@ def run_foam_agent(requirements: str, output_dir: str = "./output", custom_mesh:
|
|
| 46 |
|
| 47 |
@mcp.tool(name="run_foam_benchmark", description="Run complete Foam-Agent benchmark with OpenFOAM preprocessing")
|
| 48 |
def run_foam_benchmark(openfoam_path: str, requirements: str, output_dir: str = "./output", custom_mesh: str = None) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
| 51 |
f.write(requirements)
|
|
@@ -72,109 +97,56 @@ def run_foam_benchmark(openfoam_path: str, requirements: str, output_dir: str =
|
|
| 72 |
except Exception as e:
|
| 73 |
return {"status": "error", "message": str(e)}
|
| 74 |
|
| 75 |
-
@mcp.tool(name="
|
| 76 |
-
def
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"api_key_configured": False,
|
| 81 |
-
"api_url_configured": False,
|
| 82 |
-
"dependencies_installed": False
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
# Check OpenFOAM
|
| 86 |
-
openfoam_dir = os.environ.get('WM_PROJECT_DIR')
|
| 87 |
-
if openfoam_dir and os.path.exists(openfoam_dir):
|
| 88 |
-
status["openfoam_installed"] = True
|
| 89 |
-
|
| 90 |
-
# Check database
|
| 91 |
-
database_path = os.path.join(source_path, "database", "faiss")
|
| 92 |
-
if os.path.exists(database_path):
|
| 93 |
-
required_dbs = [
|
| 94 |
-
"openfoam_allrun_scripts",
|
| 95 |
-
"openfoam_tutorials_structure",
|
| 96 |
-
"openfoam_tutorials_details",
|
| 97 |
-
"openfoam_command_help"
|
| 98 |
-
]
|
| 99 |
-
status["database_initialized"] = all(
|
| 100 |
-
os.path.exists(os.path.join(database_path, db)) for db in required_dbs
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
# Check API key
|
| 104 |
-
api_key = os.environ.get('OPENAI_API_KEY')
|
| 105 |
-
if api_key:
|
| 106 |
-
status["api_key_configured"] = True
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
if api_url:
|
| 111 |
-
status["api_url_configured"] = True
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
try:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
@mcp.tool(name="get_foam_agent_info", description="Get Foam-Agent system information")
|
| 132 |
-
def get_foam_agent_info() -> Dict[str, Any]:
|
| 133 |
-
return {
|
| 134 |
-
"status": "success",
|
| 135 |
-
"info": {
|
| 136 |
-
"name": "Foam-Agent",
|
| 137 |
-
"description": "Multi-agent framework for OpenFOAM CFD simulations",
|
| 138 |
-
"version": "1.0.0",
|
| 139 |
-
"capabilities": [
|
| 140 |
-
"Natural language to OpenFOAM workflow",
|
| 141 |
-
"Multi-agent architecture with LangGraph",
|
| 142 |
-
"FAISS vector database for knowledge retrieval",
|
| 143 |
-
"Automatic error correction and iteration",
|
| 144 |
-
"Custom mesh support (GMSH .msh files)",
|
| 145 |
-
"Multiple LLM provider support"
|
| 146 |
-
],
|
| 147 |
-
"requirements": [
|
| 148 |
-
"OpenFOAM v10 installation",
|
| 149 |
-
"OpenAI API key or other LLM provider",
|
| 150 |
-
"Preprocessed OpenFOAM database",
|
| 151 |
-
"Python dependencies (langchain, faiss, etc.)"
|
| 152 |
-
]
|
| 153 |
-
}
|
| 154 |
-
}
|
| 155 |
|
| 156 |
-
@mcp.tool(name="validate_requirements", description="Validate user requirements for CFD simulation")
|
| 157 |
-
def validate_requirements(requirements: str) -> Dict[str, Any]:
|
| 158 |
-
required_keywords = ["flow", "velocity", "pressure", "boundary", "mesh"]
|
| 159 |
-
found_keywords = [kw for kw in required_keywords if kw in requirements.lower()]
|
| 160 |
-
|
| 161 |
-
return {
|
| 162 |
-
"status": "success",
|
| 163 |
-
"validation": {
|
| 164 |
-
"has_flow_info": "flow" in requirements.lower(),
|
| 165 |
-
"has_boundary_conditions": "boundary" in requirements.lower(),
|
| 166 |
-
"has_geometry": "mesh" in requirements.lower() or "geometry" in requirements.lower(),
|
| 167 |
-
"found_keywords": found_keywords,
|
| 168 |
-
"completeness": len(found_keywords) / len(required_keywords),
|
| 169 |
-
"suggestions": [
|
| 170 |
-
"Include flow type (incompressible/compressible)",
|
| 171 |
-
"Specify boundary conditions clearly",
|
| 172 |
-
"Define geometry or mesh requirements",
|
| 173 |
-
"Set physical parameters (viscosity, density, etc.)",
|
| 174 |
-
"Specify solver preferences if any"
|
| 175 |
-
]
|
| 176 |
-
}
|
| 177 |
-
}
|
| 178 |
|
| 179 |
def create_app():
|
| 180 |
return mcp
|
|
|
|
| 2 |
import sys
|
| 3 |
import tempfile
|
| 4 |
import subprocess
|
| 5 |
+
import json
|
| 6 |
+
from typing import Dict, Any, List, Optional
|
| 7 |
|
| 8 |
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
mcp_plugin_dir = os.path.join(project_root, "mcp_plugin")
|
|
|
|
| 20 |
|
| 21 |
@mcp.tool(name="run_foam_agent", description="Run Foam-Agent workflow with natural language requirements")
|
| 22 |
def run_foam_agent(requirements: str, output_dir: str = "./output", custom_mesh: str = None) -> Dict[str, Any]:
|
| 23 |
+
"""
|
| 24 |
+
Run Foam-Agent workflow with natural language requirements.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
requirements: Natural language description of the CFD simulation
|
| 28 |
+
output_dir: Output directory for simulation results
|
| 29 |
+
custom_mesh: Optional path to custom GMSH .msh file
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
Dict containing status, output, and case directory
|
| 33 |
+
"""
|
| 34 |
try:
|
| 35 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
| 36 |
f.write(requirements)
|
|
|
|
| 58 |
|
| 59 |
@mcp.tool(name="run_foam_benchmark", description="Run complete Foam-Agent benchmark with OpenFOAM preprocessing")
|
| 60 |
def run_foam_benchmark(openfoam_path: str, requirements: str, output_dir: str = "./output", custom_mesh: str = None) -> Dict[str, Any]:
|
| 61 |
+
"""
|
| 62 |
+
Run complete Foam-Agent benchmark with OpenFOAM preprocessing.
|
| 63 |
+
This includes database initialization and full workflow execution.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
openfoam_path: Path to OpenFOAM installation (WM_PROJECT_DIR)
|
| 67 |
+
requirements: Natural language description of the CFD simulation
|
| 68 |
+
output_dir: Output directory for simulation results
|
| 69 |
+
custom_mesh: Optional path to custom GMSH .msh file
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
Dict containing status, output, and case directory
|
| 73 |
+
"""
|
| 74 |
try:
|
| 75 |
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
| 76 |
f.write(requirements)
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
return {"status": "error", "message": str(e)}
|
| 99 |
|
| 100 |
+
@mcp.tool(name="preprocess_database", description="Preprocess OpenFOAM database for Foam-Agent")
|
| 101 |
+
def preprocess_database(openfoam_path: str) -> Dict[str, Any]:
|
| 102 |
+
"""
|
| 103 |
+
Preprocess OpenFOAM database for Foam-Agent.
|
| 104 |
+
This runs all necessary preprocessing scripts.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
Args:
|
| 107 |
+
openfoam_path: Path to OpenFOAM installation (WM_PROJECT_DIR)
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
Returns:
|
| 110 |
+
Dict containing status and preprocessing results
|
| 111 |
+
"""
|
| 112 |
try:
|
| 113 |
+
scripts = [
|
| 114 |
+
"database/script/tutorial_parser.py",
|
| 115 |
+
"database/script/faiss_command_help.py",
|
| 116 |
+
"database/script/faiss_allrun_scripts.py",
|
| 117 |
+
"database/script/faiss_tutorials_structure.py",
|
| 118 |
+
"database/script/faiss_tutorials_details.py"
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
results = []
|
| 122 |
+
for script in scripts:
|
| 123 |
+
script_path = os.path.join(source_path, script)
|
| 124 |
+
if os.path.exists(script_path):
|
| 125 |
+
if "tutorial_parser.py" in script:
|
| 126 |
+
cmd = ["python", script_path, "--output_dir=./database/raw", f"--wm_project_dir={openfoam_path}"]
|
| 127 |
+
else:
|
| 128 |
+
cmd = ["python", script_path, "--database_path=./database"]
|
| 129 |
+
|
| 130 |
+
result = subprocess.run(cmd, cwd=source_path, capture_output=True, text=True, timeout=1800)
|
| 131 |
+
results.append({
|
| 132 |
+
"script": script,
|
| 133 |
+
"status": "success" if result.returncode == 0 else "error",
|
| 134 |
+
"output": result.stdout,
|
| 135 |
+
"error": result.stderr
|
| 136 |
+
})
|
| 137 |
+
else:
|
| 138 |
+
results.append({
|
| 139 |
+
"script": script,
|
| 140 |
+
"status": "error",
|
| 141 |
+
"output": "",
|
| 142 |
+
"error": f"Script not found: {script_path}"
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
return {"status": "success", "results": results}
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return {"status": "error", "message": str(e)}
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
def create_app():
|
| 152 |
return mcp
|