Skip to content

codesnippetsforall/MCP-Server-using-Javascript-Arithmetic-Operation-via-Node-and-NPM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP Arithmetic Server (JavaScript)

A simple Model Context Protocol (MCP) server that exposes arithmetic tools over stdio using @modelcontextprotocol/sdk. Tools include: add, subtract, multiply, divide, power, and modulus.

Requirements

  • Node.js: v18 or newer (v20+ recommended)
  • npm: comes with Node.js

Setup

  1. Install dependencies:
npm install

Validate the server locally (optional)

  • Run the server directly to ensure it starts without errors. This is just a validation step; you do not need to keep it running.
npm run start
  • You should see a message similar to:
mcp-arithmetic-server-js: started and listening on stdio
  • Press Ctrl+C to stop. In normal use, your MCP client (e.g., Cursor) will launch the server when needed.

Configure in Cursor

Add an entry to your global Cursor MCP config at %USERPROFILE%\.cursor\mcp.json (Windows). Example:

{
  "mcpServers": {
    "arithmetic-js": {
      "command": "node",
      "args": [
        "D:/learning/mcp/mcp-arithmetic-ops-using-js/src/index.js"
      ]
    }
  }
}
  • Adjust the path in args if your project is located elsewhere.
  • After saving, reload Cursor or restart its MCP session so the server is discovered.

Your current Windows config (as provided)

  • File: C:\Users\USERNAME\.cursor\mcp.json
{
  "mcpServers": {
    "arithmetic-js": {
      "command": "node",
      "args": [
        "D:/learning/mcp/mcp-arithmetic-ops-using-js/src/index.js"
      ]
    }
  }
}

Verify in Cursor Settings

  • Open Cursor Settings → MCP → confirm arithmetic-js is enabled and green.

Cursor MCP Settings

Working example: Subtract 2 numbers in Cursor (via MCP tool)

  • Ensure the arithmetic-js MCP server is configured and enabled in Cursor.
  • In a Cursor chat, type: "Subtract 10 and 5".
  • Cursor will invoke the MCP tool subtract(a, b) from this server, not the GPT/Claude LLM.
  • You should see the tool result indicating 10 − 5 = 5.

Subtract 2 numbers in Cursor

Available tools

  • add(a, b): returns a + b
  • subtract(a, b): returns a − b
  • multiply(a, b): returns a × b
  • divide(a, b): returns a ÷ b (errors on division by zero)
  • power(a, b): returns a^b
  • modulus(a, b): returns a % b (errors on modulo by zero)

How it works

  • The server is implemented in src/index.js using @modelcontextprotocol/sdk and validates inputs with zod.
  • It communicates over stdio (StdioServerTransport), which is how MCP clients invoke it.

Scripts

  • npm run start: Runs node src/index.js. Use this to quickly verify the server launches without errors; it does not need to remain running when used via Cursor or Visual Studio Code or other editor which we use.

Troubleshooting

  • "node" not recognized: Ensure Node.js v18+ is installed and available in PATH. Check versions with:
node -v
npm -v
  • Path issues: On Windows, prefer forward slashes in JSON paths (e.g., D:/...) to avoid escape issues.

Purpose and Benefits of MCP Servers

The Model Control Protocol (MCP) servers in development environments like Cursor or Copilot provide several key advantages:

  1. Custom Function Integration

    • Organizations can define their own custom functions and make them available through MCP servers
    • These functions can encapsulate business logic, security policies, and standardized operations
    • Enables consistent implementation of organization-specific functionality
  2. Standardization & Consistency

    • All developers in the organization use the same validated functions
    • Prevents reinventing the wheel or implementing different versions of the same functionality
    • Ensures compliance with organization's coding standards and best practices
  3. Security & Control

    • Organizations can control which operations AI assistants can perform
    • Sensitive operations can be wrapped in secure, audited functions
    • Prevents AI from generating potentially unsafe or non-compliant code
  4. Business Logic Encapsulation

    • Complex business rules can be implemented once and reused
    • AI assistants can leverage these pre-built functions rather than trying to implement logic from scratch
    • Reduces errors and maintains consistency
  5. Version Control & Updates

    • Central management of commonly used functions
    • Updates to functions are immediately available to all developers
    • No need to update code in multiple places when business logic changes
  6. Audit & Monitoring

    • Organizations can track usage of functions
    • Monitor performance and identify bottlenecks
    • Ensure compliance with business rules
  7. Development Efficiency

    • Developers (and AI assistants) can focus on solving higher-level problems
    • No need to implement basic functionality repeatedly
    • Reduced time spent on boilerplate code
  8. Quality Assurance

    • Functions in MCP servers can be thoroughly tested
    • Reduces bugs and inconsistencies
    • Ensures reliable operation across the organization
  9. Integration with Existing Systems

    • MCP servers can wrap existing APIs and services
    • Provides a standardized interface to organization's infrastructure
    • Easier integration with legacy systems
  10. Learning & Onboarding

    • New developers can quickly learn standard practices
    • AI assistants can guide developers to use approved functions
    • Documentation can be centralized and maintained

About

MCP Server using JavaScript + Arithmetic Operation via Node and NPM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published