Use this file to discover all available pages before exploring further.
FastMCP supports streaming 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.
To enable streaming for a tool, add the streamingHint annotation and use the streamContent method:
import { FastMCP } from "fastmcp";import { z } from "zod";const server = new FastMCP({ name: "My Server", version: "1.0.0",});server.addTool({ name: "generateText", description: "Generate text incrementally", parameters: z.object({ prompt: z.string(), }), annotations: { streamingHint: true, // Signals this tool uses streaming 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 } // When using streamContent, you can: // 1. Return void (if all content was streamed) // 2. Return a final result (which will be appended to streamed content) // Option 1: All content was streamed return; // Option 2: Return final content that will be appended // return "Generation complete!"; },});
server.addTool({ name: "generateStory", description: "Generate a story with streaming output", parameters: z.object({ prompt: z.string(), }), annotations: { streamingHint: true, readOnlyHint: true, }, execute: async ({ prompt }, { streamContent, reportProgress }) => { await streamContent({ type: "text", text: `# Generating story from: "${prompt}"\n\n`, }); // Simulate AI streaming (in reality, you'd call an AI API) const paragraphs = [ "Once upon a time, in a land far away...", "The hero embarked on an epic journey...", "Through trials and tribulations...", "Finally, peace was restored to the kingdom.", ]; for (let i = 0; i < paragraphs.length; i++) { await reportProgress({ progress: i + 1, total: paragraphs.length, }); await streamContent({ type: "text", text: paragraphs[i] + "\n\n", }); await new Promise((resolve) => setTimeout(resolve, 1000)); } return "--- The End ---"; },});