Python MCP Server Generator
GitHubOfficialFirst committed by burakkaygusuzDistributed by GitHub
- Skill
- Workflow
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured workflow for developing Model Context Protocol (MCP) servers using the Python SDK v2. It covers project initialization with uv, implementing tools with type hints, writing in-memory tests, and configuring the server for local use via stdio or remote use via streamable HTTP. It emphasizes modern SDK v2 patterns, including proper error handling with ToolError and the use of standard Python logging.
What it does
- Initializes a new Python project configured for MCP development.
- Provides a template for defining typed tools that AI clients can invoke.
- Guides the creation of in-memory tests to validate tool functionality.
- Explains how to configure MCP servers for local stdio or remote HTTP transport.
- Offers best practices for handling tool errors and server lifecycles.
- Highlights deprecated patterns to avoid when using the 2026-07-28 specification.
When to use it
- When you need to expose Python functions, APIs, or databases as tools to an AI agent.
- When building a new MCP server from scratch using the latest Python SDK v2.
- When you need to ensure your MCP server is compatible with the 2026-07-28 specification.
- When you require a robust testing setup for your MCP tools.
Advantages
- Uses the official, up-to-date MCP Python SDK v2.
- Includes a standardized testing workflow using in-memory clients.
- Promotes clean, type-hinted code for automatic schema generation.
- Provides clear guidance on modern transport configurations.
Usage examples
- “How do I start a new MCP server project in Python?”
- The agent provides the uv commands to initialize the project, install the required mcp and pytest dependencies, and set up the initial server.py file structure.
- “How do I add a tool to my MCP server that divides two numbers?”
- The agent provides a code snippet using the @mcp.tool decorator with type hints for inputs and outputs, including error handling for division by zero.
- “How can I test my MCP tools without running a full server?”
- The agent shows how to use the in-memory Client class within a pytest file to call tools directly and assert the expected results.
- “How do I configure my MCP server for VS Code?”
- The agent provides the JSON configuration structure for .vscode/mcp.json, specifying the stdio transport and the command to run the server via uv.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/github/python-mcp-server-generator/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip python-mcp-server-generator.zip -d .agents/skills
Documentation by burakkaygusuz
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 pytestThen 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 transportThen 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].textThe 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.Give the server name as the first
MCPServerargument. Give all other constructor arguments as keyword arguments. Their positional order changed in SDK v2.Set
version. If you do not set it, the server reports an empty version.Give the transport options (
transport,host,port,json_response,stateless_http,transport_security) torun(), not toMCPServer(...).The SDK v2 types use snake_case fields, for example
read_only_hint,structured_content, andis_error.Raise
ToolErrorfor an error that the model must read. For other exceptions, the model gets only a generic error message.Do not raise
MCPErrorfor a tool failure. The client gets a protocol error, not a tool result withis_error. Many hosts do not show this error to the model.In a stdio server, stdout is the protocol channel. Do not call
print(). Log to stderr with theloggingmodule.Do not use
ctx.elicit(). It fails on a 2026-07-28 connection. For user input during a tool call, annotate a parameter withResolve(fn). ReturnElicit(message, Model)fromfn.Send list change notifications with
await ctx.notify_tools_changed(). A 2026-07-28 connection dropsctx.session.send_tool_list_changed().For stdio and HTTP, the lifespan runs one time, when the server starts. Each in-memory
Client(mcp)in a test runs the lifespan again. Read its object withctx.request_context.lifespan_context.The 2026-07-28 revision deprecates these features (SEP-2577). Do not add them to a new server:
Sampling (
ctx.session.create_message()): Call the LLM provider API directly.Protocol logging (
ctx.log(),ctx.info(), and similar methods): Use the standardloggingmodule.Roots (
ctx.session.list_roots()): Get paths from tool parameters, resource URIs, or the server configuration.
The 2026-07-28 revision has no SSE transport. Use Streamable HTTP.
Files
- ATTRIBUTION.mdAdded by AutoGineer897 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md6.7 KiB
- references/streamable-http.md1.9 KiB
Requirements and compatibility
Written for: GitHub Copilot
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs an MCP server, Needs Python
The author says: Requires uv, Python 3.10 or later, and network access to install packages. The MCP Inspector also requires Node.js.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- First committed by
burakkaygusuz
- Publisher
- GitHub (Official)
- Distributed by
- GitHub
- Source
- github/awesome-copilot · skills/python-mcp-server-generator · commit 797337f
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
- Contributors
- burakkaygusuz
AutoGineer is not affiliated with or endorsed by burakkaygusuz or GitHub.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download python-mcp-server-generator.zip