calculator-tools.ts•2.65 kB
/**
* Calculator Tool
*
* Example MCP tool that performs basic arithmetic operations.
*/
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { MCPModule } from "../types/index.js";
/**
* Tool registration function for calculator operations
*/
async function register(server: McpServer): Promise<void> {
// Register addition tool
server.registerTool(
"add",
{
title: "Addition Tool",
description: "Add two numbers together",
inputSchema: {
a: z.number().describe("First number"),
b: z.number().describe("Second number")
}
},
async ({ a, b }) => ({
content: [
{
type: "text",
text: `${a} + ${b} = ${a + b}`
}
]
})
);
// Register subtraction tool
server.registerTool(
"subtract",
{
title: "Subtraction Tool",
description: "Subtract two numbers",
inputSchema: {
a: z.number().describe("First number (minuend)"),
b: z.number().describe("Second number (subtrahend)")
}
},
async ({ a, b }) => ({
content: [
{
type: "text",
text: `${a} - ${b} = ${a - b}`
}
]
})
);
// Register multiplication tool
server.registerTool(
"multiply",
{
title: "Multiplication Tool",
description: "Multiply two numbers",
inputSchema: {
a: z.number().describe("First number"),
b: z.number().describe("Second number")
}
},
async ({ a, b }) => ({
content: [
{
type: "text",
text: `${a} × ${b} = ${a * b}`
}
]
})
);
// Register division tool
server.registerTool(
"divide",
{
title: "Division Tool",
description: "Divide two numbers",
inputSchema: {
a: z.number().describe("Dividend"),
b: z.number().describe("Divisor")
}
},
async ({ a, b }) => {
if (b === 0) {
return {
content: [
{
type: "text",
text: "Error: Division by zero is not allowed"
}
],
isError: true
};
}
return {
content: [
{
type: "text",
text: `${a} ÷ ${b} = ${a / b}`
}
]
};
}
);
}
/**
* Export the MCP module
*/
export const calculatorTools: MCPModule = {
register,
metadata: {
name: "calculator-tools",
description: "Basic arithmetic operations for MCP",
version: "1.0.0",
author: "PCN"
}
};
export default calculatorTools;