Merge branch 'main' into oleina/logging

This commit is contained in:
Ola Hungerford 2025-07-26 09:43:20 -07:00 • committed by GitHub
commit 1ed920297f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,15 +29,20 @@ import {
PromptListChangedNotificationSchema,
Progress,
LoggingLevel,
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,12 +62,16 @@ interface UseConnectionOptions {
env: Record<string, string>;
bearerToken?: string;
headerName?: string;
oauthClientId?: string;
oauthScope?: string;
config: InspectorConfig;
onNotification?: (notification: Notification) => void;
onStdErrNotification?: (notification: Notification) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onPendingRequest?: (request: any, resolve: any, reject: any) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onElicitationRequest?: (request: any, resolve: any) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getRoots?: () => any[];
defaultLoggingLevel?: LoggingLevel;
}
@ -75,10 +84,13 @@ export function useConnection({
env,
bearerToken,
headerName,
oauthClientId,
oauthScope,
config,
onNotification,
onStdErrNotification,
onPendingRequest,
onElicitationRequest,
getRoots,
defaultLoggingLevel,
}: UseConnectionOptions) {
@ -94,7 +106,23 @@ export function useConnection({
const [requestHistory, setRequestHistory] = useState<
{ request: string; response?: string }[]
>([]);
const [completionsSupported, setCompletionsSupported] = useState(true);
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) => [
@ -282,7 +310,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";
}
@ -298,6 +329,7 @@ export function useConnection({
{
capabilities: {
sampling: {},
elicitation: {},
roots: {
listChanged: true,
},
@ -508,7 +540,7 @@ export function useConnection({
throw error;
}
setServerCapabilities(capabilities ?? null);
setCompletionsSupported(true); // Reset completions support on new connection
setCompletionsSupported(capabilities?.completions !== undefined);
if (onPendingRequest) {
client.setRequestHandler(CreateMessageRequestSchema, (request) => {
@ -526,6 +558,13 @@ export function useConnection({
if (capabilities?.logging && defaultLoggingLevel) {
await client.setLoggingLevel(defaultLoggingLevel);
if (onElicitationRequest) {
client.setRequestHandler(ElicitRequestSchema, async (request) => {
return new Promise((resolve) => {
onElicitationRequest(request, resolve);
});
});
}
setMcpClient(client);