AI开发工具集成框架

MCP(Model Context Protocol)是一个框架,允许将自定义工具集成到AI辅助开发环境(如Cursor AI)中,通过标准I/O传输暴露功能供基于LLM的IDE调用。

  • by lucianoayres
  • Shell
  • 本地部署
  • 更新于
在 GitHub 查看 lucianoayres/mcp-server-python

使用说明

# 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>
   ```
  1. 设置虚拟环境

    创建并激活Python虚拟环境:

    python -m venv .venv
    

    在Linux和macOS上:

    source .venv/bin/activate
    

    在Windows上:

    .venv\Scripts\activate
    
  2. 再进行

    使用以下工具安装MCP包及其CLI扩展:

    pip install mcp mcp[cli]
    

在本地测试服务器

要在本地运行MCP服务器,请执行:

mcp dev mcp-server.py

启动服务器后,在浏览器中打开提供的URL。点击 连接,然后单击 列表模板,选择一个并添加您的姓名,最后单击 阅读资源 以验证输出。您也可以点击 工具,选择一个,输入所需的值,然后单击 运行工具 测试工具功能。

与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服务器:
    这是服务器配置的关键。你可以随心所欲地命名它。

  • 命令:
    指定从虚拟环境到Python可执行文件的绝对路径。例如:

    /home/john/mcp-server-python/.venv/bin/python3
    
  • args:
    包含MCP服务器文件绝对路径的数组。例如:

    ["/home/john/mcp-server-python/mcp-server.py"]
    
  • 环境: (可选)
    为MCP服务器进程定义环境变量。在这个例子中 API_KEY 设置为 "abc-1234567890"。根据您的环境需要调整此值。

    您可以通过运行以下命令来验证Python可执行文件的绝对路径 which python 在你的终端。

可选:配置自动化脚本

通过使用正确的绝对路径自动更新mcp.json文件,可以轻松配置本地环境。要应用本地设置,请从项目根运行以下命令:

chmod +x ./scripts/update_config.sh
./scripts/update_config.sh

此脚本将mcp.json中的占位符路径替换为Python和服务器脚本的机器绝对路径,确保您的配置设置始终准确。

可选:全局光标设置

您还可以移动 mcp.json 将文件保存到位于以下位置的全局Cursor AI配置目录 ~/.cursor 以使配置在全球范围内可用。

在游标编辑器中使用MCP工具

将MCP服务器集成到Cursor AI中,并在Cursor Composer中启用代理模式,只需使用自然语言提示,如:

add 3 and 5

或

what is my API key?

AI代理将推断可用 add 或 getApiKey 从您的MCP服务器下载工具并相应地执行它。

代码概述

该项目包括以下关键部分:

  • MCP服务器初始化:
    MCP服务器使用MCP库实例化,并通过标准I/O传输连接。

  • 工具定义:

    • 添加:
      定义为接受两个数字输入并返回它们的总和。
    • getApiKey:
      从环境变量中检索API键 API_KEY 并将其返回。

什么是MCP?

模型上下文协议(MCP) 提供了一种标准化的方法,将自定义工具集成到人工智能辅助开发环境中。使用MCP,您可以定义执行特定任务的工具,例如检索外部数据、验证代码或执行编码标准,IDE中的AI助手可以根据上下文自动调用这些工具。这有助于提高开发人员的生产力,确保一致的质量,并简化工作流程。

参考和来源

许可证

该项目根据 MIT许可证.