-
Notifications
You must be signed in to change notification settings - Fork 145
Surface HTTP MCP bearer header for opencode on --foreground (part of #1120) #1141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Local-only — REPRO + guard for "I've been running executor as stdio as get | ||
| // errors trying http one in opencode". The local app's HTTP `/mcp` endpoint is | ||
| // bearer-gated (hardened so loopback is not a free pass) and serves NO OAuth | ||
| // discovery. An external agent like opencode, pointed at the URL, tries MCP | ||
| // OAuth auto-detection, gets a plain `401 Bearer realm="executor"` with no | ||
| // resource-metadata to discover an authorization server from, and errors out. | ||
| // | ||
| // The HTTP transport itself is fine — it works the moment the bearer is supplied | ||
| // (opencode's remote MCP supports `headers` + `oauth: false`). This scenario | ||
| // proves exactly that: tools list over HTTP WITH the bearer, and the gate 401s | ||
| // WITHOUT it. It also asserts the `--foreground` ready output now prints a | ||
| // ready-to-paste opencode config (URL + bearer header + `oauth: false`) so a | ||
| // user does not have to reverse-engineer the gate. | ||
| import { expect } from "@effect/vitest"; | ||
|
Comment on lines
+1
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: e2e/AGENTS.md (source) |
||
| import { Effect } from "effect"; | ||
| import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
| import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; | ||
|
|
||
| import { scenario } from "../src/scenario"; | ||
| import { Cli, RunDir } from "../src/services"; | ||
| import { withLocalServer } from "./local-server"; | ||
|
|
||
| /** Connect an MCP client to the local `/mcp` over HTTP and list tools. Rejects | ||
| * if the bearer gate (or transport) refuses the connection. */ | ||
| const listToolsOverHttp = async ( | ||
| origin: string, | ||
| headers?: Record<string, string>, | ||
| ): Promise<readonly string[]> => { | ||
| const client = new Client({ name: "e2e-http-mcp", version: "0.0.0" }); | ||
| const transport = new StreamableHTTPClientTransport(new URL(`${origin}/mcp`), { | ||
| requestInit: headers ? { headers } : undefined, | ||
| }); | ||
| await client.connect(transport); | ||
| try { | ||
| const { tools } = await client.listTools(); | ||
| return tools.map((t) => t.name); | ||
| } finally { | ||
| await client.close().catch(() => {}); | ||
| } | ||
| }; | ||
|
|
||
| scenario( | ||
| "Local · HTTP MCP works with the bearer header and 401s without it", | ||
| { timeout: 180_000 }, | ||
| Effect.gen(function* () { | ||
| const cli = yield* Cli; | ||
| const runDir = yield* RunDir; | ||
|
|
||
| yield* withLocalServer(cli, runDir, (server) => | ||
| Effect.gen(function* () { | ||
| // WITH the bearer: the HTTP transport connects and lists tools — the very | ||
| // thing that "errors in opencode" when the agent omits the token. | ||
| const tools = yield* Effect.promise(() => | ||
| listToolsOverHttp(server.origin, { authorization: `Bearer ${server.token}` }), | ||
| ); | ||
| expect( | ||
| tools.length, | ||
| "HTTP MCP lists tools once the bearer is supplied", | ||
| ).toBeGreaterThan(0); | ||
|
|
||
| // WITHOUT the bearer: the gate rejects, which is what trips opencode's | ||
| // default OAuth auto-detection (no resource-metadata to recover from). | ||
| const unauthorized = yield* Effect.promise(async () => { | ||
| try { | ||
| await listToolsOverHttp(server.origin); | ||
| return "connected"; | ||
| } catch { | ||
| return "rejected"; | ||
| } | ||
| }); | ||
| expect(unauthorized, "HTTP MCP rejects a tokenless connection").toBe("rejected"); | ||
| }), | ||
| ); | ||
| }), | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
—character anywhere, including code comments: "Never use em-dashes anywhere: prose, docs, code comments, commit messages, or PRs. Use commas, colons, parentheses, or separate sentences instead." The same violation appears ine2e/local/http-mcp-bearer.test.tsline 1 (// Local-only — REPRO + guard). Replace both with a comma, colon, or parenthetical.Context Used: AGENTS.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!