FastMCP 是一個專為構建 Model Context Protocol (MCP) 伺服器而設計的 TypeScript 框架。它提供了一套完整的工具和功能,使開發者能夠快速創建能夠處理客戶端會話的高性能 MCP 伺服器。
項目地址: 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",
});
工具允許伺服器向客戶端公開可執行的函數,這些函數可以被客戶端調用並被 LLM 用於執行操作。
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);
},
});
工具可以包含注解,提供更豐富的上下文和控制信息:
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);
},
});
資源代表 MCP 伺服器希望向客戶端提供的任何類型的數據,包括:
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}`,
};
},
});
提示使伺服器能夠定義可重用的提示模板和工作流:
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 支持自定義身份驗證功能:
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 功能:
server.start({
transportType: "sse",
sse: {
endpoint: "/sse",
port: 8080,
},
});
FastMCP 為每個客戶端連接分配新的伺服器實例,實現 1:1 通信:
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
在 Claude Desktop 中添加配置:
{
"mcpServers": {
"my-mcp-server": {
"command": "npx",
"args": ["tsx", "/PATH/TO/YOUR_PROJECT/src/index.ts"],
"env": {
"YOUR_ENV_VAR": "value"
}
}
}
}
FastMCP 已被多個項目採用,包括:
FastMCP 是一個功能強大、易於使用的 TypeScript 框架,為構建 MCP 伺服器提供了完整的解決方案。無論是簡單的工具集成還是複雜的 AI 服務構建,FastMCP 都能提供所需的功能和靈活性。其豐富的特性集合和良好的開發者體驗使其成為 MCP 生態系統中的重要工具。