MCP Server Framework - v1.1.2
    Preparing search index...
    • Define a tool with automatic registration.

      This is the recommended way to define tools in the framework. The tool is automatically registered in the global registry when defined.

      Type Parameters

      Parameters

      Returns ToolDefinition<TInput, TOutput>

      The registered tool definition (for re-export and type inference)

      Important: Call defineTool() at module scope (top-level). The tool is registered as a side-effect of module import. For conditional or runtime tool management, use McpServerBuilder with a custom ToolProvider.

      // tools/greet.ts
      import { defineTool, text } from 'mcp-server-framework';
      import { z } from 'zod';

      export const greetTool = defineTool({
      name: 'greet',
      description: 'Greet a user by name',
      input: z.object({
      name: z.string().describe('The name to greet'),
      }),
      handler: async ({ name }) => text(`Hello, ${name}!`),
      });
      // tools/items.ts - With annotations and external API client
      import { defineTool, json } from 'mcp-server-framework';
      import { z } from 'zod';
      import { apiClient } from '../api/client.js';

      export const listItems = defineTool({
      name: 'list_items',
      description: 'List all items',
      input: z.object({ category: z.string() }),
      annotations: { readOnlyHint: true, openWorldHint: true },
      handler: async ({ category }) => {
      const items = await apiClient.items.list(category);
      return json(items);
      },
      });