Create your first FastMCP server in minutes with this step-by-step guide
This guide will walk you through creating a simple MCP server with FastMCP. You’ll learn how to define tools, run your server, and test it with the CLI.
import { FastMCP } from "fastmcp";import { z } from "zod";const server = new FastMCP({ name: "My First Server", version: "1.0.0",});server.addTool({ name: "add", description: "Add two numbers", parameters: z.object({ a: z.number().describe("The first number"), b: z.number().describe("The second number"), }), execute: async (args) => { return String(args.a + args.b); },});server.start({ transportType: "stdio",});
For long-running operations, stream partial results:
server.addTool({ name: "stream-poem", description: "Generate a poem line by line", parameters: z.object({ theme: z.string().describe("Theme for the poem"), }), annotations: { streamingHint: true, }, execute: async (args, { streamContent }) => { const lines = [ `Poem about ${args.theme} - line 1`, `Poem about ${args.theme} - line 2`, `Poem about ${args.theme} - line 3`, ]; for (const line of lines) { await streamContent({ type: "text", text: line + "\n", }); await new Promise(resolve => setTimeout(resolve, 1000)); } },});