Create Your First MCP Server (Engineer Track)

Add a Tool to Your Server

Open src/index.ts and update it:

1import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3import { z } from 'zod';
4
5const server = new McpServer({
6    name: 'my-mcp-server',
7    version: '1.0.0'
8});
9
10// Register the greet tool
11server.registerTool(
12    'greet',
13    {
14        title: 'Greet',
15        description: 'Greet someone by name',
16        inputSchema: {
17            name: z.string().describe('The name of the person to greet')
18        },
19        outputSchema: {
20            greeting: z.string()
21        }
22    },
23    async ({ name }) => {
24        const output = { greeting: `Hello, ${name}! Welcome to MCP.` };
25        return {
26            content: [{ type: 'text', text: output.greeting }],
27            structuredContent: output
28        };
29    }
30);
31
32const transport = new StdioServerTransport();
33await server.connect(transport);

Understanding the Code

Import Zod

1import { z } from 'zod';

Zod is a schema validation library. It defines what inputs your tool accepts and what outputs it returns.

Register a Tool

1server.registerTool(
2    'greet',              // Tool name (used by AI to call it)
3    { ... },              // Metadata (title, description, schemas)
4    async ({ name }) => { // Handler function
5        // Your logic here
6    }
7);

Input Schema

1inputSchema: {
2    name: z.string().describe('The name of the person to greet')
3}
  • z.string() - The parameter must be a string
  • .describe() - Helps the AI understand what to pass

Common Zod types:

  • z.string() - Text
  • z.number() - Numbers
  • z.boolean() - True/false
  • z.array(z.string()) - Array of strings
  • z.object({ key: z.string() }) - Nested object
  • z.string().optional() - Optional parameter

Output Schema

1outputSchema: {
2    greeting: z.string()
3}

Defines the structure of your tool's response. Helps clients understand what to expect.

Return Value

1return {
2    content: [{ type: 'text', text: output.greeting }],
3    structuredContent: output
4};
  • content - Array of content blocks (text, images, etc.) for display
  • structuredContent - Typed object matching outputSchema for programmatic use

Adding More Tools

You can register multiple tools:

1server.registerTool('greet', { ... }, async ({ name }) => { ... });
2server.registerTool('farewell', { ... }, async ({ name }) => { ... });
3server.registerTool('add-numbers', { ... }, async ({ a, b }) => { ... });

Example: Tool with Multiple Parameters

1server.registerTool(
2    'calculate',
3    {
4        title: 'Calculate',
5        description: 'Perform basic math operations',
6        inputSchema: {
7            a: z.number().describe('First number'),
8            b: z.number().describe('Second number'),
9            operation: z.enum(['add', 'subtract', 'multiply', 'divide'])
10                       .describe('Math operation to perform')
11        },
12        outputSchema: {
13            result: z.number(),
14            expression: z.string()
15        }
16    },
17    async ({ a, b, operation }) => {
18        let result: number;
19        switch (operation) {
20            case 'add': result = a + b; break;
21            case 'subtract': result = a - b; break;
22            case 'multiply': result = a * b; break;
23            case 'divide': result = a / b; break;
24        }
25        const output = { 
26            result, 
27            expression: `${a} ${operation} ${b} = ${result}` 
28        };
29        return {
30            content: [{ type: 'text', text: output.expression }],
31            structuredContent: output
32        };
33    }
34);