MCP Client Example ☀️
Okay, here's a basic example of a Python client and server using the `mcp` library (assuming you meant the Minecraft Protocol, and you're looking for a simplified example, not a full Minecraft server implementation). This example focuses on establishing a connection and sending/receiving simple data. **Important Considerations:** * **`mcp` Library:** There isn't a standard Python library called `mcp`. If you're referring to the Minecraft Protocol, you'll likely need to use a library like `mcstatus` or `nbt` for more complex interactions. This example uses standard sockets for a simplified demonstration. * **Minecraft Protocol Complexity:** The actual Minecraft protocol is *very* complex. This example is a *highly* simplified illustration and won't actually connect to a real Minecraft server or handle the full protocol. * **Error Handling:** This example lacks robust error handling for brevity. In a real application, you'd need to handle exceptions (e.g., connection refused, socket errors, timeouts) properly. * **Security:** This example is for demonstration purposes only and is not secure. Do not use it in a production environment without proper security measures. **Simplified Example (Sockets):** **Server (server.py):** ```python import socket HOST = '127.0.0.1' # Standard loopback interface address (localhost) PORT = 65432 # Port to listen on (non-privileged ports are > 1023) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() print(f"Server listening on {HOST}:{PORT}") conn, addr = s.accept() with conn: print(f"Connected by {addr}") while True: data = conn.recv(1024) if not data: break decoded_data = data.decode('utf-8') print(f"Received: {decoded_data}") response = f"Server received: {decoded_data}".encode('utf-8') conn.sendall(response) ``` **Client (client.py):** ```python import socket HOST = '127.0.0.1' # The server's hostname or IP address PORT = 65432 # The port used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) message = "Hello, Server!" s.sendall(message.encode('utf-8')) data = s.recv(1024) print(f"Received: {data.decode('utf-8')}") ``` **How to Run:** 1. **Save:** Save the code as `server.py` and `client.py`. 2. **Run the Server:** Open a terminal or command prompt and run `python server.py`. The server will start listening for connections. 3. **Run the Client:** Open another terminal or command prompt and run `python client.py`. The client will connect to the server, send a message, and receive a response. **Explanation:** * **Server:** * Creates a socket, binds it to an address and port, and listens for incoming connections. * `s.accept()` blocks until a client connects. * `conn.recv(1024)` receives data from the client (up to 1024 bytes at a time). * `conn.sendall()` sends data back to the client. * **Client:** * Creates a socket and connects to the server's address and port. * `s.sendall()` sends data to the server. * `s.recv(1024)` receives data from the server. * `decode('utf-8')` converts the received bytes to a string. * `encode('utf-8')` converts the string to bytes for sending. **To connect to a real Minecraft server (which is much more complex):** You'll need a library that handles the Minecraft protocol. Here's a basic example using `mcstatus`: ```python from mcstatus import JavaServer server = JavaServer.lookup("example.com:25565") # Replace with your server address status = server.status() print(f"The server has {status.players.online} players online") ``` **Chinese Translation of the Simplified Example's Comments:** **Server (server.py):** ```python import socket HOST = '127.0.0.1' # 标准的回环接口地址 (localhost) - Standard loopback interface address (localhost) PORT = 65432 # 监听的端口 (非特权端口大于 1023) - Port to listen on (non-privileged ports are > 1023) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() print(f"服务器监听在 {HOST}:{PORT}") # Server listening on {HOST}:{PORT} conn, addr = s.accept() with conn: print(f"连接来自 {addr}") # Connected by {addr} while True: data = conn.recv(1024) if not data: break decoded_data = data.decode('utf-8') print(f"接收到: {decoded_data}") # Received: {decoded_data} response = f"服务器接收到: {decoded_data}".encode('utf-8') # Server received: {decoded_data} conn.sendall(response) ``` **Client (client.py):** ```python import socket HOST = '127.0.0.1' # 服务器的主机名或 IP 地址 - The server's hostname or IP address PORT = 65432 # 服务器使用的端口 - The port used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) message = "你好,服务器!" # Hello, Server! s.sendall(message.encode('utf-8')) data = s.recv(1024) print(f"接收到: {data.decode('utf-8')}") # Received: {data.decode('utf-8')} ``` **Chinese Translation of the `mcstatus` example:** ```python from mcstatus import JavaServer server = JavaServer.lookup("example.com:25565") # 替换为你的服务器地址 - Replace with your server address status = server.status() print(f"服务器有 {status.players.online} 个玩家在线") # The server has {status.players.online} players online ``` **Important Notes about the Chinese Translations:** * I've tried to provide accurate and natural-sounding translations. * The comments are translated to help understand the code's purpose. * Remember to replace `"example.com:25565"` with the actual address of the Minecraft server you want to connect to. This should give you a good starting point. Remember to install the `mcstatus` library if you want to use the Minecraft server status example: `pip install mcstatus`. Good luck!
RGGH
README
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。