mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-26 15:24:48 +08:00
fix(config): Add support for MCP server configuration parameters (#812)
* fix(config): Add support for MCP server configuration parameters * refact: rename the sse_readtimeout to sse_read_timeout * update the code with review comments * update the MCP document for the latest change
This commit is contained in:
@@ -1046,13 +1046,16 @@ async def mcp_server_metadata(request: MCPServerMetadataRequest):
|
||||
)
|
||||
|
||||
try:
|
||||
# Set default timeout with a longer value for this endpoint
|
||||
timeout = 300 # Default to 300 seconds for this endpoint
|
||||
# Set default timeout for this endpoint (configurable via env)
|
||||
timeout = get_int_env("MCP_DEFAULT_TIMEOUT_SECONDS", 60)
|
||||
|
||||
# Use custom timeout from request if provided
|
||||
if request.timeout_seconds is not None:
|
||||
timeout = request.timeout_seconds
|
||||
|
||||
# Get sse_read_timeout from request if provided
|
||||
sse_read_timeout = request.sse_read_timeout
|
||||
|
||||
# Load tools from the MCP server using the utility function
|
||||
tools = await load_mcp_tools(
|
||||
server_type=request.transport,
|
||||
@@ -1062,6 +1065,7 @@ async def mcp_server_metadata(request: MCPServerMetadataRequest):
|
||||
env=request.env,
|
||||
headers=request.headers,
|
||||
timeout_seconds=timeout,
|
||||
sse_read_timeout=sse_read_timeout,
|
||||
)
|
||||
|
||||
# Create the response with tools
|
||||
|
||||
@@ -30,8 +30,17 @@ class MCPServerMetadataRequest(BaseModel):
|
||||
headers: Optional[Dict[str, str]] = Field(
|
||||
None, description="HTTP headers (for sse/streamable_http type)"
|
||||
)
|
||||
timeout_seconds: Optional[int] = Field(
|
||||
None, description="Optional custom timeout in seconds for the operation"
|
||||
timeout_seconds: Optional[int] = Field(
|
||||
None,
|
||||
ge=1,
|
||||
le=3600,
|
||||
description="Optional custom timeout in seconds for the operation (default: 60, range: 1-3600)"
|
||||
)
|
||||
sse_read_timeout: Optional[int] = Field(
|
||||
None,
|
||||
ge=1,
|
||||
le=3600,
|
||||
description="Optional SSE read timeout in seconds (for sse type, default: 30, range: 1-3600)"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -53,7 +53,8 @@ async def load_mcp_tools(
|
||||
url: Optional[str] = None,
|
||||
env: Optional[Dict[str, str]] = None,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
timeout_seconds: int = 60, # Longer default timeout for first-time executions
|
||||
timeout_seconds: Optional[int] = 30, # Reasonable default timeout
|
||||
sse_read_timeout: Optional[int] = None,
|
||||
) -> List:
|
||||
"""
|
||||
Load tools from an MCP server.
|
||||
@@ -65,7 +66,8 @@ async def load_mcp_tools(
|
||||
url: The URL of the SSE/HTTP server (for sse/streamable_http type)
|
||||
env: Environment variables (for stdio type)
|
||||
headers: HTTP headers (for sse/streamable_http type)
|
||||
timeout_seconds: Timeout in seconds (default: 60 for first-time executions)
|
||||
timeout_seconds: Timeout in seconds (default: 30)
|
||||
sse_read_timeout: SSE read timeout in seconds (for sse type, default: same as timeout_seconds)
|
||||
|
||||
Returns:
|
||||
List of available tools from the MCP server
|
||||
@@ -96,9 +98,16 @@ async def load_mcp_tools(
|
||||
status_code=400, detail="URL is required for sse type"
|
||||
)
|
||||
|
||||
# Build kwargs conditionally to avoid passing None values
|
||||
sse_kwargs = {"url": url, "headers": headers}
|
||||
if timeout_seconds is not None:
|
||||
sse_kwargs["timeout"] = timeout_seconds
|
||||
if sse_read_timeout is not None:
|
||||
sse_kwargs["sse_read_timeout"] = sse_read_timeout
|
||||
|
||||
return await _get_tools_from_client_session(
|
||||
sse_client(url=url, headers=headers, timeout=timeout_seconds),
|
||||
timeout_seconds,
|
||||
sse_client(**sse_kwargs),
|
||||
timeout_seconds if timeout_seconds is not None else 30,
|
||||
)
|
||||
|
||||
elif server_type == "streamable_http":
|
||||
@@ -107,11 +116,14 @@ async def load_mcp_tools(
|
||||
status_code=400, detail="URL is required for streamable_http type"
|
||||
)
|
||||
|
||||
# Build kwargs conditionally to avoid passing None values
|
||||
http_kwargs = {"url": url, "headers": headers}
|
||||
if timeout_seconds is not None:
|
||||
http_kwargs["timeout"] = timeout_seconds
|
||||
|
||||
return await _get_tools_from_client_session(
|
||||
streamablehttp_client(
|
||||
url=url, headers=headers, timeout=timeout_seconds
|
||||
),
|
||||
timeout_seconds,
|
||||
streamablehttp_client(**http_kwargs),
|
||||
timeout_seconds if timeout_seconds is not None else 30,
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user