Skip to main content
The JavaScript samples use the @modelcontextprotocol/sdk package and Zod for schema validation. Both samples run with Node.js and use ES module syntax.

Basic calculator server

Located at 03-GettingStarted/samples/javascript, this is the simplest possible MCP server — four arithmetic tools over a stdio transport.
1

Clone and navigate

git clone https://github.com/microsoft/mcp-for-beginners.git
cd mcp-for-beginners/03-GettingStarted/samples/javascript
2

Install dependencies

npm install
3

Run the server

npm start

package.json

package.json
{
  "name": "tutorial-mcp",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@modelcontextprotocol/sdk": ">=1.26.0",
    "zod": "^3.24.2"
  }
}

Server code

index.js
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
  name: "Calculator MCP Server",
  version: "1.0.0"
});

// Add tool
server.tool(
  "add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// Subtract tool
server.tool(
  "subtract",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a - b) }]
  })
);

// Multiply tool
server.tool(
  "multiply",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a * b) }]
  })
);

// Divide tool — includes divide-by-zero guard
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) }]
    };
  }
);

// Connect via stdio
const transport = new StdioServerTransport();
server.connect(transport).catch(console.error);

console.log("Calculator MCP Server started");
Each tool uses a Zod schema to declare its input types. The MCP SDK validates incoming arguments automatically before calling the handler.

Build docs developers (and LLMs) love