FastMCP Framework
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
-
ERR_REQUIRE_ESM: require() of ES Module ... not supported. Instead change the require of index.js to a dynamic import()
cause Attempting to use `require()` to import FastMCP or its dependencies in a CommonJS context, but the package is ESM-first.fixUpdate your project to use ES module `import` syntax (`import { Name } from 'pkg';`) or ensure your Node.js environment is configured for ESM. If using a CommonJS file, you might need to use `import()` dynamically or convert the file to ESM. -
ZodError: [ { "code": "invalid_type", "expected": "number", "received": "string", "path": [ "a" ], "message": "Expected number, received string" } ]cause Input parameters provided to a tool or prompt do not match the defined Zod schema.fixReview the schema definition (e.g., `z.object(...)`) for the tool or prompt and ensure that the arguments passed when calling it conform to the expected types and structure. Check for missing required fields or incorrect data types. -
Error: OAuthProxy: The 'redirect_uri' provided is not valid. It must be one of the registered URIs.
cause The `redirect_uri` used in an OAuth authorization request does not match the URI(s) registered with your OAuth provider or the FastMCP `OAuthProxy` configuration. This became a stricter validation in v4.0.0.fixEnsure the `redirect_uri` in your client's OAuth request precisely matches one of the URIs configured in your OAuth provider's settings and within your `FastMCP` server's `OAuthProxy` setup. Pay close attention to trailing slashes and subpaths.
Warnings
- breaking Version 4.0.0 introduced a breaking change related to authentication, specifically validating `redirect_uri` in `OAuthProxy.authorize` to fix a CWE-601 vulnerability. Ensure your OAuth configurations comply with the new strict validation.
- breaking FastMCP's versioning policy permits breaking changes in minor versions, unlike traditional semantic versioning. This is due to the rapid evolution of the underlying MCP ecosystem. Developers should anticipate and actively review release notes for minor version updates.
- breaking The `fastmcp.server.auth` module is explicitly exempted from standard compatibility guarantees and is prone to breaking changes, even in patch versions. This is due to the fast-evolving nature of MCP authentication specifications.
- gotcha There is a Python implementation also named 'FastMCP' (prefecthq/fastmcp). This entry specifically refers to the TypeScript framework (punkpeye/fastmcp-ts). Ensure you are using the correct library for your project language.
Install
-
npm install firecrawl-fastmcp -
yarn add firecrawl-fastmcp -
pnpm add firecrawl-fastmcp
Imports
- FastMCP
const FastMCP = require('fastmcp');import { FastMCP } from 'fastmcp'; - z
const z = require('zod');import { z } from 'zod'; - GoogleProvider
import GoogleProvider from 'fastmcp/auth/google'; // Older or incorrect path
import { GoogleProvider } from 'fastmcp';
Quickstart
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.");