Notes

Write a minimal MCP server with the TypeScript v2 SDK

Register a schema-validated tool with @modelcontextprotocol/server and connect it over stdio. Use MCP Inspector to list and call the tool.

Use the official TypeScript v2 package, @modelcontextprotocol/server, register a tool with a Zod input schema, and connect StdioServerTransport. The v2 SDK implements MCP revision 2026-07-28. Its package names and imports differ from the older monolithic @modelcontextprotocol/sdk examples still common in search results.

Create an isolated project

These commands create a temporary project. The example pins server SDK 2.4.0 and Inspector 2.15.0, the package versions available for this November 2026 example. Use Node 26.13.0; Inspector 2.15.0 declares a Node floor of ^22.23.4 || ^24.22.1 || >=26.12.1.

mkdir -p /tmp/mcp-add-example
cd /tmp/mcp-add-example
npm init -y
npm install @modelcontextprotocol/server@2.4.0 zod@4 \
  @modelcontextprotocol/inspector@2.15.0

Save the following as server.mjs. It is executable JavaScript using the TypeScript SDK; the .mjs extension enables ES modules without a build step. A TypeScript project can use the same imports and schema with inferred handler argument types.

import { McpServer } from '@modelcontextprotocol/server';
import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
import * as z from 'zod/v4';

const server = new McpServer({ name: 'tiny-tools', version: '1.0.0' });
server.registerTool('add', {
  description: 'Add two finite numbers',
  inputSchema: z.object({ a: z.number(), b: z.number() })
}, async ({ a, b }) => ({
  content: [{ type: 'text', text: String(a + b) }]
}));
await server.connect(new StdioServerTransport());

registerTool advertises the tool and converts the Zod object into its input schema. The SDK validates incoming arguments before calling the handler. The handler returns an MCP content array; a bare number is not a tool result. The protocol version comes from SDK negotiation, while 1.0.0 above is your server application’s version.

Test the protocol with Inspector

Run these from the same temporary directory:

npx @modelcontextprotocol/inspector --cli node server.mjs \
  --method tools/list
npx @modelcontextprotocol/inspector --cli node server.mjs \
  --method tools/call --tool-name add --tool-arg a=2 --tool-arg b=3

The first command should list add with two required numeric properties. The second returns text content containing 5. Inspector starts a child process, initializes an MCP connection and performs the requested method. Each invocation creates a fresh session. Try --tool-arg a=hello to inspect schema rejection: it should fail validation instead of entering the addition handler.

For interactive inspection, npx @modelcontextprotocol/inspector node server.mjs starts the web interface. Use its tool list and argument form to inspect the same exchange. See the official Inspector CLI guide for command semantics.

Keep stdout for protocol messages

Starting node server.mjs directly appears to wait because it is listening for JSON-RPC messages on stdin. It does not expose an HTTP port or provide a chat interface. A host or Inspector must drive the MCP lifecycle.

Do not add console.log startup banners: stdio uses stdout for protocol traffic. Send diagnostic messages to stderr with console.error. Treat future filesystem, network or shell tools as separate authorization decisions; the tiny addition tool needs none of those capabilities.

Go deeper into MCP conversations and transports. The official server guide and 2026-07-28 specification define this API and protocol combination.