Spaces:
Build error
MCP Proxy Manager
This module provides a manager class for handling FastMCP proxy instances in OpenHands, including initialization, configuration, and mounting to FastAPI applications.
Overview
The MCPProxyManager
class encapsulates all the functionality related to creating, configuring, and managing FastMCP proxy instances. It simplifies the process of:
- Initializing a FastMCP proxy
- Configuring the proxy with tools
- Mounting the proxy to a FastAPI application
- Updating the proxy configuration
- Shutting down the proxy
Usage
Basic Usage
from openhands.runtime.mcp.proxy import MCPProxyManager
from fastapi import FastAPI
# Create a FastAPI app
app = FastAPI()
# Create a proxy manager
proxy_manager = MCPProxyManager(
name="MyProxyServer",
auth_enabled=True,
api_key="my-api-key"
)
# Initialize the proxy
proxy_manager.initialize()
# Mount the proxy to the app
await proxy_manager.mount_to_app(app, allow_origins=["*"])
# Update the tools configuration
tools = [
{
"name": "my_tool",
"description": "My tool description",
"parameters": {...}
}
]
proxy_manager.update_tools(tools)
# Update and remount the proxy
await proxy_manager.update_and_remount(app, tools, allow_origins=["*"])
# Shutdown the proxy
await proxy_manager.shutdown()
In-Memory Configuration
The MCPProxyManager
maintains the configuration in-memory, eliminating the need for file-based configuration. This makes it easier to update the configuration and reduces the complexity of the code.
Benefits
- Simplified API: The
MCPProxyManager
provides a simple and intuitive API for managing FastMCP proxies. - In-Memory Configuration: Configuration is maintained in-memory, eliminating the need for file I/O operations.
- Improved Error Handling: The manager provides better error handling and logging for proxy operations.
- Cleaner Code: By encapsulating proxy-related functionality in a dedicated class, the code is more maintainable and easier to understand.
Implementation Details
The MCPProxyManager
uses the FastMCP.as_proxy()
method to create a proxy server. It manages the lifecycle of the proxy, including initialization, configuration updates, and shutdown.
When updating the tools configuration, the manager creates a new proxy with the updated configuration and remounts it to the FastAPI application, ensuring that the proxy is always up-to-date with the latest configuration.