MCP服务器Python
英语
使用模型上下文协议的Python服务器示例项目。
目录
概述
这个项目是一个服务器示例,它使用模型上下文协议(MCP)为Claude AI等LLM提供工具。它提供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
# or
mcp run server.py连接到Cursor和Claude for Desktop
连接到游标
- 打开Cursor IDE并加载项目。
- 创建一个
mcp.json在项目根目录中创建文件,并添加以下内容:
{
"mcpServers": {
"mcp-server-python": {
"command": "mcp",
"args": [
"run",
"/_____PATH_OF_FILE_____/server.py"
]
}
}
}- 在Cursor中与Claude聊天时,MCP工具将被自动识别。服务器将根据mcp.json设置自动运行。
连接到Claude for Desktop
- 安装桌面版的Claude。
- 在终端中运行服务器:
python server.py- 转到Claude中的MCP服务器部分进行桌面设置,并添加本地MCP服务器。
- 根据需要配置服务器URL或端口。
- 与克劳德聊天时,您可以使用BMI计算和天气查找工具。
项目结构
hello-mcp-server-python/
├── server.py # MCP server main file
└── requirements.txt # Dependency package list (optional)特性
服务器目前提供两个工具:
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" # 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
安装方法
- 克隆存储库:
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桌面
- 安装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文件。
