MCP server proxy

MCP server proxy

Okay, I understand. You want to extend the functionality of an MCP (Minecraft Protocol) server to also act as a worker in a distributed system. This means the server would not only handle Minecraft client connections but also perform tasks assigned to it by a central controller or manager. Here's a breakdown of the concepts, potential approaches, and considerations for achieving this, along with example code snippets (in Python, as it's commonly used for server-side scripting and automation): **1. Understanding the Core Concepts** * **MCP Server:** This is your existing Minecraft server (likely based on Spigot, Paper, Fabric, or a custom implementation). It handles player connections, game logic, and world management. * **Worker:** The extended MCP server will now also be a worker. A worker receives tasks from a central controller, executes those tasks, and reports the results back. * **Controller/Manager:** This is a separate application (or a module within the MCP server itself, though less ideal for scalability) that distributes tasks to the workers. It monitors worker status, assigns jobs, and collects results. * **Task:** A unit of work that the worker needs to perform. Examples: * Generating a specific region of the world. * Running a complex calculation related to game mechanics. * Performing automated actions (e.g., building structures). * Executing commands on the server. * **Communication:** The controller and workers need a way to communicate. Common options: * **Message Queue (e.g., RabbitMQ, Redis Pub/Sub):** A robust and scalable solution for asynchronous communication. The controller publishes tasks to a queue, and workers subscribe to the queue to receive tasks. * **RPC (Remote Procedure Call) (e.g., gRPC, Thrift):** Allows the controller to directly call functions on the worker. Suitable for more synchronous or request-response style interactions. * **HTTP/REST API:** The worker exposes an API that the controller can use to send tasks and retrieve results. Simple to implement but potentially less efficient than message queues or RPC. * **Database:** The controller writes tasks to a database, and the workers periodically check the database for new tasks. Less efficient for real-time task assignment. * **Serialization:** Tasks and results need to be serialized (converted to a format that can be transmitted over the network). Common options: * **JSON:** Human-readable and widely supported. * **Protocol Buffers (protobuf):** More efficient than JSON, especially for complex data structures. * **MessagePack:** Another efficient binary serialization format. **2. Architectural Approaches** * **Embedded Worker:** The worker functionality is integrated directly into the MCP server process. This is simpler to set up but can impact the server's performance if the worker tasks are resource-intensive. * **Separate Worker Process:** The worker runs in a separate process (potentially on the same machine or a different machine) and communicates with the MCP server via a plugin or API. This isolates the worker's workload and prevents it from directly affecting the server's performance. This is the generally preferred approach for production environments. **3. Implementation Steps (Example using Python and RabbitMQ)** This example outlines the steps using a separate worker process and RabbitMQ for communication. It assumes you have a basic understanding of RabbitMQ. **A. Controller (Python):** ```python import pika import json import time import uuid # RabbitMQ connection parameters RABBITMQ_HOST = 'localhost' RABBITMQ_QUEUE = 'mcp_tasks' def send_task(task_type, task_data): """Sends a task to the RabbitMQ queue.""" connection = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST)) channel = connection.channel() channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True) # Ensure queue exists task_id = str(uuid.uuid4()) # Generate a unique task ID task = { 'task_id': task_id, 'task_type': task_type, 'task_data': task_data } channel.basic_publish( exchange='', routing_key=RABBITMQ_QUEUE, body=json.dumps(task).encode('utf-8'), properties=pika.BasicProperties( delivery_mode=2, # Make message persistent ) ) print(f" [x] Sent task: {task}") connection.close() return task_id if __name__ == '__main__': # Example usage: task_id = send_task( task_type='generate_region', task_data={'x': 0, 'z': 0, 'radius': 100} ) print(f"Task ID: {task_id}") task_id = send_task( task_type='execute_command', task_data={'command': 'say Hello from the controller!'} ) print(f"Task ID: {task_id}") ``` **B. Worker (Python):** ```python import pika import json import time import subprocess # For executing Minecraft commands import os # RabbitMQ connection parameters RABBITMQ_HOST = 'localhost' RABBITMQ_QUEUE = 'mcp_tasks' # Path to the Minecraft server's command execution script (e.g., a shell script) MINECRAFT_COMMAND_SCRIPT = '/path/to/minecraft_command.sh' # Replace with your actual path def execute_minecraft_command(command): """Executes a command on the Minecraft server using a script.""" try: # Ensure the script is executable os.chmod(MINECRAFT_COMMAND_SCRIPT, 0o755) # Make executable (if needed) result = subprocess.run([MINECRAFT_COMMAND_SCRIPT, command], capture_output=True, text=True, check=True) return result.stdout.strip() except subprocess.CalledProcessError as e: print(f"Error executing command: {e}") return f"Error: {e.stderr.strip()}" except FileNotFoundError: print(f"Error: Command execution script not found at {MINECRAFT_COMMAND_SCRIPT}") return "Error: Command execution script not found." def process_task(task): """Processes a task received from the queue.""" task_type = task['task_type'] task_data = task['task_data'] task_id = task['task_id'] print(f" [x] Received task: {task}") if task_type == 'generate_region': # Simulate region generation (replace with actual logic) x = task_data['x'] z = task_data['z'] radius = task_data['radius'] print(f"Generating region at ({x}, {z}) with radius {radius}...") time.sleep(5) # Simulate work result = f"Region generated successfully at ({x}, {z}) with radius {radius}." elif task_type == 'execute_command': command = task_data['command'] print(f"Executing command: {command}") result = execute_minecraft_command(command) # Execute the command else: result = f"Unknown task type: {task_type}" print(f" [x] Task {task_id} completed. Result: {result}") return result def callback(ch, method, properties, body): """Callback function for handling messages from the queue.""" try: task = json.loads(body.decode('utf-8')) result = process_task(task) # Acknowledge the message (important for RabbitMQ) ch.basic_ack(delivery_tag=method.delivery_tag) # Optionally, send the result back to the controller (using another queue or RPC) # (Implementation omitted for brevity) except Exception as e: print(f"Error processing task: {e}") ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False) # Reject and don't requeue on error def start_worker(): """Starts the RabbitMQ worker.""" connection = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST)) channel = connection.channel() channel.queue_declare(queue=RABBITMQ_QUEUE, durable=True) # Ensure queue exists channel.basic_qos(prefetch_count=1) # Process one message at a time channel.basic_consume(queue=RABBITMQ_QUEUE, on_message_callback=callback) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming() if __name__ == '__main__': start_worker() ``` **C. Minecraft Command Execution Script (Bash - `minecraft_command.sh`):** ```bash #!/bin/bash # This script executes a command on the Minecraft server using rcon-cli. # Make sure rcon-cli is installed and configured correctly. COMMAND="$1" # Replace with your rcon-cli command and credentials rcon-cli -H localhost -p 25575 -P your_rcon_password "$COMMAND" ``` **D. Minecraft Server Plugin (Spigot/Paper Example - Java):** This plugin is necessary if you want the worker to directly interact with the Minecraft server's API. If you're only executing commands, the `minecraft_command.sh` script might be sufficient. ```java import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.Bukkit; public class WorkerPlugin extends JavaPlugin { @Override public void onEnable() { getLogger().info("WorkerPlugin has been enabled!"); // Example: Register a command that can be triggered by the worker getCommand("workercommand").setExecutor((sender, command, label, args) -> { if (args.length > 0) { String message = String.join(" ", args); Bukkit.broadcastMessage("Worker Command: " + message); return true; } else { sender.sendMessage("Usage: /workercommand <message>"); return false; } }); } @Override public void onDisable() { getLogger().info("WorkerPlugin has been disabled!"); } } ``` **Explanation:** 1. **Controller:** * Connects to RabbitMQ. * Defines a `send_task` function to publish tasks to the `mcp_tasks` queue. * Each task is a JSON object containing a `task_id`, `task_type`, and `task_data`. * The `delivery_mode=2` property ensures that messages are persistent (survive RabbitMQ restarts). * Example usage demonstrates sending two types of tasks: `generate_region` and `execute_command`. 2. **Worker:** * Connects to RabbitMQ. * Declares the `mcp_tasks` queue. * Sets `prefetch_count=1` to process one message at a time. * The `callback` function is called whenever a new message arrives. * `process_task` handles the different task types. * For `generate_region`, it simulates region generation (replace with your actual world generation logic). * For `execute_command`, it calls the `execute_minecraft_command` function. * `execute_minecraft_command` uses `subprocess.run` to execute a shell script (`minecraft_command.sh`) that uses `rcon-cli` to send commands to the Minecraft server. * `ch.basic_ack` acknowledges the message, telling RabbitMQ that the task has been successfully processed. This is crucial to prevent messages from being re-queued indefinitely if the worker crashes. * Error handling is included to catch exceptions during task processing and reject the message (with `ch.basic_nack`) to prevent infinite loops. 3. **`minecraft_command.sh`:** * A simple Bash script that takes a command as an argument and uses `rcon-cli` to execute it on the Minecraft server. * **Important:** Replace `localhost`, `25575`, and `your_rcon_password` with your actual RCON settings. * Make sure `rcon-cli` is installed and configured correctly on the worker machine. 4. **`WorkerPlugin.java` (Spigot/Paper):** * A basic Spigot/Paper plugin that demonstrates how to register a command that can be triggered by the worker. * This is only necessary if you need the worker to directly interact with the Minecraft server's API (e.g., to modify blocks, spawn entities, etc.). * The example registers a command `/workercommand` that broadcasts a message to all players. **To Run the Example:** 1. **Install RabbitMQ:** Follow the instructions on the RabbitMQ website to install and configure RabbitMQ on your system. 2. **Install `rcon-cli`:** Install `rcon-cli` on the worker machine (if you're using the `execute_command` task). You might need to install it using your system's package manager (e.g., `apt-get install rcon-cli` on Debian/Ubuntu). 3. **Configure RCON:** Enable RCON on your Minecraft server and set a password. 4. **Update Paths:** In the `worker.py` script, update the `MINECRAFT_COMMAND_SCRIPT` variable to point to the correct path of your `minecraft_command.sh` script. Also, update the RCON credentials in `minecraft_command.sh`. 5. **Install Python Libraries:** Install the required Python libraries: `pip install pika`. 6. **Run the Controller:** `python controller.py` 7. **Run the Worker:** `python worker.py` 8. **Start your Minecraft server.** 9. **Test:** The controller will send tasks to the worker, and the worker will execute them. You should see the results in the worker's console and in the Minecraft server's console (or in-game if you're using the plugin). **Important Considerations:** * **Security:** Secure your RabbitMQ connection with proper authentication and authorization. Never expose your RCON password directly in your code. Use environment variables or a configuration file to store sensitive information. * **Error Handling:** Implement robust error handling in both the controller and the worker. Use try-except blocks to catch exceptions and handle them gracefully. Consider using a logging library to log errors and other important events. * **Scalability:** For high-volume task processing, consider using multiple worker instances. RabbitMQ will automatically distribute tasks to available workers. * **Task Prioritization:** If you have tasks with different priorities, you can use RabbitMQ's priority queues to ensure that high-priority tasks are processed first. * **Task Results:** The example doesn't include a mechanism for sending results back to the controller. You can implement this using another RabbitMQ queue, RPC, or a database. * **Minecraft Server API:** If you need to interact with the Minecraft server's API, you'll need to use a plugin (like the example `WorkerPlugin.java`). The plugin can expose functions that the worker can call (e.g., using a custom protocol or a REST API). * **Resource Management:** Monitor the resource usage of the worker processes to ensure that they don't overload the system. Consider using resource limits (e.g., CPU and memory limits) to prevent workers from consuming too many resources. * **Idempotency:** Design your tasks to be idempotent, meaning that they can be executed multiple times without causing unintended side effects. This is important in case a task fails and needs to be retried. * **Concurrency:** If your tasks are CPU-bound, consider using multiple threads or processes within the worker to improve performance. However, be careful to avoid race conditions and other concurrency issues. * **Configuration:** Use a configuration file to store settings such as RabbitMQ connection parameters, RCON credentials, and task-specific parameters. This makes it easier to manage and deploy your system. * **Monitoring:** Implement monitoring to track the status of the controller, workers, and RabbitMQ. Use metrics such as task queue length, task processing time, and error rates to identify potential problems. **Choosing a Communication Method:** * **RabbitMQ:** Best for asynchronous, reliable task processing. Good for scalability and fault tolerance. Requires setting up a RabbitMQ server. * **gRPC:** Good for synchronous or request-response style interactions. Provides strong typing and efficient serialization. Requires defining gRPC services and messages. * **HTTP/REST:** Simple to implement but potentially less efficient than RabbitMQ or gRPC. Suitable for simple tasks and when you don't need high performance. **Example Task Types:** * **`generate_region`:** Generates a specific region of the world. Task data: `x`, `z`, `radius`, `world`. * **`execute_command`:** Executes a command on the server. Task data: `command`. * **`build_structure`:** Builds a predefined structure at a specific location. Task data: `x`, `y`, `z`, `structure_name`. * **`calculate_stats`:** Calculates statistics about the server (e.g., number of players, number of entities, memory usage). Task data: None. This comprehensive guide should give you a solid foundation for extending your MCP server to work as a worker. Remember to adapt the code and architecture to your specific needs and requirements. Good luck!

12beam

Developer Tools
Visit Server

README

MCP 服务器代理

使用 proxyMessage 将消息传递给现有的 MCP 服务器:

import { proxyMessage } from '@contextdepot/mcp-proxy/dist/index.js'

// 创建服务器并配置处理程序
const server = new Server(..);
..

export default class extends WorkerEntrypoint {
    // 将消息发送到服务器
    async message(requestMessage): Promise<void> {
        return proxyMessage(server, requestMessage)
    }
};

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.

Featured
Local
JavaScript
Claude Code MCP

Claude Code MCP

An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.

Featured
Local
JavaScript
MCP Package Docs Server

MCP Package Docs Server

Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.

Featured
Local
TypeScript
Linear MCP Server

Linear MCP Server

A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Featured
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.

Featured
Python
mermaid-mcp-server

mermaid-mcp-server

A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.

Featured
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP server to provide Jira Tickets information to AI coding agents like Cursor

Featured
TypeScript
Linear MCP Server

Linear MCP Server

Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.

Featured
JavaScript