Math MCP Server
This project sets up an HTTP server using Express and the Model Context Protocol (MCP) to evaluate mathematical expressions. It demonstrates how to create a server that registers a tool for evaluating math expressions using mathjs.
Overview
The server is built in Node.js with TypeScript and uses the following key components:
- Express: For setting up the HTTP server and handling requests.
- MCP Server: Provided by
@modelcontextprotocol/sdkto manage client-server communication. - StreamableHTTPServerTransport: To handle streaming HTTP communication over different sessions.
- Math Tool: A custom tool using
mathjsfor evaluating mathematical expressions. It is registered asmathtooland can parse and compute expressions.
Setup
Prerequisites
- Node.js (v14+ recommended)
- npm (comes with Node.js)
Installation
- Clone or download the repository.
- Navigate to the project directory:
- Install dependencies:
bash npm install
Running the Server
Start the server using:```bash
tsx index.ts
The server listens on port `3000`.
## Setting up in Copilot
create `.vscode/mcp.json`. Within the file add something similar to```json
{
"servers": {
"math-mcp": {
"type": "http",
"url": "http://localhost:3000/mcp"
}
}
}
Endpoints
- POST /mcp: For client-to-server communication. This endpoint accepts requests containing a mathematical expression. If a session is initialized, it reuses the existing transport; otherwise, it handles new initialization requests.
- GET /mcp: For receiving server-to-client notifications via Server-Sent Events (SSE).
- DELETE /mcp: For terminating a session.
Math Tool Usage
The mathematical tool (mathtool) evaluates expressions using mathjs. For example:
-
Request:
json { "message": "3+5*8" } -
Response:
json { "result": "43" }
Other examples:
sin(35)-76.42*102.3is computed according to standard mathematical rules, ensuring angles are in the correct unit.
Project Structure
index.ts: Main entry point for the server.README.md: Project documentation.
License
MIT License
