Skip to main content
Glama
by LassiterJ
custom-tool.tsโ€ข1.67 kB
/** * @example Custom Tool Implementation * * This example demonstrates how to create a custom tool * that can be auto-discovered by the MCP server. */ import { z } from "zod"; import type { RegisterableModule } from "../src/registry/types.js"; import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; /** * Example: Calculator tool that performs basic arithmetic operations */ const calculatorModule: RegisterableModule = { type: "tool", name: "calculator", description: "Perform basic arithmetic operations", register(server: McpServer) { server.tool( "calculator", "Perform basic arithmetic operations", { operation: z.enum(["add", "subtract", "multiply", "divide"]) .describe("The operation to perform"), a: z.number().describe("First operand"), b: z.number().describe("Second operand"), }, async (args) => { const { operation, a, b } = args; let result: number; switch (operation) { case "add": result = a + b; break; case "subtract": result = a - b; break; case "multiply": result = a * b; break; case "divide": if (b === 0) { throw new Error("Division by zero is not allowed"); } result = a / b; break; } return { content: [ { type: "text", text: `${a} ${operation} ${b} = ${result}`, }, ], }; } ); } }; export default calculatorModule;

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/LassiterJ/mcp-playground'

If you have feedback or need assistance with the MCP directory API, please join our Discord server