Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: component MCP Tools (SSE): 'Timeout' #5638

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
27 changes: 16 additions & 11 deletions src/backend/base/langflow/components/tools/mcp_sse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# from langflow.field_typing import Data
import asyncio
from contextlib import AsyncExitStack

import httpx
Expand All @@ -10,7 +11,6 @@
from langflow.custom import Component
from langflow.field_typing import Tool
from langflow.io import MessageTextInput, Output
from langflow.utils.async_helpers import timeout_context

# Define constant for status code
HTTP_TEMPORARY_REDIRECT = 307
Expand Down Expand Up @@ -39,18 +39,22 @@ async def connect_to_server(
headers = {}
url = await self.pre_check_redirect(url)

async with timeout_context(timeout_seconds):
sse_transport = await self.exit_stack.enter_async_context(
sse_client(url, headers, timeout_seconds, sse_read_timeout_seconds)
)
self.sse, self.write = sse_transport
self.session = await self.exit_stack.enter_async_context(ClientSession(self.sse, self.write))
try:
async with asyncio.timeout(timeout_seconds):
sse_transport = await self.exit_stack.enter_async_context(
sse_client(url, headers, timeout_seconds, sse_read_timeout_seconds)
)
self.sse, self.write = sse_transport
self.session = await self.exit_stack.enter_async_context(ClientSession(self.sse, self.write))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Python 3.10 has that function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure which function you are talking about? Can you be more precise?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.10 doesn't have asyncio.timeout(). I think asyncio.wait_for() along with minor modifications to your code would serve the purpose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why @phact created the custom context manager.


await self.session.initialize()
await self.session.initialize()

# List available tools
response = await self.session.list_tools()
return response.tools
# List available tools
response = await self.session.list_tools()
return response.tools
except asyncio.TimeoutError as err:
error_message = f"Connection to {url} timed out after {timeout_seconds} seconds"
raise TimeoutError(error_message) from err


class MCPSse(Component):
Expand Down Expand Up @@ -89,6 +93,7 @@ async def build_output(self) -> list[Tool]:
Tool(
name=tool.name, # maybe format this
description=tool.description,
args_schema=args_schema,
coroutine=create_tool_coroutine(tool.name, args_schema, self.client.session),
func=create_tool_func(tool.name, self.client.session),
)
Expand Down
Loading