Skip to main content
Glama
w0odst0ck

mcp-server-template

by w0odst0ck

MCP Server Template

범용 MCP Server 스캐폴딩 — FastAPI + FastMCP 통합 템플릿.

이게 뭔가요

MCP (Model Context Protocol) 서비스를 빠르게 구축하기 위한 깔끔한 시작점입니다.

MCP는 AI 모델이 외부 도구를 호출하기 위한 프로토콜 표준입니다. 이 템플릿을 사용하면 최소한의 설정으로 모든 기능을 AI가 호출 가능한 도구로 노출할 수 있습니다.

你的业务代码 → MCP Server → AI 智能体(Cursor / Claude / 自定义应用)

Related MCP server: Simple Remote MCP Server

아키텍처

FastAPI(主容器)
├── CORS 中间件
├── 生命周期管理
├── FastAPI 路由(传统 HTTP)
└── FastMCP 子应用(MCP 协议)
    └── @mcp.tool() 工具

핵심 특징:

  • 하나의 데코레이터 = MCP 프로토콜 + HTTP 인터페이스 동시 지원

  • 다중 모듈 독립 마운트, 각 모듈 간 간섭 없음

  • 즉시 사용 가능한 로깅, 설정, Docker 배포

빠른 시작

사전 조건

  • Python >= 3.12

  • uv

설치

# 克隆模板
git clone <your-repo-url> my-mcp-server
cd my-mcp-server

# 安装依赖
uv sync

# 配置环境变量
cp .env-sample .env

시작

# 方式一:uvicorn(推荐)
uv run uvicorn app.main:app --reload

# 方式二:python
uv run python -m app.main

# 方式三:fastapi CLI
uv run fastapi dev app/main.py

확인

# HTTP 端点
curl http://127.0.0.1:8000/example/hello
# => "Hello, World! Running mcp-server-template v0.1.0"

# MCP 协议端点(返回 MCP 端点信息,非 404 即挂载成功)
curl http://127.0.0.1:8000/example

프로젝트 구조

mcp-server-template/
│
├── app/                    # 应用主目录
│   ├── routers/            # MCP 工具 + FastAPI 路由
│   │   ├── example.py      #   示例模块(供复制参考)
│   │   └── __init__.py
│   ├── utils/              # 工具函数
│   │   └── log.py          #   日志(loguru)
│   ├── __init__.py
│   ├── config.py           # 配置管理(pydantic-settings)
│   ├── dependencies.py     # 依赖注入
│   └── main.py             # 应用入口
│
├── docs/                   # 文档
├── migrations/             # Alembic 数据库迁移(可选)
├── scripts/                # 开发脚本
├── tests/                  # 测试
│
├── .env-sample             # 环境变量模板
├── .gitignore
├── .python-version
├── alembic.ini
├── docker-compose.yaml
├── Dockerfile
├── pyproject.toml
└── README.md

새 비즈니스 모듈 추가

템플릿에는 참고용으로 example.py가 내장되어 있습니다. 새 모듈 생성:

  1. app/routers/example.py를 복사하여 app/routers/your_module.py로 만듭니다.

  2. 모듈 내용 수정 — 이름 변경, 라우트 접두사 변경, 도구 함수 작성

  3. app/main.py에 등록

# app/main.py
from app.routers import your_module
from fastmcp.utilities.lifespan import combine_lifespans

your_module_app = your_module.mcp.http_app(path="/", stateless_http=True)
app.include_router(your_module.router)
app.mount("/your_module", app=your_module_app)

# 同时把 your_module_app.lifespan 合并进 FastAPI 的 lifespan:
# app = FastAPI(lifespan=combine_lifespans(lifespan, your_module_app.lifespan))

도구 함수는 AI(MCP를 통해)와 브라우저(HTTP를 통해) 모두에서 호출할 수 있습니다:

@mcp.tool()
@router.get("/search")
async def search(query: str) -> list:
    """搜索知识库(此描述会暴露给 AI)"""
    # 你的业务逻辑
    return results

테스트

uv run pytest tests/ -v

Docker 배포

# 构建
docker build -t mcp-server:latest .

# 启动
docker compose up -d

설정

모든 설정은 환경 변수 또는 .env 파일을 통해 관리됩니다. 자세한 내용은 .env-sample을 참조하세요.

주요 설정 항목:

변수

설명

기본값

ENVIRONMENT

실행 환경

dev

HOST

수신 주소

127.0.0.1

PORT

수신 포트

8000

DATABASE_URL

데이터베이스 연결 (선택)

None

LOG_LEVEL

로그 레벨

INFO

참고

Related MCP Connectors

Related MCP Servers

  • F
    license
    B
    quality
    D
    maintenance
    A template and demonstration project for building, testing, and deploying remote MCP servers using FastMCP and uv. It provides a foundational structure for creating MCP-compliant tools that can be hosted publicly and integrated with LLM agents.
    2
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    A bare-bones FastMCP server template designed to serve as a starting point for building custom Model Context Protocol servers. It provides a foundational structure for implementing tools over HTTP and includes a built-in health check utility.
    GPL 3.0
  • F
    license
    Not graded
    quality
    D
    maintenance
    A plugin-based MCP server built on FastAPI that supports dynamic tool loading, hot reloading, and API key authentication for extensible AI tool integrations.
    -