Logo de Building Python Mcp Servers

Building Python Mcp Servers

Communauté
wdm0006
building-python-mcp-servers

Builds robust Python MCP (Model Context Protocol) servers with FastMCP — tool design, error contracts, event-loop-safe blocking work, subprocess/CLI wrapping, single-file vs packaged distribution, global-state-free testing, and prompt-injection awareness. Use when writing an MCP server, exposing a tool or CLI to an LLM client, debugging tool registration/packaging, or testing MCP tools.

Présentation

Éditeurwdm0006
Dépôtpython-skills
Nom de la compétencebuilding-python-mcp-servers
Étoiles
111
Forks
17
Fichiers inclus
Instructions uniquement
LicenceMIT
Liens
  • Instructions en Markdown

    Un fichier SKILL.md que le modèle charge à la demande : il ne consomme donc des tokens que lorsqu’une demande correspond réellement à la compétence.

  • Compatible avec tous les LLM

    Les compétences IA sont en simple Markdown, et non en code propre à un fournisseur. Elles fonctionnent donc avec GPT, Claude, Gemini, Grok ou un modèle local.

  • Tout dans les instructions

    Tout ce dont le modèle a besoin se trouve dans les instructions : aucun fichier supplémentaire à synchroniser.

  • Open source

    Publiée par wdm0006 sur GitHub. Consultez les sources avant de l’installer.

Installation

Installez la compétence IA Building Python Mcp Servers dans TypingMind pour l’utiliser avec n’importe quel LLM, ou ajoutez-la à un autre agent capable de lire SKILL.md.

1

Installer dans TypingMind

TypingMind installe une compétence directement depuis son dossier GitHub : l’application lit SKILL.md, regroupe les fichiers de ressources et enregistre le tout localement.

  1. Ouvrez l’application et accédez à Plugins → Compétences.
  2. Choisissez « Installer depuis GitHub ».
  3. Collez l’URL du dossier de la compétence indiquée ci-dessous, puis confirmez.
    https://github.com/wdm0006/python-skills/tree/main/skills/python/mcp-servers
  4. Activez la compétence dans chaque discussion où vous souhaitez pouvoir l’utiliser.
Plugins → Compétences → Ajouter → Via URL GitHub, puis collez l’URL du dossier et appuyez sur Continuer.
2

Installer dans un autre agent

Tout agent capable de lire le format Agent Skills peut utiliser cette compétence : copiez le dossier dans le répertoire des compétences de cet agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/wdm0006/python-skills.git /tmp/python-skills
mkdir -p .claude/skills
cp -r /tmp/python-skills/skills/python/mcp-servers .claude/skills/building-python-mcp-servers
Redémarrez Claude Code après la copie pour que la nouvelle compétence soit prise en compte.

Utilisez cette compétence dans TypingMind

Activez Building Python Mcp Servers dans n’importe quelle discussion TypingMind et le modèle s’occupe du reste. Le nom et la description de la compétence figurent dans le prompt système. Dès qu’une demande correspond, le modèle charge lui-même l’intégralité des instructions : vous n’avez jamais à l’invoquer manuellement, et elle ne consomme aucun token tant qu’elle n’est pas utilisée.

Le modèle charge automatiquement Building Python Mcp Servers dès qu’une demande correspond à cette compétence.

Compatible avec tous les modèles IA

Les compétences IA sont de simples instructions Markdown, et non du code propre à un fournisseur. Ainsi, Building Python Mcp Servers n’est pas lié au modèle pour lequel cette compétence a été conçue. Installez-la une seule fois dans TypingMind et utilisez-la avec GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama ou un modèle local que vous exécutez vous-même, le tout avec vos propres clés API.

  • Chargée uniquement en cas de besoin

    Le prompt système ne contient que le nom et la description. Les instructions sont récupérées à la première demande correspondante : une compétence inutilisée ne coûte donc rien.

  • Changez de modèle en cours de discussion

    La compétence étant constituée d’instructions et non de code, elle reste fonctionnelle lorsque vous changez de modèle : le nouveau modèle lit le même fichier SKILL.md.

Instructions de la compétence

Voici le contenu de SKILL.md que le modèle charge. Lisez-le avant l’installation : une compétence contient des instructions que votre modèle suivra.

Building Python MCP Servers

MCP servers expose tools to an LLM client (Claude Desktop, Claude Code, etc.). The LLM is the caller, so the failure modes differ from a normal library: errors must be machine-readable, every input is untrusted, and a green test suite often proves nothing about whether the tools actually work. This skill encodes the patterns that recur when these go wrong.

Quick Start (FastMCP)

python
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("my-server")

@mcp.tool()
def read_config(path: str) -> dict:
    """Read a config file. `path` must be absolute."""
    p = Path(path)
    if not p.is_absolute():
        return {"error": "path must be absolute"}
    if not p.exists():
        return {"error": f"no such file: {path}"}
    return {"data": p.read_text()}

if __name__ == "__main__":
    mcp.run()

Error Contract: return, don't raise — and stay consistent

An uncaught exception surfaces to the LLM as an opaque protocol error it can't reason about. Return a structured result with a predictable shape instead, and make callers check for it.

  • Pick one error shape and use it everywhere. A dict with an "error" key is the common convention. Document that callers must check for it.
  • Batch tools must report skips, not swallow them. The most common inconsistency: a single-item tool collects per-item errors, but a sibling "do this across a directory" tool silently continues past files that fail to load. For automation that is invisible data loss. Every batch tool should return both results and a per-item skipped/errors list.
python
@mcp.tool()
def validate_dir(path: str) -> dict:
    results, skipped = {}, []
    for f in Path(path).glob("*.md"):
        try:
            results[f.name] = _validate(f)
        except Exception as e:
            skipped.append({"file": f.name, "error": str(e)})  # never silently continue
    return {"results": results, "skipped": skipped}

Type footgun: YAML auto-parses an unquoted ISO date (date: 2025-06-15) into a datetime.date, not a str or datetime.datetime. A validator that handles only str/datetime will false-positive on native YAML dates. When validating parsed values, enumerate every type the parser can actually produce.

Wrapping a subprocess / CLI

A huge share of MCP servers shell out to another tool. Three failures recur:

1. Don't discard stdout on a non-zero exit. Many CLIs exit non-zero by design (a linter/mutation-tester reporting findings) and write their real output to stdout with an empty stderr. A wrapper that returns f"Error: {result.stderr}" whenever returncode != 0 reports a successful run to the LLM as an empty "Error: ".

python
def run_tool(args: list[str]) -> dict:
    r = subprocess.run(args, capture_output=True, text=True)
    return {                       # hand BOTH streams to the model; let it judge
        "returncode": r.returncode,
        "stdout": r.stdout,
        "stderr": r.stderr,
    }

2. Pin to the version you actually wrap, and verify subcommands exist. A server written against a tool's 2.x CLI while the project pins 3.x will call subcommands and flags that no longer exist — every wrapped tool breaks at runtime. Check the installed version's --help, not your memory of it.

3. Parse args safely. Splitting an extra-args string with str.split() breaks quoted, space-containing arguments — use shlex.split(). Never interpolate a client-supplied string into a shell command; pass an argv list to subprocess.run (no shell=True). When a tool accepts a target/path, remember the LLM (or content it read) chose it — validate it.

No module-level global state (it makes the server untestable)

Parsing CLI args at import time and stashing them in module globals (WORKING_DIR, MAKEFILE_PATH, caches…) forces every test to del sys.modules["server"] and re-import under a patched sys.argv just to reset state — brittle and easy to get wrong. Keep configuration in an object or pass it through; construct tools from a factory.

python
def build_server(config: Config) -> FastMCP:
    mcp = FastMCP("my-server")

    @mcp.tool()
    def do_thing(x: str) -> dict:
        return {"result": _work(x, config)}   # config captured, not global

    return mcp

This also avoids double registration: a module-level "create all tools" loop plus the same loop inside main() registers every tool twice when the file is run directly (uv run server.py, as Claude Desktop does) versus via a console entry point. Register in exactly one place.

Keep blocking work off the protocol event loop

Do not assume a framework moves synchronous tool functions to a worker thread. Some FastMCP runtimes invoke them inline on the protocol event loop. A SQLite query, filesystem walk, dependency traversal, or synchronous HTTP call that takes five seconds can therefore block pings and every unrelated request for the same five seconds.

Make the tool async and move only the blocking boundary to a thread:

python
import asyncio

@mcp.tool()
async def find_dependents(item_id: int) -> dict:
    rows = await asyncio.to_thread(repository.find_dependents, item_id)
    return {"items": [row.to_dict() for row in rows]}

Keep connection ownership in mind. Do not create a SQLite connection on the event-loop thread and hand that connection to the worker. Open and close it inside repository.find_dependents, or use a pool/driver whose concurrency contract explicitly permits the handoff. A thread wrapper around a shared, thread-affine connection merely trades event-loop starvation for intermittent database errors.

Test responsiveness, not just the slow tool's result. Start a deliberately blocked repository call, invoke a lightweight tool (or protocol ping) before releasing it, and require the lightweight request to finish first:

python
slow = asyncio.create_task(call_tool("find_dependents", {"item_id": 42}))
await entered_worker.wait()

healthy = await asyncio.wait_for(call_tool("health", {}), timeout=0.2)
assert healthy == {"ok": True}

release_worker.set()
await slow

A timing assertion on the slow call alone cannot detect event-loop starvation; the regression is that independent protocol traffic stops making progress.

Sampling is an optional client capability — contain failures in the tool

ctx.sample(...) is not guaranteed to work just because the tool itself was called successfully. The connected client may not support sampling, or its sampling handler may raise while processing the request. Those are different failure modes at the framework layer, but they are the same tool-level outcome: the requested analysis could not be produced.

Catch the exception around the sampling boundary inside the tool and convert it to the server's normal error shape. Do not rely on the framework's outer exception wrapper; by then the caller receives an opaque protocol/tool error instead of your documented contract.

python
async def sample_or_error(ctx: Context, prompt: str) -> dict:
    try:
        response = await ctx.sample(prompt)
    except Exception as exc:
        return {"error": f"sampling failed: {exc}"}
    return {"result": response.text or ""}

Keep the try block narrow so unrelated programming errors are not mislabeled as sampling failures. Test both boundaries explicitly: a client with no sampling support, and a configured sampling handler that raises. Also test an empty sampling response if the tool promises an empty-string or other fallback.

Distribution: single-file vs packaged

MCP servers are often launched as a single file (uv run server.py), so two packaging traps are easy to ship without noticing:

  • Module/package name collision. Having both a top-level server.py and a server/ package directory means import server resolves to the package (shadowing the module), so a console entry point like server:main finds no main and fails. Only running the file directly works. Pick one name.
  • Over-narrow build includes. A build config like only-include = ["server.py"] produces a wheel containing just that file — import server.analyzers raises ModuleNotFoundError for anyone who pip installs it, even though uv run server.py works locally. If you ship a package, include the package and its data files.

For a PEP 723 single-file server, pin explicit versions in the inline # /// script header and keep them in sync with pyproject.toml; a transitive-only dependency (imported but never declared) breaks the moment the intermediary drops it.

Testing: prove the tools actually work

Mocking the subprocess/transport layer and asserting that argv contains certain tokens locks in commands that may not exist in the wrapped tool — the suite stays green while every tool is broken at runtime. A passing CI here does not mean the server works.

  • Keep at least one integration test that invokes the real wrapped tool (or a real sample file) end to end.
  • A bare import server smoke test is meaningless under a name collision (it can import an empty package). Assert a tool runs and returns expected output.
  • Test the error contract: malformed input returns your "error" shape, batch tools populate skipped.

Treat all tool I/O as untrusted (prompt injection)

Tool inputs, file contents, and especially other tools' descriptions can be attacker-influenced and flow into the model's context. A server that feeds such text back into a second LLM call is itself a prompt-injection surface — its output is advisory, not authoritative. Don't grant a tool more filesystem/network reach than it needs, validate paths, and never let tool output be treated as a trusted instruction.

MCP Server Checklist

Contract:
- [ ] One consistent error shape; documented that callers check it
- [ ] Batch tools return a per-item skipped/errors list (never silent continue)
- [ ] Inputs validated (absolute paths, allowed types) before use
- [ ] Sampling failures (unsupported client and handler exception) normalized inside the tool

Subprocess:
- [ ] Both stdout and stderr returned; non-zero exit not assumed to be failure
- [ ] Pinned to the wrapped tool's actual version; subcommands verified
- [ ] shlex.split for arg strings; argv list (no shell=True)

Structure:
- [ ] No module-level CLI parsing / global state
- [ ] Tools registered in exactly one place
- [ ] Blocking database/filesystem/network work moved off the protocol event loop
- [ ] Concurrency test proves a lightweight request completes while a slow tool is blocked

Distribution & tests:
- [ ] No server.py / server/ name collision; build includes the whole package
- [ ] PEP 723 header deps pinned and synced with pyproject
- [ ] An integration test exercises a real tool (not just mocked argv)

Learn More

This skill is based on the Guide to Developing High-Quality Python Libraries by Will McGinnis.

Foire aux questions

À quoi sert la compétence IA Building Python Mcp Servers ?

Builds robust Python MCP (Model Context Protocol) servers with FastMCP — tool design, error contracts, event-loop-safe blocking work, subprocess/CLI wrapping, single-file vs packaged distribution, global-state-free testing, and prompt-injection awareness. Use when writing an MCP server, exposing a tool or CLI to an LLM client, debugging tool registration/packaging, or testing MCP tools.

Pourquoi utiliser Building Python Mcp Servers sur TypingMind ?

Parce que vous l’installez une seule fois pour l’utiliser avec n’importe quel modèle. Building Python Mcp Servers se compose de simples instructions en Markdown plutôt que de code propre à un fournisseur. La même compétence fonctionne donc avec GPT-5, Claude, Gemini, Grok ou un modèle local, et vous pouvez changer de modèle en cours de discussion sans perturber son fonctionnement. TypingMind utilise vos propres clés API : vous payez donc directement les fournisseurs plutôt qu’un abonnement par siège, et vos compétences et discussions restent dans votre propre espace de stockage.

Comment installer Building Python Mcp Servers dans TypingMind ?

Dans TypingMind, ouvrez Plugins → Compétences → Installer depuis GitHub et collez https://github.com/wdm0006/python-skills/tree/main/skills/python/mcp-servers. TypingMind lit le fichier SKILL.md et l’installe sous forme de compétence que vous pouvez activer pour chaque discussion.

Quels modèles d’IA peuvent utiliser Building Python Mcp Servers ?

N’importe quel modèle que vous connectez dans TypingMind. Les compétences IA sont de simples instructions en Markdown, et non du code propre à un fournisseur. GPT, Claude, Gemini, Grok et les modèles locaux peuvent donc tous charger cette compétence lorsqu’une demande lui correspond.

Combien de modèles d’IA puis-je utiliser avec Building Python Mcp Servers ?

Autant que vous le souhaitez. Tant qu’un modèle prend en charge les compétences, vous pouvez utiliser Building Python Mcp Servers avec lui : GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama et bien d’autres, le tout dans TypingMind avec vos propres clés API.

La compétence IA Building Python Mcp Servers est-elle gratuite ?

Oui. Cette compétence est publiée sur GitHub par wdm0006 sous licence MIT. Vous ne payez que votre propre fournisseur d’IA pour les tokens utilisés.

Que sont les compétences IA ?

Une compétence IA est un ensemble d’instructions réutilisables qui apprend à un modèle d’IA à accomplir une tâche précise. Elle suit le format ouvert Agent Skills : un fichier SKILL.md contenant un nom et une description, ainsi que les éventuels scripts, modèles de documents ou fichiers de référence dont le modèle peut avoir besoin. Le modèle ne lit les instructions que lorsque votre demande correspond à la compétence. Une compétence installée ne coûte donc rien tant qu’elle n’est pas utilisée.

En quoi les compétences IA diffèrent-elles des plugins ou des serveurs MCP ?

Un plugin ou un serveur MCP fournit au modèle de nouveaux outils à appeler : du code qui s’exécute quelque part et renvoie un résultat. Une compétence IA lui apporte plutôt des connaissances et une méthode : comment aborder une tâche, quelles étapes suivre et à quoi ressemble un bon résultat. Les compétences sont de simples instructions en Markdown. Elles ne nécessitent donc ni serveur, ni clé API, ni environnement d’exécution, et fonctionnent avec n’importe quel modèle.

Tout voir

Créez votre propre espace IA dès maintenant

Abonnez-vous à notre newsletter pour être informé des nouvelles fonctionnalités et des prochains cadeaux à gagner 👇