index.ts•1.6 kB
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
/**
* Basic Math MCP Server
* Provides two mathematical operations: sum and subtraction
*/
// Create the server instance
const server = new McpServer({
name: "basic-math-server",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
// Define the sum tool
server.tool(
"sum",
"Add two numbers together",
{
a: z.number().describe("First number"),
b: z.number().describe("Second number")
},
async ({ a, b }: { a: number; b: number }) => {
const result = a + b;
return {
content: [
{
type: "text",
text: `The sum of ${a} and ${b} is: ${result}`
}
]
};
}
);
// Define the subtraction tool
server.tool(
"subtraction",
"Subtract the second number from the first number",
{
a: z.number().describe("First number (minuend)"),
b: z.number().describe("Second number (subtrahend)")
},
async ({ a, b }: { a: number; b: number }) => {
const result = a - b;
return {
content: [
{
type: "text",
text: `The subtraction of ${b} from ${a} is: ${result}`
}
]
};
}
);
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Basic Math MCP Server is running on stdio");
}
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});