From de474e54114a427cb14086466450429d06c6fcf3 Mon Sep 17 00:00:00 2001 From: Dennis Geurts Date: Sat, 12 Jul 2025 12:42:21 +0200 Subject: [PATCH] feat: Add context argument to completion handlers This commit adds a `context` argument to the completion handlers in `PromptsTab`, `ResourcesTab`, `useCompletionState`, and `useConnection`. The context argument allows passing additional information to the completion engine, which can be used to improve the quality of the completions. The context is passed as a dictionary of key-value pairs. The `ResourcesTab` component was also updated to pass `templateValues` as the `context` to the completion handler. The `useConnection` hook was updated to include the context in the completion request sent to the server. --- client/src/components/PromptsTab.tsx | 1 + client/src/components/ResourcesTab.tsx | 2 ++ client/src/lib/hooks/useCompletionState.ts | 6 ++++++ client/src/lib/hooks/useConnection.ts | 7 +++++++ 4 files changed, 16 insertions(+) diff --git a/client/src/components/PromptsTab.tsx b/client/src/components/PromptsTab.tsx index 466b3d52..c0e188c0 100644 --- a/client/src/components/PromptsTab.tsx +++ b/client/src/components/PromptsTab.tsx @@ -48,6 +48,7 @@ const PromptsTab = ({ ref: PromptReference | ResourceReference, argName: string, value: string, + context?: Record, ) => Promise; completionsSupported: boolean; promptContent: string; diff --git a/client/src/components/ResourcesTab.tsx b/client/src/components/ResourcesTab.tsx index 8ec3f91b..874e0ff7 100644 --- a/client/src/components/ResourcesTab.tsx +++ b/client/src/components/ResourcesTab.tsx @@ -52,6 +52,7 @@ const ResourcesTab = ({ ref: ResourceReference | PromptReference, argName: string, value: string, + context?: Record, ) => Promise; completionsSupported: boolean; resourceContent: string; @@ -94,6 +95,7 @@ const ResourcesTab = ({ }, key, value, + templateValues, ); } }; diff --git a/client/src/lib/hooks/useCompletionState.ts b/client/src/lib/hooks/useCompletionState.ts index 71492e31..a63968ae 100644 --- a/client/src/lib/hooks/useCompletionState.ts +++ b/client/src/lib/hooks/useCompletionState.ts @@ -28,6 +28,7 @@ export function useCompletionState( ref: ResourceReference | PromptReference, argName: string, value: string, + context?: Record, signal?: AbortSignal, ) => Promise, completionsSupported: boolean = true, @@ -66,6 +67,7 @@ export function useCompletionState( ref: ResourceReference | PromptReference, argName: string, value: string, + context?: Record, ) => { if (!completionsSupported) { return; @@ -82,10 +84,13 @@ export function useCompletionState( })); try { + console.log(state); + const values = await handleCompletion( ref, argName, value, + context, abortController.signal, ); @@ -97,6 +102,7 @@ export function useCompletionState( })); } } catch { + console.error("completion failed"); if (!abortController.signal.aborted) { setState((prev) => ({ ...prev, diff --git a/client/src/lib/hooks/useConnection.ts b/client/src/lib/hooks/useConnection.ts index 5e592bde..97c36fdd 100644 --- a/client/src/lib/hooks/useConnection.ts +++ b/client/src/lib/hooks/useConnection.ts @@ -170,6 +170,7 @@ export function useConnection({ ref: ResourceReference | PromptReference, argName: string, value: string, + context?: Record, signal?: AbortSignal, ): Promise => { if (!mcpClient || !completionsSupported) { @@ -187,6 +188,12 @@ export function useConnection({ }, }; + if (context) { + request["params"]["context"] = { + arguments: context, + }; + } + try { const response = await makeRequest(request, CompleteResultSchema, { signal,