Skip to main content
Glama

factorial

Calculate the factorial of any non-negative integer. Computes the product of all positive integers from 1 to the given number for mathematical operations and calculations.

Instructions

Calculate the factorial of a non-negative integer. The factorial of n (written as n!) is the product of all positive integers less than or equal to n. For example: 5! = 5 × 4 × 3 × 2 × 1 = 120 Args: n: A non-negative integer Returns: The factorial of n Raises: ValueError: If n is negative

Input Schema

NameRequiredDescriptionDefault
nYes

Input Schema (JSON Schema)

{ "properties": { "n": { "title": "N", "type": "integer" } }, "required": [ "n" ], "type": "object" }

Implementation Reference

  • The factorial tool handler, registered via @mcp.tool() decorator. It validates that n is non-negative and computes factorial using the math library.
    @mcp.tool() def factorial(n: int) -> int: """ Calculate the factorial of a non-negative integer. The factorial of n (written as n!) is the product of all positive integers less than or equal to n. For example: 5! = 5 × 4 × 3 × 2 × 1 = 120 Args: n: A non-negative integer Returns: The factorial of n Raises: ValueError: If n is negative """ if n < 0: raise ValueError("Factorial is only defined for non-negative integers") return math.factorial(n)
  • server.py:125-125 (registration)
    Registers the factorial function as an MCP tool using FastMCP's decorator.
    @mcp.tool()
  • Input schema defined by type hint 'n: int', output 'int', and comprehensive docstring describing parameters, return value, and exceptions.
    def factorial(n: int) -> int: """ Calculate the factorial of a non-negative integer. The factorial of n (written as n!) is the product of all positive integers less than or equal to n. For example: 5! = 5 × 4 × 3 × 2 × 1 = 120 Args: n: A non-negative integer Returns: The factorial of n Raises: ValueError: If n is negative """

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/xiaoyuchenhot/MCP-example'

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