joonheeu/mcp-server-template
If you are the rightful owner of mcp-server-template and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This project is a template for creating a Model Context Protocol (MCP) server using FastAPI and FastMCP.
get_data_by_id
Retrieve specific data by its ID.
search_data_by_query
Search for data using keywords.
create_new_data
Create new data entries.
update_existing_data
Update existing data entries.
delete_data_by_id
Delete data by its ID.
MCP Server Template
μ΄ νλ‘μ νΈλ FastAPIμ FastMCPλ₯Ό μ¬μ©ν MCP(Model Context Protocol) μλ² ν νλ¦Ώμ λλ€. Python + uv + FastMCP + FastAPI + Scalar UIλ₯Ό κ²°ν©νμ¬ λΉ λ₯΄κ³ ν¨μ¨μ μΈ MCP μλ² κ°λ°μ μ§μν©λλ€.
π μμνκΈ°
1. κ°μ νκ²½ μμ± λ° νμ±ν
uv
λ₯Ό μ¬μ©νμ¬ Python κ°μ νκ²½μ μμ±νκ³ νμ±νν©λλ€:
# uv μ¬μ© (κΆμ₯)
uv venv
source .venv/bin/activate # Linux/macOS
# λλ .venv\Scripts\activate # Windows
# λλ κΈ°μ‘΄ venv μ¬μ©
python -m venv .venv
source .venv/bin/activate
2. μμ‘΄μ± μ€μΉ
# uv μ¬μ© μ (κΆμ₯)
uv pip install -r requirements.txt
# λλ pyproject.toml μ¬μ©
uv sync
# pip μ¬μ© μ
pip install -r requirements.txt
3. νκ²½ λ³μ μ€μ
env.example
νμΌμ 볡μ¬νμ¬ .env
νμΌμ μμ±ν©λλ€:
cp env.example .env
μμ±λ .env
νμΌμ μ΄μ΄ νμν νκ²½ λ³μλ₯Ό μ€μ νμΈμ:
# Server Configuration
SERVER_NAME=my-awesome-mcp-server
SERVER_VERSION=1.0.0
SERVER_DESCRIPTION="My awesome MCP server"
# API Configuration
API_HOST=localhost
API_PORT=8001
# External API Configuration (μ€μ APIμ λ§κ² μμ )
EXTERNAL_API_KEY=your_actual_api_key
EXTERNAL_API_SECRET=your_actual_api_secret
EXTERNAL_API_BASE_URL=https://your-api.example.com
π₯οΈ μλ² μ€ν
κ°λ° λͺ¨λλ‘ μ€ν
# uvicornμΌλ‘ μ€ν (hot reload ν¬ν¨)
uvicorn app.main:app --host 0.0.0.0 --port 8001 --reload
# λλ PythonμΌλ‘ μ§μ μ€ν
python -m app.main
νλ‘λμ λͺ¨λλ‘ μ€ν
uvicorn app.main:app --host 0.0.0.0 --port 8001
μλ²κ° μ€νλλ©΄ λ€μ URLλ€μ μ κ·Όν μ μμ΅λλ€:
- Scalar UI:
http://localhost:8001/scalar
(μΆμ²) - Swagger UI:
http://localhost:8001/docs
- MCP SSE μλν¬μΈνΈ:
http://localhost:8001/mcp/sse
- Health Check:
http://localhost:8001/health
π νλ‘μ νΈ κ΅¬μ‘°
mcp-server-template/
βββ app/ # λ©μΈ μ ν리μΌμ΄μ
μ½λ
β βββ main.py # FastAPI μ± λ° MCP μλ² μ€μ
β βββ config.py # νκ²½ λ³μ λ° μ€μ κ΄λ¦¬
β βββ services/ # μΈλΆ μλΉμ€ μ°λ λ‘μ§
β βββ example_api_client.py # μμ API ν΄λΌμ΄μΈνΈ
βββ static/ # μ μ νμΌ
β βββ scalar.html # Scalar UI HTML
βββ docs/ # λ¬Έμ (μ νμ¬ν)
βββ pyproject.toml # uv/Python νλ‘μ νΈ μ€μ
βββ requirements.txt # Python μμ‘΄μ± λͺ©λ‘
βββ env.example # νκ²½ λ³μ μμ
βββ README.md # νλ‘μ νΈ λ¬Έμ
π MCP (Model Context Protocol) μ¬μ©
MCP ν΄λΌμ΄μΈνΈμμ μ°κ²°
MCP ν΄λΌμ΄μΈνΈ μ€μ νμΌμ λ€μκ³Ό κ°μ΄ μΆκ°νμΈμ:
{
"mcpServers": {
"My MCP Server": {
"command": "npx",
"args": [
"-y",
"mcp-remote@latest",
"http://localhost:8001/sse"
]
}
}
}
μ§μλλ MCP λꡬ
μ΄ ν νλ¦Ώμμ μ 곡νλ κΈ°λ³Έ MCP λꡬλ€:
- get_data_by_id: λ°μ΄ν° IDλ‘ νΉμ λ°μ΄ν° μ‘°ν
- search_data_by_query: ν€μλλ‘ λ°μ΄ν° κ²μ
- create_new_data: μλ‘μ΄ λ°μ΄ν° μμ±
- update_existing_data: κΈ°μ‘΄ λ°μ΄ν° μ λ°μ΄νΈ
- delete_data_by_id: λ°μ΄ν° μμ
π οΈ μ»€μ€ν°λ§μ΄μ§
1. API ν΄λΌμ΄μΈνΈ μμ
app/services/example_api_client.py
νμΌμ μ€μ μ¬μ©ν μΈλΆ APIμ λ§κ² μμ νμΈμ:
# μ€μ API μλν¬μΈνΈ λ° μΈμ¦ λ°©μμ λ§κ² μμ
class YourAPIClient:
def __init__(self):
self.base_url = "https://your-api.example.com"
self.api_key = "your-api-key"
async def your_custom_method(self, param: str):
# μ€μ API νΈμΆ λ‘μ§ κ΅¬ν
pass
2. MCP λꡬ μΆκ°
app/main.py
μμ μλ‘μ΄ MCP λꡬλ₯Ό μΆκ°ν μ μμ΅λλ€:
@mcp.tool(description="μλ‘μ΄ κΈ°λ₯μ μνν©λλ€.")
async def new_tool(param: str) -> str:
"""μλ‘μ΄ λꡬ μ€λͺ
"""
try:
# μ€μ λ‘μ§ κ΅¬ν
result = {"message": f"μ²λ¦¬ μλ£: {param}"}
return json.dumps(result, indent=2, ensure_ascii=False)
except Exception as e:
return json.dumps({"error": str(e)}, indent=2, ensure_ascii=False)
3. FastAPI μλν¬μΈνΈ μΆκ°
REST API μλν¬μΈνΈλ μ½κ² μΆκ°ν μ μμ΅λλ€:
@app.get("/new-endpoint", tags=["Custom"])
async def new_endpoint():
"""μλ‘μ΄ REST API μλν¬μΈνΈ"""
return {"message": "Hello from new endpoint!"}
π§ͺ ν μ€νΈ
# ν
μ€νΈ μ€ν (pytest μ€μΉ ν)
pytest
# 컀λ²λ¦¬μ§μ ν¨κ» ν
μ€νΈ
pytest --cov=app
π κ°λ° λꡬ
μ΄ ν νλ¦Ώμ λ€μ κ°λ° λꡬλ€μ μ§μν©λλ€:
- Black: μ½λ ν¬λ§€ν
- Ruff: λ¦°ν λ° μ½λ νμ§ κ²μ¬
- pytest: ν μ€νΈ νλ μμν¬
# μ½λ ν¬λ§€ν
black app/
# λ¦°ν
ruff check app/
# μλ μμ
ruff check app/ --fix
π³ Docker μ§μ
Dockerλ₯Ό μ¬μ©νλ €λ©΄ λ€μ Dockerfileμ μΆκ°νμΈμ:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8001
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]
π μ°Έκ³ μλ£
π€ κΈ°μ¬
μ΄μλ κ°μ μ¬νμ΄ μμΌμλ©΄ μΈμ λ μ§ κΈ°μ¬ν΄ μ£ΌμΈμ!
π λΌμ΄μ μ€
MIT License