关于 Mcp Server Python
Mcp Server Python 是供开发者学习如何向 Cursor AI 等 MCP 环境提供工具的 Python 示例项目,包含加法工具和从环境变量读取 API_KEY 的工具。
主要功能
- 01Python MCP 服务示例
- 02计算两个数之和
- 03读取环境变量 API_KEY
- 04Cursor AI 配置
- 05本地服务测试
适用场景
- 01学习基础 MCP 工具定义
- 02在 Cursor Composer 中测试工具调用
- 03验证环境变量读取
MCP 相关
通过标准输入输出实现 MCP 服务,提供数字相加和读取 API_KEY 环境变量两个工具。附带的配置可让 Cursor AI 发现并启动该服务。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
# MCP Server in Python
## Overview
**MCP (Model Context Protocol)** is a framework that allows you to integrate custom tools into AI-assisted development environments—such as Cursor AI. MCP servers expose functionality (like data retrieval or code analysis) so that an LLM-based IDE can call these tools on demand.
This project demonstrates an MCP server built in Python that provides two basic tools. One tool, **add**, accepts two numbers and returns their sum, while the other, **getApiKey**, retrieves the API key from the environment (via the `API_KEY` variable). Learn more about MCP in the [Model Context Protocol Introduction](https://modelcontextprotocol.io/introduction).
## Requirements
- **Python:** A modern version of Python (3.8 or higher is recommended).
- **Virtual Environment:** It is recommended to use a virtual environment for dependency management.
## Features
- **MCP Integration:** Exposes tool functionality to LLM-based IDEs.
- **Addition Tool:** Accepts two numeric parameters and returns their sum.
- **Env Var Retrieval:** Demonstrates how to load an example environment variable from the configuration file.
- **CLI Support:** Provides a command-line interface via `mcp[cli]` for easy local development and testing.
- **Standard I/O Transport:** Integrates with development environments using standard I/O for seamless communication.
## Installation
1. **Clone the Repository**
```bash
git clone <repository_url>
cd <repository_directory>
```
-
设置虚拟环境
创建并激活 Python 虚拟环境:
python -m venv .venv在 Linux 和 macOS 上:
source .venv/bin/activate在 Windows 上:
.venv\Scripts\activate -
安装依赖项
使用以下命令安装 MCP 包及其 CLI 扩展:
pip install mcp mcp[cli]
在本地测试服务
要在本地运行 MCP 服务,请执行:
mcp dev mcp-server.py
启动服务后,在浏览器中打开提供的 URL。点击 Connect,然后点击 List Templates,选择一个模板并输入你的姓名,最后点击 Read Resources 以验证输出。你也可以点击 Tools,选择一个工具,输入所需的值,然后点击 Run Tool 来测试工具功能。
与 Cursor AI 集成
本项目包含一个 mcp.json 配置文件,可让 Cursor AI 自动发现并启动你的 MCP 服务。打开或创建 mcp.json 文件,并使用以下结构:
{
"mcpServers": {
"MCP Server": {
"command": "/path/to/mcp-server-python/.venv/bin/python3",
"args": ["/path/to/mcp-server-python/mcp-server.py"],
"env": {
"API_KEY": "abc-1234567890"
}
}
}
}
-
mcpServers:
一个将服务名称映射到其配置的对象。 -
MCP Server:
这是你的服务配置对应的键。你可以自行命名。 -
command:
指定虚拟环境中 Python 可执行文件的绝对路径。例如:/home/john/mcp-server-python/.venv/bin/python3 -
args:
一个包含 MCP 服务文件绝对路径的数组。例如:["/home/john/mcp-server-python/mcp-server.py"] -
env:(可选)
定义 MCP 服务进程的环境变量。在此示例中,API_KEY设置为"abc-1234567890"。请根据你的环境调整此值。你可以在终端中运行
which python,以验证 Python 可执行文件的绝对路径。
可选:配置自动化脚本
通过自动更新 mcp.json 文件并填入正确的绝对路径,轻松配置本地环境。要应用本地设置,请在项目根目录运行以下命令:
chmod +x ./scripts/update_config.sh
./scripts/update_config.sh
此脚本会将 mcp.json 中的占位路径替换为 Python 和服务脚本在你计算机上的绝对路径,确保配置设置始终准确无误。
可选:Cursor 全局设置
你也可以将 mcp.json 文件移至位于 ~/.cursor 的 Cursor AI 全局配置目录,以便全局使用该配置。
在 Cursor Composer 中使用 MCP 工具
将 MCP 服务集成到 Cursor AI,并在 Cursor Composer 中启用 Agent 模式后,只需使用如下自然语言提示词:
add 3 and 5
或
what is my API key?
AI Agent 会根据上下文推断 MCP 服务提供的 add 或 getApiKey 工具,并相应地执行。
代码概览
本项目包含以下关键部分:
-
MCP 服务初始化:
使用 MCP 库实例化 MCP 服务,并通过标准 I/O 传输方式连接。 -
工具定义:
- add:
定义为接收两个数值输入并返回它们的和。 - getApiKey:
从环境变量API_KEY中获取 API 密钥并返回。
- add:
什么是 MCP?
Model Context Protocol(MCP) 提供了一种标准化方式,可将自定义工具集成到 AI 辅助开发环境中。借助 MCP,你可以定义执行特定任务的工具,例如获取外部数据、验证代码或强制执行编码规范;IDE 中的 AI 助手可以根据上下文自动调用这些工具。这有助于提高开发者的工作效率、确保质量一致,并简化工作流。
参考资料与来源
许可证
本项目根据 MIT License 授权。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。