Skillpedia
الوكلاء40k · #7351

python-mcp-server-generator

ما الذي تفعله

تنشئ هذه المهارة «خادماً» بلغة Python يتيح لأنظمة ذكاء اصطناعي أخرى (مثل Claude) استخدام الدوال التي تعرّفها. تعرض دوالك أو قاعدة بياناتك أو واجهتك البرمجية ليتمكن الذكاء الاصطناعي من التفاعل معها.

متى تستخدمها

  • ✓عندما تريد أن يصل الذكاء الاصطناعي إلى البيانات في قاعدة بيانات.
  • ✓عندما تريد أن ينفذ الذكاء الاصطناعي دالة محددة على حاسوبك.
  • ✓عندما تريد إنشاء جسر بين الذكاء الاصطناعي وواجهة برمجية موجودة.

رابط المهارة

https://github.com/github/awesome-copilot/blob/main/skills/python-mcp-server-generator/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/github/awesome-copilot/blob/main/skills/python-mcp-server-generator/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“أنشئ خادم Python يسمح لي بقسمة الأعداد باستخدام الذكاء الاصطناعي.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

تكتب كود Python يعرّف الدوال التي يستطيع الذكاء الاصطناعي استخدامها. ينظم البرنامج هذه الدوال ويجهزها ليستطيع الذكاء الاصطناعي استدعاءها واستقبال النتائج.

المحتوى التقني (ملف SKILL.md الأصلي)

النص الأصلي من المؤلف، مباشرة من GitHub.

Python MCP Server Generator

Create a Model Context Protocol (MCP) server in Python with the official MCP Python SDK v2. SDK v2 supports the MCP specification revision 2026-07-28. The same server also serves clients that use earlier revisions.

Workflow

Copy this checklist and track your progress:

  • Step 1: Create the project.
  • Step 2: Write the server.
  • Step 3: Write the tests.
  • Step 4: Run the tests until they pass.
  • Step 5: Configure the client.

Step 1: Create the project

Run these commands:

uv init --app --no-package project-name
cd project-name
uv add "mcp[cli]>=2,<3"
uv add --dev pytest

Then delete main.py. The server goes in server.py.

  • Without --no-package, uv makes a src/ package layout, and the tests cannot import server.py.
  • uv init makes the .gitignore file. Do not write a different one.

Step 2: Write the server

Start from this template:

from mcp.server import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("Demo", version="0.1.0")


@mcp.tool()
def divide(a: float, b: float) -> float:
    """Divide a by b."""
    if b == 0:
        raise ToolError("b must not be zero.")
    return a / b


if __name__ == "__main__":
    mcp.run()  # stdio is the default transport

Then change the template for the request of the user:

  1. Replace divide with the tools that the user needs.
  2. Give each tool full type hints and a docstring. The SDK makes the input schema from the type hints. The docstring becomes the tool description.
  3. Return a Pydantic model or a TypedDict when the client needs machine-readable data. The SDK makes the output schema from the return type.
  4. Use async def for I/O. The SDK runs a sync tool on a worker thread.
  5. Mark a read-only tool with @mcp.tool(annotations=ToolAnnotations(read_only_hint=True)) (from mcp.types). Use destructive_hint=True for a destructive tool.
  6. Add resources (@mcp.resource("users://{user_id}")) and prompts (@mcp.prompt()) only when the user asks for them.

Use stdio by default. Read references/streamable-http.md only when the user asks for a remote server or an HTTP server.

Step 3: Write the tests

Put test_server.py next to server.py. Write a test for each tool and for each ToolError:

import pytest

from mcp import Client

from server import mcp


@pytest.mark.anyio
async def test_divide():
    async with Client(mcp) as client:
        result = await client.call_tool("divide", {"a": 6, "b": 3})
        assert result.structured_content == {"result": 2.0}


@pytest.mark.anyio
async def test_divide_by_zero():
    async with Client(mcp) as client:
        result = await client.call_tool("divide", {"a": 1, "b": 0})
        assert result.is_error
        assert "must not be zero" in result.content[0].text
  • The in-memory Client needs no subprocess and no port. It connects with the 2026-07-28 revision.
  • The mcp package installs anyio, which supplies the anyio marker. Do not add pytest-asyncio.

Step 4: Run the tests until they pass

  1. Run uv run pytest.
  2. If a test fails, read the error. Fix the server or the test.
  3. Run uv run pytest again.
  4. Continue only when all tests pass.

Step 5: Configure the client

For VS Code, write .vscode/mcp.json:

{
  "servers": {
    "demo": {
      "type": "stdio",
      "command": "uv",
      "args": ["--directory", "/absolute/path/to/project", "run", "server.py"]
    }
  }
}

For Claude Desktop, run uv run mcp install server.py.

Tell the user these commands:

  • uv run server.py starts the stdio server. The server waits for a host on stdin and prints nothing.
  • uv run mcp dev server.py opens the MCP Inspector. The Inspector needs Node.js.

Gotchas

SDK v2 and the 2026-07-28 revision changed many SDK v1 patterns. Do not copy SDK v1 examples.

  • SDK v2 removed FastMCP and mcp.server.fastmcp. Import the server class with from mcp.server import MCPServer.
  • Import Context, Image, Audio, Resolve, Elicit, ElicitationResult, AcceptedElicitation, Message, UserMessage, and AssistantMessage from mcp.server.mcpserver.

…

الأسئلة الشائعة

ما هو MCP؟

MCP اختصار لـ Model Context Protocol، وهو طريقة موحدة تتيح للبرامج (الخوادم) والذكاء الاصطناعي التواصل مع بعضهما.

ما هو «الخادم» في هذا السياق؟

هو برنامج يستمع ويكون جاهزاً لاستقبال الطلبات من الذكاء الاصطناعي وتنفيذ المهام وإرجاع النتائج.

هل يمكنني استخدامها لإنشاء عميل ذكاء اصطناعي؟

لا، هذا البرنامج مخصص لإنشاء «الخادم» الذي سيستخدمه الذكاء الاصطناعي، وليس البرنامج الذي يعمل عليه الذكاء الاصطناعي نفسه.

التفاصيل

الوصف الأصلي من المؤلف

Use this skill when the user wants to create a new Model Context Protocol (MCP) server in Python. Also use it to expose Python functions, an API, or a database to AI clients as MCP tools. The skill makes a uv project with the MCP Python SDK v2 (MCPServer), typed tools, in-memory tests, and client configuration. Do not use it for an MCP client or for a server in another language.

repo: github/awesome-copilot

الشعبية

نجوم GitHub
40k
الترتيب
#7351
الترخيص
MIT
آخر تحديث
15‏/10‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة