File size: 3,131 Bytes
fffd26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7b7975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fffd26d
 
 
 
 
 
 
 
 
 
 
 
 
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
# Servidor Gradio MCP para leer y crear archivos Word
# Requiere: gradio[mcp], python-docx
import gradio as gr
from docx import Document
import os

def leer_word(ruta):
    if not os.path.exists(ruta):
        return f"Archivo no encontrado: {ruta}"
    try:
        doc = Document(ruta)
        texto = "\n".join([p.text for p in doc.paragraphs])
        return texto
    except Exception as e:
        return f"Error al leer el archivo: {e}"

def crear_word(ruta, contenido):
    try:
        doc = Document()
        for linea in contenido.split("\n"):
            doc.add_paragraph(linea)
        doc.save(ruta)
        return f"Archivo creado en: {ruta}"
    except Exception as e:
        return f"Error al crear el archivo: {e}"

with gr.Blocks() as demo:
    gr.Markdown("# Servidor Gradio MCP - Word Reader/Writer")
    with gr.Tab('Intro'):
        gr.Markdown("""## MCP Word Tools 🚀

This Gradio MCP server exposes Word document utilities to LLM copilots via Model Context Protocol.



### 🧰 Tools

- read_word(path) → Read .docx preserving paragraph breaks.

- create_word(path, content) → Create / overwrite a .docx from text lines.



### ⚙️ Usage (Prompt Examples)

1. Read & summarize:

   Use read_word on C:\\Docs\\report.docx, summarize to 5 bullet points, then create_word to C:\\Docs\\report_summary.docx

2. Translate:

   Read C:\\Docs\\notes.docx, translate to Spanish, write C:\\Docs\\notes_es.docx

3. Generate:

   create_word to C:\\Docs\\plan.docx with a 5-day Tokyo itinerary.



### 🔐 Security Tips

- Limit allowed_directories in your MCP client config.

- Avoid sensitive / personal data in remote sessions.

- Paths overwrite existing files without asking.



### 🧪 Troubleshooting

File not found → Check path & permissions  

Empty output → Document has empty paragraphs  

Corrupt file → Re-open & re-save in Word



### 📡 Remote Endpoint

https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/



### 🔗 LinkedIn Showcase

Project demo post: https://www.linkedin.com/posts/oscar-felipe-gonz%C3%A1lez-gonz%C3%A1lez-019971387_mcp-artificialintelligence-hackathon-activity-7400155174138245121-DKiX



### 📦 Stack

Python · Gradio (mcp_server) · python-docx · MCP



### 📖 License

Apache 2.0



❤️ Hackathon Edition""")
    with gr.Tab("Leer Word"):
        ruta_lectura = gr.Textbox(label="Ruta local del archivo Word a leer")
        salida_lectura = gr.Textbox(label="Contenido del archivo Word", lines=10)
        btn_leer = gr.Button("Leer archivo")
        btn_leer.click(leer_word, inputs=[ruta_lectura], outputs=[salida_lectura])
    with gr.Tab("Crear Word"):
        ruta_creacion = gr.Textbox(label="Ruta local para guardar el archivo Word")
        contenido_creacion = gr.Textbox(label="Contenido para el archivo Word", lines=10)
        salida_creacion = gr.Textbox(label="Resultado de la creación")
        btn_crear = gr.Button("Crear archivo")
        btn_crear.click(crear_word, inputs=[ruta_creacion, contenido_creacion], outputs=[salida_creacion])

demo.launch(mcp_server=True)