Agent Zero AI framework
new option for mcp servers
Eric Leblondf32cc92999a9
diff --git a/python/helpers/mcp_handler.py b/python/helpers/mcp_handler.pyindex b71af7acf..990dbc2d7 100644--- a/python/helpers/mcp_handler.py+++ b/python/helpers/mcp_handler.py@@ -24,6 +24,8 @@ import jsonfrom python.helpers import errorsfrom python.helpers import settings+import httpx+from mcp import ClientSession, StdioServerParametersfrom mcp.client.stdio import stdio_clientfrom mcp.client.sse import sse_client@@ -216,6 +218,7 @@ class MCPServerRemote(BaseModel):headers: dict[str, Any] | None = Field(default_factory=dict[str, Any])init_timeout: int = Field(default=0)tool_timeout: int = Field(default=0)+ verify: bool = Field(default=True, description="Verify SSL certificates")disabled: bool = Field(default=False)__lock: ClassVar[threading.Lock] = PrivateAttr(default=threading.Lock())@@ -265,6 +268,7 @@ class MCPServerRemote(BaseModel):"init_timeout","tool_timeout","disabled",+ "verify",]:if key == "name":value = normalize_name(value)@@ -293,6 +297,7 @@ class MCPServerLocal(BaseModel):)init_timeout: int = Field(default=0)tool_timeout: int = Field(default=0)+ verify: bool = Field(default=True, description="Verify SSL certificates")disabled: bool = Field(default=False)__lock: ClassVar[threading.Lock] = PrivateAttr(default=threading.Lock())@@ -1018,6 +1023,36 @@ class MCPClientLocal(MCPClientBase):# do not read or close the file here, as stdio is asyncreturn stdio_transport+class CustomHTTPClientFactory(ABC):+ def __init__(self, verify: bool = True):+ self.verify = verify++ def __call__(+ self,+ headers: dict[str, str] | None = None,+ timeout: httpx.Timeout | None = None,+ auth: httpx.Auth | None = None,+ ) -> httpx.AsyncClient:+ # Set MCP defaults+ kwargs: dict[str, Any] = {+ "follow_redirects": True,+ }++ # Handle timeout+ if timeout is None:+ kwargs["timeout"] = httpx.Timeout(30.0)+ else:+ kwargs["timeout"] = timeout++ # Handle headers+ if headers is not None:+ kwargs["headers"] = headers++ # Handle authentication+ if auth is not None:+ kwargs["auth"] = auth++ return httpx.AsyncClient(**kwargs, verify=self.verify)class MCPClientRemote(MCPClientBase):@@ -1040,6 +1075,7 @@ class MCPClientRemote(MCPClientBase):init_timeout = min(server.init_timeout or set["mcp_client_init_timeout"], 5)tool_timeout = min(server.tool_timeout or set["mcp_client_tool_timeout"], 10)+ client_factory = CustomHTTPClientFactory(verify=server.verify)# Check if this is a streaming HTTP typeif _is_streaming_http_type(server.type):# Use streamable HTTP client@@ -1049,6 +1085,7 @@ class MCPClientRemote(MCPClientBase):headers=server.headers,timeout=timedelta(seconds=init_timeout),sse_read_timeout=timedelta(seconds=tool_timeout),+ httpx_client_factory=client_factory,))# streamablehttp_client returns (read_stream, write_stream, get_session_id_callback)@@ -1066,6 +1103,7 @@ class MCPClientRemote(MCPClientBase):headers=server.headers,timeout=init_timeout,sse_read_timeout=tool_timeout,+ httpx_client_factory=client_factory,))return stdio_transport