Skip to main content
Glama

Demo MCP Server

by tdu-naifen

Creating a Python MCP Server: A Step-by-Step Guide

The Model Context Protocol (MCP) provides a standardized approach for connecting context sources to large language models (LLMs). This tutorial demonstrates how to build a functional MCP server using Python's MCP SDK, enabling you to expose data, tools, and templates to LLM applications.

What is MCP?

MCP creates a bridge between LLM applications and external context sources. It allows you to modularize different aspects of LLM interactions:

  • Data provision through resources (similar to read-only endpoints)
  • Action execution via tools (comparable to API functions)
  • Template management using prompts for reusable interactions

Core MCP Components

MCP servers implement three fundamental building blocks, each serving different purposes:

ComponentControlled ByPurposeCommon Uses
PromptsUserInteractive templates triggered by user selectionCommand shortcuts, menu items
ResourcesApplicationData managed by the client for LLM contextFile content, API data
ToolsLLMFunctions the model can execute independentlyCalculations, API calls, data modifications

Understanding these distinctions helps you design effective MCP servers that properly separate concerns.

Server Feature Advertising

MCP servers announce their capabilities during startup, allowing clients to adapt their behavior:

FeatureConfiguration FlagWhat It Enables
promptslistChangedDynamic prompt template updates
resourcessubscribe, listChangedData exposure with live updates
toolslistChangedFunction discovery and execution
loggingDefaultDebug output configuration
completionDefaultArgument suggestion support

Getting Started

Requirements

Ensure your environment includes:

  • Python 3.7+ (Python 3.11+ recommended)
  • pip package manager
  • Node.js 18.x

Installation Options

Choose one of these installation methods:

Standard pip installation:

pip install "mcp[cli]"

Using uv (recommended for project management):

uv init mcp-server cd mcp-server uv add "mcp[cli]"

Project Structure

Organize your project as follows:

mcp-server/ ├── server.py ├── pyproject.toml (if using uv) └── README.md

Implementation

Building Your Server

Create server.py with the following foundation:

# server.py from mcp.server.fastmcp import FastMCP # Initialize the MCP server mcp = FastMCP("Demo Server") # Define a calculation tool @mcp.tool() def add(a: int, b: int) -> int: """ Performs addition of two integers. Args: a: First number b: Second number Returns: Sum of both numbers """ return a + b # Create a dynamic resource @mcp.resource("greeting://{name}") def get_greeting(name: str) -> str: """ Generates a personalized greeting. Args: name: Person's name for the greeting Returns: Formatted greeting message """ return f"Hello, {name}!" # Add a prompt template @mcp.prompt() def review_code(code: str) -> str: """ Creates a code review template. Args: code: Source code to review Returns: Formatted review prompt """ return f"Please review this code:\n\n{code}" # Server execution if __name__ == "__main__": mcp.run()

Testing Your Server

Using the Development Inspector

The most efficient way to test your server is through the built-in development interface:

mcp dev server.py

This command:

  • Launches your server with live reloading
  • Opens a web interface at http://localhost:6274/
  • Provides interactive testing capabilities

Inspector Configuration

In the Inspector interface, configure the transport settings:

Transport Type: STDIO Command: python Arguments: server.py

Click Connect to establish the connection.

Testing Each Component

Tool Testing
  1. Navigate to the Tools section
  2. Select the add tool
  3. Input test values (e.g., a = 10, b = 15)
  4. Execute and verify the result (25)
Resource Testing
  1. Go to ResourcesResource Templates
  2. Select get_greeting
  3. Enter a name (e.g., Alice)
  4. Click Read Resource
  5. Verify the response: "Hello, Alice!"
Prompt Testing
  1. Access PromptsList prompts
  2. Select review_code
  3. Input sample code: print(1+1)
  4. Execute to see the formatted prompt output

Run this in a separate terminal while your server is active.

Understanding Server Behavior

When you run python server.py directly, the server appears inactive because it uses stdio transport and waits for client connections. This is normal behavior - the server needs a client (like the Inspector or your custom client) to interact with it.

-
security - not tested
F
license - not found
-
quality - not tested

A simple MCP server that provides a calculator tool, dynamic greeting resources, and code review prompts, demonstrating how to build standardized interfaces for LLM applications.

  1. What is MCP?
    1. Core MCP Components
      1. Server Feature Advertising
        1. Getting Started
          1. Requirements
          2. Installation Options
          3. Project Structure
        2. Implementation
          1. Building Your Server
        3. Testing Your Server
          1. Using the Development Inspector
          2. Inspector Configuration
          3. Testing Each Component
        4. Understanding Server Behavior

          Related MCP Servers

          • -
            security
            F
            license
            -
            quality
            An MCP server that allows Claude to interact with local LLMs running in LM Studio, providing access to list models, generate text, and use chat completions through local models.
            Last updated -
            8
            Python
          • A
            security
            F
            license
            A
            quality
            An MCP server that implements a structured workflow for LLM-based coding, guiding development through feature clarification, documentation generation, phased implementation, and progress tracking.
            Last updated -
            10
            14
            TypeScript
            • Apple
          • -
            security
            A
            license
            -
            quality
            An MCP server that helps novice developers deploy web applications through conversational interfaces, bridging the gap between LLMs and various hosting environments.
            Last updated -
            1
            Python
            MIT License
            • Apple
          • A
            security
            A
            license
            A
            quality
            An MCP server that enables LLMs to interact with Moodle platforms to manage courses, students, assignments, and quizzes through natural language commands.
            Last updated -
            7
            13
            JavaScript
            MIT License
            • Apple

          View all related MCP servers

          MCP directory API

          We provide all the information about MCP servers via our MCP API.

          curl -X GET 'https://glama.ai/api/mcp/v1/servers/tdu-naifen/Sample-MCP'

          If you have feedback or need assistance with the MCP directory API, please join our Discord server