This is a JavaScript sample for an MCP Server
Here's what the calculator portion of it looks like:
// Define calculator tools for each operation
server.tool(
"add",
{
a: z.number(),
b: z.number()
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }]
})
);
server.tool(
"subtract",
{
a: z.number(),
b: z.number()
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a - b) }]
})
);
server.tool(
"multiply",
{
a: z.number(),
b: z.number()
},
async ({ a, b }) => ({
content: [{ type: "text", text: String(a * b) }]
})
);
server.tool(
"divide",
{
a: z.number(),
b: z.number()
},
async ({ a, b }) => {
if (b === 0) {
return {
content: [{ type: "text", text: "Error: Cannot divide by zero" }],
isError: true
};
}
return {
content: [{ type: "text", text: String(a / b) }]
};
}
);Run the following command:
npm installnpm start