模型上下文协议实现

一个实现模型上下文协议(MCP)的开源项目,提供标准化接口连接AI系统与外部工具、服务和数据源,支持文件读取、函数执行和上下文管理。

  • by SaeedAngiz1
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 SaeedAngiz1/MCP-Local-Remote-Client

使用说明

模型上下文协议(MCP)项目

模型上下文协议(MCP)的全面实现,MCP是连接人工智能系统,特别是大型语言模型(LLM)与外部工具、服务和数据源的开放标准。

📋 目录

🎯 什么是MCP?

模型上下文协议(MCP)是Anthropic引入的一个开放标准,它规范了人工智能系统与外部工具和数据源的集成。它提供:

  • 通用接口:用于读取文件、执行函数和处理上下文提示
  • 客户端-服务器体系结构:实现安全的双向通信
  • 工具集成:将LLM连接到数据库、API、文件系统等
  • 上下文管理:为AI模型提供上下文的结构化方法

📁 项目结构

mcp-project/
├── README.md                 # This file
├── requirements.txt          # Python dependencies
├── .gitignore               # Git ignore rules
├── config/
│   └── mcp_config.json      # MCP server configuration
├── src/
│   ├── __init__.py
│   ├── mcp_server.py        # Main MCP server implementation
│   ├── tools/               # Custom MCP tools
│   │   ├── __init__.py
│   │   ├── file_tools.py   # File system operations
│   │   ├── api_tools.py    # API integration tools
│   │   └── data_tools.py   # Data processing tools
│   └── utils/
│       ├── __init__.py
│       └── helpers.py      # Utility functions
├── examples/
│   ├── basic_server.py     # Basic MCP server example
│   ├── client_example.py   # MCP client example
│   └── custom_tools.py     # Custom tools example
├── tests/
│   ├── __init__.py
│   └── test_mcp_server.py  # Unit tests
└── docs/
    ├── ARCHITECTURE.md     # Architecture documentation
    └── TUTORIAL.md         # Detailed tutorial

🚀 分步设置指南

💡 Windows CMD用户:参见 SETUP_CMD.md 获取包含所有命令的完整CMD特定指南。

第一步:先决条件

确保已安装以下内容:

  • Python 3.9或更高版本
  • pip(Python包管理器)
  • Git

步骤2:克隆或初始化存储库

Windows CMD:

mkdir mcp-project
cd mcp-project
git init

macOS/Linux:

mkdir mcp-project
cd mcp-project
git init

步骤3:创建虚拟环境

Windows CMD:

python -m venv venv
venv\Scripts\activate.bat

Windows PowerShell:

python -m venv venv
venv\Scripts\Activate.ps1

macOS/Linux:

python3 -m venv venv
source venv/bin/activate

步骤4:安装依赖项

Windows CMD/PowerShell:

pip install -r requirements.txt

macOS/Linux:

pip install -r requirements.txt

步骤5:安装MCP SDK

# Install the official MCP Python SDK
pip install mcp

# Or install from source
pip install git+https://github.com/modelcontextprotocol/python-sdk.git

步骤6:配置MCP服务器

  1. 编辑 config/mcp_config.json 配置服务器设置
  2. 定义您的工具和资源
  3. 如果需要,设置身份验证

步骤7:运行示例服务器

Windows CMD:

REM Run the basic example
python examples\basic_server.py

REM Or run the full server
python src\mcp_server.py

macOS/Linux:

# Run the basic example
python examples/basic_server.py

# Or run the full server
python src/mcp_server.py

步骤8:测试实现

Windows CMD:

REM Run tests
python -m pytest tests\

REM Or run manually
python examples\client_example.py

macOS/Linux:

# Run tests
pytest tests/

# Or run manually
python examples/client_example.py

✨ 特性

  • ✅ MCP服务器实现:功能齐全的MCP服务器,支持工具
  • ✅ 自定义工具:文件操作、API调用和数据处理示例
  • ✅ 客户端集成:连接到MCP服务器的示例客户端
  • ✅ 配置管理:基于JSON的配置系统
  • ✅ 错误处理:强大的错误处理和日志记录
  • ✅ 类型安全:在整个代码库中键入提示
  • ✅ 测试:核心功能的单元测试

📖 用法

启动MCP服务器

from src.mcp_server import MCPServer

# Initialize server
server = MCPServer(config_path="config/mcp_config.json")

# Start server
server.start()

使用MCP工具

from src.tools.file_tools import FileTools

# Initialize file tools
file_tools = FileTools()

# Read a file
content = file_tools.read_file("path/to/file.txt")

# Write to a file
file_tools.write_file("path/to/file.txt", "content")

客户端连接

from src.mcp_client import MCPClient

# Connect to MCP server
client = MCPClient(server_url="http://localhost:8000")

# Call a tool
result = client.call_tool("read_file", {"path": "example.txt"})

🎓 例子

请参阅 examples/ 目录:

  • basic_server.py:最低限度的MCP服务器实现
  • 客户端示例.py:如何连接和使用MCP服务器
  • custom_tools.py:创建自定义MCP工具

🏗️ 建筑

MCP遵循客户端-服务器架构:

┌─────────────┐         ┌─────────────┐         ┌─────────────┐
│   LLM App   │◄───────►│ MCP Client  │◄───────►│ MCP Server  │
│  (Claude)   │         │             │         │             │
└─────────────┘         └─────────────┘         └──────┬──────┘
                                                        │
                                                ┌────────┴────────┐
                                                │                 │
                                         ┌──────▼──────┐  ┌──────▼──────┐
                                         │   Tools     │  │  Resources  │
                                         │  (Actions)  │  │   (Data)    │
                                         └─────────────┘  └─────────────┘

关键部件:

  1. MCP服务器:为客户提供工具和资源
  2. MCP客户端:连接到服务器并发出请求
  3. 工具:可执行函数(例如,read_file、call_api)
  4. 资源:数据源(如文件、数据库)

🔧 发展

添加自定义工具

  1. 在中创建新文件 src/tools/
  2. 按照MCP工具界面执行工具功能
  3. 在中注册工具 mcp_server.py

例子:

from mcp import Tool

@Tool(name="my_custom_tool", description="Does something custom")
def my_custom_tool(param: str) -> str:
    # Your implementation
    return result

运行测试

# Run all tests
pytest

# Run with coverage
pytest --cov=src tests/

🔌 集成

该项目包括流行工具和服务的全面集成指南:

  • 集成.md -完整的集成指南:

    • LM工作室
    • Docker 桌面版
    • Obsidian(本地REST API和Marcel Marais的服务器)
    • Logseq
    • 光标IDE
    • 克劳德·索内特
    • 云服务器设置
  • CURSOR_LM_STUDIO_SETUP.md -以下内容的快速设置指南:

    • 将LM Studio与游标连接
    • 测试连接
    • 在Cursor中使用LM Studio工具
    • 通过iPhone A-Shell和Tailscale进行远程访问
    • 常见问题排查
  • 远程访问.md -远程访问指南:

    • 从iPhone/Android访问LM Studio
    • 尾秤设置和配置
    • A-Shell(iOS)和Termux(Android)的使用
    • SSH隧道方法
    • 安全最佳实践

📚 资源

🤝 贡献

欢迎投稿!请随时提交拉取请求。

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/AmazingFeature)
  3. 提交您的更改(git commit -m 'Add some AmazingFeature')
  4. 推到分支(git push origin feature/AmazingFeature)
  5. 打开拉取请求

📝 许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

🙏 致谢

  • 创建模型上下文协议的拟人化
  • MCP社区的贡献和支持

由以下材料制成❤️ 对于MCP社区