Build your first “Server” with Model Context Protocol (MCP)
From the publisher’s feed, collected in the ZeroLabs newsroom.
Ok, no more nonsense… let’s dive straight into the topic!generated by openaiTo explain how MCP works, you should know about three main components:MCP Host: The main app (like VS Code or any IDE or AI application IDE like Cursor) that controls everything and connects one or more MCP clients.MCP Client: The connector (client) inside the host that talks to one MCP server and brings its data or tools to the host.MCP Server: The external program that shares data, tools, or context when a client connects to it.Scenario:You create multiple MCP servers in your project—for example:serverA -> serves Github tools (runs on localhost:3000)serverB -> serves Database tools (runs on localhost:4000)Each one is an MCP server process with its own port.When you connect:Your runtime (AI app or IDE) connects to each server’s port.For each connection:→ The runtime creates a client object to talk to the specific port (server).→ The client handles message exchange with only that server.Example:Runtime (your app) ├── Client 1 → connects to MCP Server A (localhost:4000) └── Client 2 → connects to MCP Server B (localhost:5000)→ The localhost port is where your server is listening.→ The client = the runtime’s connection to that port, not the port itself.In short:→ The server runs on ports like APIs.→ The client is the code in your runtime that connects to those ports—one client per server.Technically speaking, when you write code/program in an IDE tool and execute it, it runs on localhost. Using this localhost URL, we’re going to interact with the MCP server. We create the MCP server using the programming itself.How to create MCP serversTo create MCP servers, you can simply add @mcp.tool(), @mcp.resource(), and @mcp.prompt() on top of the functions.Core components in MCP:prompt: It’s a template that tells the AI to reference the resource and toolsTools: used to perform actions (like API calls, database queries)Resource: provides information or rules to AI apps (database records, API responses)In simple terms, MCP server is just a bundle of tools, resource and prompts, basically a bunch of plain python functins working together.from fastmcp import FastMCPfrom typing import Annotated#Initialize the servermcp = FastMCP("MediumArticlePro")#RESOURCE:The "Knowledge Base"@mcp.resource("config://brand-voice")def get_brand_voice() -> str: "Provides the user's specific writing style and tone guidelines." return " - Tone: Helpful, witty, and concise. - Formatting: Use bullet points for readability. - Constraints: Never use the word 'delve' or 'comprehensive'. "#TOOL:The "Action"@mcp.tool()def calculate_seo_score(text: str, keyword: str) -> str: "Analyzes a draft to see how many times a keyword appears." count = text.lower().count(keyword.lower()) score = "Low" if count return f"Keyword '{keyword}' found {count} times. SEO Score: {score}."#PROMPT:The "Starter"@mcp.prompt()def draft_article(topic: str, keyword: str): "A template that pulls everything together for the AI." return f""" Using the style found in 'config://brand-voice', write an intro about {topic}. After writing, use the 'calculate_seo_score' tool to check it for the keyword: '{keyword}'. """if __name__ == "__main__": mcp.run(host="0.0.0.0", port=8081, transport="http")We are using the FastMCP framework hereHow to test the MCP serverTo test the MCP, we can use MCP Inspector or MCPJam Inspector.# MCP Inspector1. Install MCP inspector:(Open your command line and run the command below.)npx @modelcontextprotocol/inspectorWhen you run this, it asks for "ok" to proceed; type “y” and hit enter.2. Install FastMCP dependencies in your project directory:uv pip install fastmcp fastmcp[cli]3. Navigate to your project directory and run:uv run fastmcp dev file_name.pyIt will directly open the MCP inspector with the server connection established, or else you can launch the inspector directly with the same install command and copy-paste the localhost URL. At the end of the URL, add /mcp (http://localhost:8081/mcp)# MCPJam Inspector1. Install the MCPJam inspector:Open your command line and run the command below (no global install needed)When you run this, it asks for "ok to proceed"; type “Y” and hit enter.npx @mcpjam/inspector@latest2. Install the dependenciesuv pip install fastmcp3. Launch the MCP Jam inspector:npx @mcpjam/inspector@latestwhen you launch, it should look like thisSo when you run your Python file on a specific port, it runs on the localhost URL (http://0.0.0.0:8081/); copy it.Now go to MCPJam, click on Add serverIn the connection type, choose "http," paste the URL copied, and add /mcp to the end of the URL (http://localhost:8081/mcp); otherwise, it won’t connect.Now, click on Add server, and the server will connect to your localhostNow you can explore tools, resources, and prompts shown on the left side of the bar, where you can test them.There is also a chat option, where you can send a message in the chat, so it can directly execute the connected tools and generate the output in the chat itself.I prefer to use MCPJam, as the UI is very friendly, and there are so many options and resources available to interact with.I would say that the understanding of MCPs is a little bit confusing. But I wrote this article in a simple way, which is easy to understand. Even though you didn’t understand, read it 3x/4x or use AI to explain it at a level until you understand. Once you understand, come back to this article and read it again! Then, it will make sense!!Build your first “Server” with Model Context Protocol (MCP) was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.