-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
325 lines (239 loc) · 10.2 KB
/
main.py
File metadata and controls
325 lines (239 loc) · 10.2 KB
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
from mcp.server.fastmcp import FastMCP
import json
from pydantic import ValidationError
import logging
from utils import logging_config
from schemas import models
from services import users, bills, inventory
# Initialize Logging
logging_config.setup_logging()
logger = logging.getLogger("inventorymcp")
mcp = FastMCP("inventorymcp", version="0.1.0", timeout=300)
# User management tools
@mcp.tool()
async def fetch_users():
"""
Gets the list of all users from the inventory management system server in json format.
There are three types of users: Admin, Stocker and Cashier.
Returns:
A list of users in json format, or an error message if the request fails.
"""
return await users.fetchUsers()
@mcp.tool()
async def add_user(username: str, password: str, email: str, phone: str, address: str, type: str):
"""
Adds a new user to the inventory management system.
Args:
username: The username of the new user.
password: The password for the new user.
email: The email address of the new user.
phone: The phone number of the new user.
address: The address of the new user.
type: The type of user (e.g., Admin, Stocker, Cashier).
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
user = models.User(username=username, password=password, email=email, phone=phone, address=address, type=type)
return await users.addUser(user)
except ValueError as e:
logging.error(f"ValueError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
# Inventory Management Tools
@mcp.tool()
async def fetch_inventory_items():
"""
Fetches the list of all inventory items from the inventory management system server in json format.
Returns:
A list of inventory items in json format, or an error message if the request fails.
"""
return await inventory.fetchInventoryItems()
@mcp.tool()
async def add_inventory_item(name: str, price: int, stock: int, description: str):
"""
Adds a new inventory item to the inventory management system.
Args:
name: The name of the inventory item.
price: The price of the inventory item.
stock: The new_stock quantity of the inventory item.
description: A description of the inventory item.
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
item = models.InventoryItemInsert(name=name, price=price, stock=stock, description=description)
return await inventory.addInventoryItem(item)
except ValueError as e:
logging.error(f"ValueError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def update_inventory_item(prod_id: int, name: str, price: int, stock: int, description: str):
"""
Updates an existing inventory item in the inventory management system.
Args:
prod_id: The ID of the inventory item to update.
name: The new name of the inventory item.
price: The new price of the inventory item.
stock: The new new_stock quantity of the inventory item.
description: The new description of the inventory item.
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
item = models.InventoryItemUpdate(prod_id=prod_id, name=name, stock=stock, description=description)
return await inventory.updateInventoryItem(item)
except ValueError as e:
logging.error(f"ValueError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def update_inventory_item_stock(prod_id: int, stock: int):
"""
Updates the new_stock quantity of an existing inventory item in the inventory management system.
Args:
prod_id: The ID of the inventory item to update.
stock: The new new_stock quantity of the inventory item.
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
item = models.InventoryItemUpdateStock(prod_id=prod_id, new_stock=stock)
return await inventory.updateInventoryItemStock(item)
except ValueError as e:
logging.error(f"ValueError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
# Billing Tools
@mcp.tool()
async def fetch_bill_records():
"""
Fetches the list of all bill records from the inventory management system server in json format.
Each bill record contains it invoice_no, Date, total_price and paymethod.
If total_price and paymethod are NULL then that bill is not issued (Products inside the bill are not sold).
Returns:
A list of bill records in json format, or an error message if the request fails.
"""
return await bills.fetchBillRecords()
@mcp.tool()
async def fetch_bill_records_by_cashier(cashier_id: int):
"""
Fetches the list of all bill records from the inventory management system server in json format.
Args:
cashier_id: Unique id of the cashier.
Returns:
A list of bill records created by this cashier in json format, or an error message if the request fails.
"""
return await bills.fetchBillRecordsByCashier(cashier_id)
@mcp.tool()
async def open_bill(invoice_no: int):
"""
Reads the products inside a bill with the provided invoice_no.
Args:
invoice_no: The Invoice No of the bill
Returns:
A list of products inside a bill with the provided invoice_no in a json format.
"""
return await bills.openBill(invoice_no)
@mcp.tool()
async def create_draft_bill(products: list[dict[str, int]]):
"""
Creates a draft bill and adds the provided products to it.
Args:
products: A list of products, each element of this list should be a python dictionary of the format:
- prod_id: int, a unique identifier for the product (must be a positive value)
- quantity: int, quantity of the product (must be a positive value and cannot be zero)
Example Usage for LLM:
User: "Create a bill for 2 units of product 101 and 5 units of product 102"
LLM Input:
{
"products": [
{"prod_id": 101, "quantity": 2},
{"prod_id": 102, "quantity": 5}
]
}
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
draft_bill = models.DraftBill(**{"products": products})
return await bills.createDraftBill(draft_bill)
except ValueError as e:
logging.error(f"ValueError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def add_item_to_bill(invoice_no: int, prod_id: int, quantity: int):
"""
Adds an item to the bill with the provided invoice_no.
Args:
invoice_no:
prod_id:
quantity:
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
entry = models.BillItemAddOrUpdate(invoice_no=invoice_no, prod_id=prod_id, quantity=quantity)
return await bills.addToExistingBill(entry)
except ValidationError as e:
logging.error(f"ValidationError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def edit_bill_item_quantity(invoice_no: int, prod_id: int, quantity: int):
"""
Updates the quantity of an existing unpaid/unissued bill item in the inventory management system.
Args:
invoice_no: Invoice Number of the bill
prod_id: int, a unique identifier for the product (must be a positive value)
quantity: int, quantity of the product (must be a positive value and cannot be zero)
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
entry = models.BillItemAddOrUpdate(invoice_no=invoice_no, prod_id=prod_id, quantity=quantity)
return await bills.editProductQty(entry)
except ValidationError as e:
logging.error(f"ValidationError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def delete_bill_item(invoice_no: int, prod_id: int):
"""
Deletes an existing item from an unpaid/unissued bill.
Args:
invoice_no: Invoice Number of the bill
prod_id: Id of the product (must be a positive value)
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
entry = models.BillItemDelete(invoice_no=invoice_no, prod_id=prod_id)
return await bills.deleteBillItem(entry)
except ValidationError as e:
logging.error(f"ValidationError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def issue_bill(invoice_no: int, pay_method: str):
"""
Issues a draft bill i.e. The bill is finalised and considered paid.
After calling this tool, the bill cannot be edited or deleted.
Args:
invoice_no: Invoice Number of the bill
pay_method: Payment Method in uppercase letters (e.g. Cash, Card, UPI)
Returns:
A confirmation message or an error message in json format if the request fails.
"""
try:
bill = models.IssueBill(invoice_no=invoice_no, pay_method=pay_method)
return await bills.issueBill(bill)
except ValidationError as e:
logging.error(f"ValidationError occurred: {e}")
return json.dumps({"error": f"ValueError occurred: {e}"})
@mcp.tool()
async def discard_bill(invoice_no: int):
"""
Discards a draft Bill.
Args:
invoice_no: Invoice Number of the bill
Returns:
A confirmation message or an error message in json format if the request fails.
"""
return await bills.discardBill(invoice_no)
if __name__ == "__main__":
mcp.run(transport="stdio")