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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
btn_read.click(read_word, inputs=[read_path], outputs=[read_output])
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
btn_create.click(create_word, inputs=[create_path, create_content], outputs=[create_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
demo.launch(mcp_server=True)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from server import read_word, create_word
|
| 5 |
|
| 6 |
+
# Custom CSS for a more beautiful interface
|
| 7 |
+
custom_css = """
|
| 8 |
+
.gradio-container {
|
| 9 |
+
font-family: 'Inter', sans-serif;
|
| 10 |
+
}
|
| 11 |
+
.main-header {
|
| 12 |
+
text-align: center;
|
| 13 |
+
padding: 2rem;
|
| 14 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 15 |
+
color: white;
|
| 16 |
+
border-radius: 10px;
|
| 17 |
+
margin-bottom: 2rem;
|
| 18 |
+
}
|
| 19 |
+
.info-box {
|
| 20 |
+
background: #f8f9fa;
|
| 21 |
+
border-left: 4px solid #667eea;
|
| 22 |
+
padding: 1rem;
|
| 23 |
+
border-radius: 5px;
|
| 24 |
+
margin: 1rem 0;
|
| 25 |
+
}
|
| 26 |
+
.step-box {
|
| 27 |
+
background: #ffffff;
|
| 28 |
+
border: 2px solid #e9ecef;
|
| 29 |
+
padding: 1rem;
|
| 30 |
+
border-radius: 8px;
|
| 31 |
+
margin: 0.5rem 0;
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 36 |
+
|
| 37 |
+
# Welcome Tab
|
| 38 |
+
with gr.Tab("π Welcome & Setup"):
|
| 39 |
+
gr.Markdown("""
|
| 40 |
+
<div class="main-header">
|
| 41 |
+
<h1>π Gradio MCP Server - Word Reader/Writer</h1>
|
| 42 |
+
<p style="font-size: 1.2em; margin-top: 1rem;">Connect your LLM to Word documents using the Model Context Protocol</p>
|
| 43 |
+
</div>
|
| 44 |
+
""")
|
| 45 |
+
|
| 46 |
+
gr.Markdown("""
|
| 47 |
+
## π― About This Project
|
| 48 |
+
|
| 49 |
+
This project demonstrates how to use **Gradio MCP** integration to create an MCP Server that allows AI assistants
|
| 50 |
+
to read and create Word documents. Built for the **MCP 1st Birthday Hackathon**, it showcases the power of combining:
|
| 51 |
+
|
| 52 |
+
- **Gradio**: Easy-to-use Python library for creating web interfaces
|
| 53 |
+
- **MCP (Model Context Protocol)**: Standardized way for LLMs to interact with external tools
|
| 54 |
+
- **python-docx**: Library for working with Word documents
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## π How This Project Uses Gradio MCP
|
| 59 |
+
|
| 60 |
+
This project leverages Gradio's MCP integration to automatically expose Python functions as MCP tools.
|
| 61 |
+
Here's how it works:
|
| 62 |
+
""")
|
| 63 |
+
|
| 64 |
+
with gr.Accordion("π Understanding the Architecture", open=False):
|
| 65 |
+
gr.Markdown("""
|
| 66 |
+
### Server Architecture (`server.py`)
|
| 67 |
+
|
| 68 |
+
The core functionality is in `server.py`, which contains:
|
| 69 |
+
- `read_word(path)`: Reads content from Word files
|
| 70 |
+
- `create_word(path, content)`: Creates new Word documents
|
| 71 |
+
|
| 72 |
+
### Gradio Interface (`app.py`)
|
| 73 |
+
|
| 74 |
+
The Gradio app imports these functions and:
|
| 75 |
+
1. **Creates a web UI** for human interaction
|
| 76 |
+
2. **Automatically exposes them as MCP tools** by setting `mcp_server=True`
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
demo.launch(mcp_server=True)
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
This single parameter converts your Gradio functions into MCP-compatible tools that AI assistants can call!
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
gr.Markdown("""
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
## π§ How to Connect This Server to Your AI Assistant
|
| 89 |
+
|
| 90 |
+
### Option 1: Using MCPHost (Recommended)
|
| 91 |
+
|
| 92 |
+
**MCPHost** is a client application that can connect to MCP servers. Here's how to connect:
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
with gr.Accordion("π Step-by-Step MCPHost Configuration", open=True):
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
<div class="step-box">
|
| 98 |
+
|
| 99 |
+
**Step 1: Get Your MCP Server URL**
|
| 100 |
+
|
| 101 |
+
If hosted on Hugging Face Spaces, your MCP endpoint is:
|
| 102 |
+
```
|
| 103 |
+
https://YOUR-SPACE-NAME.hf.space/gradio_api/mcp/
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
For this project:
|
| 107 |
+
```
|
| 108 |
+
https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/
|
| 109 |
+
```
|
| 110 |
+
</div>
|
| 111 |
+
|
| 112 |
+
<div class="step-box">
|
| 113 |
+
|
| 114 |
+
**Step 2: Configure MCPHost**
|
| 115 |
+
|
| 116 |
+
Add this configuration to your MCPHost settings file:
|
| 117 |
+
|
| 118 |
+
**For JSON format (`config.json`):**
|
| 119 |
+
```json
|
| 120 |
+
{
|
| 121 |
+
"mcpServers": {
|
| 122 |
+
"word-docx-server": {
|
| 123 |
+
"type": "remote",
|
| 124 |
+
"url": "https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/",
|
| 125 |
+
"options": {
|
| 126 |
+
"allowed_directories": ["C:/Users/YourUser/Documents"]
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
**For YAML format (`config.yaml`):**
|
| 134 |
+
```yaml
|
| 135 |
+
mcpServers:
|
| 136 |
+
word-docx-server:
|
| 137 |
+
type: "remote"
|
| 138 |
+
url: "https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/"
|
| 139 |
+
options:
|
| 140 |
+
allowed_directories:
|
| 141 |
+
- "C:/Users/YourUser/Documents"
|
| 142 |
+
```
|
| 143 |
+
</div>
|
| 144 |
+
|
| 145 |
+
<div class="step-box">
|
| 146 |
+
|
| 147 |
+
**Step 3: Set Allowed Directories**
|
| 148 |
+
|
| 149 |
+
β οΈ **Important**: For security, specify which directories the MCP server can access:
|
| 150 |
+
- Add the paths where your Word documents are located
|
| 151 |
+
- Use absolute paths
|
| 152 |
+
- Multiple directories can be added
|
| 153 |
+
</div>
|
| 154 |
+
|
| 155 |
+
<div class="step-box">
|
| 156 |
+
|
| 157 |
+
**Step 4: Restart Your MCP Client**
|
| 158 |
+
|
| 159 |
+
After configuration, restart your MCP client (MCPHost, Claude Desktop, etc.) to load the new tools.
|
| 160 |
+
</div>
|
| 161 |
+
""")
|
| 162 |
+
|
| 163 |
+
gr.Markdown("""
|
| 164 |
+
### Option 2: Using Claude Desktop
|
| 165 |
+
|
| 166 |
+
Edit your `claude_desktop_config.json` file (usually in `~/Library/Application Support/Claude/` on Mac
|
| 167 |
+
or `%APPDATA%/Claude/` on Windows):
|
| 168 |
+
""")
|
| 169 |
+
|
| 170 |
+
with gr.Accordion("βοΈ Claude Desktop Configuration", open=False):
|
| 171 |
+
gr.Markdown("""
|
| 172 |
+
```json
|
| 173 |
+
{
|
| 174 |
+
"mcpServers": {
|
| 175 |
+
"word-docx-tools": {
|
| 176 |
+
"command": "npx",
|
| 177 |
+
"args": [
|
| 178 |
+
"mcp-remote",
|
| 179 |
+
"https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/sse"
|
| 180 |
+
]
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
Then restart Claude Desktop.
|
| 187 |
+
""")
|
| 188 |
+
|
| 189 |
+
gr.Markdown("""
|
| 190 |
+
---
|
| 191 |
+
|
| 192 |
+
## π Learn More About Gradio MCP
|
| 193 |
+
|
| 194 |
+
This project is based on the official Gradio MCP documentation. To learn more:
|
| 195 |
+
|
| 196 |
+
π **[Gradio MCP Integration Guide](https://huggingface.co/learn/mcp-course/unit1/gradio-mcp)**
|
| 197 |
+
|
| 198 |
+
Key concepts covered:
|
| 199 |
+
- How Gradio automatically converts functions to MCP tools
|
| 200 |
+
- Type hints and docstrings for proper tool definitions
|
| 201 |
+
- Hosting MCP servers on Hugging Face Spaces
|
| 202 |
+
- Troubleshooting connection issues
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
## π§ͺ Try It Out
|
| 207 |
+
|
| 208 |
+
Once connected, you can ask your AI assistant:
|
| 209 |
+
|
| 210 |
+
- *"Read the content of C:/Documents/report.docx"*
|
| 211 |
+
- *"Create a new Word document at C:/Documents/summary.docx with this content: [your text]"*
|
| 212 |
+
- *"Extract text from my meeting notes document"*
|
| 213 |
+
|
| 214 |
+
The AI will automatically use the MCP tools to interact with your Word files!
|
| 215 |
+
""")
|
| 216 |
+
|
| 217 |
+
# Read Word Tab
|
| 218 |
+
with gr.Tab("π Read Word Document"):
|
| 219 |
+
gr.Markdown("""
|
| 220 |
+
### Extract content from Word files
|
| 221 |
+
Enter the full path to a Word document to read its content.
|
| 222 |
+
""")
|
| 223 |
+
|
| 224 |
+
with gr.Row():
|
| 225 |
+
with gr.Column(scale=2):
|
| 226 |
+
read_path = gr.Textbox(
|
| 227 |
+
label="π Word File Path",
|
| 228 |
+
placeholder="e.g., C:/Users/YourName/Documents/document.docx",
|
| 229 |
+
info="Enter the absolute path to your Word file"
|
| 230 |
+
)
|
| 231 |
+
btn_read = gr.Button("π Read Document", variant="primary", size="lg")
|
| 232 |
+
|
| 233 |
+
read_output = gr.Textbox(
|
| 234 |
+
label="π Document Content",
|
| 235 |
+
lines=15,
|
| 236 |
+
placeholder="The content of your Word document will appear here...",
|
| 237 |
+
show_copy_button=True
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
btn_read.click(read_word, inputs=[read_path], outputs=[read_output])
|
| 241 |
+
|
| 242 |
+
gr.Markdown("""
|
| 243 |
+
π‘ **Tip**: You can also copy the output using the copy button in the top-right corner of the output box.
|
| 244 |
+
""")
|
| 245 |
+
|
| 246 |
+
# Create Word Tab
|
| 247 |
+
with gr.Tab("βοΈ Create Word Document"):
|
| 248 |
+
gr.Markdown("""
|
| 249 |
+
### Generate new Word documents
|
| 250 |
+
Enter the path where you want to save the document and the content you want to include.
|
| 251 |
+
""")
|
| 252 |
+
|
| 253 |
+
with gr.Row():
|
| 254 |
+
with gr.Column():
|
| 255 |
+
create_path = gr.Textbox(
|
| 256 |
+
label="πΎ Save Path",
|
| 257 |
+
placeholder="e.g., C:/Users/YourName/Documents/new_document.docx",
|
| 258 |
+
info="Enter the absolute path where the file will be created"
|
| 259 |
+
)
|
| 260 |
+
create_content = gr.Textbox(
|
| 261 |
+
label="π Document Content",
|
| 262 |
+
lines=12,
|
| 263 |
+
placeholder="Enter the content for your Word document...\nYou can use multiple lines.",
|
| 264 |
+
info="Each line will be a separate paragraph in the document"
|
| 265 |
+
)
|
| 266 |
+
btn_create = gr.Button("π Create Document", variant="primary", size="lg")
|
| 267 |
+
|
| 268 |
+
create_output = gr.Textbox(
|
| 269 |
+
label="β
Status",
|
| 270 |
+
lines=3,
|
| 271 |
+
placeholder="Creation status will appear here..."
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
btn_create.click(create_word, inputs=[create_path, create_content], outputs=[create_output])
|
| 275 |
+
|
| 276 |
+
gr.Markdown("""
|
| 277 |
+
π‘ **Tip**: Each line in the content will become a separate paragraph in the Word document.
|
| 278 |
+
""")
|
| 279 |
+
|
| 280 |
+
# About Tab
|
| 281 |
+
with gr.Tab("βΉοΈ About"):
|
| 282 |
+
gr.Markdown("""
|
| 283 |
+
## π MCP 1st Birthday Hackathon Project
|
| 284 |
+
|
| 285 |
+
This project was created for the **MCP 1st Birthday Hackathon** to demonstrate:
|
| 286 |
+
- Integration between LLMs and productivity tools
|
| 287 |
+
- Simplicity of Gradio MCP for creating MCP servers
|
| 288 |
+
- Practical use case for document processing
|
| 289 |
+
|
| 290 |
+
### π οΈ Technologies Used
|
| 291 |
+
- **Python 3.9+**
|
| 292 |
+
- **Gradio with MCP support** (`gradio[mcp]`)
|
| 293 |
+
- **python-docx** for Word file manipulation
|
| 294 |
+
- **Model Context Protocol** for standardized AI tool integration
|
| 295 |
+
|
| 296 |
+
### π‘ MCP Server Endpoint
|
| 297 |
+
```
|
| 298 |
+
https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
### π Links
|
| 302 |
+
- [Gradio MCP Documentation](https://huggingface.co/learn/mcp-course/unit1/gradio-mcp)
|
| 303 |
+
- [MCP Protocol Specification](https://spec.modelcontextprotocol.io/)
|
| 304 |
+
- [This Space on Hugging Face](https://huggingface.co/spaces/mcp-1st-birthday/mcp-docx-tools)
|
| 305 |
+
|
| 306 |
+
### π License
|
| 307 |
+
Apache License 2.0
|
| 308 |
+
|
| 309 |
+
---
|
| 310 |
+
|
| 311 |
+
Made with β€οΈ for the MCP-1st-Birthday Hackathon
|
| 312 |
+
""")
|
| 313 |
|
| 314 |
demo.launch(mcp_server=True)
|