Skip to content
gentic.news — AI News Intelligence Platform
Connecting to the Living Graph…

Listen to today's AI briefing

Daily podcast — 5 min, AI-narrated summary of top stories

Screenshot of Python code in an IDE showing a FastMCP server definition with a simple function and decorator…
Open SourceScore: 64

Write MCP Servers in Python in 10 Lines with FastMCP

FastMCP from the official Python SDK v1.x turns type-hinted functions into MCP tools. Add @mcp.tool() to any function and Claude Code discovers it via tools/list and tools/call JSON-RPC calls.

·20h ago·3 min read··11 views·AI-Generated·Report error
Share:
Source: dev.tovia devto_mcpCorroborated
How do I build an MCP server in Python for Claude Code?

FastMCP from the official Python SDK (v1.x) exposes tools via @mcp.tool() on a type-hinted function. Add uv add "mcp[cli]", write a function with a docstring, and run. Claude Code and other MCP hosts discover it automatically.

TL;DR

FastMCP turns any type-hinted Python function into an MCP tool—no JSON schemas, no boilerplate. Deploy in minutes.

Key Takeaways

  • FastMCP from the official Python SDK v1.x turns type-hinted functions into MCP tools.
  • Add @mcp.tool() to any function and Claude Code discovers it via tools/list and tools/call JSON-RPC calls.

What Changed — FastMCP Makes MCP Servers Trivial in Python

Building and Exposing MCP Servers with FastMCP (STDIO, HTTP ...

The Model Context Protocol (MCP) solves the M×N integration problem: without it, every agent framework needs bespoke glue code for every tool. With MCP, you write the server once and any MCP-aware host—Claude Code, Claude Desktop, VS Code, or your own agent—can consume it.

The Python SDK's FastMCP class (stable v1.x, not the alpha v2) is the fastest way to get started. It turns a plain, type-hinted, docstring-described Python function into a full MCP tool endpoint—no hand-written JSON Schema, no boilerplate wire protocol code.

What It Means For You — Concrete Impact on Daily Claude Code Usage

If you've been writing custom tools for Claude Code by hand, you're doing extra work. With FastMCP:

  1. One server powers all MCP hosts. Write your tool once. Claude Code, Claude Desktop, VS Code extensions, and custom agents all discover and call it through the same protocol.

  2. Validation is built-in. The example in the source validates account IDs with a regex before acting on untrusted model input—a critical security pattern you should copy.

  3. Two transport options: stdio for local subprocess (one client per server) and Streamable HTTP for remote servers with bearer-token auth.

Try It Now — Build Your First MCP Server in 10 Minutes

Step-by-Step Guide: Building an MCP Server using Python-SDK ...

Setup

uv add "mcp[cli]"

Server code (accounts_server.py)

import re
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("accounts-server")

ACCOUNTS = {"ACC-100001": 542.10, "ACC-100002": 12.50}
ACCOUNT_ID_PATTERN = re.compile(r"^ACC-\d{6}$")

@mcp.tool()
def get_account_balance(account_id: str) -> str:
    """Look up the balance for an account by ID.

    Args:
        account_id: Account ID, e.g. ACC-100001.
    """
    if not ACCOUNT_ID_PATTERN.match(account_id):
        raise ValueError("Invalid account ID format")
    balance = ACCOUNTS.get(account_id)
    return f"Balance for {account_id}: ${balance}" if balance is not None \
        else f"No account found for {account_id}"

Run it

python accounts_server.py

Then configure Claude Code to connect by adding the server path to your claude.json or MCP settings.

Key patterns to steal

  • Validate model-provided input before using it. The regex whitelist in the example is non-negotiable for production tools.
  • Use @mcp.resource() for read-only data the model should load into context (like account summaries) without triggering an action.
  • Pin to v1.x. The v2 alpha is not production-ready per the official SDK README.

When to Use MCP vs Direct Tool Calls

MCP is overkill if your tool lives in one process, one agent, one language. Use direct function calls or @tool decorators for that. Reach for MCP when:

  • Multiple agents or hosts need the same tool
  • The tool runs as a separate service (remote or microservice)
  • You want to decouple tool development from agent framework choices

For Claude Code users, MCP servers are the standard way to add custom capabilities. FastMCP makes building them as simple as writing a function.


Source: dev.to

Source: gentic.news · · author= · citation.json

AI-assisted reporting. Generated by gentic.news from multiple verified sources, fact-checked against the Living Graph of 4,300+ entities. Edited by Ala SMITH.

Following this story?

Get a weekly digest with AI predictions, trends, and analysis — free.

AI Analysis

**What Claude Code users should do differently:** 1. **Stop hand-rolling MCP servers.** If you've been writing JSON-RPC handlers manually, switch to FastMCP. It eliminates boilerplate and auto-generates the `tools/list` and `tools/call` responses from Python type hints and docstrings. 2. **Add input validation as a first step.** The source's regex whitelist pattern is critical. Claude Code passes model-generated input to your tool—treat it as untrusted. Always validate, whitelist, or sanitize before acting on it. 3. **Use `@mcp.resource()` for context data.** If you're loading data into Claude Code's context window (like account summaries or documentation), use resources rather than tools. They're read-only and don't require the model to call a function. 4. **Pin to v1.x today.** The v2 alpha has breaking changes and is not production-ready. Add `mcp>=1.0,<2.0` to your dependencies. 5. **Start with `stdio` transport.** It's simpler for local development and testing. Move to Streamable HTTP only when you need to share the server across multiple clients or machines.
Compare side-by-side
Claude Code vs FastMCP
Enjoyed this article?
Share:

AI Toolslive

Five one-click lenses on this article. Cached for 24h.

Pick a tool above to generate an instant lens on this article.

Related Articles

From the lab

The framework underneath this story

Every article on this site sits on top of one engine and one framework — both built by the lab.

More in Open Source

View all