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エコシステムにおける重要なツールとなっています。