Skip to content

Commit 3aa75ba

Browse files
hanfivbarda
andauthored
add headers support to sse requests (#17)
--------- Co-authored-by: vbarda <[email protected]>
1 parent ac27f57 commit 3aa75ba

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

langchain_mcp_adapters/client.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
DEFAULT_ENCODING = "utf-8"
1515
DEFAULT_ENCODING_ERROR_HANDLER = "strict"
1616

17+
DEFAULT_HTTP_TIMEOUT = 5
18+
DEFAULT_SSE_READ_TIMEOUT = 60 * 5
19+
1720

1821
class StdioConnection(TypedDict):
1922
transport: Literal["stdio"]
@@ -45,6 +48,15 @@ class SSEConnection(TypedDict):
4548
url: str
4649
"""The URL of the SSE endpoint to connect to."""
4750

51+
headers: dict[str, Any] | None = None
52+
"""HTTP headers to send to the SSE endpoint"""
53+
54+
timeout: float
55+
"""HTTP timeout"""
56+
57+
sse_read_timeout: float
58+
"""SSE read timeout"""
59+
4860

4961
class MultiServerMCPClient:
5062
"""Client for connecting to multiple MCP servers and loading LangChain-compatible tools from them."""
@@ -125,7 +137,13 @@ async def connect_to_server(
125137
if transport == "sse":
126138
if "url" not in kwargs:
127139
raise ValueError("'url' parameter is required for SSE connection")
128-
await self.connect_to_server_via_sse(server_name, url=kwargs["url"])
140+
await self.connect_to_server_via_sse(
141+
server_name,
142+
url=kwargs["url"],
143+
headers=kwargs.get("headers"),
144+
timeout=kwargs.get("timeout", DEFAULT_HTTP_TIMEOUT),
145+
sse_read_timeout=kwargs.get("sse_read_timeout", DEFAULT_SSE_READ_TIMEOUT),
146+
)
129147
elif transport == "stdio":
130148
if "command" not in kwargs:
131149
raise ValueError("'command' parameter is required for stdio connection")
@@ -189,15 +207,23 @@ async def connect_to_server_via_sse(
189207
server_name: str,
190208
*,
191209
url: str,
210+
headers: dict[str, Any] | None = None,
211+
timeout: float = DEFAULT_HTTP_TIMEOUT,
212+
sse_read_timeout: float = DEFAULT_SSE_READ_TIMEOUT,
192213
) -> None:
193214
"""Connect to a specific MCP server using SSE
194215
195216
Args:
196217
server_name: Name to identify this server connection
197218
url: URL of the SSE server
219+
headers: HTTP headers to send to the SSE endpoint
220+
timeout: HTTP timeout
221+
sse_read_timeout: SSE read timeout
198222
"""
199223
# Create and store the connection
200-
sse_transport = await self.exit_stack.enter_async_context(sse_client(url))
224+
sse_transport = await self.exit_stack.enter_async_context(
225+
sse_client(url, headers, timeout, sse_read_timeout)
226+
)
201227
read, write = sse_transport
202228
session = cast(
203229
ClientSession,

0 commit comments

Comments
 (0)