Skip to content

Commit 293e848

Browse files
committed
fix: preserve ModelScope MCP transport
1 parent c4693fa commit 293e848

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

astrbot/core/provider/func_tool_manager.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ def _resolve_timeout(
146146
FuncTool = FunctionTool
147147

148148

149+
def _modelscope_mcp_transport_from_url_info(url_info: Mapping[str, Any]) -> str:
150+
for key in ("transport", "transport_type", "type", "protocol"):
151+
raw_value = url_info.get(key)
152+
if isinstance(raw_value, Mapping):
153+
raw_value = raw_value.get("type") or raw_value.get("name")
154+
if isinstance(raw_value, str):
155+
value = raw_value.strip().lower().replace("-", "_").replace(" ", "_")
156+
if value in {"streamable_http", "streamablehttp", "http"}:
157+
return "streamable_http"
158+
if value in {"sse", "server_sent_events"}:
159+
return "sse"
160+
161+
path = urllib.parse.urlparse(str(url_info.get("url", ""))).path.rstrip("/").lower()
162+
if path.endswith("/sse"):
163+
return "sse"
164+
return "streamable_http"
165+
166+
149167
def _prepare_config(config: dict) -> dict:
150168
"""准备配置,处理嵌套格式"""
151169
if config.get("mcpServers"):
@@ -949,7 +967,9 @@ async def sync_modelscope_mcp_servers(self, access_token: str) -> None:
949967
# 添加到配置中(同名会覆盖)
950968
local_mcp_config["mcpServers"][server_name] = {
951969
"url": server_url,
952-
"transport": "sse",
970+
"transport": _modelscope_mcp_transport_from_url_info(
971+
url_info
972+
),
953973
"active": True,
954974
"provider": "modelscope",
955975
}

tests/unit/test_func_tool_manager.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import pytest
44

55
from astrbot.core import sp
6-
from astrbot.core.provider.func_tool_manager import FunctionToolManager
6+
from astrbot.core.provider.func_tool_manager import (
7+
FunctionToolManager,
8+
_modelscope_mcp_transport_from_url_info,
9+
)
710
from astrbot.core.tools.computer_tools.shell import ExecuteShellTool
811
from astrbot.core.tools.message_tools import SendMessageToUserTool
912
from astrbot.core.tools.web_search_tools import (
@@ -293,6 +296,20 @@ def test_is_self_detached_command_handles_quotes_and_comments(command, expected)
293296
assert _is_self_detached_command(command) is expected
294297

295298

299+
@pytest.mark.parametrize(
300+
("url_info", "expected"),
301+
[
302+
({"transport": "Streamable HTTP", "url": "https://example.com/mcp"}, "streamable_http"),
303+
({"transport_type": "sse", "url": "https://example.com/mcp"}, "sse"),
304+
({"type": "streamable_http", "url": "https://example.com/mcp"}, "streamable_http"),
305+
({"url": "https://example.com/mcp/sse"}, "sse"),
306+
({"url": "https://example.com/mcp"}, "streamable_http"),
307+
],
308+
)
309+
def test_modelscope_mcp_transport_from_url_info(url_info, expected):
310+
assert _modelscope_mcp_transport_from_url_info(url_info) == expected
311+
312+
296313
@pytest.mark.asyncio
297314
async def test_execute_shell_reports_blank_exception_type(monkeypatch):
298315
from astrbot.core.tools.computer_tools import shell as shell_tools

0 commit comments

Comments
 (0)