access external data and functionality in a secure, standardized way. It acts as a bridge that lets an LLM retrieve information or perform a specific task. For example, when an LLM needs weather information, it can use MCP to retrieve it from a weather API. Learn more in the official MCP documentation↗.
Here, @modelcontextprotocol/sdk is the MCP TypeScript SDK, and zod is a library for validating data. The typescript and @types/node packages are required for TypeScript development.
Now let's implement a server that provides a simple calculator and a greeting.
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";import { z } from "zod";const server = new McpServer({ name: "Demo", version: "1.0.0"});server.tool( "add", { a: z.number(), b: z.number() }, async ({ a, b }) => ({ content: [{ type: "text", text: String(a + b) }] }));server.resource( "greeting", new ResourceTemplate("greeting://{name}", { list: undefined }), async (uri, { name }) => ({ contents: [{ uri: uri.href, text: `안녕하세요, ${name}님!` }] }));const transport = new StdioServerTransport();await server.connect(transport);
This code creates an MCP server and adds a tool named add that calculates the sum of two numbers. It also adds a resource named greeting that generates a greeting dynamically from the user's name. Finally, it handles messages through standard input and output.
We started with the basics of MCP and built an MCP server step by step with the TypeScript SDK. MCP gives LLMs a safer, more consistent way to use external data and functionality. The SDK makes it relatively easy to build a server, so try extending yours with other features.