FastMCP Framework

1.0.4 · active · verified Sun Apr 19

FastMCP is an opinionated TypeScript framework for efficiently building Model Context Protocol (MCP) servers, currently stable at version 4.0.0. It aims to simplify the creation of MCP servers by abstracting away low-level implementation details, handling boilerplate, and providing intuitive, high-level APIs for common tasks like tool, resource, and prompt definition, authentication, and session management. The project maintains a rapid release cadence, often shipping minor versions every 3-4 weeks and immediately releasing patch versions for critical bug fixes and security updates, reflecting its commitment to staying current with the evolving MCP ecosystem. Key differentiators include built-in OAuth 2.1 authentication (with support for Google, GitHub, Azure, and custom providers), comprehensive session management, multiple transport options (HTTP streaming, SSE, stdio), edge runtime support, and custom HTTP route capabilities. It is designed for developers who want to quickly build robust MCP servers without delving into the intricacies of the underlying MCP SDK, on which it is built.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a FastMCP server, add a type-safe tool for arithmetic operations, define a reusable prompt, and start the server using HTTP streaming transport, including basic session logging.

import { FastMCP, FastMCPSession } from "fastmcp";
import { z } from "zod";

const server = new FastMCP({
  name: "My CalculatMCP",
  version: "1.0.0",
  // Configure logging to see server activity
  loggingLevel: "debug",
  // Enable ping to keep connections alive, useful for long-running sessions
  ping: {
    enabled: true,
    intervalMs: 15000 // Ping every 15 seconds
  }
});

server.addTool({
  name: "add",
  description: "Add two numbers and return the sum",
  parameters: z.object({
    a: z.number().describe("The first number to add"),
    b: z.number().describe("The second number to add")
  }),
  execute: async (args: { a: number; b: number }, { session }: { session: FastMCPSession }) => {
    // Log the operation within the session context
    session.info(`Executing 'add' tool for numbers: ${args.a} and ${args.b}`);
    return `The sum of ${args.a} and ${args.b} is ${args.a + args.b}.`;
  }
});

server.addPrompt({
  name: "greetUser",
  description: "A reusable prompt to greet a user by name.",
  parameters: z.object({
    name: z.string().describe("The name of the user to greet.")
  }),
  render: async (args: { name: string }) => {
    return `Hello, ${args.name}! Welcome to FastMCP.`;
  }
});

// Start the server, listening for connections
server.start({
  transportType: "httpStream", // Or "stdio" for local testing with MCP CLI
  httpStream: {
    port: 8080,
    endpoint: "/mcp"
  }
});

console.log("FastMCP server started on http://localhost:8080/mcp");
console.log("Use a compatible MCP client or inspector to interact.");

view raw JSON →