jongkwang/hello-mcp-server-python
If you are the rightful owner of hello-mcp-server-python 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.
A Python server example project using the Model Context Protocol.
MCP Server Python
Languages: English | ํ๊ตญ์ด
English
A Python server example project using the Model Context Protocol.
Table of Contents
- Overview
- Reference
- Prerequisites
- Installation
- Connecting to Cursor and Claude for Desktop
- Project Structure
- Features
- Weather API Setup
- License
- References
Overview
This project is a server example that provides tools to LLMs such as Claude AI using the Model Context Protocol (MCP). It offers BMI calculation and weather information lookup functions.
Reference
This project was created based on the following open source project:
Prerequisites
- Python 3.9 or higher
- pip or pip3 (package manager)
- httpx (asynchronous HTTP client)
- MCP Python SDK
Installation
- Clone the repository:
git clone https://github.com/yourusername/hello-mcp-server-python.git
cd hello-mcp-server-python
- Install the required packages:
pip install httpx "mcp[cli]"
- Run the server: (Optional if connecting through Cursor or Claude for Desktop)
python server.py
# or
mcp run server.py
Connecting to Cursor and Claude for Desktop
Connecting to Cursor
- Open Cursor IDE and load the project.
- Create an
mcp.json
file in the project root and add the following content:
{
"mcpServers": {
"mcp-server-python": {
"command": "mcp",
"args": [
"run",
"/_____PATH_OF_FILE_____/server.py"
]
}
}
}
- MCP tools will be automatically recognized when chatting with Claude in Cursor. The server will run automatically according to the mcp.json settings.
Connecting to Claude for Desktop
- Install Claude for Desktop.
- Run the server in the terminal:
python server.py
- Go to the MCP server section in Claude for Desktop settings and add a local MCP server.
- Configure the server URL or port as needed.
- When chatting with Claude, you can use the BMI calculation and weather lookup tools.
Project Structure
hello-mcp-server-python/
โโโ server.py # MCP server main file
โโโ requirements.txt # Dependency package list (optional)
Features
The server currently provides two tools:
calculate_bmi
: Calculates BMI from height and weight inputs.fetch_weather
: Retrieves weather information for a given city name. (API key required)
Weather API Setup
To use the weather API, you need an API key. Open the server.py
file and modify it as follows:
@mcp.tool()
async def fetch_weather(city: str) -> str:
"""Fetch current weather for a city"""
API_KEY = "your_api_key_here" # Enter your API key here
async with httpx.AsyncClient() as client:
response = await client.get(f"https://api.weather.com/{city}?apiKey={API_KEY}")
return response.text
License
This project is distributed under the MIT License. See the LICENSE file for details.
References
- Model Context Protocol Documentation
- Model Context Protocol Python SDK Documentation
- httpx Documentation
- Claude AI Documentation
ํ๊ตญ์ด
Model Context Protocol์ ํ์ฉํ Python ์๋ฒ ์์ ํ๋ก์ ํธ์ ๋๋ค.
๋ชฉ์ฐจ
- ๊ฐ์
- ์ฐธ์กฐํ ์ฌ์ดํธ ์ฃผ์
- ์ฌ์ ์ค์น ์ฌํญ
- ์ค์น ๋ฐฉ๋ฒ
- Cursor์ Claude for Desktop์ ์ฐ๊ฒฐ ๋ฐฉ๋ฒ
- ํ๋ก์ ํธ ๊ตฌ์กฐ
- ๊ธฐ๋ฅ
- ๋ ์จ API ์ค์
- ๋ผ์ด์ผ์ค
- ์ฐธ๊ณ ๋ฌธ์
๊ฐ์
์ด ํ๋ก์ ํธ๋ Model Context Protocol(MCP)์ ์ฌ์ฉํ์ฌ Claude AI์ ๊ฐ์ LLM์ ๋๊ตฌ(Tool) ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์๋ฒ ์์ ์ ๋๋ค. BMI ๊ณ์ฐ๊ณผ ๋ ์จ ์ ๋ณด ์กฐํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
์ฐธ์กฐํ ์ฌ์ดํธ ์ฃผ์
์ด ํ๋ก์ ํธ๋ ๋ค์ ์คํ์์ค ํ๋ก์ ํธ๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ง๋ค์ด์ก์ต๋๋ค:
์ฌ์ ์ค์น ์ฌํญ
- Python 3.9 ์ด์
- pip ๋๋ pip3 (ํจํค์ง ๊ด๋ฆฌ์)
- httpx (๋น๋๊ธฐ HTTP ํด๋ผ์ด์ธํธ)
- MCP Python SDK
์ค์น ๋ฐฉ๋ฒ
- ์ ์ฅ์๋ฅผ ํด๋ก ํฉ๋๋ค:
git clone https://github.com/yourusername/hello-mcp-server-python.git
cd hello-mcp-server-python
- ํ์ํ ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค:
pip install httpx "mcp[cli]"
- ์๋ฒ ์คํ: (Cursor๋ Claude for Desktop์ ํตํด ์ฐ๊ฒฐํ๋ ๊ฒฝ์ฐ ์ด ๋จ๊ณ๋ ์ ํ ์ฌํญ)
python server.py
# ๋๋
mcp run server.py
Cursor์ Claude for Desktop์ ์ฐ๊ฒฐ ๋ฐฉ๋ฒ
Cursor์ ์ฐ๊ฒฐํ๊ธฐ
- Cursor IDE๋ฅผ ์ด๊ณ ํ๋ก์ ํธ๋ฅผ ๋ก๋ํฉ๋๋ค.
- Cursor ํ๋ก์ ํธ ๋ฃจํธ์
mcp.json
ํ์ผ์ ์์ฑํ๊ณ ๋ค์ ๋ด์ฉ์ ์ถ๊ฐํฉ๋๋ค:
{
"mcpServers": {
"mcp-server-python": {
"command": "mcp",
"args": [
"run",
"/_____PATH_OF_FILE_____/server.py"
]
}
}
}
- Cursor์์ Claude์ ์ฑํ ํ ๋ MCP ๋๊ตฌ๊ฐ ์๋์ผ๋ก ์ธ์๋ฉ๋๋ค. ์๋ฒ๋ mcp.json ์ค์ ์ ๋ฐ๋ผ ์๋์ผ๋ก ์คํ๋ฉ๋๋ค.
Claude for Desktop์ ์ฐ๊ฒฐํ๊ธฐ
- Claude for Desktop์ ์ค์นํฉ๋๋ค.
- ํฐ๋ฏธ๋์์ ์๋ฒ๋ฅผ ์คํํฉ๋๋ค:
python server.py
- Claude for Desktop ์ค์ ์์ MCP ์๋ฒ ์น์ ์ผ๋ก ์ด๋ํ์ฌ ๋ก์ปฌ MCP ์๋ฒ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- ์๋ฒ URL ๋๋ ํฌํธ๋ฅผ ํ์์ ๋ฐ๋ผ ๊ตฌ์ฑํฉ๋๋ค.
- Claude์ ๋ํํ๋ฉด BMI ๊ณ์ฐ๊ณผ ๋ ์จ ์กฐํ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ๋ก์ ํธ ๊ตฌ์กฐ
hello-mcp-server-python/
โโโ server.py # MCP ์๋ฒ ๋ฉ์ธ ํ์ผ
โโโ requirements.txt # ์์กด์ฑ ํจํค์ง ๋ชฉ๋ก (์ ํ ์ฌํญ)
๊ธฐ๋ฅ
ํ์ฌ ์๋ฒ๋ ๋ ๊ฐ์ง ๋๊ตฌ๋ฅผ ์ ๊ณตํฉ๋๋ค:
calculate_bmi
: ํค์ ๋ชธ๋ฌด๊ฒ๋ฅผ ์ ๋ ฅ๋ฐ์ BMI๋ฅผ ๊ณ์ฐํฉ๋๋ค.fetch_weather
: ๋์ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ํด๋น ๋์์ ๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค. (API ํค ์ค์ ํ์)
๋ ์จ API ์ค์
๋ ์จ API๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ API ํค๊ฐ ํ์ํฉ๋๋ค. server.py
ํ์ผ์ ์ด๊ณ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ์ธ์:
@mcp.tool()
async def fetch_weather(city: str) -> str:
"""Fetch current weather for a city"""
API_KEY = "your_api_key_here" # ์ฌ๊ธฐ์ API ํค๋ฅผ ์
๋ ฅํ์ธ์
async with httpx.AsyncClient() as client:
response = await client.get(f"https://api.weather.com/{city}?apiKey={API_KEY}")
return response.text
๋ผ์ด์ผ์ค
์ด ํ๋ก์ ํธ๋ MIT ๋ผ์ด์ผ์ค ํ์ ๋ฐฐํฌ๋ฉ๋๋ค. ์์ธํ ๋ด์ฉ์ LICENSE ํ์ผ์ ์ฐธ์กฐํ์ธ์.