Skip to content

Commit 46a96f7

Browse files
committed
add a timeout parameter to the mcp.
1 parent a92cd6d commit 46a96f7

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
import os
3+
4+
from oxygent import MAS, oxy
5+
6+
oxy_space = [
7+
oxy.HttpLLM(
8+
name="default_llm",
9+
api_key=os.getenv("CHATRHINO_750B_API_KEY"),
10+
base_url=os.getenv("CHATRHINO_750B_BASE_URL"),
11+
model_name=os.getenv("CHATRHINO_750B_MODEL_NAME"),
12+
is_disable_system_prompt=True, # 适配少部分不支持system prompt的模型
13+
),
14+
oxy.ChatAgent(
15+
name="master_agent",
16+
llm_model="default_llm",
17+
),
18+
]
19+
20+
21+
async def main():
22+
async with MAS(oxy_space=oxy_space) as mas:
23+
await mas.start_web_service(first_query="hello")
24+
25+
26+
if __name__ == "__main__":
27+
asyncio.run(main())

oxygent/oxy/agents/local_agent.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,9 @@ async def init(self):
172172
self.llm_model
173173
].is_multimodal_supported
174174
if self.is_multimodal_supported:
175-
self.input_schema = {
176-
"properties": {
177-
"query": {
178-
"description": "The image path and the query to ask about the images, for example: ![file 1](image1.png) ![file 2](image2.png) What are file 1 and file 2, respectively?"
179-
}
180-
},
181-
"required": ["query"],
182-
}
175+
self.input_schema["properties"]["query"]["description"] = (
176+
"The image path and the query to ask about the images, for example: ![file 1](image1.png) ![file 2](image2.png) What are file 1 and file 2, respectively?"
177+
)
183178

184179
await super().init()
185180
if self.intent_understanding_agent:

oxygent/oxy/llms/base_llm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,21 @@ class BaseLLM(Oxy):
7979
default=2 * 1024 * 1024,
8080
description="Maximum non-media file size (bytes) for base64 embedding.",
8181
)
82+
is_disable_system_prompt: bool = Field(default=False)
8283

8384
async def _get_messages(self, oxy_request: OxyRequest):
85+
# merge system prompt
86+
if (
87+
self.is_disable_system_prompt
88+
and oxy_request.arguments["messages"][0].get("role") == "system"
89+
):
90+
oxy_request.arguments["messages"][1]["content"] = (
91+
oxy_request.arguments["messages"][0]["content"]
92+
+ "\nUser Input: "
93+
+ oxy_request.arguments["messages"][1]["content"]
94+
)
95+
oxy_request.arguments["messages"] = oxy_request.arguments["messages"][1:]
96+
8497
# Preprocess messages for multimoding input
8598
if not self.is_multimodal_supported:
8699
return oxy_request.arguments["messages"]

oxygent/oxy/mcp_tools/sse_mcp_client.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ async def init(self, is_fetch_tools=True) -> None:
4242
if not self.is_dynamic_headers and self.is_keep_alive:
4343
# header
4444
sse_transport = await self._exit_stack.enter_async_context(
45-
sse_client(build_url(self.sse_url), headers=self.headers)
45+
sse_client(
46+
build_url(self.sse_url),
47+
headers=self.headers,
48+
timeout=self.timeout,
49+
)
4650
)
4751
read, write = sse_transport
4852
self._session = await self._exit_stack.enter_async_context(
@@ -64,7 +68,7 @@ async def init(self, is_fetch_tools=True) -> None:
6468
await self.list_tools()
6569
else:
6670
async with sse_client(
67-
build_url(self.sse_url), headers=self.headers
71+
build_url(self.sse_url), headers=self.headers, timeout=self.timeout
6872
) as streams:
6973
async with ClientSession(*streams) as session:
7074
await session.initialize()
@@ -76,7 +80,9 @@ async def init(self, is_fetch_tools=True) -> None:
7680
raise Exception(f"Server {self.name} error")
7781

7882
async def call_tool(self, tool_name, arguments, headers=None):
79-
async with sse_client(build_url(self.sse_url), headers=headers, timeout=self.timeout) as streams:
83+
async with sse_client(
84+
build_url(self.sse_url), headers=headers, timeout=self.timeout
85+
) as streams:
8086
async with ClientSession(*streams) as session:
8187
await session.initialize()
8288
return await session.call_tool(tool_name, arguments)

oxygent/oxy/mcp_tools/streamable_mcp_client.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ async def init(self, is_fetch_tools=True) -> None:
2727
if not self.is_dynamic_headers and self.is_keep_alive:
2828
self._http_transport = await self._exit_stack.enter_async_context(
2929
streamablehttp_client(
30-
build_url(self.server_url), headers=self.headers
30+
build_url(self.server_url),
31+
headers=self.headers,
32+
timeout=self.timeout,
3133
)
3234
)
3335
read, write, _ = self._http_transport
@@ -47,12 +49,10 @@ async def init(self, is_fetch_tools=True) -> None:
4749
await self.list_tools()
4850
else:
4951
async with streamablehttp_client(
50-
build_url(self.server_url), headers=self.headers
51-
) as (
52-
read,
53-
write,
54-
_,
55-
):
52+
build_url(self.server_url),
53+
headers=self.headers,
54+
timeout=self.timeout,
55+
) as (read, write, _):
5656
async with ClientSession(read, write) as session:
5757
await session.initialize()
5858
tools_response = await session.list_tools()
@@ -64,12 +64,8 @@ async def init(self, is_fetch_tools=True) -> None:
6464

6565
async def call_tool(self, tool_name, arguments, headers=None):
6666
async with streamablehttp_client(
67-
build_url(self.server_url), headers=headers
68-
) as (
69-
read,
70-
write,
71-
_,
72-
):
67+
build_url(self.server_url), headers=headers, timeout=self.timeout
68+
) as (read, write, _):
7369
async with ClientSession(read, write) as session:
7470
await session.initialize()
7571
return await session.call_tool(tool_name, arguments)

oxygent/preset_tools/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"system_tools",
1414
"shell_tools",
1515
"python_tools",
16-
"baidu_search_tools",
1716
"image_gen_tools",
1817
]
1918

0 commit comments

Comments
 (0)