python-mcp-server-generator
Skill này làm gì
Chương trình này tạo một 'máy chủ' Python cho phép các AI khác (như Claude) dùng những hàm bạn định nghĩa. Nó đưa các hàm, cơ sở dữ liệu hoặc API của bạn ra để AI có thể tương tác.
Khi nào nên dùng
- ✓Khi bạn muốn AI truy cập dữ liệu từ cơ sở dữ liệu.
- ✓Khi bạn muốn AI thực thi một hàm cụ thể trên máy tính của bạn.
- ✓Khi bạn muốn tạo cầu nối giữa AI và một API có sẵn.
Liên kết skill
https://github.com/github/awesome-copilot/blob/main/skills/python-mcp-server-generator/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tạo một máy chủ Python cho phép tôi chia số bằng AI.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Bạn viết mã Python định nghĩa các hàm mà AI có thể dùng. Chương trình tổ chức các hàm này và chuẩn bị để AI có thể gọi chúng và nhận kết quả.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 asrc/package layout, and the tests cannot importserver.py. uv initmakes the.gitignorefile. 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:
- Replace
dividewith the tools that the user needs. - 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.
- Return a Pydantic model or a
TypedDictwhen the client needs machine-readable data. The SDK makes the output schema from the return type. - Use
async deffor I/O. The SDK runs a sync tool on a worker thread. - Mark a read-only tool with
@mcp.tool(annotations=ToolAnnotations(read_only_hint=True))(frommcp.types). Usedestructive_hint=Truefor a destructive tool. - 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
Clientneeds no subprocess and no port. It connects with the 2026-07-28 revision. - The
mcppackage installsanyio, which supplies theanyiomarker. Do not addpytest-asyncio.
Step 4: Run the tests until they pass
- Run
uv run pytest. - If a test fails, read the error. Fix the server or the test.
- Run
uv run pytestagain. - 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.pystarts the stdio server. The server waits for a host on stdin and prints nothing.uv run mcp dev server.pyopens 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
FastMCPandmcp.server.fastmcp. Import the server class withfrom mcp.server import MCPServer. - Import
Context,Image,Audio,Resolve,Elicit,ElicitationResult,AcceptedElicitation,Message,UserMessage, andAssistantMessagefrommcp.server.mcpserver.
…
Câu hỏi thường gặp
MCP là gì?
MCP là viết tắt của Model Context Protocol. Đây là cách chuẩn hóa để các chương trình (máy chủ) và AI giao tiếp với nhau.
“Máy chủ” trong ngữ cảnh này là gì?
Đó là chương trình 'lắng nghe' và sẵn sàng nhận yêu cầu từ AI, thực hiện tác vụ và trả về kết quả.
Tôi có thể dùng cái này để tạo client AI không?
Không, chương trình này chỉ dùng để tạo 'máy chủ' mà AI sẽ sử dụng, không phải chương trình mà chính AI chạy trên đó.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 40k
- Vị trí xếp hạng
- #7351
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
acquire-codebase-knowledge
40kGiúp agent của bạn hiểu và ghi tài liệu cho một dự án mã nguồn có sẵn. Hữu ích khi đón thành viên mới hoặc nắm tổng quan về kiến trúc.
acreadiness-assess
40kGiúp agent của bạn đánh giá mức độ sẵn sàng cho AI của dự án và tạo báo cáo. Hữu ích để biết dự án đã sẵn sàng dùng công cụ AI đến đâu.
acreadiness-generate-instructions
40kGiúp agent của bạn tạo hướng dẫn cho công cụ AI dựa trên kết quả đánh giá. Hữu ích để cải thiện cách AI làm việc với mã và khắc phục các khoảng trống.
acreadiness-policy
40kGiúp agent của bạn xác định hoặc áp dụng chính sách đánh giá dự án bằng AI. Hữu ích để chuẩn hóa cách đánh giá dự án và đảm bảo tuân thủ.
Skill tương tự
add-model-provider
394kGiúp agent của bạn kết nối nhà cung cấp mô hình AI mới với OpenClaw bằng lệnh an toàn, giữ API key khỏi văn bản thuần và chạy thử.
acp-router
394kGiúp agent của bạn gửi một tác vụ đến trợ lý lập trình như Claude Code hoặc Cursor qua OpenClaw, để công việc chạy ngay trong công cụ đó.
browser-automation
394kHướng dẫn agent của bạn điều khiển trang web từng bước, gồm đăng nhập, tab và cách khôi phục khi trang ngừng phản hồi.