File size: 10,366 Bytes
eb20770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Quantum ESPRESSO MCP Server - Gradio Demo

A demonstration interface for the QE-MCP server that enables
LLMs to run DFT calculations with natural language.
"""

import gradio as gr
import json

# Demo data (since we can't run Docker on HuggingFace Spaces)
DEMO_RESULTS = {
    "Si": {
        "scf": {
            "success": True,
            "total_energy_eV": -214.4906,
            "fermi_energy_eV": 6.2975,
            "converged": True,
            "n_iterations": 4,
            "parameters_used": {"spin_polarized": False, "smearing": "cold", "degauss": 0.02}
        },
        "bandstructure": {
            "success": True,
            "band_gap_eV": 0.56,
            "is_direct": False,
            "vbm_location": "Γ",
            "cbm_location": "X",
            "fermi_energy_eV": 6.2975
        }
    },
    "Fe": {
        "scf": {
            "success": True,
            "total_energy_eV": -3220.5287,
            "fermi_energy_eV": 17.8432,
            "total_magnetization": 7.63,
            "converged": True,
            "n_iterations": 12,
            "parameters_used": {"spin_polarized": True, "smearing": "cold", "degauss": 0.02}
        }
    },
    "Cu": {
        "scf": {
            "success": True,
            "total_energy_eV": -1653.2341,
            "fermi_energy_eV": 12.4521,
            "converged": True,
            "n_iterations": 6,
            "parameters_used": {"spin_polarized": False, "smearing": "cold", "degauss": 0.02}
        }
    },
    "GaAs": {
        "scf": {
            "success": True,
            "total_energy_eV": -312.8765,
            "fermi_energy_eV": 5.1234,
            "converged": True,
            "n_iterations": 5,
            "parameters_used": {"spin_polarized": False, "smearing": "cold", "degauss": 0.02}
        },
        "bandstructure": {
            "success": True,
            "band_gap_eV": 0.48,
            "is_direct": True,
            "vbm_location": "Γ",
            "cbm_location": "Γ",
            "fermi_energy_eV": 5.1234
        }
    }
}

AVAILABLE_ELEMENTS = [
    "Ag", "Al", "Ar", "As", "Au", "B", "Ba", "Be", "Bi", "Br", "C", "Ca", "Cd", "Cl", 
    "Co", "Cr", "Cs", "Cu", "F", "Fe", "Ga", "Ge", "H", "He", "Hf", "Hg", "I", "In", 
    "Ir", "K", "Kr", "La", "Li", "Mg", "Mn", "Mo", "N", "Na", "Nb", "Ne", "Ni", "O", 
    "Os", "P", "Pb", "Pd", "Pt", "Rb", "Re", "Rh", "Ru", "S", "Sb", "Sc", "Se", "Si", 
    "Sn", "Sr", "Ta", "Tc", "Te", "Ti", "Tl", "V", "W", "Xe", "Y", "Zn", "Zr"
]

MCP_TOOLS = """
## 🔧 Available MCP Tools

| Tool | Description |
|------|-------------|
| `qe_run_scf` | Self-consistent field calculation (total energy, Fermi level) |
| `qe_run_relax` | Optimize atomic positions |
| `qe_run_vc_relax` | Variable-cell relaxation (optimize positions AND cell) |
| `qe_workflow_bandstructure` | Complete band structure workflow |
| `qe_workflow_dos` | Density of states calculation |
| `qe_workflow_relax_and_scf` | Relax structure then accurate SCF |
| `qe_load_structure` | Load and inspect atomic structures |
| `qe_get_kpath` | Get high-symmetry k-path for band structure |
| `qe_suggest_kpoints` | Suggest k-point grid based on cell size |
| `qe_list_pseudopotentials` | List available elements (69 total) |
| `qe_validate_structure` | Validate structure and check for issues |
| `qe_status` | Get QE MCP server status |
"""

def run_scf_demo(material: str) -> str:
    """Simulate SCF calculation"""
    material = material.strip()
    if material in DEMO_RESULTS:
        result = DEMO_RESULTS[material]["scf"]
        output = f"""## ⚡ SCF Calculation: {material}

✅ **Success**: {result['success']}
🔋 **Total Energy**: {result['total_energy_eV']:.4f} eV
📊 **Fermi Energy**: {result['fermi_energy_eV']:.4f} eV
🔄 **Converged**: {result['converged']} ({result['n_iterations']} iterations)
"""
        if result.get('total_magnetization'):
            output += f"🧲 **Magnetization**: {result['total_magnetization']:.2f} μB\n"
        output += f"\n⚙️ **Auto-detected parameters**: {json.dumps(result['parameters_used'])}"
        return output
    else:
        return f"""## ⚡ SCF Calculation: {material}

This is a **demo** showing the MCP tool interface.

In the full version, calling `qe_run_scf(structure='{material}')` would:
1. Build the crystal structure using ASE
2. Generate QE input files
3. Run pw.x in Docker container
4. Parse and return results

**Supported elements**: {', '.join(AVAILABLE_ELEMENTS)}
"""

def run_bandstructure_demo(material: str) -> str:
    """Simulate band structure calculation"""
    material = material.strip()
    if material in DEMO_RESULTS and "bandstructure" in DEMO_RESULTS[material]:
        result = DEMO_RESULTS[material]["bandstructure"]
        gap_type = "direct" if result['is_direct'] else "indirect"
        return f"""## 📈 Band Structure: {material}

✅ **Success**: {result['success']}
🎯 **Band Gap**: {result['band_gap_eV']:.2f} eV ({gap_type})
📍 **VBM Location**: {result['vbm_location']}
📍 **CBM Location**: {result['cbm_location']}
📊 **Fermi Energy**: {result['fermi_energy_eV']:.4f} eV

### Interpretation
{"This is a **semiconductor** with a " + gap_type + " band gap." if result['band_gap_eV'] > 0 else "This is a **metal**."}
"""
    else:
        return f"""## 📈 Band Structure: {material}

This is a **demo** showing the MCP tool interface.

In the full version, calling `qe_workflow_bandstructure(structure='{material}')` would:
1. Run SCF calculation
2. Get high-symmetry k-path (Γ-X-W-K-Γ-L-U-W-L-K)
3. Calculate bands along path
4. Analyze band gap

**Try**: Si, GaAs (have demo results)
"""

def show_mcp_config() -> str:
    """Show MCP configuration for Claude Desktop"""
    return """## 🔧 Claude Desktop Configuration

Add this to `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS):

```json
{
  "mcpServers": {
    "quantum-espresso": {
      "command": "uv",
      "args": ["--directory", "/path/to/QE_MCP", "run", "qe-mcp"]
    }
  }
}
```

Then restart Claude Desktop and you can say:
- *"Calculate the total energy of silicon"*
- *"What's the band gap of GaAs?"*
- *"Run a spin-polarized calculation for iron"*

## 📋 Requirements

- Docker (with `qe-local` image)
- Python 3.10+
- uv package manager
"""

def list_elements() -> str:
    """List all available elements"""
    elements_grid = ""
    for i, elem in enumerate(AVAILABLE_ELEMENTS):
        elements_grid += f"`{elem}` "
        if (i + 1) % 10 == 0:
            elements_grid += "\n"
    
    return f"""## 🧪 Supported Elements (69 total)

SG15 ONCV Pseudopotential Library:

{elements_grid}

### Magnetic Elements (auto spin-polarized)
`Fe` `Co` `Ni` `Mn` `Cr` `V` `Gd` `Eu` `Tb` `Dy` `Ho` `Er`
"""

# Create Gradio Interface
with gr.Blocks(
    title="⚛️ Quantum ESPRESSO MCP Server",
    theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple"),
    css="""
    .gradio-container { max-width: 1200px !important; }
    .tool-card { border: 1px solid #e0e0e0; border-radius: 8px; padding: 16px; margin: 8px 0; }
    """
) as demo:
    gr.Markdown("""
    # ⚛️ Quantum ESPRESSO MCP Server
    
    > **Run DFT calculations with natural language!** An MCP server that enables LLMs to perform 
    > first-principles quantum mechanical simulations.
    
    [![MCP Compatible](https://img.shields.io/badge/MCP-Compatible-blue)](https://modelcontextprotocol.io)
    [![Quantum ESPRESSO](https://img.shields.io/badge/QE-v6.7-green)](https://www.quantum-espresso.org/)
    
    ⚠️ **Note**: This is a demo interface. The full MCP server runs locally with Docker.
    """)
    
    with gr.Tabs():
        with gr.Tab("🧪 Try It"):
            gr.Markdown("### Simulate MCP Tool Calls")
            
            with gr.Row():
                with gr.Column():
                    material_input = gr.Textbox(
                        label="Material Formula",
                        placeholder="Si, Fe, Cu, GaAs...",
                        value="Si"
                    )
                    with gr.Row():
                        scf_btn = gr.Button("⚡ Run SCF", variant="primary")
                        band_btn = gr.Button("📈 Band Structure", variant="secondary")
                
                with gr.Column():
                    output = gr.Markdown(label="Result")
            
            scf_btn.click(run_scf_demo, inputs=[material_input], outputs=[output])
            band_btn.click(run_bandstructure_demo, inputs=[material_input], outputs=[output])
            
            gr.Markdown("**Demo materials**: Si, Fe, Cu, GaAs")
        
        with gr.Tab("🔧 MCP Tools"):
            gr.Markdown(MCP_TOOLS)
            
            gr.Markdown("""
            ### 🎯 8 Prompts for Guided Workflows
            
            | Prompt | Description |
            |--------|-------------|
            | `band_structure` | Calculate electronic band structure |
            | `dos_calculation` | Density of states workflow |
            | `geometry_optimization` | Structure relaxation steps |
            | `convergence_test` | Parameter convergence testing |
            | `surface_calculation` | Surface energy calculations |
            | `magnetic_calculation` | Magnetic properties (Fe, Ni, Co) |
            | `troubleshoot` | Diagnose calculation problems |
            | `compare_structures` | Compare multiple structures |
            """)
        
        with gr.Tab("⚙️ Setup"):
            config_output = gr.Markdown(value=show_mcp_config())
        
        with gr.Tab("🧪 Elements"):
            elements_output = gr.Markdown(value=list_elements())
    
    gr.Markdown("""
    ---
    
    ### 🏗️ Architecture
    
    ```
    User (natural language) → LLM (Claude/GPT) → MCP Protocol → QE-MCP Server → Docker (QE v6.7) → Results
    ```
    
    ### 🛠️ Tech Stack
    - **Quantum ESPRESSO v6.7MaX** - DFT engine
    - **MCP SDK** (mcp>=1.0.0) - Model Context Protocol  
    - **ASE 3.26** - Atomic Simulation Environment
    - **Docker** - Containerized QE
    - **SG15 ONCV** - 69 element pseudopotentials
    
    ---
    
    *Built for [MCP's 1st Birthday Hackathon](https://huggingface.co/MCP-1st-Birthday) 🎂 by [@frimpsjoe](https://huggingface.co/frimpsjoe)*
    """)

if __name__ == "__main__":
    demo.launch()