FastMCP هو إطار عمل TypeScript مصمم خصيصًا لبناء خوادم Model Context Protocol (MCP). يوفر مجموعة كاملة من الأدوات والوظائف التي تمكن المطورين من إنشاء خوادم 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. سواء كان تكامل أدوات بسيطًا أو بناء خدمات ذكاء اصطناعي معقدة، يمكن لـ FastMCP توفير الوظائف والمرونة المطلوبة. إن مجموعة الميزات الغنية وتجربة المطور الجيدة تجعلها أداة مهمة في نظام MCP البيئي.