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.
- Node.js: v18 or newer (v20+ recommended)
- npm: comes with Node.js
- Install dependencies:
npm install
- 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.
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.
- File:
C:\Users\USERNAME\.cursor\mcp.json
{
"mcpServers": {
"arithmetic-js": {
"command": "node",
"args": [
"D:/learning/mcp/mcp-arithmetic-ops-using-js/src/index.js"
]
}
}
}
- Open Cursor Settings →
MCP
→ confirmarithmetic-js
is enabled and green.
- 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
.
- 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)
- The server is implemented in
src/index.js
using@modelcontextprotocol/sdk
and validates inputs withzod
. - It communicates over stdio (
StdioServerTransport
), which is how MCP clients invoke it.
- 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.
- "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.
The Model Control Protocol (MCP) servers in development environments like Cursor or Copilot provide several key advantages:
-
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
-
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
-
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
-
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
-
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
-
Audit & Monitoring
- Organizations can track usage of functions
- Monitor performance and identify bottlenecks
- Ensure compliance with business rules
-
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
-
Quality Assurance
- Functions in MCP servers can be thoroughly tested
- Reduces bugs and inconsistencies
- Ensures reliable operation across the organization
-
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
-
Learning & Onboarding
- New developers can quickly learn standard practices
- AI assistants can guide developers to use approved functions
- Documentation can be centralized and maintained