From 7e28ec0f229061ad7602d06cb0d0742b201fa7d8 Mon Sep 17 00:00:00 2001 From: Ke Sun Date: Wed, 24 Dec 2025 11:12:45 +0800 Subject: [PATCH] Refactor MCP server initialization and enhance Docker compatibility - Simplified FastMCP initialization by removing unnecessary allowed_hosts parameter. - Added logging for MCP server startup details. - Implemented DNS rebinding protection configuration to support Docker container hostnames. --- api/app/core/memory/agent/mcp_server/mcp_instance.py | 5 +---- api/app/core/memory/agent/mcp_server/server.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/api/app/core/memory/agent/mcp_server/mcp_instance.py b/api/app/core/memory/agent/mcp_server/mcp_instance.py index c072a438..3a2eeb78 100644 --- a/api/app/core/memory/agent/mcp_server/mcp_instance.py +++ b/api/app/core/memory/agent/mcp_server/mcp_instance.py @@ -8,7 +8,4 @@ from mcp.server.fastmcp import FastMCP # Initialize FastMCP server instance # This instance is shared across all tool modules -mcp = FastMCP( - 'data_flow', - allowed_hosts=["mcp-server", "localhost"] -) \ No newline at end of file +mcp = FastMCP('data_flow') diff --git a/api/app/core/memory/agent/mcp_server/server.py b/api/app/core/memory/agent/mcp_server/server.py index 18ea911f..f87ed529 100644 --- a/api/app/core/memory/agent/mcp_server/server.py +++ b/api/app/core/memory/agent/mcp_server/server.py @@ -147,6 +147,18 @@ def main(): # Get MCP port from environment (default: 8081) mcp_port = int(os.getenv("MCP_PORT", "8081")) + logger.info(f"Starting MCP server on {settings.SERVER_IP}:{mcp_port} with SSE transport") + + # Configure DNS rebinding protection for Docker container compatibility + from mcp.server.fastmcp.server import TransportSecuritySettings + + # Disable DNS rebinding protection to allow Docker container hostnames + # This allows containers to connect using service names like 'mcp-server' + mcp.settings.transport_security = TransportSecuritySettings( + enable_dns_rebinding_protection=False, + ) + logger.info("DNS rebinding protection: disabled for Docker container compatibility") + # logger.info(f"Starting MCP server on {settings.SERVER_IP}:{mcp_port} with SSE transport") # Run the server with SSE transport for HTTP connections