Added inventory amount per food package management
This commit is contained in:
@@ -384,6 +384,32 @@ def create_server() -> MCPServer:
|
||||
await api_request('DELETE', f'kitchen/items/{item_id}/')
|
||||
return {'success': True, 'removed_item_id': item_id}
|
||||
|
||||
@server.tool(structured_output=True, description='Read stock for an owned product ID or search query, optionally limited to a location ID. Returns package details and remaining totals. Does not change stock.')
|
||||
async def inventory_get_stock(product_id: str | None = None, query: str | None = None,
|
||||
location_id: int | None = None) -> dict[str, Any]:
|
||||
params = {'product_id': product_id, 'query': query, 'location_id': location_id}
|
||||
return await api_request('GET', 'kitchen/stock/', params={k: v for k, v in params.items() if v is not None})
|
||||
|
||||
@server.tool(structured_output=True, description='Preview exact package deductions for one owned product without changing stock. A shortage is reported rather than silently consumed.')
|
||||
async def inventory_preview_consumption(product_id: str, amount: Decimal, unit: str,
|
||||
package_id: str | None = None) -> dict[str, Any]:
|
||||
return await api_request('POST', 'kitchen/consumption/preview/', payload={
|
||||
'product_id': product_id, 'amount': str(amount), 'unit': unit, 'package_id': package_id})
|
||||
|
||||
@server.tool(structured_output=True, description='Consume an exact quantity from owned inventory packages atomically. Requires a unique idempotency key. If stock is short, preview first and use allow_partial only with explicit user choice.')
|
||||
async def inventory_consume(product_id: str, amount: Decimal, unit: str, idempotency_key: str,
|
||||
package_id: str | None = None, source_reference: str = '',
|
||||
description: str = '', allow_partial: bool = False) -> dict[str, Any]:
|
||||
return await api_request('POST', 'kitchen/consumption/', payload={
|
||||
'product_id': product_id, 'amount': str(amount), 'unit': unit,
|
||||
'package_id': package_id, 'source_reference': source_reference,
|
||||
'description': description, 'allow_partial': allow_partial},
|
||||
idempotency_key=idempotency_key)
|
||||
|
||||
@server.tool(structured_output=True, description='Undo a previous owned inventory consumption transaction when its affected packages have not changed since. Records an auditable reversal.')
|
||||
async def inventory_undo_consumption(transaction_id: str) -> dict[str, Any]:
|
||||
return await api_request('POST', f'kitchen/consumption/{transaction_id}/undo/', payload={})
|
||||
|
||||
return server
|
||||
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ class McpHttpTests(LiveServerTestCase):
|
||||
|
||||
async def check(client):
|
||||
names = {tool.name for tool in (await client.list_tools()).tools}
|
||||
self.assertEqual(len(names), 16)
|
||||
self.assertEqual(len(names), 20)
|
||||
self.assertTrue({'food_lookup_product', 'food_create_product', 'food_import_product',
|
||||
'inventory_import_groceries'} <= names)
|
||||
today = (await client.call_tool('food_get_today')).structured_content
|
||||
@@ -253,5 +253,19 @@ class McpHttpTests(LiveServerTestCase):
|
||||
self.assertEqual(batch['items'][0]['status'], 'added')
|
||||
again = (await client.call_tool('inventory_import_groceries', batch_args)).structured_content
|
||||
self.assertEqual(again['items'][0]['inventory_item_id'], batch['items'][0]['inventory_item_id'])
|
||||
self.assertEqual((await client.call_tool('inventory_list')).structured_content['count'], 1)
|
||||
self.assertEqual((await client.call_tool('inventory_list')).structured_content['count'], 2)
|
||||
stock = (await client.call_tool('inventory_get_stock', {'product_id': created['product_id']})).structured_content
|
||||
self.assertEqual(stock['products'][0]['totals']['ml'], '2000.000')
|
||||
preview = (await client.call_tool('inventory_preview_consumption', {
|
||||
'product_id': created['product_id'], 'amount': '1250', 'unit': 'ml'})).structured_content
|
||||
self.assertEqual([row['amount'] for row in preview['deductions']], ['1000.000', '250.000'])
|
||||
used = (await client.call_tool('inventory_consume', {
|
||||
'product_id': created['product_id'], 'amount': '1250', 'unit': 'ml',
|
||||
'idempotency_key': 'mcp-use-oat'})).structured_content
|
||||
self.assertEqual(used['amount'], '1250.000')
|
||||
self.assertEqual((await client.call_tool('inventory_get_stock', {
|
||||
'product_id': created['product_id']})).structured_content['products'][0]['totals']['ml'], '750.000')
|
||||
undone = (await client.call_tool('inventory_undo_consumption', {
|
||||
'transaction_id': used['transaction_id']})).structured_content
|
||||
self.assertEqual(undone['reverses'], used['transaction_id'])
|
||||
asyncio.run(self._client(self.secret, check))
|
||||
|
||||
Reference in New Issue
Block a user