Files
hermes-agent/tests/tools/test_mcp_dashboard_oauth.py
T
elphamale 11a9dcf567 feat(mcp): migrate to the mcp 2.x SDK
mcp 2.0.0 implements MCP revision 2026-07-28 and makes three breaking
changes Hermes sits on top of: `mcp.server.fastmcp` is gone, every model
field is renamed to snake_case (camelCase survives only as a
serialization alias, which pydantic does not expose to attribute
access), and the SDK's own HTTP stack moved from `httpx` to `httpx2`.

Bump the pin across the dev/mcp/computer-use extras and port the tree:

- `mcp_serve.py` and `agent/transports/hermes_tools_mcp_server.py` move
  from `FastMCP` to `mcp.server.MCPServer`, which has the same
  decorator/add_tool surface. The hermes-tools server already
  synthesised `__signature__` from Hermes' JSON Schema, which is exactly
  what 2.0's `add_tool` reads.
- SDK model reads go through `mcp_field(obj, snake, camel)`, which reads
  both spellings. A single-spelling read fails *silently* on the other
  generation — empty tool schemas, dropped structured content, tool
  results vanishing from sampling conversations — and `mcp` is an
  optional extra users install at their own version.
- `sdk_httpx()` resolves the httpx flavour from the SDK's own transport
  module, so objects handed to `streamable_http_client`, the `sse_client`
  factory, and the OAuth metadata helpers come from the module the
  installed SDK actually imports.
- HTTP support is gated on either streamable-HTTP entry point, not just
  the deprecated alias 2.0 removed.
- OAuth: `OAuthClientProvider` lost its `timeout` argument (the
  configured `oauth.timeout` now bounds the callback waiter's own poll
  loop, where the browser round-trip was always awaited), and
  `callback_handler` must return `AuthorizationCodeResult` rather than a
  tuple. 2.0 also validates the RFC 9207 `iss` parameter, so the
  callback handler and paste fallback capture it.

`mcp`/`mcp-types` 2.0.0 are inside the 14-day `exclude-newer` window, so
two narrow `exclude-newer-package` entries unblock `uv lock`, annotated
for removal on or after 2026-08-11. `httpx2` needs no exemption: 2.7.0 is
already outside the window and satisfies mcp's floor.

Refs #69931

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 23:26:10 -07:00

117 lines
3.8 KiB
Python

"""Hosted-dashboard bridge for MCP OAuth browser callbacks."""
import asyncio
import threading
import pytest
def test_dashboard_flow_exposes_authorization_url_and_accepts_callback():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow
flow = DashboardOAuthFlow(
flow_id="flow-1",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-1",
)
asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=s1"))
assert flow.snapshot() == {
"flow_id": "flow-1",
"server_name": "reports",
"status": "authorization_required",
"authorization_url": "https://idp.example/authorize?state=s1",
"error": None,
}
flow.deliver_callback(code="code-1", state="s1", error=None)
assert asyncio.run(flow.wait_for_callback()) == ("code-1", "s1")
def test_dashboard_flow_accepts_only_one_concurrent_callback():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow
flow = DashboardOAuthFlow(
flow_id="flow-race",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-race",
)
asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=state"))
start = threading.Barrier(3)
outcomes: list[str] = []
def deliver(code: str) -> None:
start.wait()
try:
flow.deliver_callback(code=code, state="state", error=None)
outcomes.append("accepted")
except ValueError:
outcomes.append("rejected")
workers = [threading.Thread(target=deliver, args=(code,)) for code in ("one", "two")]
for worker in workers:
worker.start()
start.wait()
for worker in workers:
worker.join()
assert sorted(outcomes) == ["accepted", "rejected"]
def test_mcp_oauth_helpers_use_dashboard_flow_without_loopback_port():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow, dashboard_oauth_flow
from tools.mcp_oauth import (
HermesTokenStorage,
_build_client_metadata,
_configure_callback_port,
_make_callback_waiter,
_make_redirect_handler,
)
flow = DashboardOAuthFlow(
flow_id="flow-4",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-4",
)
cfg = {}
with dashboard_oauth_flow(flow):
assert _configure_callback_port(cfg, HermesTokenStorage("reports")) == 0
metadata = _build_client_metadata(cfg)
assert str(metadata.redirect_uris[0]) == flow.redirect_uri
asyncio.run(
_make_redirect_handler(0)(
"https://idp.example/authorize?state=state-4"
)
)
flow.deliver_callback(code="code-4", state="state-4", error=None)
# mcp 2.0's callback_handler contract returns an
# AuthorizationCodeResult, not the legacy (code, state) tuple.
result = asyncio.run(_make_callback_waiter(0)())
assert (result.code, result.state) == ("code-4", "state-4")
assert flow.authorization_url == "https://idp.example/authorize?state=state-4"
def test_failed_reauth_rollback_preserves_newer_oauth_state(tmp_path, monkeypatch):
from tools.mcp_oauth import HermesTokenStorage
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
storage = HermesTokenStorage("reports")
storage._tokens_path().parent.mkdir(parents=True)
storage._tokens_path().write_text("OLD")
backup = storage.snapshot()
storage.remove()
storage._tokens_path().write_text("FRESH")
storage.restore(backup, only_if_absent=True)
assert storage._tokens_path().read_text() == "FRESH"