chore(mcp.server): remove irregular environment patch

refactor(mcp.server): prevent browser-use from affecting mcp stdio communication
This commit is contained in:
Sheng Fan 2025-03-20 13:25:56 +08:00
parent d4358ef537
commit 08a20f6880
3 changed files with 14 additions and 26 deletions

0
app/__main__.py Normal file
View File

View File

@ -1,30 +1,19 @@
import logging
import sys
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stderr)])
import argparse
import asyncio
import atexit
import json
import logging
import os
import sys
from inspect import Parameter, Signature
from typing import Any, Dict, Optional
from mcp.server.fastmcp import FastMCP
# Add directories to Python path (needed for proper importing)
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
root_dir = os.path.dirname(parent_dir)
sys.path.insert(0, parent_dir)
sys.path.insert(0, current_dir)
sys.path.insert(0, root_dir)
# Configure logging (using the same format as original)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("mcp-server")
from app.logger import logger
from app.tool.base import BaseTool
from app.tool.bash import Bash
from app.tool.browser_use_tool import BrowserUseTool
@ -45,11 +34,6 @@ class MCPServer:
self.tools["editor"] = StrReplaceEditor()
self.tools["terminate"] = Terminate()
from app.logger import logger as app_logger
global logger
logger = app_logger
def register_tool(self, tool: BaseTool, method_name: Optional[str] = None) -> None:
"""Register a tool with parameter validation and documentation."""
tool_name = method_name or tool.name

View File

@ -13,10 +13,14 @@ class MCPRunner:
def __init__(self):
self.root_path = config.root_path
self.server_script = self.root_path / "app" / "mcp" / "server.py"
self.server_reference = "app.mcp.server"
self.agent = MCPAgent()
async def initialize(self, connection_type: str, server_url: str = None) -> None:
async def initialize(
self,
connection_type: str,
server_url: str | None = None,
) -> None:
"""Initialize the MCP agent with the appropriate connection."""
logger.info(f"Initializing MCPAgent with {connection_type} connection...")
@ -24,7 +28,7 @@ class MCPRunner:
await self.agent.initialize(
connection_type="stdio",
command=sys.executable,
args=[str(self.server_script)],
args=["-m", self.server_reference],
)
else: # sse
await self.agent.initialize(connection_type="sse", server_url=server_url)