Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Keep module DAG clean: lower modules must not import from higher ones.
- File system for config discovery
- OAuth flows (use in-memory store)

**Isolation:** test scripts pass `--isolate` because `mock.module()` is process-global; without it a mocked module leaks into other files.

**Test file naming:**

- Unit tests: `{module}.test.ts` in same directory
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
"scripts": {
"build": "bun build src/cli.ts src/index.ts src/internal.ts --outdir dist --target node --sourcemap",
"clean": "rm -rf dist",
"test": "bun test src",
"test:e2e": "bun test test/e2e",
"test:all": "bun test",
"test": "bun test --isolate src",
"test:e2e": "bun test --isolate test/e2e",
"test:all": "bun test --isolate",
"typecheck": "bun tsc -p tsconfig.json --noEmit",
"format": "bun prettier --write .",
"format:check": "bun prettier --check .",
Expand Down
27 changes: 27 additions & 0 deletions src/mcp-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
import { browserAuth, inMemoryStore } from "oauth-callback/mcp";
import { MockOAuthServer } from "../test/utils/mock-oauth-server";
import { createMcpConnection } from "./mcp-client.js";
import type {
OAuthClientInformationFull,
OAuthTokens,
Expand Down Expand Up @@ -320,3 +321,29 @@ describe("OAuth Authentication", () => {
});
});
});

describe("createMcpConnection", () => {
test("forwards oauth options to browserAuth (storeKey)", async () => {
const store = inMemoryStore();
await store.set("custom-key", { accessToken: "custom-token" });

// Capture the first request, then abort the connection attempt
const requests: Headers[] = [];
const fetch = (async (
_url: URL | string,
init?: RequestInit,
): Promise<Response> => {
requests.push(new Headers(init?.headers));
throw new Error("stop");
}) as typeof globalThis.fetch;

await expect(
createMcpConnection(
{ name: "test", type: "http", url: "http://localhost:1/mcp" },
{ fetch, oauth: { store, storeKey: "custom-key" } },
),
).rejects.toThrow("stop");

expect(requests[0]?.get("authorization")).toBe("Bearer custom-token");
});
});
23 changes: 4 additions & 19 deletions src/mcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import type {
ServerCapabilities,
Tool,
} from "@modelcontextprotocol/sdk/types.js";
import {
browserAuth,
type BrowserAuthOptions,
inMemoryStore,
} from "oauth-callback/mcp";
import { browserAuth, type BrowserAuthOptions } from "oauth-callback/mcp";
import type { McpServerConfig } from "./types.js";

export interface McpClientConfig {
Expand All @@ -33,7 +29,7 @@ export interface McpClientConfig {
/** Client version for compatibility checks */
version?: string;
/** OAuth 2.1 auth settings */
oauth?: Partial<BrowserAuthOptions>;
oauth?: BrowserAuthOptions;
/** Custom fetch for proxies/interceptors */
fetch?: typeof fetch;
/** Request timeout in ms (applies to HTTP transport) */
Expand Down Expand Up @@ -84,19 +80,8 @@ export async function createMcpConnection(
// OAuth required for http/sse transports
let authProvider: any | undefined;
if (server.type === "http" || server.type === "sse") {
const port = config.oauth?.port || 3000;

authProvider = browserAuth({
port,
hostname: config.oauth?.hostname || "localhost",
callbackPath: config.oauth?.callbackPath || "/callback",
store: config.oauth?.store || inMemoryStore(),
scope: config.oauth?.scope,
clientId: config.oauth?.clientId,
clientSecret: config.oauth?.clientSecret,
launch: config.oauth?.launch,
authTimeout: config.oauth?.authTimeout || 300000,
});
// Forward options as-is; browserAuth() owns the defaults (port, store, ...).
authProvider = browserAuth(config.oauth);
}

// Transport factory - creates fresh transport for OAuth retry
Expand Down
Loading