* studio: import MCP servers from a config file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * import config' on the add-server form * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: defensively handle MCP config imports * fix: address MCP import review follow-ups * fix: preserve apostrophes in Windows MCP commands * fix: preserve apostrophe-wrapped Windows MCP args * fix: align Windows MCP parsing with list2cmdline * fix: preserve explicit MCP remote transport intent * fix: trim MCP remote URLs before transport checks --------- Co-authored-by: Roland Tannous <rolandtannous@gravityq.ai> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Roland Tannous <115670425+rolandtannous@users.noreply.github.com> Co-authored-by: Lee Jackson <130007945+Imagineer99@users.noreply.github.com> Co-authored-by: imagineer99 <samleejackson0@gmail.com>
312 lines
11 KiB
Python
312 lines
11 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
import shlex
|
|
import sys
|
|
from typing import Any, Optional
|
|
|
|
from loggers import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
MCP_TOOL_PREFIX = "mcp__"
|
|
|
|
_oauth_token_store = None
|
|
|
|
|
|
def is_stdio(address: str) -> bool:
|
|
"""A non-HTTP address is a local stdio command, e.g.
|
|
'npx -y @modelcontextprotocol/server-filesystem /path'."""
|
|
return not address.strip().lower().startswith(("http://", "https://"))
|
|
|
|
|
|
def _split_windows_command_line(address: str) -> list[str]:
|
|
"""Parse a Windows command line using the same backslash/quote rules that
|
|
subprocess.list2cmdline() writes. This keeps trailing backslashes before a
|
|
closing quote from being doubled in the resulting argv."""
|
|
parts: list[str] = []
|
|
current: list[str] = []
|
|
in_quotes = False
|
|
backslashes = 0
|
|
arg_started = False
|
|
i = 0
|
|
|
|
while i < len(address):
|
|
ch = address[i]
|
|
if ch == "\\":
|
|
backslashes += 1
|
|
i += 1
|
|
continue
|
|
if ch == '"':
|
|
current.extend("\\" * (backslashes // 2))
|
|
if backslashes % 2:
|
|
current.append('"')
|
|
else:
|
|
in_quotes = not in_quotes
|
|
arg_started = True
|
|
backslashes = 0
|
|
i += 1
|
|
continue
|
|
if ch.isspace() and not in_quotes:
|
|
if backslashes:
|
|
current.extend("\\" * backslashes)
|
|
arg_started = True
|
|
backslashes = 0
|
|
if arg_started or current:
|
|
parts.append("".join(current))
|
|
current = []
|
|
arg_started = False
|
|
i += 1
|
|
while i < len(address) and address[i].isspace():
|
|
i += 1
|
|
continue
|
|
if backslashes:
|
|
current.extend("\\" * backslashes)
|
|
arg_started = True
|
|
backslashes = 0
|
|
current.append(ch)
|
|
arg_started = True
|
|
i += 1
|
|
|
|
if backslashes:
|
|
current.extend("\\" * backslashes)
|
|
arg_started = True
|
|
if in_quotes:
|
|
raise ValueError("No closing quotation")
|
|
if arg_started or current:
|
|
parts.append("".join(current))
|
|
return parts
|
|
|
|
|
|
def parse_stdio_command(address: str) -> list[str]:
|
|
"""Split a stdio command line into argv. Shared by route validation and the
|
|
transport so both agree on quoting (notably Windows backslash paths)."""
|
|
posix = sys.platform != "win32"
|
|
if posix:
|
|
return shlex.split(address, posix = posix)
|
|
if address.lstrip().startswith("'"):
|
|
raise ValueError("Single-quoted executables are not supported on Windows")
|
|
return _split_windows_command_line(address)
|
|
|
|
|
|
def join_stdio_command(parts: list[str]) -> str:
|
|
"""Inverse of parse_stdio_command: join argv into a single command string
|
|
that parse_stdio_command() splits back into ``parts`` on this platform.
|
|
Config files (issue #5936) carry structured command + args; storage holds
|
|
one string in the url field. Windows uses list2cmdline so spaced/backslash
|
|
paths round-trip through the posix=False quote-strip; posix uses shlex."""
|
|
if sys.platform == "win32":
|
|
import subprocess
|
|
return subprocess.list2cmdline(parts)
|
|
return shlex.join(parts)
|
|
|
|
|
|
def stdio_mcp_enabled() -> bool:
|
|
"""stdio MCP servers spawn local processes as the backend user (bypassing the
|
|
sandbox), so allowed only when the host is the user's own machine. The Tauri
|
|
app sets UNSLOTH_STUDIO_ALLOW_STDIO_MCP=1; localhost/self-hosted users can opt
|
|
in with the same var. Off for Colab and any network (0.0.0.0) bind."""
|
|
return os.environ.get("UNSLOTH_STUDIO_ALLOW_STDIO_MCP") == "1"
|
|
|
|
|
|
# Probe timeouts for discovering a server's tool list. OAuth needs minutes for
|
|
# first-connect/expired-token browser sign-in; stdio allows for first-run
|
|
# package download (e.g. `npx -y ...`); HTTP fails fast.
|
|
_HTTP_PROBE_TIMEOUT = 8.0
|
|
_OAUTH_PROBE_TIMEOUT = 305.0
|
|
_STDIO_PROBE_TIMEOUT = 60.0
|
|
|
|
|
|
def probe_timeout(address: str, use_oauth: bool) -> float:
|
|
if use_oauth:
|
|
return _OAUTH_PROBE_TIMEOUT
|
|
return _STDIO_PROBE_TIMEOUT if is_stdio(address) else _HTTP_PROBE_TIMEOUT
|
|
|
|
|
|
def parse_server_headers(server: dict) -> Optional[dict]:
|
|
"""Parsed headers_json. For stdio servers this dict is the process env
|
|
instead of HTTP headers (see _client)."""
|
|
raw = server.get("headers_json")
|
|
if not raw:
|
|
return None
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except (json.JSONDecodeError, ValueError):
|
|
return None
|
|
return parsed if isinstance(parsed, dict) else None
|
|
|
|
|
|
def _oauth_store():
|
|
global _oauth_token_store
|
|
if _oauth_token_store is None:
|
|
from key_value.aio._utils.sanitization import AlwaysHashStrategy
|
|
from key_value.aio.stores.filetree import FileTreeStore
|
|
from utils.paths.storage_roots import ensure_dir, studio_root
|
|
|
|
# Hash keys/collections — fastmcp uses raw URLs as keys, and FileTreeStore
|
|
# would treat the "://" as nested directories.
|
|
_oauth_token_store = FileTreeStore(
|
|
data_directory = ensure_dir(studio_root() / "mcp-oauth-tokens"),
|
|
key_sanitization_strategy = AlwaysHashStrategy(),
|
|
collection_sanitization_strategy = AlwaysHashStrategy(),
|
|
)
|
|
return _oauth_token_store
|
|
|
|
|
|
async def clear_oauth_tokens_async(url: str) -> None:
|
|
"""Drop any persisted OAuth tokens for ``url``. fastmcp keys tokens by MCP
|
|
URL, so on server delete / URL change / OAuth disable we must clear them, else
|
|
re-registering the same URL reuses the old account's token. Best-effort: store
|
|
/ OAuth failures must not 500 the delete / update route."""
|
|
try:
|
|
from fastmcp.client.auth import OAuth
|
|
auth = OAuth(mcp_url = url, token_storage = _oauth_store())
|
|
await auth.token_storage_adapter.clear()
|
|
except Exception as exc: # noqa: BLE001
|
|
# Cleanup is best-effort; the row delete still wins.
|
|
logger.warning("Failed to clear OAuth tokens for %s: %s", url, exc)
|
|
|
|
|
|
def _client(
|
|
url: str,
|
|
headers: Optional[dict],
|
|
use_oauth: bool = False,
|
|
):
|
|
from fastmcp import Client
|
|
|
|
if is_stdio(url):
|
|
# Belt-and-suspenders: never spawn unless stdio is enabled on this host.
|
|
if not stdio_mcp_enabled():
|
|
raise PermissionError("stdio MCP servers are disabled on this host")
|
|
from fastmcp.client.transports import StdioTransport
|
|
|
|
parts = parse_stdio_command(url)
|
|
if not parts:
|
|
raise ValueError(f"Empty stdio command: {url!r}")
|
|
# env vars ride the headers field (merged over the SDK default env).
|
|
# keep_alive=False tears the subprocess down so a one-shot call leaves no orphan.
|
|
return Client(
|
|
StdioTransport(
|
|
command = parts[0],
|
|
args = parts[1:],
|
|
env = headers or None,
|
|
keep_alive = False,
|
|
)
|
|
)
|
|
|
|
from fastmcp.client.transports import SSETransport, StreamableHttpTransport
|
|
from fastmcp.mcp_config import infer_transport_type_from_url
|
|
|
|
auth = None
|
|
if use_oauth:
|
|
from fastmcp.client.auth import OAuth
|
|
auth = OAuth(mcp_url = url, token_storage = _oauth_store())
|
|
|
|
transport_cls = (
|
|
SSETransport if infer_transport_type_from_url(url) == "sse" else StreamableHttpTransport
|
|
)
|
|
return Client(transport_cls(url = url, headers = headers or None, auth = auth))
|
|
|
|
|
|
async def list_tools_async(
|
|
url: str,
|
|
headers: Optional[dict] = None,
|
|
timeout: float = 5.0,
|
|
use_oauth: bool = False,
|
|
) -> list[dict]:
|
|
async def _fetch() -> list[dict]:
|
|
async with _client(url, headers, use_oauth) as client:
|
|
tools = await client.list_tools()
|
|
return [t.model_dump(exclude_none = True) for t in tools]
|
|
|
|
return await asyncio.wait_for(_fetch(), timeout = timeout)
|
|
|
|
|
|
def _flatten_result(result: Any) -> str:
|
|
parts = []
|
|
for block in getattr(result, "content", None) or []:
|
|
text = getattr(block, "text", None)
|
|
if text:
|
|
parts.append(str(text))
|
|
body = "\n".join(parts)
|
|
if not body:
|
|
structured = getattr(result, "structured_content", None)
|
|
body = str(structured) if structured is not None else ""
|
|
|
|
if getattr(result, "is_error", False):
|
|
# "Error: " prefix triggers tool_call_parser's TOOL_ERROR_PREFIXES nudge.
|
|
return f"Error: {body}" if body else "Error: tool returned no content"
|
|
return body
|
|
|
|
|
|
def call_tool_sync(
|
|
url: str,
|
|
headers: Optional[dict],
|
|
name: str,
|
|
args: dict,
|
|
timeout: Optional[float] = 300.0,
|
|
use_oauth: bool = False,
|
|
cancel_event = None,
|
|
) -> str:
|
|
"""Synchronously call an MCP tool.
|
|
|
|
``cancel_event``: optional ``threading.Event``. When set, the in-flight call is
|
|
cancelled and a cancellation Error returned. Polled alongside the tool call via
|
|
``asyncio.wait`` so a /cancel POST interrupts even mid-network-read.
|
|
"""
|
|
|
|
async def _call() -> Any:
|
|
async with _client(url, headers, use_oauth) as client:
|
|
return await client.call_tool(name, args)
|
|
|
|
async def _watch_cancel() -> None:
|
|
# 50 ms cadence keeps cancellation responsive without busy-looping;
|
|
# matches routes/inference.py's cancel watcher cadence.
|
|
while cancel_event is not None and not cancel_event.is_set():
|
|
await asyncio.sleep(0.05)
|
|
|
|
async def _race() -> Any:
|
|
# Check cancellation before spawning the call task so a pre-set event
|
|
# short-circuits before opening the transport / HTTP connection.
|
|
if cancel_event is not None and cancel_event.is_set():
|
|
raise _MCPCancelled
|
|
call_task = asyncio.create_task(_call())
|
|
if cancel_event is None:
|
|
return await asyncio.wait_for(call_task, timeout = timeout)
|
|
watch_task = asyncio.create_task(_watch_cancel())
|
|
try:
|
|
done, pending = await asyncio.wait(
|
|
{call_task, watch_task},
|
|
timeout = timeout,
|
|
return_when = asyncio.FIRST_COMPLETED,
|
|
)
|
|
finally:
|
|
for t in (call_task, watch_task):
|
|
if not t.done():
|
|
t.cancel()
|
|
if not done:
|
|
raise asyncio.TimeoutError
|
|
if call_task in done:
|
|
return call_task.result()
|
|
raise _MCPCancelled
|
|
|
|
try:
|
|
result = asyncio.run(_race())
|
|
except _MCPCancelled:
|
|
return f"Error: MCP tool '{name}' cancelled"
|
|
except asyncio.TimeoutError:
|
|
return f"Error: MCP tool '{name}' timed out after {timeout:g}s"
|
|
except Exception as exc:
|
|
logger.exception("MCP call_tool failed for %s: %s", name, exc)
|
|
return f"Error: MCP tool '{name}' failed: {exc}"
|
|
|
|
return _flatten_result(result)
|
|
|
|
|
|
class _MCPCancelled(Exception):
|
|
"""Internal sentinel raised when cancel_event fires before the tool returns."""
|