permutations
Calculate permutations to determine the number of ordered arrangements when selecting items from a set without repetition.
Instructions
Calculate the number of ways to choose k items from n items without repetition and with order.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| n | Yes | The number of items to choose from. | |
| k | No | The optional number of items to choose. |
Implementation Reference
- src/pymcp/server.py:218-252 (handler)The main handler function for the 'permutations' tool, which calculates the permutation P(n, k) = n! / (n-k)! using math.perm, with validation for parameters n and k.async def permutations( self, ctx: Context, n: Annotated[ int, Field( ge=1, description="The number of items to choose from.", ), ], k: Annotated[ int | None, Field( default=None, ge=1, description="The optional number of items to choose.", ), ], ) -> int: """Calculate the number of ways to choose k items from n items without repetition and with order.""" """If k is not provided, it defaults to n.""" assert isinstance(n, int) and n >= 1, "n must be a positive integer." if k is None: k = n if k > n: raise McpError( error=ErrorData( code=INVALID_PARAMS, message=f"k ({k}) cannot be greater than n ({n}).", ) ) return math.perm(n, k)
- src/pymcp/server.py:66-70 (registration)Registration entry for the 'permutations' tool in the PyMCP class tools list, including tags and annotations.{ "fn": "permutations", "tags": ["math", "permutation", "example"], "annotations": {"readOnlyHint": True}, },
- src/pymcp/server.py:414-414 (registration)Inclusion of 'permutations' tool in the response caching middleware settings.included_tools=["greet", "permutations"],
- src/pymcp/server.py:221-252 (schema)Pydantic schema definitions for input parameters n and k of the permutations tool, including constraints and descriptions.n: Annotated[ int, Field( ge=1, description="The number of items to choose from.", ), ], k: Annotated[ int | None, Field( default=None, ge=1, description="The optional number of items to choose.", ), ], ) -> int: """Calculate the number of ways to choose k items from n items without repetition and with order.""" """If k is not provided, it defaults to n.""" assert isinstance(n, int) and n >= 1, "n must be a positive integer." if k is None: k = n if k > n: raise McpError( error=ErrorData( code=INVALID_PARAMS, message=f"k ({k}) cannot be greater than n ({n}).", ) ) return math.perm(n, k)