GuidesMCP
Tutorial: building an MCP server in TypeScript
1. Install
mkdir mcp-stock && cd mcp-stock
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node
In package.json, add "type": "module".
2. The server
// src/index.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
const server = new McpServer({ name: 'stock', version: '1.0.0' });
server.registerTool(
'read_stock',
{
description: 'Returns the available stock of a product from its SKU.',
inputSchema: { sku: z.string().describe('Product SKU, e.g. TSH-042') },
},
async ({ sku }) => {
const qty = await readStock(sku); // your database or API
return { content: [{ type: 'text', text: `${sku}: ${qty} in stock` }] };
},
);
await server.connect(new StdioServerTransport());
3. Good practice
- A precise descriptionIt is what the AI reads to pick the tool.
- Typed parametersThe Zod schema rejects invalid input.
- Logs on stderrWith stdio, standard output is reserved for the protocol.
- Limited rightsRead-only as long as that is enough.
4. Compile and connect
npx tsc produces build/index.js. To see it in Claude: connect an MCP server to Claude Desktop. The principle: the MCP server.
Frequently asked questions
- How is it different from a regular API?
- The MCP server describes its tools in a standard format that AI clients know how to read. The same server works with Claude Desktop, Claude Code or a custom agent.
- Where does the server run?
- Locally, started by the client (stdio transport), or on a remote server reached over HTTP. Start locally.
- How do I secure the tools?
- A tool only accesses what it needs, with a limited-rights account. Actions that change things (payment, deletion) ask for confirmation.