Skip to main content
Glama
hongfanmeng

Bookstore MCP Server

by hongfanmeng

buy_book

Purchase books from the Bookstore MCP Server by specifying book ID and quantity. Complete transactions to acquire desired titles directly through the system.

Instructions

Buy a book.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
book_idYes
quantityNo

Implementation Reference

  • The buy_book tool handler function, decorated with @mcp.tool() for registration. It handles purchasing books by checking stock, updating count, and saving to JSON.
    @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"}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hongfanmeng/bookstore-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server