شعار FastMCP

FastMCP

المجتمعشائع
punkpeye

A TypeScript framework for building MCP servers.

الناشرpunkpeye
المستودعfastmcp
اللغةTypeScript
التفريعات
317
النجوم
3.3K
الأدوات المتاحة
0
نوع نقل البياناتstdio
الفئات
الترخيصMIT
الروابط
  • اربط الأدوات بسير عمل الذكاء الاصطناعي

    يتيح FastMCP إمكانات MCP التي يمكن لتطبيقات الذكاء الاصطناعي العميلة ووكلاء الذكاء الاصطناعي المتوافقة استخدامها.

  • الأدوات المتاحة: 0

    تصفّح الإجراءات القابلة للاستدعاء أدناه، بما في ذلك أسماؤها وأوصافها عندما يوفّرها الخادم.

  • إعدادات جاهزة للنسخ

    استخدم أمثلة إعداد التثبيت لتهيئة هذا الخادم في تطبيق MCP العميل المفضّل لديك.

  • مؤشرات الاهتمام بالمصدر المفتوح

    النجوم: 3.3K، والنسخ المتفرعة: 317 في المستودع المرتبط.

FastMCP

A TypeScript framework for building MCP servers capable of handling client sessions.

[!IMPORTANT]

FastMCP implements the legacy, handshake-based MCP revisions (2025-11-25 and earlier). It does not support the current specification, 2026-07-28, which made the protocol stateless — no initialize handshake and no Mcp-Session-Id. For a framework targeting the current spec, use ViteMCP.

Features

When to use FastMCP over the official SDK?

FastMCP is built on top of the official SDK.

The official SDK provides foundational blocks for building MCPs, but leaves many implementation details to you:

FastMCP eliminates this complexity by providing an opinionated framework that:

  • Handles all the boilerplate automatically
  • Provides simple, intuitive APIs for common tasks
  • Includes built-in best practices and error handling
  • Lets you focus on your MCP's core functionality

When to choose FastMCP: You want to build MCP servers quickly without dealing with low-level implementation details.

When to use the official SDK: You need maximum control or have specific architectural requirements. In this case, we encourage referencing FastMCP's implementation to avoid common pitfalls.

Installation

bash
npm install fastmcp

Quickstart

[!NOTE]

There are many real-world examples of using FastMCP in the wild. See the Showcase for examples.

ts
import { FastMCP } from "fastmcp";
import { z } from "zod"; // Or any validation library that supports Standard Schema

const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
});

server.addTool({
  name: "add",
  description: "Add two numbers",
  parameters: z.object({
    a: z.number(),
    b: z.number(),
  }),
  execute: async (args) => {
    return String(args.a + args.b);
  },
});

server.start({
  transportType: "stdio",
});

That's it! You have a working MCP server.

You can test the server in terminal with:

bash
git clone https://github.com/punkpeye/fastmcp.git
cd fastmcp

pnpm install
pnpm build

# Test the addition server example using CLI:
npx fastmcp dev src/examples/addition.ts
# Test the addition server example using MCP Inspector:
npx fastmcp inspect src/examples/addition.ts

If you are looking for a boilerplate repository to build your own MCP server, check out fastmcp-boilerplate.

Remote Server Options

FastMCP supports multiple transport options for remote communication, allowing an MCP hosted on a remote machine to be accessed over the network.

HTTP Streaming

HTTP streaming provides a more efficient alternative to SSE in environments that support it, with potentially better performance for larger payloads.

You can run the server with HTTP streaming support:

ts
server.start({
  transportType: "httpStream",
  httpStream: {
    port: 8080,
  },
});

This will start the server and listen for HTTP streaming connections on http://localhost:8080/mcp.

Note: You can also customize the endpoint path using the httpStream.endpoint option (default is /mcp).

Note: To serve HTTP streaming and built-in OAuth routes under an issuer path, set httpStream.basePath (for example, /issuer1). This exposes authorization server metadata at /.well-known/oauth-authorization-server/issuer1 per RFC 8414.

Note: This also starts an SSE server on http://localhost:8080/sse.

You can connect to these servers using the appropriate client transport.

For HTTP streaming connections:

ts
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const client = new Client(
  {
    name: "example-client",
    version: "1.0.0",
  },
  {
    capabilities: {},
  },
);

const transport = new StreamableHTTPClientTransport(
  new URL(`http://localhost:8080/mcp`),
);

await client.connect(transport);

For SSE connections:

ts
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";

const client = new Client(
  {
    name: "example-client",
    version: "1.0.0",
  },
  {
    capabilities: {},
  },
);

const transport = new SSEClientTransport(new URL(`http://localhost:8080/sse`));

await client.connect(transport);
HTTPS Support

FastMCP supports HTTPS for secure connections by providing SSL certificate options:

ts
server.start({
  transportType: "httpStream",
  httpStream: {
    port: 8443,
    sslCert: "./path/to/cert.pem",
    sslKey: "./path/to/key.pem",
    sslCa: "./path/to/ca.pem", // Optional: for client certificate authentication
  },
});

This will start the server with HTTPS on https://localhost:8443/mcp.

SSL Options:

  • sslCert - Path to SSL certificate file
  • sslKey - Path to SSL private key file
  • sslCa - (Optional) Path to CA certificate for mutual TLS authentication

For testing, you can generate self-signed certificates:

bash
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"

For production, obtain certificates from a trusted CA like Let's Encrypt.

See the https-server example for a complete demonstration.

CORS Configuration

By default, FastMCP enables CORS with a standard set of allowed headers. You can customize the CORS behavior by passing a cors option:

ts
server.start({
  transportType: "httpStream",
  httpStream: {
    port: 8080,
    cors: {
      origin: "http://localhost:3000",
      allowedHeaders: [
        "Content-Type",
        "Authorization",
        "Accept",
        "Mcp-Session-Id",
        "Mcp-Protocol-Version",
        "Last-Event-Id",
        "X-Custom-Header",
      ],
      credentials: true,
    },
  },
});

The cors option accepts:

  • true (default) - enable CORS with default settings
  • false - disable CORS entirely
  • An object with these fields:
    • origin - a string, array of strings, or a function (origin: string) => boolean
    • allowedHeaders - a string or array of strings
    • methods - array of allowed HTTP methods
    • exposedHeaders - array of headers to expose
    • credentials - boolean to allow credentials
    • maxAge - preflight cache duration in seconds

The CorsOptions type is exported from fastmcp for convenience.

Custom HTTP Routes

FastMCP allows you to add custom HTTP routes alongside MCP endpoints, enabling you to build comprehensive HTTP services that include REST APIs, webhooks, admin interfaces, and more - all within the same server process.

ts
const app = server.getApp();

// Add REST API endpoints with Hono's native API
app.get("/api/users", async (c) => {
  return c.json({ users: [] });
});

// Handle path parameters
app.get("/api/users/:id", async (c) => {
  return c.json({
    userId: c.req.param("id"),
    query: c.req.query(), // Access query parameters
  });
});

// Handle POST requests with body parsing
app.post("/api/users", async (c) => {
  const body = await c.req.json();
  return c.json({ created: body }, 201);
});

// Serve HTML content
app.get("/admin", async (c) => {
  return c.html("<html><body><h1>Admin Panel</h1></body></html>");
});

// Handle webhooks
app.post("/webhook/github", async (c) => {
  const payload = await c.req.json();
  const event = c.req.header("x-github-event");

  // Process webhook...
  return c.json({ received: true });
});

Custom routes use the underlying Hono app returned by server.getApp() and support:

  • Hono's HTTP methods: get, post, put, delete, patch, options, and more
  • Path parameters (:param) and wildcards (*)
  • Query string parsing
  • JSON, text, form, and other body helpers from c.req
  • Custom status codes and headers
  • Middleware and route groups through Hono

Routes are matched in the order they are registered, allowing you to define specific routes before catch-all patterns.

Public and Protected Routes

Custom Hono routes are public unless you add your own route middleware or authentication checks. For protected custom routes, put your auth logic in a reusable helper and call it from both FastMCP's authenticate option and your Hono route handlers:

ts
import type { IncomingMessage } from "node:http";
import type { Context } from "hono";
import { FastMCP } from "fastmcp";

async function authenticateRequest(request: IncomingMessage) {
  const apiKey = request.headers["x-api-key"];
  return apiKey === "123" ? { userId: "123" } : undefined;
}

const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  authenticate: authenticateRequest,
});

const app = server.getApp();

async function requireAuth(c: Context) {
  const auth = await authenticateRequest(c.env.incoming);

  if (!auth) {
    return c.json({ error: "Authentication required" }, 401);
  }

  return auth;
}

// Public route - no authentication required
app.get("/.well-known/openid-configuration", async (c) => {
  return c.json({
    issuer: "https://example.com",
    authorization_endpoint: "https://example.com/auth",
    token_endpoint: "https://example.com/token",
  });
});

// Private route - requires authentication
app.get("/api/users", async (c) => {
  const auth = await requireAuth(c);
  if (auth instanceof Response) {
    return auth;
  }

  return c.json({ users: [] });
});

// Public static files
app.get("/public/*", async (c) => {
  return c.text(`File: ${c.req.path}`);
});

Public routes are perfect for:

  • OAuth discovery endpoints (.well-known/*)
  • Health checks and status pages
  • Static assets and documentation
  • Webhook endpoints from external services
  • Public APIs that don't require user authentication

See the custom-routes example for a complete demonstration.

Edge Runtime Support

FastMCP supports edge runtimes like Cloudflare Workers, enabling deployment of MCP servers to the edge with minimal latency worldwide.

Choosing Between FastMCP and EdgeFastMCP
Use CaseClassImport
Node.js, Express, BunFastMCPimport { FastMCP } from "fastmcp"
Cloudflare Workers, Deno DeployEdgeFastMCPimport { EdgeFastMCP } from "fastmcp/edge"
FeatureFastMCPEdgeFastMCP
RuntimeNode.jsEdge (V8 isolates)
Start methodserver.start({ port })export default server
Transportstdio, httpStream, SSEHTTP Streamable only
SessionsStateful or statelessStateless only
File systemYesNo
OAuth/AuthenticationBuilt-in authenticate optionUse Hono middleware (built-in planned)
Custom routesserver.getApp()server.getApp()

Note: Built-in authentication for EdgeFastMCP is planned for a future release. Both FastMCP and EdgeFastMCP use Hono internally, so there's no technical barrier—EdgeFastMCP was simply written before OAuth was added to FastMCP. PRs are welcome to add an authenticate option that accepts web Request instead of Node.js http.IncomingMessage.

In the meantime, use Hono middleware:

ts
const app = server.getApp();
app.use("/api/*", async (c, next) => {
  if (c.req.header("authorization") !== "Bearer secret") {
    return c.json({ error: "Unauthorized" }, 401);
  }
  await next();
});
Cloudflare Workers

To deploy FastMCP to Cloudflare Workers, use the EdgeFastMCP class from the /edge subpath:

ts
import { EdgeFastMCP } from "fastmcp/edge";
import { z } from "zod";

const server = new EdgeFastMCP({
  name: "My Edge Server",
  version: "1.0.0",
  description: "MCP server running on Cloudflare Workers",
});

// Add tools, resources, prompts as usual
server.addTool({
  name: "greet",
  description: "Greet someone",
  parameters: z.object({
    name: z.string(),
  }),
  execute: async ({ name }) => {
    return `Hello, ${name}! Served from the edge.`;
  },
});

// Export the server as the default (required for Cloudflare Workers)
export default server;
Edge Runtime Differences

When running on edge runtimes:

  • Stateless by default: Each request is handled independently
  • No filesystem access: Use fetch APIs for external data
  • V8 Isolates: Fast cold starts and efficient resource usage
  • Global deployment: Automatic distribution to edge locations
Custom Routes on Edge

You can access the underlying Hono app to add custom HTTP routes:

ts
const app = server.getApp();

// Add a landing page
app.get("/", (c) => c.html("<h1>Welcome to my MCP server</h1>"));

// Add REST API endpoints
app.get("/api/status", (c) => c.json({ status: "ok" }));
Deployment

Configure your wrangler.toml:

toml
name = "my-mcp-server"
main = "src/index.ts"
compatibility_date = "2024-01-01"

Deploy with:

bash
wrangler deploy

See the edge-cloudflare-worker example for a complete demonstration.

Stateless Mode

FastMCP supports stateless operation for HTTP streaming, where each request is handled independently without maintaining persistent sessions. This is ideal for serverless environments, load-balanced deployments, or when session state isn't required.

In stateless mode:

  • No sessions are tracked on the server
  • Each request creates a temporary session that's discarded after the response
  • Reduced memory usage and better scalability
  • Perfect for stateless deployment environments

You can enable stateless mode by adding the stateless: true option:

ts
server.start({
  transportType: "httpStream",
  httpStream: {
    port: 8080,
    stateless: true,
  },
});

Note: Stateless mode is only available with HTTP streaming transport. Features that depend on persistent sessions (like session-specific state) will not be available in stateless mode.

You can also enable stateless mode using CLI arguments or environment variables:

bash
# Via CLI argument
npx fastmcp dev src/server.ts --transport http-stream --port 8080 --stateless true

# Via environment variable
FASTMCP_STATELESS=true npx fastmcp dev src/server.ts

The /ready health check endpoint will indicate when the server is running in stateless mode:

json
{
  "mode": "stateless",
  "ready": 1,
  "status": "ready",
  "total": 1
}

Core Concepts

Tools

Tools in MCP allow servers to expose executable functions that can be invoked by clients and used by LLMs to perform actions.

FastMCP uses the Standard Schema specification for defining tool parameters. This allows you to use your preferred schema validation library (like Zod, ArkType, or Valibot) as long as it implements the spec.

Zod Example:

typescript
import { z } from "zod";

server.addTool({
  name: "fetch-zod",
  description: "Fetch the content of a url (using Zod)",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return await fetchWebpageContent(args.url);
  },
});

ArkType Example:

typescript
import { type } from "arktype";

server.addTool({
  name: "fetch-arktype",
  description: "Fetch the content of a url (using ArkType)",
  parameters: type({
    url: "string",
  }),
  execute: async (args) => {
    return await fetchWebpageContent(args.url);
  },
});

Valibot Example:

Valibot requires the peer dependency @valibot/to-json-schema.

typescript
import * as v from "valibot";

server.addTool({
  name: "fetch-valibot",
  description: "Fetch the content of a url (using Valibot)",
  parameters: v.object({
    url: v.string(),
  }),
  execute: async (args) => {
    return await fetchWebpageContent(args.url);
  },
});

Plain JSON Schema Example:

If you already have a JSON Schema — from an OpenAPI document, a config file, or another server — jsonSchemaAdapter wraps it so it can be used directly, with no schema library in between.

It requires the peer dependency ajv, which does the validation, plus ajv-formats if your schema uses format keywords such as email or uri. Both are imported the first time a tool is called, so servers that don't use this pay nothing for it.

bash
npm install ajv ajv-formats
typescript
import { jsonSchemaAdapter } from "fastmcp";

server.addTool({
  name: "fetch-json-schema",
  description: "Fetch the content of a url (using plain JSON Schema)",
  parameters: jsonSchemaAdapter({
    type: "object",
    properties: {
      url: { type: "string", format: "uri" },
    },
    required: ["url"],
  }),
  execute: async (args) => {
    const { url } = args as { url: string };
    return await fetchWebpageContent(url);
  },
});

Works for outputSchema too. Note that FastMCP advertises input schemas with additionalProperties: false, whatever your schema said — the same treatment Zod and Valibot schemas get. Output schemas keep whatever additional-properties rule your schema declared.

Unlike the schema libraries above, a plain JSON Schema carries no TypeScript types, so execute receives unknown arguments. Cast or narrow them yourself.

Tools Without Parameters

When creating tools that don't require parameters, you have two options:

  1. Omit the parameters property entirely:

    typescript
    server.addTool({
      name: "sayHello",
      description: "Say hello",
      // No parameters property
      execute: async () => {
        return "Hello, world!";
      },
    });
  2. Explicitly define empty parameters:

    typescript
    import { z } from "zod";
    
    server.addTool({
      name: "sayHello",
      description: "Say hello",
      parameters: z.object({}), // Empty object
      execute: async () => {
        return "Hello, world!";
      },
    });

[!NOTE]

Both approaches are fully compatible with all MCP clients, including Cursor. FastMCP automatically generates the proper schema in both cases.

Structured Tool Output

Tools can declare an outputSchema and return structured data. FastMCP exposes that value as MCP structuredContent, while also returning a JSON text fallback for clients that only render text content.

typescript
server.addTool({
  name: "get-weather",
  description: "Get weather for a city",
  parameters: z.object({
    city: z.string(),
  }),
  outputSchema: z.object({
    temperature: z.number(),
    humidity: z.number(),
  }),
  execute: async ({ city }) => {
    const weather = await getWeather(city);

    return {
      temperature: weather.temperature,
      humidity: weather.humidity,
    };
  },
});

You can also return explicit text content and structured content together:

typescript
server.addTool({
  name: "get-weather",
  description: "Get weather for a city",
  parameters: z.object({
    city: z.string(),
  }),
  outputSchema: z.object({
    temperature: z.number(),
    humidity: z.number(),
  }),
  execute: async ({ city }) => {
    const weather = await getWeather(city);

    return {
      content: [
        {
          type: "text",
          text: `${city}: ${weather.temperature}F`,
        },
      ],
      structuredContent: {
        temperature: weather.temperature,
        humidity: weather.humidity,
      },
    };
  },
});

When outputSchema is provided, FastMCP validates structuredContent before sending the tool result. Invalid structured output is returned to the client as a tool error instead of silently violating the advertised schema.

Tool Authorization

You can control which tools are available to authenticated users by adding an optional canAccess function to a tool's definition. This function receives the authentication context and should return true if the user is allowed to access the tool.

typescript
server.addTool({
  name: "admin-tool",
  description: "An admin-only tool",
  canAccess: (auth) => auth?.role === "admin",
  execute: async () => "Welcome, admin!",
});

Returning a string

execute can return a string:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return "Hello, world!";
  },
});

The latter is equivalent to:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return {
      content: [
        {
          type: "text",
          text: "Hello, world!",
        },
      ],
    };
  },
});

Returning a list

If you want to return a list of messages, you can return an object with a content property:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return {
      content: [
        { type: "text", text: "First message" },
        { type: "text", text: "Second message" },
      ],
    };
  },
});

Returning an image

Use the imageContent to create a content object for an image:

js
import { imageContent } from "fastmcp";

server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return imageContent({
      url: "https://example.com/image.png",
    });

    // or...
    // return imageContent({
    //   path: "/path/to/image.png",
    // });

    // or...
    // return imageContent({
    //   buffer: Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=", "base64"),
    // });

    // or...
    // return {
    //   content: [
    //     await imageContent(...)
    //   ],
    // };
  },
});

The imageContent function takes the following options:

  • url: The URL of the image.
  • timeoutMs: Optional timeout for a URL download in milliseconds (defaults to 30 seconds).
  • path: The path to the image file.
  • buffer: The image data as a buffer.

Only one of url, path, or buffer must be specified.

The above example is equivalent to:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return {
      content: [
        {
          type: "image",
          data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
          mimeType: "image/png",
        },
      ],
    };
  },
});

Configurable Ping Behavior

FastMCP includes a configurable ping mechanism to maintain connection health. The ping behavior can be customized through server options:

ts
const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  ping: {
    // Explicitly enable or disable pings (defaults vary by transport)
    enabled: true,
    // Configure ping interval in milliseconds (default: 5000ms)
    intervalMs: 10000,
    // Set log level for ping-related messages (default: 'debug')
    logLevel: "debug",
  },
});

By default, ping behavior is optimized for each transport type:

  • Enabled for SSE and HTTP streaming connections (which benefit from keep-alive)
  • Disabled for stdio connections (where pings are typically unnecessary)

This configurable approach helps reduce log verbosity and optimize performance for different usage scenarios.

Keeping Long Tool Calls Alive (streamKeepalive)

Pings travel on the standalone server-to-client stream, so they never keep an individual tool call's connection open — and in stateless mode that stream does not exist at all. A tool that runs for minutes without producing output leaves its response connection silent, and an idle-connection timeout in front of the server (AWS ALB defaults to 60 seconds) closes it before the result is written.

This applies to both session modes. A stateful deployment keeps its standalone stream warm with pings while the connection carrying the tool call goes idle and is closed anyway; stateless has no standalone stream to begin with.

streamKeepalive writes periodically to the in-flight tool call's own response stream, which is the connection that would otherwise be closed. It is opt-in:

ts
const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  streamKeepalive: {
    // Opt in; disabled by default
    enabled: true,
    // Keep comfortably below the shortest idle timeout on the path
    intervalMs: 20000,
  },
});

Each keepalive is a notifications/message (level configurable via logLevel, default debug) tagged with the logger fastmcp-keepalive, related to the tool call being served. Keepalives start when a tool begins executing and stop when the request stops waiting for it — on success, error, timeout, or client cancellation — so idle connections stay quiet. Because the messages are related to the request, this works in stateful and stateless mode alike, and needs no client support beyond the standard logging notification.

Limits worth knowing:

  • It has no effect with httpStream.enableJsonResponse, which buffers a single JSON reply instead of streaming, so there is no open stream to write to.
  • It only covers tool calls. Other long-running requests (resources/read, prompts/get) still write nothing until they finish.
  • On stdio there is no proxy to keep alive, so enabling it there only adds notification traffic.

Cancelling Long Tool Calls (context.signal)

Every execute receives an AbortSignal that fires once its result can no longer reach anyone. Forward it to whatever does the real work so the work stops with the call instead of outliving it:

ts
server.addTool({
  name: "fetch_report",
  parameters: z.object({ url: z.string() }),
  timeoutMs: 30000,
  execute: async (args, { signal }) => {
    const response = await fetch(args.url, { signal });
    return await response.text();
  },
});

It aborts on any of three events:

  • The client cancelled the call — it sent notifications/cancelled, which is what the MCP SDK emits when a caller aborts its own request.
  • The session ended — the transport closed, or the session was closed explicitly. No cancellation notification is involved here.
  • timeoutMs elapsed — signal.reason is the same UserError the caller receives, so a tool can tell a timeout apart from a cancellation.

The signal is never aborted after a call completes normally, so attaching cleanup to it is safe.

Limits worth knowing:

  • Nothing is killed for you. FastMCP stops waiting for the tool, but the promise execute returned keeps running until it settles. A tool that ignores the signal still runs to completion — it just does so with nowhere to report.
  • An HTTP client that vanishes mid-request is not detected. The MCP SDK only aborts a request's own signal for an explicit notifications/cancelled, and StreamableHTTPServerTransport does not treat an abandoned response stream as a session close. A caller that hangs up without terminating its session (DELETE) leaves the tool running until it finishes or times out. Set timeoutMs on anything expensive rather than relying on disconnect detection.
  • load does not get one. Resources, resource templates, and prompts receive a smaller context without signal.

Health-check Endpoint

When you run FastMCP with the httpStream transport you can optionally expose a simple HTTP endpoint that returns a plain-text response useful for load-balancer or container orchestration liveness checks.

Enable (or customise) the endpoint via the health key in the server options:

ts
const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  health: {
    // Enable / disable (default: true)
    enabled: true,
    // Body returned by the endpoint (default: 'ok')
    message: "healthy",
    // Path that should respond (default: '/health')
    path: "/healthz",
    // HTTP status code to return (default: 200)
    status: 200,
  },
});

await server.start({
  transportType: "httpStream",
  httpStream: { port: 8080 },
});

Now a request to http://localhost:8080/healthz will return:

HTTP/1.1 200 OK
content-type: text/plain

healthy

The endpoint is ignored when the server is started with the stdio transport.

Roots Management

FastMCP supports Roots - Feature that allows clients to provide a set of filesystem-like root locations that can be listed and dynamically updated. The Roots feature can be configured or disabled in server options:

ts
const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  roots: {
    // Set to false to explicitly disable roots support
    enabled: false,
    // By default, roots support is enabled (true)
  },
});

This provides the following benefits:

  • Better compatibility with different clients that may not support Roots
  • Reduced error logs when connecting to clients that don't implement roots capability
  • More explicit control over MCP server capabilities
  • Graceful degradation when roots functionality isn't available

You can listen for root changes in your server:

ts
server.on("connect", (event) => {
  const session = event.session;

  // Access the current roots
  console.log("Initial roots:", session.roots);

  // Listen for changes to the roots
  session.on("rootsChanged", (event) => {
    console.log("Roots changed:", event.roots);
  });
});

When a client doesn't support roots or when roots functionality is explicitly disabled, these operations will gracefully handle the situation without throwing errors.

Returning an audio

Use the audioContent to create a content object for an audio:

js
import { audioContent } from "fastmcp";

server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return audioContent({
      url: "https://example.com/audio.mp3",
    });

    // or...
    // return audioContent({
    //   path: "/path/to/audio.mp3",
    // });

    // or...
    // return audioContent({
    //   buffer: Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=", "base64"),
    // });

    // or...
    // return {
    //   content: [
    //     await audioContent(...)
    //   ],
    // };
  },
});

The audioContent function takes the following options:

  • url: The URL of the audio.
  • timeoutMs: Optional timeout for a URL download in milliseconds (defaults to 30 seconds).
  • path: The path to the audio file.
  • buffer: The audio data as a buffer.

Only one of url, path, or buffer must be specified.

The above example is equivalent to:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return {
      content: [
        {
          type: "audio",
          data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
          mimeType: "audio/mpeg",
        },
      ],
    };
  },
});

Return combination type

You can combine various types in this way and send them back to AI

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    return {
      content: [
        {
          type: "text",
          text: "Hello, world!",
        },
        {
          type: "image",
          data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
          mimeType: "image/png",
        },
        {
          type: "audio",
          data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
          mimeType: "audio/mpeg",
        },
      ],
    };
  },

  // or...
  // execute: async (args) => {
  //   const imgContent = await imageContent({
  //     url: "https://example.com/image.png",
  //   });
  //   const audContent = await audioContent({
  //     url: "https://example.com/audio.mp3",
  //   });
  //   return {
  //     content: [
  //       {
  //         type: "text",
  //         text: "Hello, world!",
  //       },
  //       imgContent,
  //       audContent,
  //     ],
  //   };
  // },
});

Custom Logger

FastMCP allows you to provide a custom logger implementation to control how the server logs messages. This is useful for integrating with existing logging infrastructure or customizing log formatting.

ts
import { FastMCP, Logger } from "fastmcp";

class CustomLogger implements Logger {
  debug(...args: unknown[]): void {
    console.log("[DEBUG]", new Date().toISOString(), ...args);
  }

  error(...args: unknown[]): void {
    console.error("[ERROR]", new Date().toISOString(), ...args);
  }

  info(...args: unknown[]): void {
    console.info("[INFO]", new Date().toISOString(), ...args);
  }

  log(...args: unknown[]): void {
    console.log("[LOG]", new Date().toISOString(), ...args);
  }

  warn(...args: unknown[]): void {
    console.warn("[WARN]", new Date().toISOString(), ...args);
  }
}

const server = new FastMCP({
  name: "My Server",
  version: "1.0.0",
  logger: new CustomLogger(),
});

See src/examples/custom-logger.ts for examples with Winston, Pino, and file-based logging.

Logging

Tools can log messages to the client using the log object in the context object:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args, { log }) => {
    log.info("Downloading file...", {
      url,
    });

    // ...

    log.info("Downloaded file");

    return "done";
  },
});

The log object has the following methods:

  • debug(message: string, data?: SerializableValue)
  • error(message: string, data?: SerializableValue)
  • info(message: string, data?: SerializableValue)
  • warn(message: string, data?: SerializableValue)

Errors

The errors that are meant to be shown to the user should be thrown as UserError instances:

js
import { UserError } from "fastmcp";

server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args) => {
    if (args.url.startsWith("https://example.com")) {
      throw new UserError("This URL is not allowed");
    }

    return "done";
  },
});

Progress

Tools can report progress by calling reportProgress in the context object:

js
server.addTool({
  name: "download",
  description: "Download a file",
  parameters: z.object({
    url: z.string(),
  }),
  execute: async (args, { reportProgress }) => {
    await reportProgress({
      progress: 0,
      total: 100,
    });

    // ...

    await reportProgress({
      progress: 100,
      total: 100,
    });

    return "done";
  },
});

reportProgress accepts an optional human-readable message alongside the numeric fields, which clients can display next to the progress indicator:

js
await reportProgress({
  progress: 40,
  total: 100,
  message: "Downloading chunk 4 of 10…",
});

Progress notifications are only emitted when the client opts in by supplying a progressToken on the tool call; otherwise reportProgress is a no-op. Because notifications/progress is part of the MCP specification (the message field since revision 2025-03-26), this is the portable way to send incremental updates during a long-running tool call — see Streaming Output below for the difference.

Streaming Output

FastMCP can stream partial results from tools while they're still executing, enabling responsive UIs and real-time feedback. This is particularly useful for:

  • Long-running operations that generate content incrementally
  • Progressive generation of text, images, or other media
  • Operations where users benefit from seeing immediate partial results

[!IMPORTANT] streamContent is a FastMCP extension, not part of the MCP specification. It emits a notifications/tool/streamContent notification, which the MCP specification does not define — as of revision 2025-11-25 there is no standard mechanism for streaming tool output (SEP-2998 is the in-progress proposal to add one).

Clients discard notifications they have no handler registered for, silently and without error. A client only sees streamed content if it registers a handler for the method (or sets a fallbackNotificationHandler), and no client is known to render it as tool output — MCP Inspector, for example, logs it in its notifications pane via a fallback handler, but the tool result itself still shows only what execute returned. Streaming is therefore mainly useful when you also control the client — see Consuming streamed content below. If you need incremental updates that work on any client, use reportProgress with a message instead.

To stream from a tool, use the streamContent method:

js
server.addTool({
  name: "generateText",
  description: "Generate text incrementally",
  parameters: z.object({
    prompt: z.string(),
  }),
  annotations: {
    streamingHint: true, // Advisory only; see below
    readOnlyHint: true,
  },
  execute: async (args, { streamContent }) => {
    // Send initial content immediately
    await streamContent({ type: "text", text: "Starting generation...\n" });

    // Simulate incremental content generation
    const words = "The quick brown fox jumps over the lazy dog.".split(" ");
    for (const word of words) {
      await streamContent({ type: "text", text: word + " " });
      await new Promise((resolve) => setTimeout(resolve, 300)); // Simulate delay
    }

    // Always return a final result. Returning nothing sends an empty tool
    // result, so clients that ignore the streamed notifications see no output
    // at all.
    return "The quick brown fox jumps over the lazy dog.";
  },
});

[!WARNING] Returning undefined from execute produces a tool result with empty content. If you stream everything and return nothing, the tool call resolves to an empty result with no indication that anything was lost — including on clients that do log the notification. Return the complete result as well, and treat streamed content purely as a progressive-rendering enhancement.

The streamingHint annotation is advisory metadata. It is forwarded verbatim to clients in tools/list, but it does not enable or gate streamContent, and FastMCP itself never reads it. No client is known to act on it today, though SEP-2998 proposes standardizing the same annotation name.

Consuming streamed content

A client sees these notifications only if it registers a handler for the method (or sets a fallbackNotificationHandler):

ts
import { z } from "zod";

const StreamContentNotificationSchema = z.object({
  method: z.literal("notifications/tool/streamContent"),
  params: z.object({
    content: z.array(z.any()),
    toolName: z.string(),
  }),
});

client.setNotificationHandler(
  StreamContentNotificationSchema,
  (notification) => {
    const { content, toolName } = notification.params;
    // Render the partial content however you like.
  },
);

Note that notifications carry only toolName, not a request or progress token, so concurrent calls to the same tool on one session cannot be told apart.

Streaming works with all content types (text, image, audio) and can be combined with progress reporting:

js
server.addTool({
  name: "processData",
  description: "Process data with streaming updates",
  parameters: z.object({
    datasetSize: z.number(),
  }),
  annotations: {
    streamingHint: true,
  },
  execute: async (args, { streamContent, reportProgress }) => {
    const total = args.datasetSize;

    for (let i = 0; i < total; i++) {
      // Standard progress notification: reaches every spec-compliant client
      await reportProgress({
        progress: i,
        total,
        message: `Processed ${i} of ${total} items`,
      });

      // Richer partial content: only reaches clients that opt in
      if (i % 10 === 0) {
        await streamContent({
          type: "text",
          text: `Processed ${i} of ${total} items...\n`,
        });
      }

      await new Promise((resolve) => setTimeout(resolve, 50));
    }

    return "Processing complete!";
  },
});

Elicitation

Tools can request additional information from the user mid-execution via elicitation, using the elicit method in the context object. The client must advertise the matching elicitation capability mode — elicitation: { form: {} } for form requests (the default) and/or elicitation: { url: {} } for url requests.

js
server.addTool({
  name: "delete-file",
  description: "Delete a file",
  parameters: z.object({
    path: z.string(),
  }),
  execute: async (args, { elicit }) => {
    const response = await elicit({
      message: `Are you sure you want to delete ${args.path}?`,
      requestedSchema: {
        type: "object",
        properties: {
          confirmed: {
            type: "boolean",
          },
        },
        required: ["confirmed"],
      },
    });

    if (response.action !== "accept" || !response.content?.confirmed) {
      return "Deletion cancelled.";
    }

    // ...

    return `Deleted ${args.path}`;
  },
});

The response action is "accept", "decline", or "cancel"; on accept, content holds the user's answers matching requestedSchema. Elicitation is also available outside of tools via session.requestElicitation.

Tool Annotations

As of the MCP Specification (2025-03-26), tools can include annotations that provide richer context and control by adding metadata about a tool's behavior:

typescript
server.addTool({
  name: "fetch-content",
  description: "Fetch content from a URL",
  parameters: z.object({
    url: z.string(),
  }),
  annotations: {
    title: "Web Content Fetcher", // Human-readable title for UI display
    readOnlyHint: true, // Tool doesn't modify its environment
    openWorldHint: true, // Tool interacts with external entities
  },
  execute: async (args) => {
    return await fetchWebpageContent(args.url);
  },
});

The available annotations are:

AnnotationTypeDefaultDescription
titlestring-A human-readable title for the tool, useful for UI display
readOnlyHintbooleanfalseIf true, indicates the tool does not modify its environment
destructiveHintbooleantrueIf true, the tool may perform destructive updates (only meaningful when readOnlyHint is false)
idempotentHintbooleanfalseIf true, calling the tool repeatedly with the same arguments has no additional effect (only meaningful when readOnlyHint is false)
openWorldHintbooleantrueIf true, the tool may interact with an "open world" of external entities

These annotations help clients and LLMs better understand how to use the tools and what to expect when calling them.

Resources

Resources represent any kind of data that an MCP server wants to make available to clients. This can include:

  • File contents
  • Screenshots and images
  • Log files
  • And more

Each resource is identified by a unique URI and can contain either text or binary data.

ts
server.addResource({
  uri: "file:///logs/app.log",
  name: "Application Logs",
  mimeType: "text/plain",
  async load() {
    return {
      text: await readLogFile(),
    };
  },
});

[!NOTE]

load can return multiple resources. This could be used, for example, to return a list of files inside a directory when the directory is read.

ts
async load() {
  return [
    {
      text: "First file content",
    },
    {
      text: "Second file content",
    },
  ];
}

You can also return binary contents in load:

ts
async load() {
  return {
    blob: 'base64-encoded-data'
  };
}

load also receives auth (the value returned by your authenticate function, if any) and a context object as its second and third arguments. context mirrors the client, log, session, and sessionId fields available to tool.execute (see Session ID and Request ID Tracking); reportProgress and streamContent are not included since they are tied to a tool call's progress token:

ts
server.addResource({
  uri: "file:///logs/app.log",
  name: "Application Logs",
  mimeType: "text/plain",
  async load(auth, context) {
    context.log.info("loading application logs", { requestedBy: auth?.userId });

    return {
      text: await readLogFile(),
    };
  },
});

Subscribing to resource updates

Clients can subscribe to a resource with the MCP resources/subscribe method to be notified whenever its contents change. FastMCP advertises the subscribe capability automatically for any server that exposes resources, tracks each client's subscriptions, and lets you emit an update with sendResourceUpdated:

ts
server.addResource({
  uri: "file:///logs/app.log",
  name: "Application Logs",
  mimeType: 

التثبيت

TypingMind
{
  "mcpServers": {
    "fastmcp": {
      "command": "npx",
      "args": [
        "fastmcp",
        "dev",
        "src/examples/addition.ts"
      ]
    }
  }
}

استخدم MCP الخاص بـ FastMCP مع نماذج ذكاء اصطناعي متعددة

يربط TypingMind أدوات MCP على مستوى مساحة العمل، لذا بمجرد توصيل FastMCP، يمكنك استخدامه مع نماذج ذكاء اصطناعي مختلفة في TypingMind بدلًا من إعداده لكل نموذج على حدة. يعمل تكامل MCP هذا محليًا عبر موصل MCP الخاص بـ TypingMind على جهازك.

دليل الإعداد لاستخدام الموصل المحلي

استخدم هذا الخيار عندما يحتاج خادم MCP إلى الوصول إلى ملفات محلية أو تطبيقات أو موارد خاصة على جهاز الكمبيوتر لديك.

1

فتح إعدادات MCP

في TypingMind، انتقل إلى «الإعدادات»، ثم «الإعدادات المتقدمة»، ثم «بروتوكول سياق النموذج»، واختر «إعداد الموصل».

  1. افتح TypingMind في متصفحك.
  2. انقر على أيقونة الإعدادات.
  3. انتقل إلى الإعدادات المتقدمة.
  4. افتح قسم بروتوكول سياق النموذج.
  5. انقر على «إعداد الموصل» واختر «هذا الجهاز».
شاشة إعداد موصل MCP في TypingMind مع تحديد «هذا الجهاز»
2

تنفيذ أمر الموصل

اختر «هذا الجهاز»، وانسخ الأمر من TypingMind وشغّله في Terminal. أبقِ العملية قيد التشغيل أثناء استخدام MCP.

  1. انسخ أمر الإعداد الذي يعرضه TypingMind.
  2. افتح Terminal على macOS أو Windows Terminal على Windows.
  3. الصق الأمر ونفّذه.
  4. وافق على تثبيت الحزمة إذا طلب منك Terminal تأكيد المتابعة.
  5. أبقِ نافذة Terminal والعملية التي تعمل فيها قيد التشغيل أثناء استخدام أدوات MCP.
3

أضف FastMCP كخادم

عندما تظهر حالة الموصل «جاهز»، انقر على «تعديل الخوادم» والصق إعدادات خادم MCP.

  1. انتظر حتى تظهر حالة الموصل «جاهز».
  2. انقر على «تعديل الخوادم».
  3. الصق إعدادات خادم MCP الخاص بـ FastMCP.
  4. احفظ قائمة الخوادم.
  5. حدّث الحالة إذا أردت التأكد من أن الموصل لا يزال جاهزًا.
إعدادات MCP في TypingMind تعرض خادمًا نشطًا وزر «تعديل الخوادم»
{
  "mcpServers": {
    "fastmcp": {
      "command": "npx",
      "args": [
        "-y",
        "fastmcp"
      ]
    }
  }
}
4

استخدام الاتصال مع نماذج مختلفة

احفظ قائمة الخوادم، وافتح «الإضافات»، وفعّل أدوات MCP الخاصة بـ FastMCP، ثم اختر أي نموذج ذكاء اصطناعي مدعوم في TypingMind واستخدم الأدوات في المحادثة أو أسندها إلى وكيل ذكاء اصطناعي.

  1. افتح صفحة الإضافات في TypingMind.
  2. فعّل أدوات MCP الخاصة بـ FastMCP.
  3. ابدأ محادثة واختر نموذج الذكاء الاصطناعي الذي تريد استخدامه.
  4. استخدم أدوات MCP في المحادثة أو عيّنها لوكيل ذكاء اصطناعي.
  5. انتقل إلى نموذج ذكاء اصطناعي آخر عند الحاجة دون إعادة الاتصال بـ MCP.
محادثة في TypingMind تستخدم أدوات MCP المفعّلة مع نموذج ذكاء اصطناعي محدّد
هل يمكنك استخدام FastMCP لمساعدتي في هذه المهمة؟
FastMCP
بالتأكيد. قرأته.
إليك ما وجدته باستخدام FastMCP.

الأسئلة الشائعة

ما استخدامات خادم MCP FastMCP؟

FastMCP هو خادم MCP يتيح لتطبيقات الذكاء الاصطناعي المتوافقة الاتصال بأدوات ومصادر سياق خارجية. في TypingMind، يمكنك إضافة خادم MCP هذا مرة واحدة وإتاحة أدواته في مساحة عملك للذكاء الاصطناعي.

هل يمكنني استخدام خادم MCP FastMCP مع نماذج ذكاء اصطناعي متعددة في TypingMind؟

نعم. يربط TypingMind أدوات MCP على مستوى مساحة العمل، لذا يمكنك استخدام FastMCP مع نماذج ذكاء اصطناعي مختلفة مثل Claude أو ChatGPT أو Gemini أو غيرها من النماذج التي أعددتها في TypingMind، دون إعداد خادم MCP لكل نموذج على حدة.

لماذا تستخدم موصل MCP FastMCP مع TypingMind؟

يُعد TypingMind من أفضل واجهات المحادثة مع النماذج اللغوية الكبيرة، إذ يجمع نماذج ذكاء اصطناعي متعددة ومطالبات وإضافات ووكلاء ذكاء اصطناعي ومفاتيح API وأدوات MCP في مساحة عمل واحدة. بعد توصيل FastMCP، يمكنك استخدام أدوات MCP الخاصة به عبر نماذجك المفضلة مع الحفاظ على سير عمل محادثاتك منظمًا في TypingMind.

كيف أوصّل خادم MCP FastMCP بـ TypingMind؟

يعمل FastMCP عبر موصل MCP المحلي في TypingMind. هذا هو الخيار الأنسب عندما يحتاج خادم MCP إلى الوصول إلى ملفات محلية أو تطبيقات سطح المكتب أو أدوات سطر الأوامر أو موارد خاصة على حاسوبك.

ما الأدوات التي يوفّرها خادم MCP FastMCP في TypingMind؟

يتيح FastMCP إمكانات MCP يمكنك تفعيلها من صفحة الإضافات في TypingMind واستخدامها في المحادثات أو تعيينها لوكلاء ذكاء اصطناعي.

هل أحتاج إلى مشاركة مفاتيح API الخاصة بي مع TypingMind لاستخدام خادم MCP FastMCP؟

لا. يعطي TypingMind الأولوية للعمل المحلي ويُبقي مزوّدي النماذج ومفاتيح API والمطالبات وإعدادات MCP تحت تحكمك. إذا كان FastMCP يتطلب مصادقة، فأضف الترويسات أو إعدادات OAuth أو الإعدادات المحلية المطلوبة لخادم MCP هذا عند إنشاء الاتصال.

خوادم MCP ذات صلة

عرض الكل

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا البريدية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇