Skip to main content
Glama

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
NameRequiredDescriptionDefault
nYesThe number of items to choose from.
kNoThe optional number of items to choose.

Implementation Reference

  • 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)
  • 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},
    },
  • Inclusion of 'permutations' tool in the response caching middleware settings.
    included_tools=["greet", "permutations"],
  • 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)

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/anirbanbasu/pymcp'

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