Skip to main content
Glama
yigitkonur

example-mcp-server-stdio

by yigitkonur

advanced_calculate

Perform advanced mathematical operations including factorial, logarithm, combinations, and permutations calculations for mathematical analysis and problem-solving.

Instructions

Perform advanced mathematical operations (factorial, log, combinations, permutations)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationYes
nYesPrimary input
kNoSecondary input for combinations/permutations
baseNoBase for logarithm (default: e)

Implementation Reference

  • The main handler function for the 'advanced_calculate' tool. It processes operations like factorial, logarithm, combinations, and permutations. Performs validation for required parameters (e.g., k for combinations), calls helper functions, logs to history, and returns structured output with error handling.
    async function handleAdvancedCalculate({
      operation,
      n,
      k,
      base,
    }: {
      operation: 'factorial' | 'log' | 'combinations' | 'permutations';
      n: number;
      k?: number | undefined;
      base?: number | undefined;
    }) {
      log.info(`Executing advanced calculation: ${operation}`);
      requestCount++;
    
      try {
        let result: number;
        let expression: string;
    
        switch (operation) {
          case 'factorial':
            result = factorial(n);
            expression = `${n}! = ${result}`;
            break;
          case 'log':
            if (base) {
              result = Math.log(n) / Math.log(base);
              expression = `log${base}(${n}) = ${result}`;
            } else {
              result = Math.log(n);
              expression = `ln(${n}) = ${result}`;
            }
            break;
          case 'combinations':
            if (k === undefined)
              throw new McpError(
                ErrorCode.InvalidParams,
                'The parameter "k" is required for the "combinations" operation',
              );
            result = combinations(n, k);
            expression = `C(${n}, ${k}) = ${result}`;
            break;
          case 'permutations':
            if (k === undefined)
              throw new McpError(
                ErrorCode.InvalidParams,
                'The parameter "k" is required for the "permutations" operation',
              );
            result = permutations(n, k);
            expression = `P(${n}, ${k}) = ${result}`;
            break;
          default:
            throw new McpError(ErrorCode.InvalidParams, `Unknown operation: ${operation}`);
        }
    
        const historyEntry = createHistoryEntry(operation, n, k, result);
        addToHistory(historyEntry);
    
        return {
          content: [
            {
              type: 'text' as const,
              text: expression,
            },
          ],
          structuredContent: {
            value: result,
            expression,
            calculationId: historyEntry.id,
          },
        };
      } catch (error) {
        log.error(
          `Advanced calculation failed: ${error instanceof Error ? error.message : String(error)}`,
        );
        throw new McpError(
          ErrorCode.InvalidParams,
          `Advanced calculation failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
          { operation, n, k, base },
        );
      }
    }
  • Input and output schemas using Zod for the 'advanced_calculate' tool. Defines parameters: operation (enum), n (required), k/base (optional). Output includes value, expression, and calculationId.
    const advancedCalculateInputSchema = {
      operation: z.enum(['factorial', 'log', 'combinations', 'permutations']),
      n: z.number().describe('Primary input'),
      k: z.number().optional().describe('Secondary input for combinations/permutations'),
      base: z.number().optional().describe('Base for logarithm (default: e)'),
    };
    
    const advancedCalculateOutputSchema = {
      value: z.number(),
      expression: z.string(),
      calculationId: z.string(),
    };
  • src/server.ts:560-570 (registration)
    Registration of the 'advanced_calculate' tool with the MCP server inside registerExtendedTools function. Associates name, title, description, schemas, and handler.
    server.registerTool(
      'advanced_calculate',
      {
        title: 'Advanced Calculate',
        description:
          'Perform advanced mathematical operations (factorial, log, combinations, permutations)',
        inputSchema: advancedCalculateInputSchema,
        outputSchema: advancedCalculateOutputSchema,
      },
      handleAdvancedCalculate,
    );

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/yigitkonur/example-mcp-server-stdio'

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