libreoffice-mcp
Enables interaction with LibreOffice documents (Writer, Calc, Impress, Draw) via MCP, supporting document management, data analysis, and database operations using the OooDev library.
README
LibreOffice MCP (WIP)
Explanation of the Refactor
Why OooDev?
The OooDev library, as detailed in api_documentation.md, provides a Pythonic abstraction over the LibreOffice UNO API, with classes like CalcDoc, WriteDoc, and utilities for charts, forms, and macros. It simplifies tasks like document creation, cell manipulation, and styling, reducing the need for low-level UNO interface queries (e.g., UnoRuntime.queryInterface). OooDev also supports modern Python features (e.g., type hints, context managers) and integrates with extensions like APSO and OooDev.oxt, making it ideal for a 1:1 MCP mapping.
Key Changes from Your Original Implementation
-
OooDev Integration:
- Replaced direct UNO API calls (e.g.,
XSpreadsheetDocument,XTextDocument) with OooDev classes (CalcDoc,WriteDoc). - Used OooDev’s
Loclass to manage LibreOffice connections, replacing manualUnoUrlResolversetup. - Leveraged OooDev utilities like
Calc,Write, andChart2for document operations.
- Replaced direct UNO API calls (e.g.,
-
Preserved Features:
- Kept all your original tools:
open_document,close_document,get_sheet_names,get_cell_value,set_cell_value,create_new_sheet,insert_text,create_chart, andapply_style. - Enhanced these tools to use OooDev methods (e.g.,
CalcDoc.get_sheet_names()instead ofXSpreadsheetDocument.getSheets()). - Maintained the
parse_cell_addresshelper, though OooDev’sCalcDoc.rng()method reduces its necessity in some cases.
- Kept all your original tools:
-
New Base Tools:
- Added
run_query,list_tables,create_table, andinsert_datato support LibreOffice Base operations, using OooDev’s database access methods. - These tools align with the
com::sun::star::sdbnamespace fromapi.mdbut use OooDev’s simplified interfaces.
- Added
-
New Data Analysis Tools:
- Added
create_pivot_table,sort_range, andcalculate_statisticsto enhance Calc’s data analysis capabilities. - Used OooDev’s
Chart2module for pivot table creation andCalcmodule for sorting and statistics.
- Added
-
Additional Tools:
- Added
new_documentto create new documents of various types, leveraging OooDev’screate_docmethods. - Added
save_documentto save documents using OooDev’ssave_doc. - Added
run_macroto execute Python macros, using OooDev’sMacroLoadercontext manager. - Added
insert_form_controlto insert form controls in Calc sheets, using OooDev’sFormsmodule.
- Added
Alignment with OooDev Features
- Document Management: Uses
WriteDoc,CalcDoc,DrawDocfor Writer, Calc, Draw, and Impress documents, coveringcom::sun::star::text,sheet,draw, andpresentationnamespaces. - Base Operations: Leverages OooDev’s database access for
com::sun::star::sdbandsdbcfunctionalities. - Data Analysis: Utilizes OooDev’s
Chart2andCalcmodules for pivot tables, sorting, and statistics, aligning withcom::sun::star::chart2andsheet. - Forms and Macros: Incorporates OooDev’s
FormsandMacroLoaderforcom::sun::star::formandscriptnamespaces. - Styling: Uses OooDev’s style methods (e.g.,
Write.style) forcom::sun::star::style.
Benefits of the Refactor
- Pythonic Interface: OooDev’s classes and methods are more intuitive and reduce boilerplate compared to UNO’s interface queries.
- Comprehensive Coverage: The adapter covers a broad range of OooDev functionalities, from basic document operations to advanced database and analysis tasks.
- Maintainability: OooDev’s abstractions simplify maintenance and future expansions (e.g., adding Math or more form controls).
- Error Handling: Robust error checking ensures reliability for LLM interactions via MCP.
Limitations and Future Enhancements
- Base Document Handling: The current Base tools use raw UNO interfaces for database operations, as OooDev’s Base support is less developed. Future versions could integrate OooDev’s Base-specific classes if added.
- Additional Tools: More OooDev features (e.g.,
ooodev.unitsfor unit conversions,ooodev.formfor advanced forms,ooodev.drawfor shapes) could be added as tools. - Performance: OooDev’s abstractions may introduce slight overhead; caching (e.g.,
Lo.cache) could be implemented for frequent operations.
How to Use
- Setup: Ensure OooDev is installed (
pip install ooo-dev-tools) and LibreOffice is running with a socket connection (port 2083). - Run the Server: Start the FastAPI server with this adapter.
- Interact via MCP: Use the MCP client to call tools like
open_document,run_query, orcreate_pivot_table.
- Coverage Across Applications: The adapter now supports Writer, Calc, Impress, and Draw, with potential for more Base and Math tools to be added.
- Rich Feature Set: It includes not just basic operations (e.g., opening documents, setting cell values) but also advanced features like styling, table insertion, image handling, charting, slide management, and shape drawing.
- Cross-Cutting Functionality: Tools like saving, exporting to PDF, and managing document properties apply to all LibreOffice document types, enhancing versatility.
- Error Handling: Each tool includes robust checks (e.g., document existence, type validation) to ensure reliability.
Old Documetnation
sudo useradd -m -s /bin/bash mcp-libreoffice
sudo mkdir -p /home/mcp-libreoffice/output
sudo chown -R mcp-libreoffice:mcp-libreoffice /home/mcp-libreoffice
sudo -u mcp-libreoffice soffice --headless --accept="socket,host=localhost,port=${LIBREOFFICE_PORT:-2083};urp;"
Original Features:
open_document: Opens a document from a URL and assigns it an ID.close_document: Closes a document by its ID.get_sheet_names: Retrieves all sheet names from a Calc spreadsheet.get_cell_value: Gets the value (numeric, text, or formula) of a specified cell in a Calc sheet.set_cell_value: Sets a cell’s value in a Calc sheet, handling both numbers and text.get_text_content: Retrieves the full text content of a Writer document.insert_text: Inserts text at a specific position in a Writer document.create_chart: Creates a chart in a Calc sheet based on a data range and chart type.parse_cell_address: Helper function to convert cell addresses (e.g., "A1") to column and row indices.- Spreadsheet Enhancements (Calc):
create_new_sheet: Adds a new sheet to a spreadsheet.set_cell_formula: Sets a formula in a spreadsheet cell (e.g., "=SUM(A1:A10)").
- Text Document Enhancements (Writer):
insert_table: Inserts a table with specified rows and columns at a position.apply_style: Applies a paragraph style to a text range.insert_image: Inserts an image from a URL at a specified position.
- Additional Document Management:
save_document: Saves a document to a URL with a specified filter (e.g., "writer8" for ODT).export_to_pdf: Exports a document to PDF format.get_document_properties: Retrieves metadata like title, author, subject, and keywords.set_document_properties: Updates document metadata.
- Presentation Tools (Impress):
insert_slide: Inserts a new slide at a specified position in a presentation.
- Drawing Tools (Draw):
add_shape: Adds a shape (e.g., rectangle, circle) to a Draw page with position and size.
- Macro Execution:
run_macro: Runs a Basic macro stored in the document.
Base Tools
The following tools further enhance database management capabilities:
list_tables(doc_id: str) -> list[str]: Retrieves a list of all table names in the database.delete_table(doc_id: str, table_name: str) -> str: Deletes a specified table from the database.insert_data(doc_id: str, table_name: str, data: dict) -> str: Inserts a single row of data into a table, with column names and values provided as a dictionary.- List Tables: Use
list_tables("doc_id")to get all table names in a database. - Delete Table: Call
delete_table("doc_id", "table_name")to remove a table. - Insert Data: Use
insert_data("doc_id", "table_name", {"ID": 1, "Name": "Test"})to add a row.
Data Analysis Tools
These tools extend Calc’s data analysis capabilities:
create_chart(doc_id: str, sheet_name: str, data_range: str, chart_type: str, target_cell: str) -> str: Creates a chart (e.g., bar, line) from a data range and places it at the target cell.apply_conditional_formatting(doc_id: str, sheet_name: str, range_address: str, condition: str, style: str) -> str: Applies conditional formatting to a range based on a condition (e.g., "value > 10") and a style.group_range(doc_id: str, sheet_name: str, range_address: str, by_rows: bool) -> str: Groups rows or columns in a range for outlining purposes.- Create Chart: Use
create_chart("doc_id", "Sheet1", "A1:B10", "bar", "C12")to create a bar chart. - Conditional Formatting: Apply with
apply_conditional_formatting("doc_id", "Sheet1", "A1:A10", "A1>10", "Good"), assuming "Good" is a defined cell style.
Potential Additions
- Base Tools: Create databases, manage forms, or run queries.
- Math Tools: Insert and edit mathematical formulas.
- Formatting Tools: Adjust font size, color, or alignment in Writer or Impress.
- Animation/Transition Tools: Add animations to Impress slides.
- Data Analysis Tools: Implement pivot tables or sorting in Calc.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。