How to build and test an MCP server with two tools

Day 2, 30 minutes · advanced

Goal of this lessonBuild a small Python MCP server with two tools, one read-only and one for rules, run it on your own computer and test it with the MCP Inspector command line.

Why it matters

To build an MCP server in Python, you write each tool as a function with a clear description and honest safety flags, then test it locally before ChatGPT sees it.

ChatGPT cannot use your service until your service speaks MCP, and every later lesson reuses this server. If the first version is vague, ChatGPT picks the wrong tool, or it asks the visitor to confirm things that only read data.

Today Ingrid writes the first version of the Moorhen server: one tool that lists free kayaks for a day, one that returns the written launch rules. You run it on your own computer and call it with a tool that is not ChatGPT, so you know it works before anything is exposed to the internet. The server fills in the three safety flags the docs ask for and describes each tool by when it should be used. You end with a server that answers a real call and a short file with proof.

What you build

One Python file, moorhen.py, with two tools. A tool is a function ChatGPT may call. If MCP basics are new to you, first read the MCP introduction and the advanced course, which cover tools, transports and the general security of a server. This lesson only adds what the plugin pages ask for.

The code uses the Python MCP SDK, pinned to mcp==1.30.0, the version the plugin docs show. The sibling courses use a newer SDK with different class names, so do not mix the two. The server runs with stateless_http=True and json_response=True: each call stands alone and the reply is plain JSON, as the docs example does. It listens on port 3000 at the path /mcp.

What the docs ask of every tool

  • A description that says when to use it. The model picks tools by reading these. The tools page says to describe the user's intent, not your internal code.
  • Three safety flags, always written out as true or false: readOnlyHint, destructiveHint and openWorldHint. A tool without readOnlyHint is treated as a write, so ChatGPT asks the person to confirm it. Both tools here only read, and neither touches the open internet.
  • A security scheme per tool. Both tools are public for now: {"type": "noauth"}. Lesson 4 adds tools that need sign-in.
  • Structured output. The first tool returns both a sentence and a data object (structuredContent). The data object is what a later card reads.

The host guard

The SDK blocks calls whose Host header is not on an allowed list, to stop DNS rebinding attacks (see the advanced MCP course). That is why moorhen.py has an allowed list. It already reads a PUBLIC_URL setting that lesson 3 fills in. If you skip it later you will see "Invalid Host header" and a 421 status.

Test it without ChatGPT

The MCP Inspector has a command line mode. You give it the address, --transport http, a --method and, for a call, --tool-name and --tool-arg key=value. It needs Node.js: the Inspector page names Node 22.23.4 or newer, 24.22.1 or newer, or 26.12.1 or newer. In a test on Node 24.12 it only printed a warning and still worked. On Windows it may print a harmless "Assertion failed" line when it exits. Do not rely on its exit code.

The steps

  1. Make the folder and virtual environment.
  2. Install mcp==1.30.0, python-dotenv and PyJWT in one go.
  3. Write moorhen.py in three parts.
  4. Run it and leave it running.
  5. Call both tools with the Inspector command line.
  6. Save the output in tool-check.txt.

Pick your tool below and follow the steps.

If something fails

  • ModuleNotFoundError: the virtual environment is not active. Activate it again.
  • The Inspector cannot connect: check that the server printed a line that it is running and that the address ends in /mcp.
  • A tool call with a bad date returns an error result: that is the schema working. The date must look like 2027-06-12.

Common mistakes

  • Leaving out the flags and getting confirmation prompts for a read-only tool.
  • Writing a description like "Calendar tool" instead of "Use this when the visitor asks which kayaks are free".
  • Installing a newer mcp than the one this code was run with.

Today's actions

Ticks are not saved without an account

Do it in OpenAI API

OpenAI API

  1. In your moorhen-plugin folder run python -m venv .venv and activate it: source .venv/bin/activate (macOS, Linux) or .venv\Scripts\activate (Windows). Then run python --version: the build page says to use Python 3.11 or later.
  2. Run pip install mcp==1.30.0 python-dotenv pyjwt. PyJWT is only used in lesson 4. Check that Node.js is installed with node --version.
  3. Create moorhen.py and paste this first part: the imports, the host guard and the made-up sample data.
    import os
    from typing import Annotated, Literal
    from urllib.parse import urlparse
    
    from dotenv import load_dotenv
    from mcp.server.fastmcp import FastMCP
    from mcp.server.transport_security import TransportSecuritySettings
    from mcp.types import CallToolResult, TextContent, ToolAnnotations
    from pydantic import BaseModel, Field
    
    load_dotenv()
    
    # Lesson 3 puts your public address in a file called .env. Until then only
    # localhost is allowed to call this server.
    HOSTS = ["localhost:*", "127.0.0.1:*"]
    if os.environ.get("PUBLIC_URL"):
        HOSTS.append(urlparse(os.environ["PUBLIC_URL"]).netloc)
    
    server = FastMCP(
        "moorhen-paddle-hire",
        instructions=(
            "Moorhen Paddle Hire rents kayaks on Saturdays and Sundays. "
            "Use list_free_kayaks before suggesting a kayak. "
            "Use get_launch_rules for safety questions."
        ),
        port=3000,
        stateless_http=True,
        json_response=True,
        transport_security=TransportSecuritySettings(
            allowed_hosts=HOSTS,
            allowed_origins=["http://localhost:6274", "http://127.0.0.1:6274"],
        ),
    )
    
    KAYAKS = [
        {"id": "K1", "name": "Heron", "kind": "single"},
        {"id": "K2", "name": "Swallow", "kind": "single"},
        {"id": "K3", "name": "Pondskater", "kind": "double"},
        {"id": "K4", "name": "Lilypad", "kind": "double"},
    ]
    # Made-up sample data: kayaks that are already taken on a day.
    TAKEN = {"2027-06-12": {"K1", "K3"}, "2027-06-13": {"K2"}}
    
  4. Add the first tool to the end of moorhen.py: free kayaks on a day, with an input check and structured output.
    
    
    class Kayak(BaseModel):
        id: str
        name: str
        kind: Literal["single", "double"]
    
    
    class FreeKayaks(BaseModel):
        date: str
        kayaks: list[Kayak]
    
    
    @server.tool(
        title="List free kayaks",
        description=(
            "Use this when the visitor asks which kayaks are free on a given day. "
            "Do not use it to make a booking."
        ),
        annotations=ToolAnnotations(
            readOnlyHint=True, destructiveHint=False, openWorldHint=False
        ),
        meta={"securitySchemes": [{"type": "noauth"}]},
    )
    def list_free_kayaks(
        date: Annotated[str, Field(pattern=r"^\d{4}-\d{2}-\d{2}$", description="Day as YYYY-MM-DD")],
        kind: Literal["single", "double"] | None = None,
    ) -> Annotated[CallToolResult, FreeKayaks]:
        taken = TAKEN.get(date, set())
        free = [k for k in KAYAKS if k["id"] not in taken]
        if kind is not None:
            free = [k for k in free if k["kind"] == kind]
        return CallToolResult(
            content=[TextContent(type="text", text=f"Free kayaks on {date}: {len(free)}.")],
            structuredContent={"date": date, "kayaks": free},
        )
    
  5. Add the second tool and the start line to the end of the same file.
    
    
    @server.tool(
        title="Get launch rules",
        description=(
            "Use this when the visitor asks about safety, life jackets or the wind "
            "limit at the Moorhen launch. Returns the written rules only."
        ),
        annotations=ToolAnnotations(
            readOnlyHint=True, destructiveHint=False, openWorldHint=False
        ),
        meta={"securitySchemes": [{"type": "noauth"}]},
    )
    def get_launch_rules() -> str:
        return (
            "Everyone wears a life jacket. No launches when the wind is above "
            "force 5. Children under 12 paddle in a double kayak with an adult."
        )
    
    
    if __name__ == "__main__":
        server.run(transport="streamable-http")
    
  6. Run python moorhen.py and leave that terminal open. In a second terminal, run the Inspector: npx @modelcontextprotocol/inspector --cli http://localhost:3000/mcp --transport http --method tools/list. You should see both tools with their flags.
  7. Call the first tool: npx @modelcontextprotocol/inspector --cli http://localhost:3000/mcp --transport http --method tools/call --tool-name list_free_kayaks --tool-arg date=2027-06-12. Free on that day should be K2 and K4. Then try --tool-arg date=tomorrow and read the error.
  8. Call get_launch_rules the same way, then save both outputs and the date in tool-check.txt. That file is the proof of this lesson.

Note: Pinned to mcp 1.30.0, the version the plugin docs show. Sibling courses use another SDK version, so do not mix them. The Inspector CLI needs a recent Node.js. All names and data are made up.

No plan needed: everything in this lesson runs on your own computer.

Checked on 4 December 2026 · Source: OpenAI Plugins docs: Build your MCP server (opens in a new tab)

Create a free account to save your progress. Sign in or sign up