Lesson 13 Building Your Own MCP Server
Theme:
Lesson 13 · Slide 1

Building Your Own MCP Server

Turn any Python application into an MCP server that Claude can use. This lesson covers the FastMCP SDK, implementing tools and resources, input validation, and local testing.

Learning Objectives

Lesson 13 · Slide 2

MCP Python SDK

The mcp Python package provides FastMCP, a high-level framework for building MCP servers with minimal boilerplate.

pip install "mcp[cli]"
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("my-server")

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

if __name__ == "__main__":
    mcp.run()

Key features: Automatic JSON Schema generation from type hints, built-in stdio and SSE transports, resource URI routing, and a development inspector.

Lesson 13 · Slide 3

Server Lifecycle

An MCP server goes through three phases:

  1. Initialisation: Client connects, sends initialize request. Server responds with capabilities.
  2. Operation: Client discovers tools/resources, then calls them as needed. The server processes requests and returns results.
  3. Shutdown: Client disconnects or sends shutdown signal. Server cleans up resources.

With FastMCP, the lifecycle is handled automatically. You just define your tools and resources, and the framework manages the protocol.

Note: For stdio transport, the server runs as a subprocess. It starts when the host launches it and stops when the host kills the process. For SSE, the server runs as a long-lived web service.

Lesson 13 · Slide 4

Implementing Tools

Tools are functions the LLM can call. FastMCP uses decorators and type hints to generate schemas automatically.

@mcp.tool()
def add_note(title: str, content: str) -> str:
    """Add a new note to the collection.

    Args:
        title: Short title for the note
        content: Full text of the note
    """
    notes[title] = content
    return f"Note '{title}' saved ({len(content)} chars)"

@mcp.tool()
def list_notes() -> str:
    """List all saved notes with their titles."""
    if not notes:
        return "No notes yet."
    return "\n".join(f"- {title}" for title in notes)

The @mcp.tool() decorator registers the function as an MCP tool. The docstring becomes the tool description. Type hints become the JSON Schema input definition.

Lesson 13 · Slide 5

Implementing Resources

Resources provide data that the host can load into context. They use URI patterns for routing.

@mcp.resource("notes://all")
def get_all_notes() -> str:
    """Return all notes as formatted text."""
    if not notes:
        return "No notes saved."
    parts = [f"## {title}\n{content}" for title, content in notes.items()]
    return "\n\n".join(parts)

@mcp.resource("notes://{title}")
def get_note(title: str) -> str:
    """Return a specific note by title."""
    if title not in notes:
        return f"Note '{title}' not found."
    return notes[title]

URI templates: Use {param} in the URI pattern to capture dynamic segments. FastMCP extracts the parameter and passes it to your function.

Lesson 13 · Slide 6

Testing Locally

The MCP CLI includes an Inspector for testing servers without connecting to Claude.

# Run your server with the inspector
mcp dev notes_server.py

# Or test via the CLI
mcp run notes_server.py

The Inspector provides a web UI where you can:

Workflow: Develop with mcp dev for rapid iteration. When ready, add the server to claude_desktop_config.json for integration testing with Claude.

Lesson 13 · Slide 7

Demo

Live demonstration: building and testing a notes MCP server.

Demo Steps

  1. Create notes_server.py with FastMCP
  2. Add add_note and list_notes tools
  3. Add a notes://all resource
  4. Test with mcp dev notes_server.py
  5. Call tools from the Inspector UI
  6. Add the server to Claude Desktop config and test with Claude
Notes: See demo/notes_server.py for the complete working server. Students can run it immediately.

📥 Demo Files

⬇ notes_server.py
Lesson 13 · Slide 8

Lab & Homework

Lab Exercises

  1. Echo Server — minimal MCP server with one echo tool
  2. Notes Tools — add add_note + list_notes tools
  3. Notes Resource — add notes:// resource URI

Homework Tasks

  1. Add a search_notes tool with keyword matching
  2. Build a real MCP server for a domain of your choice
  3. Connect your server to Claude Desktop and demonstrate
1 / ?