xls-rs 0.1.2

A powerful CLI tool and library for spreadsheet manipulation with pandas-style operations. Supports CSV, Excel (XLSX, XLS, ODS), Parquet, and Avro formats with formula evaluation, data transformation, and comprehensive analytics capabilities.
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use rmcp::{
    ServerHandler,
    handler::server::wrapper::Parameters,
    model::{ErrorData as McpError, *},
    schemars, tool, tool_handler, tool_router,
};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::sync::Arc;

use crate::capabilities::{
    AddChartCapability, AddSparklineCapability, ApplyFormulaCapability, CapabilityRegistry,
    ConditionalFormatCapability, ConvertCapability, FilterCapability, ListSheetsCapability,
    ReadAllSheetsCapability, ReadExcelCapability, SortCapability, WorkflowCapability,
    WriteStyledCapability,
};
use rmcp::handler::server::tool::ToolRouter;
use crate::capability_catalog;

#[derive(Clone)]
pub struct XlsRsMcpServer {
    tool_router: ToolRouter<XlsRsMcpServer>,
    registry: Arc<CapabilityRegistry>,
}

// Manually define requests for now until we can bridge schema
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct SortRequest {
    #[schemars(description = "Input file path")]
    pub input: String,
    #[schemars(description = "Output file path")]
    pub output: String,
    #[schemars(description = "Column name or index to sort by")]
    pub column: String,
    #[schemars(description = "Sort in ascending order (default: true)")]
    pub ascending: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ConvertRequest {
    #[schemars(description = "Input file path")]
    pub input: String,
    #[schemars(description = "Output file path")]
    pub output: String,
    #[schemars(description = "Optional sheet name when reading Excel or ODS")]
    pub sheet: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct FilterRequest {
    #[schemars(description = "Input file path")]
    pub input: String,
    #[schemars(description = "Output file path")]
    pub output: String,
    #[schemars(description = "Column to filter on")]
    pub column: String,
    #[schemars(description = "Operator: =, !=, >, >=, <, <=, contains, starts_with, ends_with, regex")]
    pub operator: String,
    #[schemars(description = "Value to compare against")]
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ExecuteWorkflowRequest {
    #[schemars(description = "Workflow configuration object (JSON)")]
    pub workflow: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct CapabilitiesRequest {}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WriteStyledRequest {
    #[schemars(description = "Output file path (.xlsx)")]
    pub output: String,
    #[schemars(description = "2D array of string values")]
    pub data: Vec<Vec<String>>,
    #[schemars(description = "Sheet name (default: Sheet1)")]
    pub sheet_name: Option<String>,
    #[schemars(description = "Apply header styling to first row")]
    pub style_header: Option<bool>,
    #[schemars(description = "Freeze first row")]
    pub freeze_header: Option<bool>,
    #[schemars(description = "Enable auto-filter")]
    pub auto_filter: Option<bool>,
    #[schemars(description = "Auto-fit column widths")]
    pub auto_fit: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AddChartRequest {
    #[schemars(description = "Output file path (.xlsx)")]
    pub output: String,
    #[schemars(description = "2D array of string values")]
    pub data: Vec<Vec<String>>,
    #[schemars(description = "Chart type: bar, column, line, area, pie, scatter, doughnut")]
    pub chart_type: Option<String>,
    #[schemars(description = "Chart title")]
    pub title: Option<String>,
    #[schemars(description = "Column index for category labels")]
    pub category_column: Option<i64>,
    #[schemars(description = "Column indices for values")]
    pub value_columns: Option<Vec<i64>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct AddSparklineRequest {
    #[schemars(description = "Output file path (.xlsx)")]
    pub output: String,
    #[schemars(description = "Data range for sparkline (e.g., A1:A10)")]
    pub data_range: String,
    #[schemars(description = "Cell to place sparkline (e.g., B1)")]
    pub sparkline_cell: String,
    #[schemars(description = "Sheet name (default: Sheet1)")]
    pub sheet_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ConditionalFormatRequest {
    #[schemars(description = "Output file path (.xlsx)")]
    pub output: String,
    #[schemars(description = "Range to format (e.g., A1:B10)")]
    pub range: String,
    #[schemars(description = "Formula condition (e.g., '=A1>100')")]
    pub condition: String,
    #[schemars(description = "Background color hex (e.g., 'FF0000')")]
    pub bg_color: Option<String>,
    #[schemars(description = "Font color hex (e.g., 'FFFFFF')")]
    pub font_color: Option<String>,
    #[schemars(description = "Bold text")]
    pub bold: Option<bool>,
    #[schemars(description = "Sheet name (default: Sheet1)")]
    pub sheet_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ListSheetsRequest {
    #[schemars(description = "Input file path (.xlsx, .xls, .ods)")]
    pub input: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ReadExcelRequest {
    #[schemars(description = "Input file path (.xlsx, .xls, .ods)")]
    pub input: String,
    #[schemars(description = "Sheet name (default: first sheet)")]
    pub sheet: Option<String>,
    #[schemars(description = "Cell range in A1 notation (e.g., A1:B10)")]
    pub range: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ReadAllSheetsRequest {
    #[schemars(description = "Input file path (.xlsx, .xls, .ods)")]
    pub input: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ApplyFormulaRequest {
    #[schemars(description = "Input file path")]
    pub input: String,
    #[schemars(description = "Output file path")]
    pub output: String,
    #[schemars(description = "Formula to apply")]
    pub formula: String,
    #[schemars(description = "Target cell (e.g., B1)")]
    pub cell: Option<String>,
    #[schemars(description = "Target range (e.g., B1:B10)")]
    pub range: Option<String>,
    #[schemars(description = "Sheet name for Excel files")]
    pub sheet: Option<String>,
}

fn make_error(msg: String) -> McpError {
    let detail = msg.clone();
    McpError {
        code: ErrorCode::INTERNAL_ERROR,
        message: Cow::from(msg),
        data: Some(serde_json::json!({
            "kind": "xls_rs_error",
            "detail": detail,
        })),
    }
}

#[tool_router]
impl XlsRsMcpServer {
    pub fn new() -> Self {
        let registry = Arc::new(CapabilityRegistry::new());

        // Register capabilities
        registry.register(Arc::new(SortCapability));
        registry.register(Arc::new(FilterCapability));
        registry.register(Arc::new(ConvertCapability));
        registry.register(Arc::new(WorkflowCapability::new()));

        // Register Excel read capabilities
        registry.register(Arc::new(ListSheetsCapability));
        registry.register(Arc::new(ReadExcelCapability));
        registry.register(Arc::new(ReadAllSheetsCapability));

        // Register Excel write capabilities
        registry.register(Arc::new(WriteStyledCapability));
        registry.register(Arc::new(AddChartCapability));
        registry.register(Arc::new(AddSparklineCapability));
        registry.register(Arc::new(ConditionalFormatCapability));

        // Register formula capabilities
        registry.register(Arc::new(ApplyFormulaCapability));

        Self {
            tool_router: Self::tool_router(),
            registry,
        }
    }

    #[tool(description = "Sort data by a specific column")]
    async fn sort_data(
        &self,
        request: Parameters<SortRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        
        match self.registry.execute("sort", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to sort: {}", e))),
        }
    }

    #[tool(description = "Convert a spreadsheet file to another format (csv, xlsx, parquet, avro, ods, …)")]
    async fn convert_data(
        &self,
        request: Parameters<ConvertRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("convert", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to convert: {}", e))),
        }
    }

    #[tool(description = "Filter rows based on a condition")]
    async fn filter_data(
        &self,
        request: Parameters<FilterRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        
        match self.registry.execute("filter", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to filter: {}", e))),
        }
    }

    #[tool(description = "Execute a data processing workflow from a JSON plan")]
    async fn execute_workflow(
        &self,
        request: Parameters<ExecuteWorkflowRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        
        match self.registry.execute("execute_workflow", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to execute workflow: {}", e))),
        }
    }

    #[tool(description = "List supported capabilities and formats")]
    async fn capabilities(
        &self,
        _request: Parameters<CapabilitiesRequest>,
    ) -> Result<CallToolResult, McpError> {
        let caps: Vec<serde_json::Value> = capability_catalog::CAPABILITIES
            .iter()
            .map(|c| {
                serde_json::json!({
                    "name": c.name,
                    "kind": format!("{:?}", c.kind),
                })
            })
            .collect();

        let formats = capability_catalog::FORMATS;

        let payload = serde_json::json!({
            "capabilities": caps,
            "formats": formats,
        });

        Ok(CallToolResult::success(vec![Content::text(
            payload.to_string(),
        )]))
    }

    #[tool(description = "Write data to Excel with styling options")]
    async fn write_styled(
        &self,
        request: Parameters<WriteStyledRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("write_styled", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to write styled: {}", e))),
        }
    }

    #[tool(description = "Write data to Excel with an embedded chart")]
    async fn add_chart(
        &self,
        request: Parameters<AddChartRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("add_chart", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to add chart: {}", e))),
        }
    }

    #[tool(description = "Add a sparkline to an Excel file")]
    async fn add_sparkline(
        &self,
        request: Parameters<AddSparklineRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("add_sparkline", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to add sparkline: {}", e))),
        }
    }

    #[tool(description = "Apply conditional formatting to an Excel range")]
    async fn conditional_format(
        &self,
        request: Parameters<ConditionalFormatRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("conditional_format", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to apply conditional format: {}", e))),
        }
    }

    #[tool(description = "List all sheet names in an Excel workbook")]
    async fn list_sheets(
        &self,
        request: Parameters<ListSheetsRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("list_sheets", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to list sheets: {}", e))),
        }
    }

    #[tool(description = "Read data from an Excel file with optional sheet and range selection")]
    async fn read_excel(
        &self,
        request: Parameters<ReadExcelRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("read_excel", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to read Excel: {}", e))),
        }
    }

    #[tool(description = "Read data from all sheets in an Excel workbook")]
    async fn read_all_sheets(
        &self,
        request: Parameters<ReadAllSheetsRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("read_all_sheets", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to read all sheets: {}", e))),
        }
    }

    #[tool(description = "Apply a formula to a cell or range in a spreadsheet")]
    async fn apply_formula(
        &self,
        request: Parameters<ApplyFormulaRequest>,
    ) -> Result<CallToolResult, McpError> {
        let args = serde_json::to_value(&request.0).map_err(|e| make_error(e.to_string()))?;
        match self.registry.execute("apply_formula", args) {
            Ok(result) => Ok(CallToolResult::success(vec![Content::text(result.to_string())])),
            Err(e) => Err(make_error(format!("Failed to apply formula: {}", e))),
        }
    }
}

#[tool_handler]
impl ServerHandler for XlsRsMcpServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            protocol_version: ProtocolVersion::V_2024_11_05,
            capabilities: ServerCapabilities::builder().enable_tools().build(),
            server_info: Implementation::from_build_env(),
            instructions: Some(
                "A spreadsheet tool for reading, writing, converting CSV and Excel files with formula support. \
                Use convert_data to change formats, sort_data / filter_data for row operations, and execute_workflow for pipelines."
                    .to_string(),
            ),
        }
    }
}