Files
optimclaw/tests/fixtures/llm_traces/advanced/mcp_extension_lifecycle.json
T
8a26cfae73 fix(mcp): open MCP OAuth in same browser as gateway (#951)
* fix(mcp): use gateway callback for MCP OAuth so auth opens in same browser

When MCP OAuth is triggered from the web gateway, the auth URL was being
opened via `open::that()` which launches the OS default browser instead
of the browser already running the gateway UI. This changes the MCP OAuth
flow to use the same gateway callback pattern as WASM extensions: in
gateway mode, the auth URL is returned to the frontend via SSE and opened
with `window.open()`, keeping the user in the same browser.

Also adds RFC 8707 `resource` parameter support to the gateway token
exchange path, scoping issued tokens to the correct MCP server.

Closes #299

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: cargo fmt

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(mcp): persist DCR client_id in gateway OAuth callback for token refresh

The gateway callback handler stored access and refresh tokens but not
the DCR client_id. When the token expired, refresh failed with "No
client ID found" because get_client_id() could not find it in secrets.

Adds client_id_secret_name to PendingOAuthFlow so the gateway callback
handler persists the client_id alongside the tokens, matching the
behavior of the CLI flow in authorize_mcp_server().

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(mcp): return AuthRequired on 401 so activate triggers OAuth flow

activate_mcp() returned ActivationFailed for all errors including 401
auth responses, so the activate handler never triggered the OAuth flow.
Now 401/auth errors return AuthRequired, which the handler detects and
redirects to the OAuth flow — matching the WASM extension pattern.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(mcp): fix gateway OAuth flow, approval cards, and auto-activation

- Add explicit gateway_mode flag on ExtensionManager (set at startup by
  web gateway) so MCP OAuth returns auth URLs to the frontend instead of
  calling open::that() on the server machine.
- Auto-activate extensions after successful OAuth callback so the UI
  transitions from "Activate" to "Active" without a second click.
- Send ApprovalNeeded status (not generic "Awaiting approval") from
  thread_ops.rs for all three NeedApproval paths so the web UI shows
  approval cards for deferred tool calls.
- Remove duplicate ApprovalNeeded send from agent_loop.rs (thread_ops.rs
  is now the canonical sender).
- Skip approval for tool_auth in gateway mode since it only returns a URL.
- Revert fragile active-server detection heuristic from system prompt.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: address PR review findings

- Use Release/Acquire ordering for gateway_mode AtomicBool instead of
  Relaxed to ensure visibility across threads.
- Report activation failure as error in OAuth callback SSE event instead
  of silently falling back to the success message.
- Fix EnvGuard::drop to remove env var when original was unset.
- Replace hardcoded /tmp/ path with std::env::temp_dir() in test helper.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* test(mcp): add E2E trace test for MCP extension lifecycle with mock server

Add a full MCP extension lifecycle E2E test that exercises:
- Turn 1: tool_search → tool_install → text (extension discovery and install)
- Token injection + activate (simulating OAuth completion)
- Turn 2: MCP tool calls (notion-search → notion-fetch → text)

Includes a mock MCP server (tests/support/mock_mcp_server.rs) with OAuth
discovery, DCR, token exchange, and JSON-RPC endpoints. The mock server
validates Bearer auth and serves pre-configured tool responses.

Also adds inject_registry_entry() to ExtensionManager for test use and
exposes extension_manager from TestRig.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: address PR review findings (round 2)

- Only fall back to manual token entry on AuthNotSupported, propagate
  real errors from auth_mcp_build_url() instead of masking them
- Use mcp:-prefixed provider string in PendingOAuthFlow for consistency
  with CLI MCP auth token storage
- Only persist client_id_secret_name for DCR flows (not pre-configured OAuth)
- Fix gateway_callback_redirect_uri to use /oauth/callback path
- Bypass exchange proxy when flow has RFC 8707 resource parameter
- Remove client_id double-prefix in oauth callback handler
- Remove weak tests that didn't exercise production logic
- Add clarifying comments for exchange_oauth_code delegation

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: keep OAuth success independent of activation, fix wait_for_responses scoping

- OAuth success is now reported accurately even when auto-activation
  fails (tokens are already stored, so auth succeeded)
- E2E test waits for turn1_count + 1 responses to ensure turn-2
  behavior is actually observed

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-03-12 11:16:23 -07:00

99 lines
2.9 KiB
JSON

{
"model_name": "advanced-mcp-extension-lifecycle",
"expects": {
"tools_used": ["tool_search", "tool_install"],
"tools_order": ["tool_search", "tool_install"],
"all_tools_succeeded": true,
"min_responses": 2
},
"turns": [
{
"user_input": "setup mock-notion",
"steps": [
{
"request_hint": { "last_user_message_contains": "setup mock-notion" },
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_search_1",
"name": "tool_search",
"arguments": { "query": "mock-notion" }
}
],
"input_tokens": 500,
"output_tokens": 30
}
},
{
"request_hint": { "last_user_message_contains": "setup mock-notion", "min_message_count": 4 },
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_install_1",
"name": "tool_install",
"arguments": { "name": "mock-notion" }
}
],
"input_tokens": 600,
"output_tokens": 30
}
},
{
"request_hint": { "last_user_message_contains": "setup mock-notion", "min_message_count": 6 },
"response": {
"type": "text",
"content": "I've installed Mock Notion. Please authenticate to connect your account — once done, tell me and I'll load the MCP tools.",
"input_tokens": 700,
"output_tokens": 35
}
}
]
},
{
"user_input": "it's done, check what's in my notion",
"steps": [
{
"request_hint": { "last_user_message_contains": "notion" },
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_ns_1",
"name": "mock-notion_notion-search",
"arguments": { "query": "recent notes" }
}
],
"input_tokens": 900,
"output_tokens": 30
}
},
{
"request_hint": { "min_message_count": 4 },
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_nf_1",
"name": "mock-notion_notion-fetch",
"arguments": { "query": "page-001" }
}
],
"input_tokens": 1000,
"output_tokens": 30
}
},
{
"response": {
"type": "text",
"content": "Here's what I found in your Notion:\n\n**Project Alpha** — Status: In Progress\n- Sprint planning on March 15\n- API redesign review pending\n\nLet me know if you want more details on any item.",
"input_tokens": 1100,
"output_tokens": 50
}
}
]
}
]
}