Skip to content

beginner

Build your first MCP server in Python

A hands-on walkthrough of building and connecting a Model Context Protocol server from scratch.

· 1 min read
Share:

This tutorial walks through building a minimal MCP server that exposes one tool, then connecting it to an MCP-compatible client.

1. Install the SDK

pip install mcp

2. Define the server

# server.py
from mcp.server import Server
from mcp.server.stdio import stdio_server

server = Server("hello-tools")

@server.tool()
async def greet(name: str) -> str:
    """Greet someone by name."""
    return f"Hello, {name}!"

async def main():
    async with stdio_server() as (read, write):
        await server.run(read, write, server.create_initialization_options())

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

3. Register it with a client

Point your MCP client’s configuration at the server command:

{
  "mcpServers": {
    "hello-tools": {
      "command": "python",
      "args": ["server.py"]
    }
  }
}

4. Test it

Ask the client to call the greet tool with a name. If everything is wired correctly, the model will discover the tool automatically and call it without any additional glue code.

Next steps

  • Add a second tool that reads from a real data source
  • Add structured error responses for invalid input
  • Move from stdio to an HTTP-based transport for a shared/remote deployment