sayHello
Greets a user by name within the TypeScript MCP Server Template, using Fastify and Docker, to simplify personalized interactions in server applications.
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 handler function that executes the sayHello tool logic: validates the input using SayHelloSchema and returns an MCP-formatted text response with a personalized greeting.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 by NameSchema.export const SayHelloSchema = z.object({ name: NameSchema, });
- src/server.ts:30-48 (registration)Registration of the sayHello tool on the MCP server instance, including metadata (title, description) and the inline handler function.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/commonSchemas.ts:136-140 (schema)Reusable Zod schema for person names, used within SayHelloSchema for input validation.export const NameSchema = z .string() .min(1, 'Name is required') .max(100, 'Name cannot exceed 100 characters') .describe("Person's name");
- src/schemas/toolSchemas.ts:65-77 (helper)Utility function to parse and validate tool arguments against a Zod schema, throwing descriptive errors on failure; used in the sayHello handler.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; } }