Skip to content
Open
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
19 changes: 13 additions & 6 deletions packages/opencode/src/mcp/oauth-provider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
import type {
OAuthClientMetadata,
OAuthTokens,
OAuthClientInformation,
OAuthClientInformationFull,
OAuthClientMetadata,
OAuthTokens,
} from "@modelcontextprotocol/sdk/shared/auth.js"
import { McpAuth } from "./auth"
import { Log } from "../util/log"
import { McpAuth } from "./auth"

const log = Log.create({ service: "mcp.oauth" })

Expand Down Expand Up @@ -122,8 +122,14 @@ export class McpOAuthProvider implements OAuthClientProvider {
}

async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
log.info("redirecting to authorization", { mcpName: this.mcpName, url: authorizationUrl.toString() })
await this.callbacks.onRedirect(authorizationUrl)
const url = new URL(authorizationUrl.toString())

if (url.hostname === "login.microsoftonline.com") {
url.searchParams.delete("resource")
}

log.info("redirecting to authorization", { mcpName: this.mcpName, url: url.toString() })
await this.callbacks.onRedirect(url)
}

async saveCodeVerifier(codeVerifier: string): Promise<void> {
Expand Down Expand Up @@ -151,4 +157,5 @@ export class McpOAuthProvider implements OAuthClientProvider {
}
}

export { OAUTH_CALLBACK_PORT, OAUTH_CALLBACK_PATH }
export { OAUTH_CALLBACK_PATH, OAUTH_CALLBACK_PORT }

30 changes: 30 additions & 0 deletions packages/opencode/test/mcp/oauth-resource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from "bun:test"

import { McpOAuthProvider } from "../../src/mcp/oauth-provider"

test("McpOAuthProvider omits resource for login.microsoftonline.com", async () => {
let captured: URL | undefined

const provider = new McpOAuthProvider(
"test",
"http://localhost:3000",
{},
{
onRedirect: async (url) => {
captured = url
},
},
)

const url = new URL(
"https://login.microsoftonline.com/tenant/oauth2/v2.0/authorize?client_id=x&resource=http%3A%2F%2Flocalhost%3A5533%2F&scope=openid",
)

await provider.redirectToAuthorization(url)

expect(captured).toBeDefined()
expect(captured!.hostname).toBe("login.microsoftonline.com")
expect(captured!.searchParams.get("resource")).toBeNull()
expect(captured!.searchParams.get("client_id")).toBe("x")
expect(captured!.searchParams.get("scope")).toBe("openid")
})
Loading