TypeScript Calculator

Module
Getting Started
Progress
100%

Sample

This is a Typescript sample for an MCP Server

Here's what the calculator portion looks like:


// Define calculator tools for each operation

server.tool(

  "add",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a + b) }]

  })

);



server.tool(

  "subtract",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a - b) }]

  })

);



server.tool(

  "multiply",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a * b) }]

  })

);



server.tool(

  "divide",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => {

    if (b === 0) {

      return {

        content: [{ type: "text", text: "Error: Cannot divide by zero" }],

        isError: true

      };

    }

    return {

      content: [{ type: "text", text: String(a / b) }]

    };

  }

);

Install

Run the following command:


npm install

Run


npm start

샘플

이것은 MCP 서버용 Typescript 샘플입니다

계산기 부분은 다음과 같습니다:


// Define calculator tools for each operation

server.tool(

  "add",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a + b) }]

  })

);



server.tool(

  "subtract",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a - b) }]

  })

);



server.tool(

  "multiply",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => ({

    content: [{ type: "text", text: String(a * b) }]

  })

);



server.tool(

  "divide",

  {

    a: z.number(),

    b: z.number()

  },

  async ({ a, b }) => {

    if (b === 0) {

      return {

        content: [{ type: "text", text: "Error: Cannot divide by zero" }],

        isError: true

      };

    }

    return {

      content: [{ type: "text", text: String(a / b) }]

    };

  }

);

설치

다음 명령어를 실행하세요:


npm install

실행


npm start

면책 조항:

이 문서는 AI 번역 서비스 Co-op Translator를 사용하여 번역되었습니다.

정확성을 위해 노력하고 있으나, 자동 번역에는 오류나 부정확한 부분이 있을 수 있음을 유의하시기 바랍니다.

원문은 해당 언어의 원본 문서가 권위 있는 자료로 간주되어야 합니다.

중요한 정보의 경우 전문적인 인간 번역을 권장합니다.

본 번역의 사용으로 인해 발생하는 오해나 잘못된 해석에 대해 당사는 책임을 지지 않습니다.

MCP Academy — microsoft/mcp-for-beginners