Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 88 additions & 89 deletions stackone_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,67 +667,84 @@ async def _mcp_transport(
yield streams


def fetch_mcp_tools(
endpoint: str, headers: dict[str, str], *, timeout: float = 60.0
) -> list[McpToolDefinition]:
"""List every tool the MCP endpoint serves, following pagination.

``timeout`` bounds the whole exchange, handshake included.
def _run_mcp_session(
endpoint: str,
headers: dict[str, str],
timeout: float,
work: Callable[[Any], Coroutine[Any, Any, T]],
) -> T:
"""Open an initialized MCP session to ``endpoint``, run ``work`` on it and close it.

``timeout`` bounds the whole exchange, handshake included. Any failure is raised
through :func:`_raise_mcp_failure`.
"""
try:
from mcp import types as mcp_types
from mcp.client.session import ClientSession
except ImportError as exc: # pragma: no cover - depends on optional extra
except ImportError as exc: # pragma: no cover - only a broken install gets here
raise ToolsetConfigError(
"mcp is a core dependency of stackone-ai but could not be imported — reinstall the package."
) from exc

throttle = _Throttle()

async def _list() -> list[McpToolDefinition]:
async def _run() -> T:
with anyio.fail_after(timeout):
return await _list_within_deadline()

async def _list_within_deadline() -> list[McpToolDefinition]:
async with _mcp_transport(endpoint, headers, timeout, throttle) as (
read_stream,
write_stream,
_,
):
session = ClientSession(
async with _mcp_transport(endpoint, headers, timeout, throttle) as (
read_stream,
write_stream,
client_info=mcp_types.Implementation(name="stackone-ai-python", version=_SDK_VERSION),
)
async with session:
await session.initialize()
cursor: str | None = None
collected: list[McpToolDefinition] = []
while True:
params = mcp_types.PaginatedRequestParams(cursor=cursor) if cursor is not None else None
result = await session.list_tools(params=params)
for tool in result.tools:
input_schema = tool.inputSchema or {}
collected.append(
McpToolDefinition(
name=tool.name,
description=tool.description,
input_schema=dict(input_schema),
)
)
cursor = result.nextCursor
if cursor is None:
break
return collected
_,
):
session = ClientSession(
read_stream,
write_stream,
client_info=mcp_types.Implementation(name="stackone-ai-python", version=_SDK_VERSION),
)
async with session:
await session.initialize()
return await work(session)

try:
return run_async(_list())
return run_async(_run())
except Exception as exc:
# Exception, not BaseException: Ctrl-C and SystemExit must stop the caller, not come
# back as a ToolsetLoadError that an agent framework hands the model as a tool result.
_raise_mcp_failure(exc, endpoint, timeout, throttle)


def fetch_mcp_tools(
endpoint: str, headers: dict[str, str], *, timeout: float = 60.0
) -> list[McpToolDefinition]:
"""List every tool the MCP endpoint serves, following pagination.

``timeout`` bounds the whole exchange, handshake included.
"""

async def _list(session: Any) -> list[McpToolDefinition]:
from mcp import types as mcp_types

cursor: str | None = None
collected: list[McpToolDefinition] = []
while True:
params = mcp_types.PaginatedRequestParams(cursor=cursor) if cursor is not None else None
result = await session.list_tools(params=params)
for tool in result.tools:
input_schema = tool.inputSchema or {}
collected.append(
McpToolDefinition(
name=tool.name,
description=tool.description,
input_schema=dict(input_schema),
)
)
cursor = result.nextCursor
if cursor is None:
break
return collected

return _run_mcp_session(endpoint, headers, timeout, _list)


def _response_body(response: httpx.Response) -> str:
"""Best-effort read of an error body.

Expand Down Expand Up @@ -947,44 +964,21 @@ def call_mcp_tool(
``arguments`` are sent exactly as given; the server maps them onto the action.
"""
from mcp import types as mcp_types
from mcp.client.session import ClientSession

throttle = _Throttle()

async def _call() -> JsonDict:
with anyio.fail_after(timeout):
return await _call_within_deadline()

async def _call_within_deadline() -> JsonDict:
async with _mcp_transport(endpoint, headers, timeout, throttle) as (
read_stream,
write_stream,
_,
):
session = ClientSession(
read_stream,
write_stream,
client_info=mcp_types.Implementation(name="stackone-ai-python", version=_SDK_VERSION),
async def _call(session: Any) -> JsonDict:
# send_request rather than call_tool: call_tool validates structuredContent
# against the tool's output schema, and on a fresh session that means a
# tools/list first — relisting the whole catalog on every call, which the
# Node SDK never does. The result is parsed below either way.
request = mcp_types.ClientRequest(
mcp_types.CallToolRequest(
params=mcp_types.CallToolRequestParams(name=name, arguments=arguments),
)
async with session:
await session.initialize()
# send_request rather than call_tool: call_tool validates structuredContent
# against the tool's output schema, and on a fresh session that means a
# tools/list first — relisting the whole catalog on every call, which the
# Node SDK never does. The result is parsed below either way.
request = mcp_types.ClientRequest(
mcp_types.CallToolRequest(
params=mcp_types.CallToolRequestParams(name=name, arguments=arguments),
)
)
result = await session.send_request(request, mcp_types.CallToolResult)
return parse_tool_result(result, name)
)
result = await session.send_request(request, mcp_types.CallToolResult)
return parse_tool_result(result, name)

try:
return run_async(_call())
except Exception as exc:
# Not BaseException, as in fetch_mcp_tools: KeyboardInterrupt and SystemExit propagate.
_raise_mcp_failure(exc, endpoint, timeout, throttle)
return _run_mcp_session(endpoint, headers, timeout, _call)


def is_end_user_refusal(exc: BaseException, account_id: str) -> bool:
Expand All @@ -1011,6 +1005,21 @@ def is_end_user_refusal(exc: BaseException, account_id: str) -> bool:
return message.rstrip().rsplit(" ", 1)[-1].strip("\"'") == account_id


def retry_for_end_user(
call: Callable[[], T], accounts: Iterable[str], look_up: Callable[[str, Exception], bool] | None
) -> T:
"""``call()``, made once more if the API refused it for want of the end user of one of
``accounts`` (those it was sent without one for) and ``look_up`` then finds that end user.
"""
try:
return call()
except StackOneAPIError as exc:
refused = next((account for account in accounts if is_end_user_refusal(exc, account)), None)
if refused is None or look_up is None or not look_up(refused, exc):
raise
return call()


def _strip_internal_keys(schema: Any) -> Any:
"""Recursively drop the SDK's internal markers from a property schema.

Expand Down Expand Up @@ -1506,20 +1515,10 @@ def call() -> JsonDict:
)

account_id = self._account_id
without_end_user = account_id is not None and self._recorded_end_user_id() is None
try:
return call()
except StackOneAPIError as exc:
# Sent with no recorded end user and refused for want of one: the account is
# non-shared and its end user can be looked up. Retried once, with it.
if not (
without_end_user
and account_id is not None
and self._look_up_end_user is not None
and self._look_up_end_user(account_id, exc)
):
raise
return call()
without_end_user = (
[account_id] if account_id is not None and self._recorded_end_user_id() is None else []
)
return retry_for_end_user(call, without_end_user, self._look_up_end_user)


@dataclass(frozen=True)
Expand Down
34 changes: 11 additions & 23 deletions stackone_ai/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
_ssl_context,
build_request_headers,
fetch_mcp_tools,
is_end_user_refusal,
is_rate_limited,
is_sdk_owned_header,
retry_for_end_user,
)
from stackone_ai.types import (
DEFAULT_BASE_URL,
Expand Down Expand Up @@ -498,15 +498,12 @@ def _catalog(
def _fetch_for_account(
account: str | None,
) -> list[_Listing]:
without_end_user = account is not None and self._end_user_id(account) is None
try:
listed = fetch_mcp_tools(endpoint, self._build_mcp_headers(account), timeout=self._timeout)
except StackOneAPIError as exc:
# Refused for want of an end user it was sent without: look it up, and retry
# once with it.
if not (without_end_user and account is not None and self._look_up_end_user(account, exc)):
raise
listed = fetch_mcp_tools(endpoint, self._build_mcp_headers(account), timeout=self._timeout)
without_end_user = [account] if account is not None and self._end_user_id(account) is None else []
listed = retry_for_end_user(
lambda: fetch_mcp_tools(endpoint, self._build_mcp_headers(account), timeout=self._timeout),
without_end_user,
self._look_up_end_user,
)
return [(tool_def, account, endpoint) for tool_def in listed]

def _store(catalog: _Catalog) -> None:
Expand Down Expand Up @@ -583,13 +580,7 @@ def _list() -> list[_Listing]:
return [(tool_def, None, endpoint) for tool_def in listed]

without_end_user = [account for account in account_scope if self._end_user_id(account) is None]
try:
listings = _list()
except StackOneAPIError as exc:
refused = next((a for a in without_end_user if is_end_user_refusal(exc, a)), None)
if refused is None or not self._look_up_end_user(refused, exc):
raise
listings = _list()
listings = retry_for_end_user(_list, without_end_user, self._look_up_end_user)

with self._cache_lock:
if generation == self._cache_generation:
Expand Down Expand Up @@ -1018,13 +1009,10 @@ def _look_up_end_user(self, account_id: str, refusal: Exception) -> bool:
"""Given the API's refusal of an account's request for want of its end user, list the
accounts and say whether that one now has an end user recorded to retry with.

Only the API's 400 naming this account counts. A GET /accounts already in flight is
joined rather than another made. If it fails, the refusal is raised with that failure
as its cause: a key without the scope to list accounts would otherwise be told only
that the header does not match.
A GET /accounts already in flight is joined rather than another made. If it fails, the
refusal is raised with that failure as its cause: a key without the scope to list
accounts would otherwise be told only that the header does not match.
"""
if not is_end_user_refusal(refusal, account_id):
return False
try:
self._join_accounts_listing()
except Exception as exc:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_fetch_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,9 @@ async def mock_streamable_client(endpoint, **_kwargs: object):
assert result[0].name == "tool_1"
assert result[1].name == "tool_2"
assert result[1].input_schema == {} # None should become empty dict
assert mock_session.list_tools.call_count == 2
first_call, second_call = mock_session.list_tools.call_args_list
assert first_call.kwargs == {"params": None}
assert second_call.kwargs["params"].cursor == "cursor_page_2"


class TestCatalogCache:
Expand Down
Loading