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
208 changes: 76 additions & 132 deletions bun.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@
"@tsconfig/bun": "catalog:",
"@types/mime-types": "3.0.1",
"@typescript/native-preview": "catalog:",
"glob": "13.0.5",
"glob": "14.0.2",
"husky": "9.1.7",
"minimatch": "10.0.3",
"minimatch": "11.0.1",
"oxlint": "1.60.0",
"oxlint-tsgolint": "0.21.0",
"prettier": "3.6.2",
"semver": "^7.6.0",
"turbo": "2.8.13"
"turbo": "2.9.14"
},
"dependencies": {
"@opencode-ai/plugin": "workspace:*",
Expand Down Expand Up @@ -143,7 +143,9 @@
"@types/bun": "catalog:",
"@types/node": "catalog:",
"@smithy/is-array-buffer": "4.2.2",
"@smithy/util-buffer-from": "4.2.2"
"@smithy/util-buffer-from": "4.2.2",
"@ai-sdk/provider-utils": "4.0.33",
"@babel/core": "7.29.6"
},
"patchedDependencies": {
"@ff-labs/fff-bun@0.9.4": "patches/@ff-labs%2Ffff-bun@0.9.4.patch",
Expand All @@ -157,7 +159,7 @@
"@ai-sdk/google@3.0.73": "patches/@ai-sdk%2Fgoogle@3.0.73.patch",
"@tanstack/solid-virtual@3.13.28": "patches/@tanstack%2Fsolid-virtual@3.13.28.patch",
"@pierre/trees@1.0.0-beta.4": "patches/@pierre%2Ftrees@1.0.0-beta.4.patch",
"@modelcontextprotocol/sdk@1.29.0": "patches/@modelcontextprotocol%2Fsdk@1.29.0.patch",
"@modelcontextprotocol/sdk@1.32.2": "patches/@modelcontextprotocol%2Fsdk@1.32.2.patch",
"@tanstack/virtual-core@3.17.0": "patches/@tanstack%2Fvirtual-core@3.17.0.patch"
}
}
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
"@ai-sdk/openai": "3.0.53",
"@ai-sdk/openai-compatible": "2.0.41",
"@ai-sdk/perplexity": "3.0.26",
"@ai-sdk/provider": "3.0.8",
"@ai-sdk/provider-utils": "4.0.23",
"@ai-sdk/provider": "3.0.12",
"@ai-sdk/provider-utils": "4.0.33",
"@ai-sdk/togetherai": "2.0.41",
"@ai-sdk/vercel": "2.0.39",
"@ai-sdk/xai": "3.0.82",
Expand Down Expand Up @@ -106,15 +106,15 @@
"effect": "catalog:",
"fuzzysort": "3.1.0",
"gitlab-ai-provider": "6.9.3",
"glob": "13.0.5",
"glob": "14.0.2",
"google-auth-library": "10.5.0",
"gray-matter": "4.0.3",
"htmlparser2": "8.0.2",
"immer": "11.1.4",
"ignore": "7.0.5",
"jsonc-parser": "3.3.1",
"mime-types": "3.0.2",
"minimatch": "10.2.5",
"minimatch": "11.0.1",
"npm-package-arg": "13.0.2",
"semver": "^7.6.3",
"turndown": "7.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"optionalDependencies": {
"pg": "^8.0.0",
"snowflake-sdk": "^2.0.3",
"snowflake-sdk": "~3.3.0",
"@google-cloud/bigquery": "^8.0.0",
"@databricks/sql": "^1.0.0",
"mysql2": "^3.0.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"devDependencies": {
"@babel/core": "7.28.4",
"@babel/core": "7.29.6",
"@octokit/webhooks-types": "7.6.1",
"@opencode-ai/core": "workspace:*",
"@opencode-ai/http-recorder": "workspace:*",
Expand Down Expand Up @@ -90,7 +90,7 @@
"@ff-labs/fff-bun": "0.9.4",
"@gitlab/opencode-gitlab-auth": "1.3.3",
"@hono/standard-validator": "catalog:",
"@modelcontextprotocol/sdk": "1.29.0",
"@modelcontextprotocol/sdk": "1.32.2",
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
"@octokit/graphql": "9.0.2",
"@octokit/rest": "catalog:",
"@openauthjs/openauth": "catalog:",
Expand Down Expand Up @@ -129,7 +129,7 @@
"effect": "catalog:",
"fuzzysort": "3.1.0",
"gitlab-ai-provider": "6.9.3",
"glob": "13.0.5",
"glob": "14.0.2",
"google-auth-library": "10.5.0",
"gray-matter": "4.0.3",
"hono": "catalog:",
Expand All @@ -139,7 +139,7 @@
"immer": "11.1.4",
"jsonc-parser": "3.3.1",
"mime-types": "3.0.2",
"minimatch": "10.0.3",
"minimatch": "11.0.1",
"npm-package-arg": "13.0.2",
"open": "10.1.2",
"opencode-gitlab-auth": "2.1.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/opencode/src/mcp/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const Tokens = Schema.Struct({
refreshToken: Schema.mutableKey(Schema.optional(Schema.String)),
expiresAt: Schema.mutableKey(Schema.optional(Schema.Number)),
scope: Schema.mutableKey(Schema.optional(Schema.String)),
// altimate_change start — MCP SDK >= 1.32 binds stored credentials to the authorization server that issued them
issuer: Schema.mutableKey(Schema.optional(Schema.String)),
// altimate_change end
})
export type Tokens = Schema.Schema.Type<typeof Tokens>

Expand All @@ -19,6 +22,9 @@ export const ClientInfo = Schema.Struct({
clientSecret: Schema.mutableKey(Schema.optional(Schema.String)),
clientIdIssuedAt: Schema.mutableKey(Schema.optional(Schema.Number)),
clientSecretExpiresAt: Schema.mutableKey(Schema.optional(Schema.Number)),
// altimate_change start — see Tokens.issuer
issuer: Schema.mutableKey(Schema.optional(Schema.String)),
// altimate_change end
})
export type ClientInfo = Schema.Schema.Type<typeof ClientInfo>

Expand Down
18 changes: 16 additions & 2 deletions packages/opencode/src/mcp/oauth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
OAuthClientMetadata,
OAuthTokens,
OAuthClientInformation,
OAuthClientInformationFull,
OAuthClientInformationMixed,
} from "@modelcontextprotocol/sdk/shared/auth.js"
import { Effect } from "effect"
import { McpAuth } from "./auth"
Expand Down Expand Up @@ -71,17 +71,22 @@ export class McpOAuthProvider implements OAuthClientProvider {
if (entry.clientInfo.clientSecretExpiresAt && entry.clientInfo.clientSecretExpiresAt < Date.now() / 1000) {
return undefined
}
// altimate_change start — return the full registration: the SDK saves this object back after a token exchange
return {
client_id: entry.clientInfo.clientId,
client_secret: entry.clientInfo.clientSecret,
client_id_issued_at: entry.clientInfo.clientIdIssuedAt,
client_secret_expires_at: entry.clientInfo.clientSecretExpiresAt,
issuer: entry.clientInfo.issuer,
}
// altimate_change end
}

// No client info or URL changed - will trigger dynamic registration
return undefined
}

async saveClientInformation(info: OAuthClientInformationFull): Promise<void> {
async saveClientInformation(info: OAuthClientInformationMixed): Promise<void> {
await Effect.runPromise(
this.auth.updateClientInfo(
this.mcpName,
Expand All @@ -90,6 +95,9 @@ export class McpOAuthProvider implements OAuthClientProvider {
clientSecret: info.client_secret,
clientIdIssuedAt: info.client_id_issued_at,
clientSecretExpiresAt: info.client_secret_expires_at,
// altimate_change start — keep the authorization server binding
issuer: info.issuer,
// altimate_change end
},
this.serverUrl,
),
Expand All @@ -109,6 +117,9 @@ export class McpOAuthProvider implements OAuthClientProvider {
? Math.max(0, Math.floor(entry.tokens.expiresAt - Date.now() / 1000))
: undefined,
scope: entry.tokens.scope,
// altimate_change start — keep the authorization server binding
issuer: entry.tokens.issuer,
// altimate_change end
}
}

Expand All @@ -121,6 +132,9 @@ export class McpOAuthProvider implements OAuthClientProvider {
refreshToken: tokens.refresh_token,
expiresAt: tokens.expires_in ? Date.now() / 1000 + tokens.expires_in : undefined,
scope: tokens.scope,
// altimate_change start — keep the authorization server binding
issuer: tokens.issuer,
// altimate_change end
},
this.serverUrl,
),
Expand Down
86 changes: 86 additions & 0 deletions packages/opencode/test/mcp/oauth-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { test, expect, describe } from "bun:test"
import { McpOAuthProvider, OAUTH_CALLBACK_PORT, OAUTH_CALLBACK_PATH } from "../../src/mcp/oauth-provider"
import type { McpAuth } from "../../src/mcp/auth"
// altimate_change start — SDK auth() round-trip tests
import { auth } from "@modelcontextprotocol/sdk/client/auth.js"
import { Effect } from "effect"
// altimate_change end

// Stub auth — only synchronous getters are exercised in these tests
const stubAuth = {} as McpAuth.Interface
Expand Down Expand Up @@ -59,3 +63,85 @@ describe("McpOAuthProvider.clientMetadata", () => {
expect(provider.clientMetadata.token_endpoint_auth_method).toBe("none")
})
})

// altimate_change start — MCP SDK >= 1.32 stamps stored OAuth credentials with their issuer
describe("McpOAuthProvider with SDK auth()", () => {
const SERVER = "https://mcp.example.com/mcp"
const AS = "https://as.example.com"

const memoryAuth = (initial: McpAuth.Entry) => {
let entry: McpAuth.Entry = { ...initial, serverUrl: SERVER }
const auth: Partial<McpAuth.Interface> = {
get: () => Effect.succeed(entry),
getForUrl: (_name, url) => Effect.succeed(entry.serverUrl === url ? entry : undefined),
set: (_name, next) => Effect.sync(() => void (entry = next)),
updateClientInfo: (_name, clientInfo) => Effect.sync(() => void (entry = { ...entry, clientInfo })),
updateTokens: (_name, tokens) => Effect.sync(() => void (entry = { ...entry, tokens })),
updateCodeVerifier: (_name, codeVerifier) => Effect.sync(() => void (entry = { ...entry, codeVerifier })),
updateOAuthState: (_name, oauthState) => Effect.sync(() => void (entry = { ...entry, oauthState })),
}
return { auth: auth as McpAuth.Interface, current: () => entry }
}

const fakeServer = () => {
const calls: { url: string; body: string }[] = []
const fetchFn = async (input: string | URL, init?: RequestInit) => {
const url = String(input)
calls.push({ url, body: init?.body ? String(init.body) : "" })
const json = (data: unknown) => Response.json(data)
if (url === "https://mcp.example.com/.well-known/oauth-protected-resource/mcp")
return json({ resource: SERVER, authorization_servers: [AS] })
if (url === `${AS}/.well-known/oauth-authorization-server`)
return json({
issuer: AS,
authorization_endpoint: `${AS}/authorize`,
token_endpoint: `${AS}/token`,
registration_endpoint: `${AS}/register`,
response_types_supported: ["code"],
code_challenge_methods_supported: ["S256"],
})
if (url === `${AS}/token`) return json({ access_token: "access", token_type: "Bearer", refresh_token: "refresh" })
if (url === `${AS}/register`) return json({ client_id: "registered", redirect_uris: ["http://127.0.0.1/cb"] })
return new Response("not found", { status: 404 })
}
return { calls, fetchFn }
}

test("token exchange keeps the client secret expiry and stores the issuer", async () => {
const expiresAt = Math.floor(Date.now() / 1000) + 3600
const store = memoryAuth({
clientInfo: { clientId: "dyn", clientSecret: "s", clientIdIssuedAt: 1, clientSecretExpiresAt: expiresAt },
codeVerifier: "verifier",
})
const provider = new McpOAuthProvider("test-server", SERVER, {}, { onRedirect: async () => {} }, store.auth)
const server = fakeServer()

expect(await auth(provider, { serverUrl: SERVER, authorizationCode: "code", fetchFn: server.fetchFn })).toBe(
"AUTHORIZED",
)

expect(store.current().clientInfo).toMatchObject({
clientId: "dyn",
clientIdIssuedAt: 1,
clientSecretExpiresAt: expiresAt,
})
expect(store.current().clientInfo?.issuer).toStartWith(AS)
expect(store.current().tokens?.issuer).toStartWith(AS)
})

test("does not send a refresh token to a different authorization server", async () => {
const store = memoryAuth({
clientInfo: { clientId: "old-client", issuer: "https://old-as.example.com/" },
tokens: { accessToken: "old", refreshToken: "old-refresh", issuer: "https://old-as.example.com/" },
})
const provider = new McpOAuthProvider("test-server", SERVER, {}, { onRedirect: async () => {} }, store.auth)
const server = fakeServer()

expect(await auth(provider, { serverUrl: SERVER, fetchFn: server.fetchFn })).toBe("REDIRECT")

expect(server.calls.some((c) => c.body.includes("old-refresh"))).toBe(false)
expect(server.calls.some((c) => c.url === `${AS}/token`)).toBe(false)
expect(store.current().clientInfo?.clientId).toBe("registered")
})
})
// altimate_change end
Loading
Loading