buy_book
Purchase books from the bookstore by specifying the book ID and desired quantity. This tool enables users to complete book transactions through the bookstore management system.
Instructions
Buy a book.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| book_id | Yes | ||
| quantity | No |
Implementation Reference
- src/bookstore_mcp/server.py:60-84 (handler)The 'buy_book' tool handler. Validates input, checks stock availability, deducts quantity from stock, and persists changes to the books database. Returns success or error details.@mcp.tool() def buy_book(book_id: int, quantity: int = 1): """Buy a book.""" if quantity <= 0: return {"success": False, "error": "Quantity must be > 0"} books = load_books() book = next((b for b in books if b["id"] == book_id), None) if not book: return {"success": False, "error": f"Book {book_id} not found"} if book["count"] < quantity: return {"success": False, "error": f"Only {book['count']} available"} book["count"] -= quantity if save_books(books): return { "success": True, "message": f"Purchased {quantity} of '{book['title']}'", } else: return {"success": False, "error": "Save failed"}