MCP服务器工具

一个基于Model Context Protocol的Python服务器示例项目,提供BMI计算和天气查询功能,适用于LLM工具集成。

  • by jongkwang
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 jongkwang/hello-mcp-server-python

使用说明

MCP服务器Python

语言: 英语 | 韩语

英语

使用模型上下文协议的Python服务器示例项目。

目录

概述

这个项目是一个服务器示例,它使用模型上下文协议(MCP)为Claude AI等LLM提供工具。它提供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
# or
mcp run server.py

连接到Cursor和Claude for Desktop

连接到游标

  1. 打开Cursor IDE并加载项目。
  2. 创建一个 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。
  2. 在终端中运行服务器:
python server.py
  1. 转到Claude中的MCP服务器部分进行桌面设置,并添加本地MCP服务器。
  2. 根据需要配置服务器URL或端口。
  3. 与克劳德聊天时,您可以使用BMI计算和天气查找工具。

项目结构

hello-mcp-server-python/
├── server.py       # MCP server main file
└── requirements.txt  # Dependency package list (optional)

特性

服务器目前提供两个工具:

  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"  # 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

许可证

该项目在MIT许可证下分发。有关详细信息,请参阅LICENSE文件。

参考文献


韩语

使用Model Context Protocol的Python服务器示例项目。

目录

概述

此项目是使用Model Context Protocol(MCP)为LLM(如Claude AI)提供工具功能的服务器示例。提供BMI计算和天气信息查询功能。

参考的网址

此项目是参考以下开源项目创建的:

预装事项

  • Python3.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桌面

  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文件。

参考文档