FastMCP is a TypeScript framework specifically designed for building Model Context Protocol (MCP) servers. It provides a complete set of tools and features, enabling developers to quickly create high-performance MCP servers capable of handling client sessions.
Project Address: https://github.com/punkpeye/fastmcp
npm install fastmcp
import { FastMCP } from "fastmcp";
import { z } from "zod";
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",
});
Tools allow the server to expose executable functions to the client, which can be called by the client and used by LLMs to perform operations.
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);
},
});
Tools can include annotations to provide richer context and control information:
server.addTool({
name: "fetch-content",
description: "Fetch content from a URL",
parameters: z.object({
url: z.string(),
}),
annotations: {
title: "Web Content Fetcher",
readOnlyHint: true,
openWorldHint: true,
},
execute: async (args) => {
return await fetchWebpageContent(args.url);
},
});
Resources represent any type of data that the MCP server wants to provide to the client, including:
server.addResource({
uri: "file:///logs/app.log",
name: "Application Logs",
mimeType: "text/plain",
async load() {
return {
text: await readLogFile(),
};
},
});
server.addResourceTemplate({
uriTemplate: "file:///logs/{name}.log",
name: "Application Logs",
mimeType: "text/plain",
arguments: [
{
name: "name",
description: "Name of the log",
required: true,
},
],
async load({ name }) {
return {
text: `Example log content for ${name}`,
};
},
});
Prompts enable the server to define reusable prompt templates and workflows:
server.addPrompt({
name: "git-commit",
description: "Generate a Git commit message",
arguments: [
{
name: "changes",
description: "Git diff or description of changes",
required: true,
},
],
load: async (args) => {
return `Generate a concise but descriptive commit message for these changes:\n\n${args.changes}`;
},
});
FastMCP supports custom authentication functionality:
import { AuthError } from "fastmcp";
const server = new FastMCP({
name: "My Server",
version: "1.0.0",
authenticate: ({ request }) => {
const apiKey = request.headers["x-api-key"];
if (apiKey !== "123") {
throw new Response(null, {
status: 401,
statusText: "Unauthorized",
});
}
return {
id: 1,
};
},
});
SSE functionality for real-time communication:
server.start({
transportType: "sse",
sse: {
endpoint: "/sse",
port: 8080,
},
});
FastMCP assigns a new server instance to each client connection, enabling 1:1 communication:
server.on("connect", (event) => {
console.log("Client connected:", event.session);
});
server.on("disconnect", (event) => {
console.log("Client disconnected:", event.session);
});
npx fastmcp dev src/examples/addition.ts
npx fastmcp inspect src/examples/addition.ts
Add the configuration in Claude Desktop:
{
"mcpServers": {
"my-mcp-server": {
"command": "npx",
"args": ["tsx", "/PATH/TO/YOUR_PROJECT/src/index.ts"],
"env": {
"YOUR_ENV_VAR": "value"
}
}
}
}
FastMCP has been adopted by several projects, including:
FastMCP is a powerful and easy-to-use TypeScript framework that provides a complete solution for building MCP servers. Whether it's simple tool integration or complex AI service construction, FastMCP provides the necessary functionality and flexibility. Its rich feature set and good developer experience make it an important tool in the MCP ecosystem.