Build an MCP server in Python with the official SDK

The MCP Python SDK lets you expose an internal function to an AI assistant through a defined interface. This tutorial builds a small server, tests it and serves it over HTTP, giving you a base for tools with clear limits.

Niklas Schult4 min read

Illustration: A plug links a socket to a toolbox whose drawer opens only a limited way, showing a single tool.

What you will have at the end and what you need

At the end you will have a running MCP server with one tool and one templated resource, checked in the MCP Inspector and reachable over HTTP. A short Python client then calls the tool. The example uses a function that adds two numbers, which you can later replace with something real, such as a document search.

The SDK README describes the Model Context Protocol as a standardised way to expose data and functionality to LLM applications. It compares the protocol to a web API designed for LLM interactions. A server can offer tools, resources and prompts to any MCP host. You need Python 3.11 or newer and either uv or pip.

The same SDK also builds clients that connect to any MCP server. That is why the tutorial ends with a small client: it lets you confirm the whole round trip from one package, without a separate assistant application.

Step 1: install the SDK

The command below installs the SDK together with the cli extra. That extra adds the mcp command-line tool with mcp dev, mcp run and mcp install. If you do not need the tool, the README says plain mcp is enough.

Shell
uv add "mcp[cli]"
# alternative: pip install "mcp[cli]"

For one-off commands without a project, the README names the form uv run --with "mcp[cli]" mcp followed by your arguments. That suits a quick trial on an administrator's machine before a proper project exists.

Step 2: write a server with a tool and a resource

Create a file called server.py with the code below. The decorator @mcp.tool() registers the function add as a tool, and @mcp.resource registers a resource whose address contains a name placeholder. The type hints a: int and b: int serve as the schema, so you write no JSON Schema, request parsing or validation code yourself.

Python
from mcp.server import MCPServer

mcp = MCPServer("Demo")


@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
    """Greet someone by name."""
    return f"Hello, {name}!"

In an internal tool the narrow signature also acts as the boundary, since the assistant can only pass the parameters you declare. A read-only document search with a query string is a sensible first candidate, although the README does not go beyond the simple example here.

The resource differs from the tool in how it is addressed. The placeholder in greeting://{name} is filled from the requested address and passed to the function as the argument name. A tool, by contrast, is called with named parameters, as the client later in this article shows.

Step 3: check it in the Inspector and serve it over HTTP

The first command opens your server in the MCP Inspector, a tool for trying out tools and resources by hand. Call add with a=1 and b=2 and the result should be 3.

Shell
uv run mcp dev server.py

The second command serves the same file over Streamable HTTP. The README calls this the transport you deploy, and the SDK also speaks stdio and SSE.

Shell
uv run mcp run server.py --transport streamable-http

Testing by hand first is worthwhile, because you see exactly what the assistant will see before any model is involved. Once the Inspector shows the right result, switching to HTTP changes only the transport, not the code in server.py.

Check with a client and typical pitfalls

The same package contains a client. The script below connects to the local address, calls the tool add with two numbers and prints the structured result, which should be {'result': 3}. Start the server from the previous step first, in a second terminal.

Python
import asyncio

from mcp import Client


async def main() -> None:
    async with Client("http://localhost:8000/mcp") as client:
        result = await client.call_tool("add", {"a": 1, "b": 2})
        print(result.structured_content)  # {'result': 3}


asyncio.run(main())

The README lists few pitfalls, so these points follow from its text. A URL as client target means Streamable HTTP, while a local server can also be launched as a stdio subprocess. The cli extra must be installed for the mcp command to exist, and Python below 3.11 does not meet the requirements. The README also mentions a v2 and a migration guide, so check the SDK documentation if older examples use different imports.

Where this fits in a company

An MCP server is a small service, so it needs an owner, a place to run and a review of what it exposes. Typical questions are which data a tool can reach, who may call it and whether personal data passes through, which belongs in your GDPR review. Tools that write to systems usually need a human approval step, as described on the page about agents with approval. The README does not cover authentication or deployment hardening, so plan that separately.

souveraen.ai is a platform for company knowledge by clouditiv GmbH in Auerbach, where agents prepare tasks and anything that writes to a system waits for approval. It also offers an OpenAI-compatible developer API for your own applications. Operation is possible on demand, hosted in Germany, or on-premise.

Sources

More articles

All articles

Self-host Langfuse for LLM monitoring in your company

Langfuse records prompts, answers and costs of AI applications, and the README describes how to run it on your own infrastructure. This tutorial takes you from an empty machine to a first logged model call.

Tutorial4 min

AI applicant profiles: what employers may use from the web

AI tools can turn social media posts and press articles into an applicant profile within seconds. Employers in Germany need to know which public data the GDPR and the BDSG allow them to use in hiring.

Data protection4 min

Do lawyers need client consent to use AI?

A Legal Tribune Online article by Nico Kuhlmann concludes that using AI in a law firm is treated like engaging any other service provider. Consent is usually unnecessary, though borderline cases exist and the duty of confidentiality applies in full.

Data protection5 min

Test it with your own documents.

Free for 3 days, then from €24.90 per user per month. Cancellable monthly.

Free for 3 days, then from €24.90 per user per month.Free for 3 days
Try free for 3 days