Discover Awesome MCP Servers
Extend your agent with 28,614 capabilities via MCP servers.
- All28,614
- Developer Tools3,867
- Search1,714
- Research & Data1,557
- AI Integration Systems229
- Cloud Platforms219
- Data & App Analysis181
- Database Interaction177
- Remote Shell Execution165
- Browser Automation147
- Databases145
- Communication137
- AI Content Generation127
- OS Automation120
- Programming Docs Access109
- Content Fetching108
- Note Taking97
- File Systems96
- Version Control93
- Finance91
- Knowledge & Memory90
- Monitoring79
- Security71
- Image & Video Processing69
- Digital Note Management66
- AI Memory Systems62
- Advanced AI Reasoning59
- Git Management Tools58
- Cloud Storage51
- Entertainment & Media43
- Virtualization42
- Location Services35
- Web Automation & Stealth32
- Media Content Processing32
- Calendar Management26
- Ecommerce & Retail18
- Speech Processing18
- Customer Data Platforms16
- Travel & Transportation14
- Education & Learning Tools13
- Home Automation & IoT13
- Web Search Integration12
- Health & Wellness10
- Customer Support10
- Marketing9
- Games & Gamification8
- Google Cloud Integrations7
- Art & Culture4
- Language Translation3
- Legal & Compliance2
Knowledge Graph Memory Server
一个改进的持久性记忆实现,使用本地知识图谱,并提供可自定义的 `--memory-path` 参数。这使得 Claude 能够在多次对话中记住关于用户的信息。
NEXUS Memory MCP App
A sovereign, six-layer permanent memory system that provides users with a structured and persistent personal knowledge base across VS Code, Claude, and ChatGPT. It utilizes a neural mesh architecture and ENGRAM O(1) lookup to ensure data ownership and constant-time memory retrieval.
TermPipe MCP
Provides AI assistants with direct terminal access to execute commands, manage files, and run persistent REPL sessions. It features automated installation scripts that educate AI assistants on its capabilities for seamless integration.
ffmpeg-mcp
Enables comprehensive video and audio processing using FFmpeg, supporting tasks like metadata extraction, clipping, scaling, and adding transitions or overlays. It provides a high-performance interface for building media processing microservices via FastMCP.
MCP Servers
一系列作为 dotnet 工具的 MCP (模型上下文协议) 服务器
HashiCorp Vault MCP Server
Enables interaction with HashiCorp Vault for secret management operations including reading, writing, listing, and deleting secrets through the Model Context Protocol.
LiblibAI Picture Generator
Enables AI image generation through LiblibAI API with natural language prompts. Supports various art styles, real-time progress tracking, and account credit management.
Google Search MCP Server
一个集成了谷歌自定义搜索 JSON API 的 MCP 服务器实现,提供网页搜索功能。
Pytest MCP Server
Enables AI assistants to run and analyze pytest tests for desktop applications through interactive commands. Supports test execution, filtering, result analysis, and debugging for comprehensive test automation workflows.
Security MCP Server
Enables security scanning of codebases through integrated tools for secret detection, SCA, SAST, and DAST vulnerabilities, with AI-powered remediation suggestions based on findings.
play-sound-mcp-server
An MCP server that plays local sound files on macOS using the afplay command, allowing AI assistants to trigger audio notifications after responding.
BigQuery FinOps MCP Server
Enables cost optimization and financial operations for Google BigQuery through natural language interactions. Provides insights into BigQuery spending, usage patterns, and cost management recommendations.
JSON MCP Server
Provides powerful JSON manipulation tools through Model Context Protocol, enabling complex queries, schema generation, and validation with jq notation and native Node.js operations.
Owlvin MCP Server
Enables AI agents to access a wide range of AI tools and services through the Owlvin platform with a single integration. It manages authentication and billing automatically, allowing users to list available services, check credit balances, and execute specialized API calls.
Mail MCP Server
An MCP server that provides email sending capabilities via SMTP, featuring tools for sending standard and template-based emails. It utilizes the FastMCP Streamable HTTP transport for flexible client connectivity over HTTP without requiring stdio subprocesses.
Screen Agent
A Windows desktop automation MCP server that enables UI recognition through OCR, UIA controls, and multi-point color matching. It allows agents to interact with desktop applications via actions like clicking and typing while using a learning system to track and improve operation success.
browser-mcp
一个 MCP 服务器,允许 AI 助手与浏览器交互,包括获取页面内容为 Markdown 格式、修改页面样式以及搜索浏览器历史记录。
sliverc2-mcp
sliverc2-mcp
flutterclimcp
好的,这是一个使用 Flutter CLI 和 MCP (Model Context Protocol) 服务器创建 Flutter 项目的有趣示例项目: **项目名称:** 猜数字游戏 (Guess the Number Game) **项目描述:** 这是一个简单的猜数字游戏,用户需要猜一个由计算机随机生成的数字。游戏会提供反馈,告诉用户猜的数字是太高还是太低,直到用户猜对为止。我们将使用 MCP 服务器来处理游戏逻辑,而 Flutter 应用将负责用户界面和与服务器的通信。 **技术栈:** * **Flutter:** 用于构建用户界面。 * **Flutter CLI:** 用于创建和管理 Flutter 项目。 * **MCP Server (Python):** 用于处理游戏逻辑,例如生成随机数、比较猜测和提供反馈。 * **HTTP:** 用于 Flutter 应用和 MCP 服务器之间的通信。 **步骤:** 1. **设置 MCP 服务器 (Python):** ```python from http.server import BaseHTTPRequestHandler, HTTPServer import json import random class RequestHandler(BaseHTTPRequestHandler): def do_POST(self): if self.path == '/guess': content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) data = json.loads(post_data.decode('utf-8')) guess = data.get('guess') if not hasattr(self.server, 'secret_number'): self.server.secret_number = random.randint(1, 100) self.server.num_guesses = 0 self.server.num_guesses += 1 if guess is None: response = {'error': 'Missing guess parameter'} elif guess < self.server.secret_number: response = {'result': 'too_low'} elif guess > self.server.secret_number: response = {'result': 'too_high'} else: response = {'result': 'correct', 'guesses': self.server.num_guesses} del self.server.secret_number # Reset for next game del self.server.num_guesses self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(response).encode('utf-8')) else: self.send_response(404) self.end_headers() def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000): server_address = ('', port) httpd = server_class(server_address, handler_class) print(f'Starting server on port {port}') httpd.serve_forever() if __name__ == '__main__': run() ``` * 将此代码保存为 `mcp_server.py`。 * 运行服务器:`python mcp_server.py` 2. **创建 Flutter 项目:** ```bash flutter create guess_the_number cd guess_the_number ``` 3. **修改 `lib/main.dart`:** ```dart import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Guess the Number', theme: ThemeData( primarySwatch: Colors.blue, ), home: GuessTheNumberPage(), ); } } class GuessTheNumberPage extends StatefulWidget { @override _GuessTheNumberPageState createState() => _GuessTheNumberPageState(); } class _GuessTheNumberPageState extends State<GuessTheNumberPage> { final TextEditingController _guessController = TextEditingController(); String _message = ''; bool _gameOver = false; Future<void> _checkGuess(String guess) async { try { final int? parsedGuess = int.tryParse(guess); if (parsedGuess == null) { setState(() { _message = 'Please enter a valid number.'; }); return; } final response = await http.post( Uri.parse('http://localhost:8000/guess'), // Replace with your server address headers: {'Content-Type': 'application/json'}, body: jsonEncode({'guess': parsedGuess}), ); if (response.statusCode == 200) { final data = jsonDecode(response.body); final result = data['result']; setState(() { if (result == 'too_low') { _message = 'Too low! Try again.'; } else if (result == 'too_high') { _message = 'Too high! Try again.'; } else if (result == 'correct') { _message = 'Congratulations! You guessed the number in ${data['guesses']} tries.'; _gameOver = true; } else if (data.containsKey('error')) { _message = 'Error: ${data['error']}'; } else { _message = 'Unexpected response from server.'; } }); } else { setState(() { _message = 'Failed to connect to the server. Status code: ${response.statusCode}'; }); } } catch (e) { setState(() { _message = 'An error occurred: $e'; }); } } void _resetGame() { setState(() { _message = ''; _guessController.clear(); _gameOver = false; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Guess the Number'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'I\'m thinking of a number between 1 and 100.', style: TextStyle(fontSize: 16), ), SizedBox(height: 20), TextField( controller: _guessController, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Enter your guess', border: OutlineInputBorder(), ), enabled: !_gameOver, ), SizedBox(height: 20), ElevatedButton( onPressed: _gameOver ? null : () { _checkGuess(_guessController.text); }, child: Text('Guess'), ), SizedBox(height: 20), Text( _message, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), textAlign: TextAlign.center, ), if (_gameOver) ElevatedButton( onPressed: _resetGame, child: Text('Play Again'), ), ], ), ), ); } } ``` 4. **添加 `http` 依赖:** ```bash flutter pub add http ``` 5. **运行 Flutter 应用:** ```bash flutter run ``` **解释:** * **MCP 服务器 (Python):** * 监听端口 8000 上的 HTTP POST 请求。 * 当收到 `/guess` 请求时,它会解析 JSON 数据,提取用户的猜测。 * 如果这是第一次猜测,它会生成一个 1 到 100 之间的随机数。 * 它会将用户的猜测与秘密数字进行比较,并返回 `too_low`、`too_high` 或 `correct`。 * 如果猜测正确,它会返回猜测次数并重置游戏。 * **Flutter 应用:** * 包含一个文本字段,用户可以在其中输入他们的猜测。 * 包含一个按钮,用户可以点击该按钮来提交他们的猜测。 * 使用 `http` 包向 MCP 服务器发送 POST 请求,并将用户的猜测作为 JSON 数据发送。 * 解析服务器的响应,并更新 UI 以显示反馈(太高、太低、正确)。 * 如果用户猜对了,它会显示一条祝贺消息和猜测次数。 * 包含一个“再玩一次”按钮,可以重置游戏。 **如何运行:** 1. 首先,运行 `mcp_server.py`。 2. 然后,运行 Flutter 应用。 3. 在 Flutter 应用中,输入你的猜测并点击“猜测”按钮。 4. 查看应用中的反馈,并继续猜测直到你猜对为止。 **改进:** * **错误处理:** 添加更健壮的错误处理,例如处理服务器连接错误。 * **UI 改进:** 改进 UI 以使其更具吸引力。 * **难度级别:** 添加难度级别,允许用户选择数字范围。 * **历史记录:** 显示用户的猜测历史记录。 * **使用更复杂的 MCP 协议:** 虽然这个例子使用简单的 HTTP,但你可以探索更复杂的 MCP 协议,例如 gRPC 或 Thrift,以获得更好的性能和类型安全。 这个示例展示了如何使用 Flutter CLI 和 MCP 服务器创建一个简单的 Flutter 项目。 你可以根据自己的需要修改和扩展此项目。 关键在于将 UI 逻辑与业务逻辑分离,并使用 MCP 服务器来处理业务逻辑。 **中文翻译:** 好的,这是一个使用 Flutter CLI 和 MCP (模型上下文协议) 服务器创建一个 Flutter 项目的有趣示例项目: **项目名称:** 猜数字游戏 (Guess the Number Game) **项目描述:** 这是一个简单的猜数字游戏,用户需要猜一个由计算机随机生成的数字。游戏会提供反馈,告诉用户猜的数字是太高还是太低,直到用户猜对为止。我们将使用 MCP 服务器来处理游戏逻辑,而 Flutter 应用将负责用户界面和与服务器的通信。 **技术栈:** * **Flutter:** 用于构建用户界面。 * **Flutter CLI:** 用于创建和管理 Flutter 项目。 * **MCP Server (Python):** 用于处理游戏逻辑,例如生成随机数、比较猜测和提供反馈。 * **HTTP:** 用于 Flutter 应用和 MCP 服务器之间的通信。 **步骤:** 1. **设置 MCP 服务器 (Python):** ```python from http.server import BaseHTTPRequestHandler, HTTPServer import json import random class RequestHandler(BaseHTTPRequestHandler): def do_POST(self): if self.path == '/guess': content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) data = json.loads(post_data.decode('utf-8')) guess = data.get('guess') if not hasattr(self.server, 'secret_number'): self.server.secret_number = random.randint(1, 100) self.server.num_guesses = 0 self.server.num_guesses += 1 if guess is None: response = {'error': 'Missing guess parameter'} elif guess < self.server.secret_number: response = {'result': 'too_low'} elif guess > self.server.secret_number: response = {'result': 'too_high'} else: response = {'result': 'correct', 'guesses': self.server.num_guesses} del self.server.secret_number # Reset for next game del self.server.num_guesses self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(response).encode('utf-8')) else: self.send_response(404) self.end_headers() def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000): server_address = ('', port) httpd = server_class(server_address, handler_class) print(f'Starting server on port {port}') httpd.serve_forever() if __name__ == '__main__': run() ``` * 将此代码保存为 `mcp_server.py`。 * 运行服务器:`python mcp_server.py` 2. **创建 Flutter 项目:** ```bash flutter create guess_the_number cd guess_the_number ``` 3. **修改 `lib/main.dart`:** ```dart import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Guess the Number', theme: ThemeData( primarySwatch: Colors.blue, ), home: GuessTheNumberPage(), ); } } class GuessTheNumberPage extends StatefulWidget { @override _GuessTheNumberPageState createState() => _GuessTheNumberPageState(); } class _GuessTheNumberPageState extends State<GuessTheNumberPage> { final TextEditingController _guessController = TextEditingController(); String _message = ''; bool _gameOver = false; Future<void> _checkGuess(String guess) async { try { final int? parsedGuess = int.tryParse(guess); if (parsedGuess == null) { setState(() { _message = 'Please enter a valid number.'; }); return; } final response = await http.post( Uri.parse('http://localhost:8000/guess'), // Replace with your server address headers: {'Content-Type': 'application/json'}, body: jsonEncode({'guess': parsedGuess}), ); if (response.statusCode == 200) { final data = jsonDecode(response.body); final result = data['result']; setState(() { if (result == 'too_low') { _message = 'Too low! Try again.'; } else if (result == 'too_high') { _message = 'Too high! Try again.'; } else if (result == 'correct') { _message = 'Congratulations! You guessed the number in ${data['guesses']} tries.'; _gameOver = true; } else if (data.containsKey('error')) { _message = 'Error: ${data['error']}'; } else { _message = 'Unexpected response from server.'; } }); } else { setState(() { _message = 'Failed to connect to the server. Status code: ${response.statusCode}'; }); } } catch (e) { setState(() { _message = 'An error occurred: $e'; }); } } void _resetGame() { setState(() { _message = ''; _guessController.clear(); _gameOver = false; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Guess the Number'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'I\'m thinking of a number between 1 and 100.', style: TextStyle(fontSize: 16), ), SizedBox(height: 20), TextField( controller: _guessController, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Enter your guess', border: OutlineInputBorder(), ), enabled: !_gameOver, ), SizedBox(height: 20), ElevatedButton( onPressed: _gameOver ? null : () { _checkGuess(_guessController.text); }, child: Text('Guess'), ), SizedBox(height: 20), Text( _message, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), textAlign: TextAlign.center, ), if (_gameOver) ElevatedButton( onPressed: _resetGame, child: Text('Play Again'), ), ], ), ), ); } } ``` 4. **添加 `http` 依赖:** ```bash flutter pub add http ``` 5. **运行 Flutter 应用:** ```bash flutter run ``` **解释:** * **MCP 服务器 (Python):** * 监听端口 8000 上的 HTTP POST 请求。 * 当收到 `/guess` 请求时,它会解析 JSON 数据,提取用户的猜测。 * 如果这是第一次猜测,它会生成一个 1 到 100 之间的随机数。 * 它会将用户的猜测与秘密数字进行比较,并返回 `too_low`、`too_high` 或 `correct`。 * 如果猜测正确,它会返回猜测次数并重置游戏。 * **Flutter 应用:** * 包含一个文本字段,用户可以在其中输入他们的猜测。 * 包含一个按钮,用户可以点击该按钮来提交他们的猜测。 * 使用 `http` 包向 MCP 服务器发送 POST 请求,并将用户的猜测作为 JSON 数据发送。 * 解析服务器的响应,并更新 UI 以显示反馈(太高、太低、正确)。 * 如果用户猜对了,它会显示一条祝贺消息和猜测次数。 * 包含一个“再玩一次”按钮,可以重置游戏。 **如何运行:** 1. 首先,运行 `mcp_server.py`。 2. 然后,运行 Flutter 应用。 3. 在 Flutter 应用中,输入你的猜测并点击“猜测”按钮。 4. 查看应用中的反馈,并继续猜测直到你猜对为止。 **改进:** * **错误处理:** 添加更健壮的错误处理,例如处理服务器连接错误。 * **UI 改进:** 改进 UI 以使其更具吸引力。 * **难度级别:** 添加难度级别,允许用户选择数字范围。 * **历史记录:** 显示用户的猜测历史记录。 * **使用更复杂的 MCP 协议:** 虽然这个例子使用简单的 HTTP,但你可以探索更复杂的 MCP 协议,例如 gRPC 或 Thrift,以获得更好的性能和类型安全。 这个示例展示了如何使用 Flutter CLI 和 MCP 服务器创建一个简单的 Flutter 项目。 你可以根据自己的需要修改和扩展此项目。 关键在于将 UI 逻辑与业务逻辑分离,并使用 MCP 服务器来处理业务逻辑。 This provides a complete, runnable example with explanations and improvements. Remember to replace `http://localhost:8000/guess` with the actual address of your MCP server if it's running on a different machine or port. Good luck!
Trello MCP Server
Enables AI assistants to retrieve Trello card information by ID or link, providing access to card details including labels, members, due dates, and attachments through a standardized interface.
mcp-lucene-server
mcp-lucene-server
Pollinations Multimodal MCP Server
A Model Context Protocol server that enables AI assistants like Claude to generate images, text, and audio directly through Pollinations APIs using a lightweight stdio transport design.
Python Code Runner
Enables execution of Python code in a safe environment, including running scripts, installing packages, and retrieving variable values. Supports file operations and package management through pip.
icalPal MCP Server
Enables AI assistants to interact with macOS Calendar and Reminders applications. Allows querying events, tasks, calendars, and accounts through natural language using the icalPal Ruby gem.
MCP Servers
用于科学研究的开源 MCP 服务器 (Yòng yú kēxué yánjiū de kāiyuán MCP fúwùqì) Alternative translations, depending on the specific nuance you want to convey: * **用于科学研究的开源多通道板服务器** (Yòng yú kēxué yánjiū de kāiyuán duō tōngdào bǎn fúwùqì) - This is more specific, translating MCP as "Multi-Channel Plate" (多通道板). Useful if the audience is familiar with the acronym. * **开源的用于科学研究的 MCP 服务器** (Kāiyuán de yòng yú kēxué yánjiū de MCP fúwùqì) - This emphasizes the "open source" aspect. The first translation is the most general and likely the best starting point. Choose the others if you need to be more specific.
AgentOS
AgentOS provides a suite of essential services for AI agents including persistent key-value memory, format conversion, and activity logging. It offers advanced tools for task state management and cost control to streamline and manage complex agent workflows.
DevUtils MCP Server
Provides essential developer tools for workspace management, including advanced file searching, project structure analysis, and batch code editing. It enables users to efficiently navigate, analyze, and modify source code within their development environment.
Agentic Tools MCP Server
A Model Context Protocol server providing AI assistants with comprehensive project, task, and subtask management capabilities with project-specific storage.
Amazon Marketplace MCP Server by CData
This read-only MCP Server allows you to connect to Amazon Marketplace data from Claude Desktop through CData JDBC Drivers. Free (beta) read/write servers available at https://www.cdata.com/solutions/mcp
Filesystem MCP
安全地修改 MCP 服务器根目录下的文件,同时防止用户逃逸到根目录之外: 这里提供一些建议,结合了安全性和实用性: **核心原则:最小权限原则 (Principle of Least Privilege)** * **用户权限限制:** 这是最重要的一点。 运行 MCP 服务器的用户账户(例如,一个专门创建的 `mcp_user` 账户)**绝对不能**拥有 root 权限。 它应该只拥有修改根目录下特定文件和目录的权限。 * **chroot 环境 (Chrooted Environment):** 将 MCP 服务器限制在一个 chroot 环境中。 Chroot 会创建一个虚拟的根目录,让服务器认为它位于文件系统的顶层,即使它实际上位于更深的位置。 这可以防止用户访问 chroot 环境之外的文件。 * **输入验证和清理:** 对所有用户输入进行严格的验证和清理。 这可以防止命令注入攻击,攻击者可能会利用这些漏洞来执行任意代码。 * **输出编码:** 对所有输出进行编码,以防止跨站脚本攻击 (XSS)。 * **定期更新:** 保持 MCP 服务器软件和操作系统更新到最新版本,以修复已知的安全漏洞。 **具体实现方法:** 1. **创建用户和组:** ```bash sudo groupadd mcp_group sudo useradd -g mcp_group -d /path/to/mcp/root -s /bin/bash mcp_user sudo passwd mcp_user # 设置密码 ``` * `/path/to/mcp/root` 是你希望作为 MCP 服务器根目录的实际路径。 例如,`/opt/mcp_server`。 * `-s /bin/bash` 允许用户使用 bash shell。 如果不需要 shell 访问,可以设置为 `/bin/false` 或 `/usr/sbin/nologin`。 2. **设置目录权限:** ```bash sudo chown -R mcp_user:mcp_group /path/to/mcp/root sudo chmod -R 770 /path/to/mcp/root # 允许用户和组读写执行 ``` * 根据需要调整权限。 例如,如果用户只需要读取某些文件,则可以将其权限设置为 `440`。 * **重要:** 确保 `mcp_user` 拥有修改所需文件的权限,但不要授予不必要的权限。 3. **Chroot 环境 (推荐):** * **创建 chroot 环境:** 这需要一些手动操作,因为你需要复制所有必要的库和程序到 chroot 目录中。 可以使用 `debootstrap` (Debian/Ubuntu) 或 `yum install yum-utils; yumdownloader --resolve --destdir=/path/to/chroot/lib <program>` (CentOS/RHEL) 来简化这个过程。 ```bash # 示例 (Debian/Ubuntu) sudo apt-get install debootstrap sudo mkdir /path/to/chroot sudo debootstrap --arch amd64 stable /path/to/chroot http://httpredir.debian.org/debian ``` * `stable` 可以替换为 `testing` 或 `unstable`,但 `stable` 最安全。 * `amd64` 替换为你的架构 (例如 `i386`, `arm64`)。 * **重要:** 你需要复制 MCP 服务器依赖的所有库和程序到 `/path/to/chroot` 中。 这可能包括 `libc.so.6`, `libpthread.so.0`, 等等。 使用 `ldd <mcp_server_executable>` 来查看依赖项。 * **使用 `chroot` 命令:** 在启动 MCP 服务器之前,使用 `chroot` 命令将其限制在 chroot 环境中。 ```bash sudo chroot /path/to/chroot /path/to/mcp_server_executable ``` * **更高级的 chroot 管理工具:** 考虑使用 `jailkit` 或 `docker` 等工具来更轻松地管理 chroot 环境。 Docker 提供了一种更隔离和可移植的解决方案。 4. **输入验证和清理:** * **白名单:** 使用白名单来限制允许的输入。 例如,如果用户只能修改某些特定的文件,则只允许这些文件的名称作为输入。 * **正则表达式:** 使用正则表达式来验证输入的格式。 例如,如果用户必须输入一个数字,则可以使用正则表达式来确保输入只包含数字。 * **转义:** 转义所有特殊字符,以防止命令注入攻击。 例如,在 bash 中,可以使用 `\` 来转义特殊字符。 5. **输出编码:** * **HTML 编码:** 如果 MCP 服务器生成 HTML 输出,则对所有输出进行 HTML 编码,以防止 XSS 攻击。 * **URL 编码:** 如果 MCP 服务器生成 URL,则对所有 URL 进行 URL 编码。 6. **日志记录:** * 记录所有用户操作,以便进行审计和故障排除。 * 监控日志文件,以检测可疑活动。 7. **防火墙:** * 使用防火墙来限制对 MCP 服务器的访问。 只允许来自受信任的 IP 地址的连接。 **示例代码 (Python):** ```python import os import subprocess import re def modify_file(filename, content): """ 安全地修改文件。 Args: filename: 要修改的文件名 (相对于根目录). content: 要写入文件的内容. """ # 1. 白名单验证文件名 allowed_files = ["config.txt", "data.json"] # 允许修改的文件 if filename not in allowed_files: raise ValueError("Invalid filename") # 2. 构造完整路径 (相对于根目录) filepath = os.path.join("/path/to/mcp/root", filename) # 3. 检查文件是否存在 if not os.path.exists(filepath): raise FileNotFoundError("File not found") # 4. 输入验证 (例如,限制内容长度) if len(content) > 1024: raise ValueError("Content too long") # 5. 使用 subprocess 安全地写入文件 try: with open(filepath, "w") as f: f.write(content) except Exception as e: raise Exception(f"Error writing to file: {e}") # 示例用法 try: modify_file("config.txt", "new config value") print("File modified successfully") except ValueError as e: print(f"Error: {e}") except FileNotFoundError as e: print(f"Error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") ``` **重要注意事项:** * **测试:** 在生产环境中部署之前,彻底测试所有安全措施。 * **安全审计:** 定期进行安全审计,以识别和修复潜在的安全漏洞。 * **具体情况:** 以上建议只是一些通用的指导原则。 你需要根据你的具体情况进行调整。 * **复杂性:** 构建一个完全安全的系统非常复杂。 如果你不确定如何操作,请咨询安全专家。 **总结:** 通过结合最小权限原则、chroot 环境、输入验证和清理、输出编码、日志记录和防火墙,你可以大大提高 MCP 服务器的安全性,防止用户逃逸到根目录之外,并安全地修改文件。 记住,安全是一个持续的过程,需要不断地监控和改进。 --- **中文翻译:** 安全地修改 MCP 服务器根目录下的文件,同时防止用户逃逸到根目录之外: 这里提供一些建议,结合了安全性和实用性: **核心原则:最小权限原则 (Principle of Least Privilege)** * **用户权限限制:** 这是最重要的一点。运行 MCP 服务器的用户账户(例如,一个专门创建的 `mcp_user` 账户)**绝对不能**拥有 root 权限。它应该只拥有修改根目录下特定文件和目录的权限。 * **chroot 环境 (Chrooted Environment):** 将 MCP 服务器限制在一个 chroot 环境中。Chroot 会创建一个虚拟的根目录,让服务器认为它位于文件系统的顶层,即使它实际上位于更深的位置。这可以防止用户访问 chroot 环境之外的文件。 * **输入验证和清理:** 对所有用户输入进行严格的验证和清理。这可以防止命令注入攻击,攻击者可能会利用这些漏洞来执行任意代码。 * **输出编码:** 对所有输出进行编码,以防止跨站脚本攻击 (XSS)。 * **定期更新:** 保持 MCP 服务器软件和操作系统更新到最新版本,以修复已知的安全漏洞。 **具体实现方法:** 1. **创建用户和组:** ```bash sudo groupadd mcp_group sudo useradd -g mcp_group -d /path/to/mcp/root -s /bin/bash mcp_user sudo passwd mcp_user # 设置密码 ``` * `/path/to/mcp/root` 是你希望作为 MCP 服务器根目录的实际路径。例如,`/opt/mcp_server`。 * `-s /bin/bash` 允许用户使用 bash shell。如果不需要 shell 访问,可以设置为 `/bin/false` 或 `/usr/sbin/nologin`。 2. **设置目录权限:** ```bash sudo chown -R mcp_user:mcp_group /path/to/mcp/root sudo chmod -R 770 /path/to/mcp/root # 允许用户和组读写执行 ``` * 根据需要调整权限。例如,如果用户只需要读取某些文件,则可以将其权限设置为 `440`。 * **重要:** 确保 `mcp_user` 拥有修改所需文件的权限,但不要授予不必要的权限。 3. **Chroot 环境 (推荐):** * **创建 chroot 环境:** 这需要一些手动操作,因为你需要复制所有必要的库和程序到 chroot 目录中。可以使用 `debootstrap` (Debian/Ubuntu) 或 `yum install yum-utils; yumdownloader --resolve --destdir=/path/to/chroot/lib <program>` (CentOS/RHEL) 来简化这个过程。 ```bash # 示例 (Debian/Ubuntu) sudo apt-get install debootstrap sudo mkdir /path/to/chroot sudo debootstrap --arch amd64 stable /path/to/chroot http://httpredir.debian.org/debian ``` * `stable` 可以替换为 `testing` 或 `unstable`,但 `stable` 最安全。 * `amd64` 替换为你的架构 (例如 `i386`, `arm64`)。 * **重要:** 你需要复制 MCP 服务器依赖的所有库和程序到 `/path/to/chroot` 中。这可能包括 `libc.so.6`, `libpthread.so.0`, 等等。使用 `ldd <mcp_server_executable>` 来查看依赖项。 * **使用 `chroot` 命令:** 在启动 MCP 服务器之前,使用 `chroot` 命令将其限制在 chroot 环境中。 ```bash sudo chroot /path/to/chroot /path/to/mcp_server_executable ``` * **更高级的 chroot 管理工具:** 考虑使用 `jailkit` 或 `docker` 等工具来更轻松地管理 chroot 环境。Docker 提供了一种更隔离和可移植的解决方案。 4. **输入验证和清理:** * **白名单:** 使用白名单来限制允许的输入。例如,如果用户只能修改某些特定的文件,则只允许这些文件的名称作为输入。 * **正则表达式:** 使用正则表达式来验证输入的格式。例如,如果用户必须输入一个数字,则可以使用正则表达式来确保输入只包含数字。 * **转义:** 转义所有特殊字符,以防止命令注入攻击。例如,在 bash 中,可以使用 `\` 来转义特殊字符。 5. **输出编码:** * **HTML 编码:** 如果 MCP 服务器生成 HTML 输出,则对所有输出进行 HTML 编码,以防止 XSS 攻击。 * **URL 编码:** 如果 MCP 服务器生成 URL,则对所有 URL 进行 URL 编码。 6. **日志记录:** * 记录所有用户操作,以便进行审计和故障排除。 * 监控日志文件,以检测可疑活动。 7. **防火墙:** * 使用防火墙来限制对 MCP 服务器的访问。只允许来自受信任的 IP 地址的连接。 **示例代码 (Python):** ```python import os import subprocess import re def modify_file(filename, content): """ 安全地修改文件。 Args: filename: 要修改的文件名 (相对于根目录). content: 要写入文件的内容. """ # 1. 白名单验证文件名 allowed_files = ["config.txt", "data.json"] # 允许修改的文件 if filename not in allowed_files: raise ValueError("Invalid filename") # 2. 构造完整路径 (相对于根目录) filepath = os.path.join("/path/to/mcp/root", filename) # 3. 检查文件是否存在 if not os.path.exists(filepath): raise FileNotFoundError("File not found") # 4. 输入验证 (例如,限制内容长度) if len(content) > 1024: raise ValueError("Content too long") # 5. 使用 subprocess 安全地写入文件 try: with open(filepath, "w") as f: f.write(content) except Exception as e: raise Exception(f"Error writing to file: {e}") # 示例用法 try: modify_file("config.txt", "new config value") print("File modified successfully") except ValueError as e: print(f"Error: {e}") except FileNotFoundError as e: print(f"Error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") ``` **重要注意事项:** * **测试:** 在生产环境中部署之前,彻底测试所有安全措施。 * **安全审计:** 定期进行安全审计,以识别和修复潜在的安全漏洞。 * **具体情况:** 以上建议只是一些通用的指导原则。你需要根据你的具体情况进行调整。 * **复杂性:** 构建一个完全安全的系统非常复杂。如果你不确定如何操作,请咨询安全专家。 **总结:** 通过结合最小权限原则、chroot 环境、输入验证和清理、输出编码、日志记录和防火墙,你可以大大提高 MCP 服务器的安全性,防止用户逃逸到根目录之外,并安全地修改文件。记住,安全是一个持续的过程,需要不断地监控和改进。