1. What You're Testing
- **
preserve-mcp-ui-embedded-resources:** DoesRemoteMcpClient.callTool(...)strip HTML noise from the model-facing text block while preservingui://EmbeddedResourcepayloads and_metain_uiResources? - OAuth 2.1 PKCE loop: Does
401+WWW-Authenticatediscovery (/oauth/register,/oauth/authorize,/oauth/upstream-callback,/oauth/token) flow cleanly end-to-end againstMemoryStore? - Non-interactive discovery safety: Does
discoverAgentEndpointanswer"oauth-required"cleanly on401without accidentally opening a browser auth flow?
2. Visual Target (Architecture & Mockup)
Architecture & Flow Diagram
Mermaid
sequenceDiagram
participant Probe as "discoverAgentEndpoint"
participant Client as "RemoteMcpClient (pi-mcp)"
participant Server as "createMcpServer (127.0.0.1:7455)"
participant Up as "Local Stub OAuth Upstream (:0)"
Probe->>Server: "POST /mcp (no token)"
Server-->>Probe: "401 Unauthorized (non-interactive -> oauth-required)"
Client->>Server: "Dynamic client registration + PKCE authorize"
Server->>Up: "Upstream OAuth redirect -> code exchange"
Up-->>Server: "upstream tokens + userId"
Server-->>Client: "Bearer access_token"
Client->>Server: "callTool(ui_card)"
Server-->>Client: "text summary + ui:// EmbeddedResource + _meta"
Note over Client: "Split: 18-tok text -> LLM, full HTML -> Human UI"
Target split-pane terminal/browser inspector:
Visual Target Wireframe
ASCII Layout
╔══════════════════════════════════════════════════════════════════════════════╗
║ MCP OAUTH + ui:// ROUND-TRIP LAB · server=http://127.0.0.1:7455 ║
╠══════════════════════════════════════════════════════════════════════════════╣
║ 1. Discovery Probe: kind=mcp auth=oauth-required (browser NOT launched) ║
║ 2. OAuth PKCE Grant: client=lab-cli user=demo-user-1 token=sha256:4a1e.. ║
╠──────────────────────────────────────┬───────────────────────────────────────╣
║ WHAT THE MODEL SEES (text blocks) │ WHAT THE HUMAN RENDERS (_uiResources) ║
│ │ │
│ tool: release_readiness_card │ uri: ui://release/v0.6-card │
│ text: "Release v0.6 readiness check: │ mimeType: text/html │
│ 4 passing suites, 1 pending." │ _meta: { "ui/panel": "summary" } │
│ │ │
│ estimated tokens: ~16 tokens │ rawHtml bytes: 2,418 bytes (0 tokens!)║
╚══════════════════════════════════════┴───────────────────────────────────────┘
3. Spin-Up & Prerequisites
Confirm mcp-serve mount tests and discoverAgentEndpoint pass before starting:
zsh
pnpm exec vitest run packages/protocols/src/
4. Coding-Agent Prompt (Copy-Paste)
Coding-Agent Prompt · Ready to Paste
Build a self-contained lab prototype under `examples/lab-05-mcp-oauth-ui/` testing the full OAuth 2.1 PKCE + Streamable HTTP MCP flow using `createMcpServer` (`@umwelten/protocols`), `RemoteMcpClient`, and `discoverAgentEndpoint`. Read these files first to match exact types and invariants: - `docs/specs/interfaces/protocols-and-extensions.md` - `packages/protocols/src/mcp-serve/server.ts` - `packages/protocols/src/mcp-serve/types.ts` - `packages/protocols/src/mcp-serve/memory-store.ts` - `packages/protocols/src/mcp/client/remote.ts` - `packages/protocols/src/discovery/discover.ts` Requirements: 1. Create `examples/lab-05-mcp-oauth-ui/run.ts` (plus `README.md`) with: - `--smoke`: fully automated loopback verification on ephemeral ports using a local mock upstream OAuth server (no external browser needed!). - `--serve --port 7455`: starts the `createMcpServer` instance on `127.0.0.1:7455` + mock upstream OAuth server so you can connect interactively or test PKCE login manually (`open`/browser). - `--probe <url> [--token <bearer>]`: runs `discoverAgentEndpoint` against any MCP or A2A URL. 2. Register two MCP tools in `McpToolRegistrar`: - `echo_identity`: proves `userId` and `getUpstreamToken()` resolved cleanly inside the tool handler without exposing raw upstream tokens to the model text. - `show_dashboard_card`: uses `buildHabitatUIResource` (or `createUIResource` from `@mcp-ui/server`) to return a concise 1-sentence text summary PLUS an `EmbeddedResource` (`uri: "ui://lab/dashboard"`, `mimeType: "text/html"`) and `_meta` fields. 3. In `--smoke` mode: - Call `discoverAgentEndpoint(mcpUrl)` without a token and assert `auth === "oauth-required"`. - Perform an automated loopback PKCE authorization-code exchange against `/oauth/register` -> `/oauth/authorize` -> local mock upstream redirect -> `/oauth/upstream-callback` -> `/oauth/token` (never touching `~/.umwelten/mcp-auth`; use a temp dir or in-memory bearer token). - Connect using `RemoteMcpClient` (`connect` -> `listTools` -> `callTool` on `show_dashboard_card`) and print the side-by-side **What the Model Sees** vs **What the Human Renders (`_uiResources` + `_meta`)** comparison table, asserting `_uiResources` contains `ui://lab/dashboard` and raw HTML was NOT stuffed into the plain model text! 4. Clean up servers and temp state in `finally` with zero `any` types and strict `tsc --noEmit` cleanliness.
Works in Pi, Claude Code, Codex & Antigravity
Offline mock first →
--live opt-in
5. Interactive Walkthrough (Kick the Tires)
- **Observe
401safety during probing:** Run--probe http://127.0.0.1:7455/mcpwhile--serveis running and verify discovery immediately reportsoauth-requiredwith zero interactive prompts. - **Test real
RemoteMcpClientPKCE browser flow:** Connect to--serveusing a temporary~/.umweltenor Safari login flow, authorize via the local upstream stub, and inspect the cached client/token JSON (0600permissions). - Measure token savings: Compare byte size of
_uiResources[0].textvs. the concise model text returned byshow_dashboard_card.
6. Did the Prompt Work? (Verification & Tuning)
zsh
pnpm exec tsx examples/lab-05-mcp-oauth-ui/run.ts --smoke
pnpm exec tsc --noEmit --target ES2022 --module NodeNext --moduleResolution NodeNext --esModuleInterop --strict --skipLibCheck examples/lab-05-mcp-oauth-ui/run.ts
Pass criteria:
--smokeexits0, assertingauth === "oauth-required", successful PKCE token issuance,echo_identitysuccess, and_uiResources+_metapreservation inRemoteMcpClient.- Plain text response passed to the model does NOT duplicate raw HTML tags.
Tuning hints if the agent struggles:
- **
McpHttpServerpublic URL mismatch on port0:** Inspectpackages/protocols/src/mcp-serve/public-url.tsand passgetPublicBaseUrlor usecreateMcpServeMountwith an explicit Nodehttp.createServerlistened on0so the OAuth metadata returns the actual bound loopback port (http://127.0.0.1:<port>). - PKCE redirects: Have the automated
--smokeharness follow HTTP302redirects manually (redirect: "manual") so it deterministically extractscodeandstatewithout opening a browser.