Merge branch 'main' into feature/elicitations
This commit is contained in:
commit
eda6c05ba1
4 changed files with 237 additions and 67 deletions
|
|
@ -9,8 +9,64 @@ import {
|
|||
} from "@modelcontextprotocol/sdk/shared/auth.js";
|
||||
import { SESSION_KEYS, getServerSpecificKey } from "./constants";
|
||||
|
||||
export const getClientInformationFromSessionStorage = async ({
|
||||
serverUrl,
|
||||
isPreregistered,
|
||||
}: {
|
||||
serverUrl: string;
|
||||
isPreregistered?: boolean;
|
||||
}) => {
|
||||
const key = getServerSpecificKey(
|
||||
isPreregistered
|
||||
? SESSION_KEYS.PREREGISTERED_CLIENT_INFORMATION
|
||||
: SESSION_KEYS.CLIENT_INFORMATION,
|
||||
serverUrl,
|
||||
);
|
||||
|
||||
const value = sessionStorage.getItem(key);
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return await OAuthClientInformationSchema.parseAsync(JSON.parse(value));
|
||||
};
|
||||
|
||||
export const saveClientInformationToSessionStorage = ({
|
||||
serverUrl,
|
||||
clientInformation,
|
||||
isPreregistered,
|
||||
}: {
|
||||
serverUrl: string;
|
||||
clientInformation: OAuthClientInformation;
|
||||
isPreregistered?: boolean;
|
||||
}) => {
|
||||
const key = getServerSpecificKey(
|
||||
isPreregistered
|
||||
? SESSION_KEYS.PREREGISTERED_CLIENT_INFORMATION
|
||||
: SESSION_KEYS.CLIENT_INFORMATION,
|
||||
serverUrl,
|
||||
);
|
||||
sessionStorage.setItem(key, JSON.stringify(clientInformation));
|
||||
};
|
||||
|
||||
export const clearClientInformationFromSessionStorage = ({
|
||||
serverUrl,
|
||||
isPreregistered,
|
||||
}: {
|
||||
serverUrl: string;
|
||||
isPreregistered?: boolean;
|
||||
}) => {
|
||||
const key = getServerSpecificKey(
|
||||
isPreregistered
|
||||
? SESSION_KEYS.PREREGISTERED_CLIENT_INFORMATION
|
||||
: SESSION_KEYS.CLIENT_INFORMATION,
|
||||
serverUrl,
|
||||
);
|
||||
sessionStorage.removeItem(key);
|
||||
};
|
||||
|
||||
export class InspectorOAuthClientProvider implements OAuthClientProvider {
|
||||
constructor(public serverUrl: string) {
|
||||
constructor(protected serverUrl: string) {
|
||||
// Save the server URL to session storage
|
||||
sessionStorage.setItem(SESSION_KEYS.SERVER_URL, serverUrl);
|
||||
}
|
||||
|
|
@ -31,24 +87,30 @@ export class InspectorOAuthClientProvider implements OAuthClientProvider {
|
|||
}
|
||||
|
||||
async clientInformation() {
|
||||
const key = getServerSpecificKey(
|
||||
SESSION_KEYS.CLIENT_INFORMATION,
|
||||
this.serverUrl,
|
||||
);
|
||||
const value = sessionStorage.getItem(key);
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
// Try to get the preregistered client information from session storage first
|
||||
const preregisteredClientInformation =
|
||||
await getClientInformationFromSessionStorage({
|
||||
serverUrl: this.serverUrl,
|
||||
isPreregistered: true,
|
||||
});
|
||||
|
||||
return await OAuthClientInformationSchema.parseAsync(JSON.parse(value));
|
||||
// If no preregistered client information is found, get the dynamically registered client information
|
||||
return (
|
||||
preregisteredClientInformation ??
|
||||
(await getClientInformationFromSessionStorage({
|
||||
serverUrl: this.serverUrl,
|
||||
isPreregistered: false,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
saveClientInformation(clientInformation: OAuthClientInformation) {
|
||||
const key = getServerSpecificKey(
|
||||
SESSION_KEYS.CLIENT_INFORMATION,
|
||||
this.serverUrl,
|
||||
);
|
||||
sessionStorage.setItem(key, JSON.stringify(clientInformation));
|
||||
// Save the dynamically registered client information to session storage
|
||||
saveClientInformationToSessionStorage({
|
||||
serverUrl: this.serverUrl,
|
||||
clientInformation,
|
||||
isPreregistered: false,
|
||||
});
|
||||
}
|
||||
|
||||
async tokens() {
|
||||
|
|
@ -92,9 +154,10 @@ export class InspectorOAuthClientProvider implements OAuthClientProvider {
|
|||
}
|
||||
|
||||
clear() {
|
||||
sessionStorage.removeItem(
|
||||
getServerSpecificKey(SESSION_KEYS.CLIENT_INFORMATION, this.serverUrl),
|
||||
);
|
||||
clearClientInformationFromSessionStorage({
|
||||
serverUrl: this.serverUrl,
|
||||
isPreregistered: false,
|
||||
});
|
||||
sessionStorage.removeItem(
|
||||
getServerSpecificKey(SESSION_KEYS.TOKENS, this.serverUrl),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ export const SESSION_KEYS = {
|
|||
SERVER_URL: "mcp_server_url",
|
||||
TOKENS: "mcp_tokens",
|
||||
CLIENT_INFORMATION: "mcp_client_information",
|
||||
PREREGISTERED_CLIENT_INFORMATION: "mcp_preregistered_client_information",
|
||||
SERVER_METADATA: "mcp_server_metadata",
|
||||
AUTH_DEBUGGER_STATE: "mcp_auth_debugger_state",
|
||||
} as const;
|
||||
|
|
|
|||
|
|
@ -86,6 +86,8 @@ jest.mock("../../auth", () => ({
|
|||
InspectorOAuthClientProvider: jest.fn().mockImplementation(() => ({
|
||||
tokens: jest.fn().mockResolvedValue({ access_token: "mock-token" }),
|
||||
})),
|
||||
clearClientInformationFromSessionStorage: jest.fn(),
|
||||
saveClientInformationToSessionStorage: jest.fn(),
|
||||
}));
|
||||
|
||||
describe("useConnection", () => {
|
||||
|
|
|
|||
|
|
@ -31,13 +31,17 @@ import {
|
|||
ElicitRequestSchema,
|
||||
} from "@modelcontextprotocol/sdk/types.js";
|
||||
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useToast } from "@/lib/hooks/useToast";
|
||||
import { z } from "zod";
|
||||
import { ConnectionStatus } from "../constants";
|
||||
import { Notification, StdErrNotificationSchema } from "../notificationTypes";
|
||||
import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
|
||||
import { InspectorOAuthClientProvider } from "../auth";
|
||||
import {
|
||||
clearClientInformationFromSessionStorage,
|
||||
InspectorOAuthClientProvider,
|
||||
saveClientInformationToSessionStorage,
|
||||
} from "../auth";
|
||||
import packageJson from "../../../package.json";
|
||||
import {
|
||||
getMCPProxyAddress,
|
||||
|
|
@ -57,6 +61,8 @@ interface UseConnectionOptions {
|
|||
env: Record<string, string>;
|
||||
bearerToken?: string;
|
||||
headerName?: string;
|
||||
oauthClientId?: string;
|
||||
oauthScope?: string;
|
||||
config: InspectorConfig;
|
||||
onNotification?: (notification: Notification) => void;
|
||||
onStdErrNotification?: (notification: Notification) => void;
|
||||
|
|
@ -76,6 +82,8 @@ export function useConnection({
|
|||
env,
|
||||
bearerToken,
|
||||
headerName,
|
||||
oauthClientId,
|
||||
oauthScope,
|
||||
config,
|
||||
onNotification,
|
||||
onStdErrNotification,
|
||||
|
|
@ -97,6 +105,22 @@ export function useConnection({
|
|||
>([]);
|
||||
const [completionsSupported, setCompletionsSupported] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!oauthClientId) {
|
||||
clearClientInformationFromSessionStorage({
|
||||
serverUrl: sseUrl,
|
||||
isPreregistered: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
saveClientInformationToSessionStorage({
|
||||
serverUrl: sseUrl,
|
||||
clientInformation: { client_id: oauthClientId },
|
||||
isPreregistered: true,
|
||||
});
|
||||
}, [oauthClientId, sseUrl]);
|
||||
|
||||
const pushHistory = (request: object, response?: object) => {
|
||||
setRequestHistory((prev) => [
|
||||
...prev,
|
||||
|
|
@ -283,7 +307,10 @@ export function useConnection({
|
|||
if (is401Error(error)) {
|
||||
const serverAuthProvider = new InspectorOAuthClientProvider(sseUrl);
|
||||
|
||||
const result = await auth(serverAuthProvider, { serverUrl: sseUrl });
|
||||
const result = await auth(serverAuthProvider, {
|
||||
serverUrl: sseUrl,
|
||||
scope: oauthScope,
|
||||
});
|
||||
return result === "AUTHORIZED";
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue