mcp-erp 1.0.1

Enterprise ERP MCP Server — unified access to SAP S/4HANA, NetSuite, Odoo, Zoho Books, and Microsoft Dynamics 365 Business Central with lifecycle-based document management
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! MCP tool router for ERP operations.
use adk_mcp_sdk::{HealthCheck, HealthStatus};
use crate::types::{ErpBackend, LineItemInput};
use rmcp::{handler::server::wrapper::Parameters, schemars, tool, tool_router};
use serde::Deserialize;
use std::sync::Arc;

// ─── Input types ─────────────────────────────────────────────────────────────

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct ListInput {
    #[serde(default = "d20")]
    pub limit: u32,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct IdInput {
    pub id: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CreateCustomerInput {
    pub name: String,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub phone: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct UpdateCustomerInput {
    pub id: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub phone: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CreateVendorInput {
    pub name: String,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub phone: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct UpdateVendorInput {
    pub id: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub phone: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CreateProductInput {
    pub name: String,
    #[serde(default)]
    pub sku: Option<String>,
    #[serde(default)]
    pub unit_price: Option<f64>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct UpdateProductInput {
    pub id: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub sku: Option<String>,
    #[serde(default)]
    pub unit_price: Option<f64>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CreateOrderInput {
    /// Customer ID (sales) or Vendor ID (purchase)
    pub party_id: String,
    pub line_items: Vec<LineItemInput>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CreateInvoiceInput {
    pub customer_id: String,
    pub line_items: Vec<LineItemInput>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct StockQueryInput {
    #[serde(default)]
    pub product_id: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct AdjustStockInput {
    pub product_id: String,
    /// Positive to increase, negative to decrease
    pub quantity: f64,
    pub reason: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TransferStockInput {
    pub product_id: String,
    pub from_warehouse: String,
    pub to_warehouse: String,
    pub quantity: f64,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct DateRangeInput {
    /// ISO date (YYYY-MM-DD)
    pub from: String,
    /// ISO date (YYYY-MM-DD)
    pub to: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct AsOfInput {
    /// ISO date (YYYY-MM-DD)
    pub as_of: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct ApprovalInput {
    pub entity_type: String,
    pub entity_id: String,
    #[serde(default)]
    pub note: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct EvidenceInput {
    pub entity_type: String,
    pub entity_id: String,
    pub description: String,
    #[serde(default)]
    pub url: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct AuditInput {
    pub entity_type: String,
    pub entity_id: String,
}

fn d20() -> u32 { 20 }

// ─── Server ──────────────────────────────────────────────────────────────────

#[derive(Clone)]
pub struct ErpServer {
    pub backend: Arc<dyn ErpBackend>,
}

#[tool_router(server_handler)]
impl ErpServer {
    // ─── Customers ───────────────────────────────────────────────────────────

    #[tool(description = "List customers with optional filters")]
    async fn list_customers(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_customers(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get a customer by ID")]
    async fn get_customer(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_customer(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create a new customer record")]
    async fn create_customer(&self, Parameters(i): Parameters<CreateCustomerInput>) -> String {
        match self.backend.create_customer(&i.name, i.email.as_deref(), i.phone.as_deref()).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Update an existing customer record")]
    async fn update_customer(&self, Parameters(i): Parameters<UpdateCustomerInput>) -> String {
        match self.backend.update_customer(&i.id, i.name.as_deref(), i.email.as_deref(), i.phone.as_deref()).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Vendors ─────────────────────────────────────────────────────────────

    #[tool(description = "List vendors/suppliers with optional filters")]
    async fn list_vendors(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_vendors(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get a vendor by ID")]
    async fn get_vendor(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_vendor(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create a new vendor/supplier record")]
    async fn create_vendor(&self, Parameters(i): Parameters<CreateVendorInput>) -> String {
        match self.backend.create_vendor(&i.name, i.email.as_deref(), i.phone.as_deref()).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Update an existing vendor record")]
    async fn update_vendor(&self, Parameters(i): Parameters<UpdateVendorInput>) -> String {
        match self.backend.update_vendor(&i.id, i.name.as_deref(), i.email.as_deref(), i.phone.as_deref()).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Products ────────────────────────────────────────────────────────────

    #[tool(description = "List products/items with optional filters")]
    async fn list_products(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_products(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get a product by ID")]
    async fn get_product(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_product(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create a new product/item record")]
    async fn create_product(&self, Parameters(i): Parameters<CreateProductInput>) -> String {
        match self.backend.create_product(&i.name, i.sku.as_deref(), i.unit_price).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Update an existing product record")]
    async fn update_product(&self, Parameters(i): Parameters<UpdateProductInput>) -> String {
        match self.backend.update_product(&i.id, i.name.as_deref(), i.sku.as_deref(), i.unit_price).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Sales Orders ────────────────────────────────────────────────────────

    #[tool(description = "List sales orders with optional filters")]
    async fn list_sales_orders(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_sales_orders(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get a sales order by ID with line items")]
    async fn get_sales_order(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_sales_order(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create a sales order in draft state")]
    async fn create_sales_order_draft(&self, Parameters(i): Parameters<CreateOrderInput>) -> String {
        match self.backend.create_sales_order_draft(&i.party_id, &i.line_items).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Submit a draft sales order for approval/release")]
    async fn submit_sales_order(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.submit_sales_order(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Purchase Orders ─────────────────────────────────────────────────────

    #[tool(description = "List purchase orders with optional filters")]
    async fn list_purchase_orders(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_purchase_orders(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get a purchase order by ID with line items")]
    async fn get_purchase_order(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_purchase_order(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create a purchase order in draft state")]
    async fn create_purchase_order_draft(&self, Parameters(i): Parameters<CreateOrderInput>) -> String {
        match self.backend.create_purchase_order_draft(&i.party_id, &i.line_items).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Submit a draft purchase order for approval/release")]
    async fn submit_purchase_order(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.submit_purchase_order(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Invoices ────────────────────────────────────────────────────────────

    #[tool(description = "List invoices with optional filters")]
    async fn list_invoices(&self, Parameters(i): Parameters<ListInput>) -> String {
        match self.backend.list_invoices(i.limit).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get an invoice by ID with line items and payment status")]
    async fn get_invoice(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.get_invoice(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Create an invoice in draft state")]
    async fn create_invoice_draft(&self, Parameters(i): Parameters<CreateInvoiceInput>) -> String {
        match self.backend.create_invoice_draft(&i.customer_id, &i.line_items).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Submit a draft invoice for approval")]
    async fn submit_invoice(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.submit_invoice(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Post an approved invoice to the ledger")]
    async fn post_invoice(&self, Parameters(i): Parameters<IdInput>) -> String {
        match self.backend.post_invoice(&i.id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Inventory ───────────────────────────────────────────────────────────

    #[tool(description = "Get current stock levels for products")]
    async fn get_stock_levels(&self, Parameters(i): Parameters<StockQueryInput>) -> String {
        match self.backend.get_stock_levels(i.product_id.as_deref()).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Adjust stock quantity for a product (increase/decrease)")]
    async fn adjust_stock(&self, Parameters(i): Parameters<AdjustStockInput>) -> String {
        match self.backend.adjust_stock(&i.product_id, i.quantity, &i.reason).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Transfer stock between warehouses/locations")]
    async fn transfer_stock(&self, Parameters(i): Parameters<TransferStockInput>) -> String {
        match self.backend.transfer_stock(&i.product_id, &i.from_warehouse, &i.to_warehouse, i.quantity).await {
            Ok(()) => "Stock transferred".into(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── General Ledger ──────────────────────────────────────────────────────

    #[tool(description = "List chart of accounts")]
    async fn list_accounts(&self) -> String {
        match self.backend.list_accounts().await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get journal entries for a date range")]
    async fn get_journal_entries(&self, Parameters(i): Parameters<DateRangeInput>) -> String {
        match self.backend.get_journal_entries(&i.from, &i.to).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    #[tool(description = "Get trial balance for a period")]
    async fn get_trial_balance(&self, Parameters(i): Parameters<AsOfInput>) -> String {
        match self.backend.get_trial_balance(&i.as_of).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }

    // ─── Governance ──────────────────────────────────────────────────────────

    #[tool(description = "Request approval for a pending ERP document (order, invoice, etc.)")]
    async fn request_erp_approval(&self, Parameters(i): Parameters<ApprovalInput>) -> String {
        format!("Approval requested for {} {} (note: {})", i.entity_type, i.entity_id, i.note.as_deref().unwrap_or("none"))
    }

    #[tool(description = "Attach supporting evidence/documents to an ERP record")]
    async fn attach_erp_evidence(&self, Parameters(i): Parameters<EvidenceInput>) -> String {
        let url_info = i.url.as_deref().unwrap_or("no URL");
        format!("Evidence attached to {} {}: {} ({})", i.entity_type, i.entity_id, i.description, url_info)
    }

    #[tool(description = "Get the audit trail/history for an ERP document")]
    async fn get_erp_audit_trail(&self, Parameters(i): Parameters<AuditInput>) -> String {
        match self.backend.get_audit_trail(&i.entity_type, &i.entity_id).await {
            Ok(v) => serde_json::to_string_pretty(&v).unwrap(),
            Err(e) => format!("Error: {e}"),
        }
    }
}

#[async_trait::async_trait]
impl HealthCheck for ErpServer {
    async fn check_health(&self) -> HealthStatus {
        // Verify backend is reachable by listing 1 customer
        match self.backend.list_customers(1).await {
            Ok(_) => HealthStatus { healthy: true, message: Some(format!("{} connected", self.backend.name())), latency_ms: Some(1) },
            Err(e) => HealthStatus { healthy: false, message: Some(format!("{}: {e}", self.backend.name())), latency_ms: None },
        }
    }
}