Tools

MCP Tools: Actions AI Can Perform

Tools are the most commonly used MCP capability. They represent actions the AI can perform — like creating a file, querying a database, or sending a message. Each tool has a name, description, input schema, and a handler function.
typescript
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

const server = new McpServer({ name: "demo", version: "1.0.0" });

// Basic tool with typed parameters
server.tool(
  "search_users",
  "Search for users by name or email",
  {
    query: z.string().describe("Search query"),
    limit: z.number().optional().default(10).describe("Max results"),
  },
  async ({ query, limit }) => {
    const users = await db.users.search(query, limit);
    return {
      content: [{
        type: "text",
        text: JSON.stringify(users, null, 2),
      }],
    };
  }
);

// Tool that returns an error gracefully
server.tool(
  "delete_user",
  "Delete a user by ID",
  { userId: z.string() },
  async ({ userId }) => {
    try {
      await db.users.delete(userId);
      return { content: [{ type: "text", text: `User ${userId} deleted` }] };
    } catch (error) {
      return {
        isError: true,
        content: [{ type: "text", text: `Failed: ${error.message}` }],
      };
    }
  }
);

Tip:Write clear descriptions for your tools — the AI uses them to decide WHEN to call each tool. Good descriptions lead to better AI behavior.

PreviewTypeScriptRead Only
Copy this code and run it locally with npx tsx server.ts

💬 Got questions? Ask me anything!