sayHello
Generate personalized greetings by name using this TypeScript MCP server template tool for testing and demonstration purposes.
Instructions
Says hello to a person by name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:36-47 (handler)The asynchronous handler function for the 'sayHello' tool. It validates the input arguments using SayHelloSchema and returns a text response greeting the provided name.async (args: { [x: string]: any }) => { const { name }: SayHelloArgs = validateToolArgs(SayHelloSchema, args); return { content: [ { type: 'text', text: `Hello, ${name}!`, }, ], }; }
- src/schemas/toolSchemas.ts:15-17 (schema)Zod schema defining the input structure for the sayHello tool, requiring a 'name' field validated against NameSchema.export const SayHelloSchema = z.object({ name: NameSchema, });
- src/server.ts:30-48 (registration)Registration of the 'sayHello' tool on the MCP server, including name, metadata (title, description), and the handler function with Zod validation.server.registerTool( 'sayHello', { title: 'Say Hello', description: 'Says hello to a person by name', }, async (args: { [x: string]: any }) => { const { name }: SayHelloArgs = validateToolArgs(SayHelloSchema, args); return { content: [ { type: 'text', text: `Hello, ${name}!`, }, ], }; } );
- src/schemas/toolSchemas.ts:65-77 (helper)Helper utility function used in the sayHello handler to validate tool arguments against the Zod schema, providing detailed error messages.export function validateToolArgs<T>(schema: z.ZodSchema<T>, args: unknown): T { try { return schema.parse(args); } catch (error) { if (error instanceof z.ZodError) { const errorMessages = error.errors .map(err => `${err.path.join('.')}: ${err.message}`) .join(', '); throw new Error(`Validation failed: ${errorMessages}`); } throw error; } }