home / mcp / example mcp server

Example MCP Server

Provides MCP-based data sources and actions that you can expose to language models through resources, tools, and prompts.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "hoyoun86-mcp": {
      "command": "python",
      "args": [
        "server.py"
      ]
    }
  }
}

The MCP server lets you expose data and functionality to language models through a standardized, secure interface. You define resources, tools, and prompts that an LLM can access or invoke, then run the server locally or deploy it to a remote runtime. This approach keeps your core application logic separate from how the model consumes context, enabling safer, more scalable interactions.

How to use

Connect to an MCP server using a compatible client and browse the available resources and tools. You can read resources to supply context to the LLM, invoke tools to perform computations or actions, and apply prompts to guide interactions. You can also run the server in different transport modes, test locally during development, and mount multiple MCP servers inside a single web application.

How to install

Prerequisites: install Python and a Python package manager. You will also need the MCP command line tooling to start and manage servers during development.

uv init mcp-server-demo
cd mcp-server-demo
```

```bash
uv add "mcp[cli]"
```

Alternatively, use pip to install the MCP CLI for dependency management:
```bash
pip install "mcp[cli]"
````

To run the MCP development tools with the CLI, start the development server workflow:
```bash
uv run mcp
```

For direct server execution, you typically create your Python server file and run it with Python or via the MCP runner:
```bash
python server.py
```

Or start via the MCP command if you prefer the runner syntax:
```bash
mcp run server.py
```

If you are testing in a browser or a GUI integration, you can also install and run servers through integration tools like Claude Desktop by installing your server:
```bash
mcp install server.py
```

Custom names and environment variables may be supplied when you install the server, for example:
```bash
mcp install server.py --name "My Analytics Server" -v API_KEY=abc123 -v DB_URL=postgres://...
```

For development testing, you can also run a server in development mode using the inspector:
```bash
mcp dev server.py
```

If you want to mount on an existing ASGI server or combine multiple MCP servers, see the mounting options described in the advanced usage notes.

Additional sections

Configuration and transport options let you tailor how MCP servers communicate with clients and LLMs. The platform supports multiple transports, including standard streaming transports and streamable HTTP, with a note that streamable HTTP is the recommended transport for production deployments. You can run a single server in a standalone process, or mount several servers into a single web application. You can also choose between development tooling that inspects and validates your server locally and production-ready deployment patterns that emphasize scalability and resumable sessions.

Example server lifecycle and usage patterns include building resources, tools, and prompts, then starting a server with a straightforward entry point. You can use the helper APIs to manage server lifespan, access lifecycle context, and instrument long-running tasks with progress updates or partial results. If you need to expose protected resources, you can configure OAuth-based authentication by providing a token verifier implementation and an authorization server configuration to validate access tokens.

A practical pattern is to compose multiple MCP servers and mount them under distinct paths in a single application, making it easy to segregate concerns such as analytics, data services, and utilities. For local experimentation, use the development workflow, then switch to a production transport such as streamable HTTP for multi-node deployments. When ready, you can mount servers into a Starlette or FastAPI app and expose dedicated subpaths for each server.

Security and authentication

If you need protected access to your MCP resources, you can enable token-based authentication by implementing a TokenVerifier. The server then validates tokens issued by an authorization server before granting access to protected resources or tools.

Examples

Echo and SQLite exploration examples demonstrate how to expose resources and tools that return static or computed data. These examples show how to define resources that return static values, tools that perform actions or calculations, and prompts that guide LLM interactions.

Low-level server and client usage

For advanced scenarios, you can use a low-level server implementation to customize the protocol handling and lifecycle management. This approach gives you full control over prompts, resources, and tools, and allows you to wire in a bespoke lifespan protocol for starting and stopping services.

Notes on deployment and transport

Streamable HTTP transports are recommended for production deployments due to their resilience and scalability. You can mount multiple MCP servers into a single web application and configure mount paths to organize access to different servers. If you are integrating into an existing ASGI server, you can mount the MCP servers at specific routes, or attach them to a host-based route for organization.

Available tools

add

Add two numbers within a tool to provide a simple arithmetic operation for the MCP server.

get_greeting

Return a personalized greeting based on an input name, exposing dynamic data through a resource-like tool.

calculate_bmi

Compute Body Mass Index from weight and height inputs, returning a structured numeric result.

fetch_weather

Retrieve weather information for a given city using an HTTP call within a tool.

get_weather

Return structured weather data for a specified city, illustrating a tool that provides rich context.

list_cities

Provide a list of city names as a tool output to demonstrate list-typed structured results.

get_temperature

Return a simple temperature value for a city as a tool output.

echo

Return the input message to demonstrate a basic echo tool.

query_db

Interact with a database through a tool that demonstrates context-aware usage and data access.