feat: add elicitations to useConnection

This commit is contained in:
Alex Andru 2025-06-19 15:03:39 +02:00
commit e163aea0e0

View file

@ -28,6 +28,7 @@ import {
ToolListChangedNotificationSchema, ToolListChangedNotificationSchema,
PromptListChangedNotificationSchema, PromptListChangedNotificationSchema,
Progress, Progress,
ElicitRequestSchema,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js"; import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
import { useState } from "react"; import { useState } from "react";
@ -62,6 +63,8 @@ interface UseConnectionOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
onPendingRequest?: (request: any, resolve: any, reject: any) => void; onPendingRequest?: (request: any, resolve: any, reject: any) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // 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[]; getRoots?: () => any[];
} }
@ -77,6 +80,7 @@ export function useConnection({
onNotification, onNotification,
onStdErrNotification, onStdErrNotification,
onPendingRequest, onPendingRequest,
onElicitationRequest,
getRoots, getRoots,
}: UseConnectionOptions) { }: UseConnectionOptions) {
const [connectionStatus, setConnectionStatus] = const [connectionStatus, setConnectionStatus] =
@ -295,6 +299,7 @@ export function useConnection({
{ {
capabilities: { capabilities: {
sampling: {}, sampling: {},
elicitation: {},
roots: { roots: {
listChanged: true, listChanged: true,
}, },
@ -521,6 +526,14 @@ export function useConnection({
}); });
} }
if (onElicitationRequest) {
client.setRequestHandler(ElicitRequestSchema, async (request) => {
return new Promise((resolve) => {
onElicitationRequest(request, resolve);
});
});
}
setMcpClient(client); setMcpClient(client);
setConnectionStatus("connected"); setConnectionStatus("connected");
} catch (e) { } catch (e) {