hello-mcp-server-python

jongkwang/hello-mcp-server-python

3.1

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

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

  1. Clone the repository:
git clone https://github.com/yourusername/hello-mcp-server-python.git
cd hello-mcp-server-python
  1. Install the required packages:
pip install httpx "mcp[cli]"
  1. 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
  1. Open Cursor IDE and load the project.
  2. 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"
                ]
      }
  }
}
  1. 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
  1. Install Claude for Desktop.
  2. Run the server in the terminal:
python server.py
  1. Go to the MCP server section in Claude for Desktop settings and add a local MCP server.
  2. Configure the server URL or port as needed.
  3. 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:

  1. calculate_bmi: Calculates BMI from height and weight inputs.
  2. 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์„ ํ™œ์šฉํ•œ Python ์„œ๋ฒ„ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค.

๋ชฉ์ฐจ

๊ฐœ์š”

์ด ํ”„๋กœ์ ํŠธ๋Š” Model Context Protocol(MCP)์„ ์‚ฌ์šฉํ•˜์—ฌ Claude AI์™€ ๊ฐ™์€ LLM์— ๋„๊ตฌ(Tool) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฒ„ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. BMI ๊ณ„์‚ฐ๊ณผ ๋‚ ์”จ ์ •๋ณด ์กฐํšŒ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ฐธ์กฐํ•œ ์‚ฌ์ดํŠธ ์ฃผ์†Œ

์ด ํ”„๋กœ์ ํŠธ๋Š” ๋‹ค์Œ ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค:

์‚ฌ์ „ ์„ค์น˜ ์‚ฌํ•ญ

  • Python 3.9 ์ด์ƒ
  • pip ๋˜๋Š” pip3 (ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž)
  • httpx (๋น„๋™๊ธฐ HTTP ํด๋ผ์ด์–ธํŠธ)
  • MCP Python SDK

์„ค์น˜ ๋ฐฉ๋ฒ•

  1. ์ €์žฅ์†Œ๋ฅผ ํด๋ก ํ•ฉ๋‹ˆ๋‹ค:
git clone https://github.com/yourusername/hello-mcp-server-python.git
cd hello-mcp-server-python
  1. ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค:
pip install httpx "mcp[cli]"
  1. ์„œ๋ฒ„ ์‹คํ–‰: (Cursor๋‚˜ Claude for Desktop์„ ํ†ตํ•ด ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋‹จ๊ณ„๋Š” ์„ ํƒ ์‚ฌํ•ญ)
python server.py
# ๋˜๋Š”
mcp run server.py

Cursor์™€ Claude for Desktop์— ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•

Cursor์— ์—ฐ๊ฒฐํ•˜๊ธฐ
  1. Cursor IDE๋ฅผ ์—ด๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
  2. Cursor ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— mcp.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค:
{
  "mcpServers": {
      "mcp-server-python": {
            "command": "mcp",
            "args": [
                "run",
                "/_____PATH_OF_FILE_____/server.py"
                ]
      }
  }
}
  1. Cursor์—์„œ Claude์™€ ์ฑ„ํŒ…ํ•  ๋•Œ MCP ๋„๊ตฌ๊ฐ€ ์ž๋™์œผ๋กœ ์ธ์‹๋ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„๋Š” mcp.json ์„ค์ •์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
Claude for Desktop์— ์—ฐ๊ฒฐํ•˜๊ธฐ
  1. Claude for Desktop์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
  2. ํ„ฐ๋ฏธ๋„์—์„œ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:
python server.py
  1. Claude for Desktop ์„ค์ •์—์„œ MCP ์„œ๋ฒ„ ์„น์…˜์œผ๋กœ ์ด๋™ํ•˜์—ฌ ๋กœ์ปฌ MCP ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  2. ์„œ๋ฒ„ URL ๋˜๋Š” ํฌํŠธ๋ฅผ ํ•„์š”์— ๋”ฐ๋ผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. Claude์™€ ๋Œ€ํ™”ํ•˜๋ฉด BMI ๊ณ„์‚ฐ๊ณผ ๋‚ ์”จ ์กฐํšŒ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

hello-mcp-server-python/
โ”œโ”€โ”€ server.py       # MCP ์„œ๋ฒ„ ๋ฉ”์ธ ํŒŒ์ผ
โ””โ”€โ”€ requirements.txt  # ์˜์กด์„ฑ ํŒจํ‚ค์ง€ ๋ชฉ๋ก (์„ ํƒ ์‚ฌํ•ญ)

๊ธฐ๋Šฅ

ํ˜„์žฌ ์„œ๋ฒ„๋Š” ๋‘ ๊ฐ€์ง€ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

  1. calculate_bmi: ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ BMI๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  2. 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 ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์ฐธ๊ณ ๋ฌธ์„œ