MCP Template

MCP Template

A minimal, production-ready FastMCP server template with auto-discovery, YAML configuration, authentication, and a knowledgebase, enabling quick scaffolding of new MCP servers.

Category
访问服务器

README

MCP Template

A minimal, production-ready FastMCP server template. Use this as a starting point for building your own MCP (Model Context Protocol) server.

What This Is

A clean reference implementation that includes:

Feature Details
Auto-discovery Drop .py files into src/tools/, src/prompts/, or src/resources/ — they register automatically
YAML config Base + environment-specific overrides with env-var substitution
Auth middleware Bearer token, toggle on/off via config — no code change needed
Graceful shutdown SIGINT / SIGTERM handlers
Request logging Correlation IDs, per-request timing, level-aware
Knowledgebase YAML-backed topic store, live-reloadable without restart
Docker Dockerfile + docker-compose.yml included
3 example tools echo_message, greet, calculate
2 KB tools search_knowledgebase, get_knowledgebase_topic
3 example prompts general_assistant, task_helper, analysis_mode
3 example resources capabilities, quick-reference, server info

Project Structure

├── src/
│   ├── mcp_app.py                      # FastMCP instance — import `mcp` from here
│   ├── server.py                       # Entry point: Starlette app, middleware, routing
│   ├── tools/
│   │   ├── example_tools.py            # echo_message, greet, calculate
│   │   └── knowledgebase_tools.py      # search_knowledgebase, get_knowledgebase_topic
│   ├── prompts/
│   │   └── example_prompts.py          # general_assistant, task_helper, analysis_mode
│   ├── resources/
│   │   ├── help_resources.py           # help://<name>/capabilities, quick-reference
│   │   └── server_info.py              # data://server/info
│   └── utils/
│       └── logging_middleware.py       # HTTP logging with correlation IDs
├── config/
│   ├── config.py                       # Config loader (YAML + env vars)
│   ├── settings.yaml                   # Base configuration
│   ├── settings.dev.yaml               # Overrides for ENV=dev
│   └── settings.prod.yaml              # Overrides for ENV=prod
├── knowledgebase/
│   ├── loader.py                       # Loads topics.yaml (no caching)
│   └── topics.yaml                     # Knowledgebase topic definitions
├── Dockerfile
├── docker-compose.yml
├── .dockerignore
├── .env.example
├── requirements.txt
├── README.md
└── CLAUDE.md

Installation

python -m venv .venv

# Windows:
.venv\Scripts\activate
# macOS / Linux:
source .venv/bin/activate

pip install -r requirements.txt

Running Locally

# Default (port 8000, no auth, INFO logging):
python src/server.py

# Development mode (DEBUG logging):
ENV=dev python src/server.py

# Custom port:
MCP_PORT=9000 python src/server.py

# With authentication:
AUTH_ENABLED=true AUTH_TOKEN=my-secret python src/server.py

# Production mode (auth on, WARNING logging):
ENV=prod AUTH_TOKEN=my-secret python src/server.py

Server is available at http://localhost:8000.

Running with Docker

# Build and start:
docker compose up --build

# With auth enabled:
AUTH_ENABLED=true AUTH_TOKEN=my-secret docker compose up --build

# Detached:
docker compose up -d

# Stop:
docker compose down

The container exposes port 8000 (override with MCP_PORT env var for the host port). knowledgebase/topics.yaml is bind-mounted so you can update topics without rebuilding the image.

Configuration

Environment Variables

Variable Default Description
ENV default Config file selector: default, dev, prod
MCP_PORT 8000 HTTP listen port
AUTH_ENABLED false Enable bearer token auth (true / false)
AUTH_TOKEN — Bearer token value (required when auth is enabled)
AUTO_DISCOVER true Auto-import all tools / prompts / resources

YAML Configuration

Edit config/settings.yaml for persistent defaults. Copy .env.example to .env for local overrides (never commit .env).

Logging Level

Set logging.level in config/settings.yaml to DEBUG, INFO, WARNING, or ERROR. Override per environment in settings.dev.yaml / settings.prod.yaml.

Authentication

Disabled by default. To enable:

  1. Set AUTH_ENABLED=true env var or security.authentication.enabled: true in YAML
  2. Set AUTH_TOKEN=<your-token> env var or security.authentication.token in YAML

All MCP requests then require:

Authorization: Bearer <your-token>

Health-check paths (/healthz, /health, /version) are always unauthenticated.

How Auto-Discovery Works

On startup src/server.py calls import_submodules() for the tools, resources, and prompts packages. This uses pkgutil.iter_modules() to find every non-private .py file, imports it, and the @mcp.tool() / @mcp.resource() / @mcp.prompt() decorators register with the global mcp instance in src/mcp_app.py.

  • Files starting with _ are skipped automatically — use this to temporarily disable a module.
  • To disable entirely: AUTO_DISCOVER=false, then import modules manually in src/server.py.

How to Add a New Tool

# src/tools/my_tool.py
from mcp_app import mcp

@mcp.tool(name="my_tool", description="Does something useful.")
def my_tool(input: str) -> dict:
    return {"result": input.upper()}

Restart the server — no other changes needed.

How to Add a New Prompt

# src/prompts/my_prompt.py
from mcp_app import mcp

@mcp.prompt(name="my_prompt", description="A focused system prompt.")
def my_prompt(context: str = "") -> str:
    return f"You are a helpful assistant. Context: {context}"

How to Add a New Resource

# src/resources/my_resource.py
from mcp_app import mcp

@mcp.resource("data://my-domain/my-resource")
def my_resource() -> dict:
    return {"data": "your payload here"}

How to Add Knowledgebase Topics

Edit knowledgebase/topics.yaml — no server restart needed:

topics:
  my_topic:
    title: "My Topic"
    tags: [example]
    content: |
      Topic content goes here.

How to Test

# Health check:
curl http://localhost:8000/healthz

# Version:
curl http://localhost:8000/version

# With auth:
curl -H "Authorization: Bearer my-secret" http://localhost:8000/healthz

Connect with any MCP-compatible client (Claude Desktop, MCP Inspector, etc.).

Getting Started with AI Agents

This template is designed to be cloned and extended with the help of AI coding agents (Claude Code, Amazon Q, Cursor, etc.).

Claude Code (CLI)

git clone <this-repo> my-mcp-server
cd my-mcp-server
claude

Claude Code automatically reads CLAUDE.md at the project root — no extra setup needed. Just describe what you want to build.

Amazon Q (IDE)

Open the project in your IDE with the Amazon Q plugin installed. The .amazonq/rules/mcp-conventions.md file is auto-loaded into every chat session. Describe what you want to build — Q will follow the template conventions.

Cursor / Windsurf / Other IDEs

Reference the conventions file explicitly:

@CLAUDE.md Build me an MCP server that does X, Y, Z.

Starter Prompts

See STARTER_PROMPTS.md for ready-to-paste prompts you can give any AI agent to scaffold a new MCP server from this template.


FastMCP Notes

Targets FastMCP >= 2.0.0. Key patterns:

Pattern Where
FastMCP(name="...") src/mcp_app.py
mcp.http_app(transport="streamable-http") src/server.py
async with mcp_http_app.lifespan(app) src/server.py lifespan
@mcp.tool() / @mcp.resource() / @mcp.prompt() tools / resources / prompts

If you upgrade to a new FastMCP major version, verify these four patterns first. See CLAUDE.md for full architecture details and coding rules.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选