Skip to content
Open
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
8 changes: 8 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SESSION_KEYS, getServerSpecificKey } from "./lib/constants";
import { AuthDebuggerState, EMPTY_DEBUGGER_STATE } from "./lib/auth-types";
import { OAuthStateMachine } from "./lib/oauth-state-machine";
import { cacheToolOutputSchemas } from "./utils/schemaUtils";
import { saveToolParamsForCache } from "./utils/toolCache";
import { cleanParams } from "./utils/paramUtils";
import type { JsonSchemaType } from "./utils/jsonUtils";
import React, {
Expand Down Expand Up @@ -787,6 +788,12 @@ const App = () => {
const callTool = async (name: string, params: Record<string, unknown>) => {
lastToolCallOriginTabRef.current = currentTabRef.current;

// Save tool parameters to cache before making the call
const tool = tools.find((t) => t.name === name);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save the tool parameters to localStorage cache on execution.

if (tool && sseUrl) {
saveToolParamsForCache(sseUrl, name, tool, params);
}

try {
// Find the tool schema to clean parameters properly
const tool = tools.find((t) => t.name === name);
Expand Down Expand Up @@ -1129,6 +1136,7 @@ const App = () => {
clearError("resources");
readResource(uri);
}}
serverUrl={sseUrl}
/>
<ConsoleTab />
<PingTab
Expand Down
31 changes: 26 additions & 5 deletions client/src/components/ToolsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { useEffect, useState, useRef } from "react";
import ListPane from "./ListPane";
import JsonView from "./JsonView";
import ToolResults from "./ToolResults";
import { loadToolParamsFromCache } from "@/utils/toolCache";
import { useToast } from "@/lib/hooks/useToast";
import useCopy from "@/lib/hooks/useCopy";

Expand All @@ -56,6 +57,7 @@ const ToolsTab = ({
error,
resourceContent,
onReadResource,
serverUrl,
}: {
tools: Tool[];
listTools: () => void;
Expand All @@ -68,6 +70,7 @@ const ToolsTab = ({
error: string | null;
resourceContent: Record<string, string>;
onReadResource?: (uri: string) => void;
serverUrl: string;
}) => {
const [params, setParams] = useState<Record<string, unknown>>({});
const [isToolRunning, setIsToolRunning] = useState(false);
Expand All @@ -88,24 +91,42 @@ const ToolsTab = ({
};

useEffect(() => {
const params = Object.entries(
selectedTool?.inputSchema.properties ?? [],
if (!selectedTool) {
setParams({});
return;
}

// Generate default parameters from schema
const defaultParams = Object.entries(
selectedTool.inputSchema.properties ?? [],
).map(([key, value]) => [
key,
generateDefaultValue(
value as JsonSchemaType,
key,
selectedTool?.inputSchema as JsonSchemaType,
selectedTool.inputSchema as JsonSchemaType,
),
]);
setParams(Object.fromEntries(params));
const defaultParamsObj = Object.fromEntries(defaultParams);

// Fetch cached params from localStorage if they exist
const cachedParams = loadToolParamsFromCache(
serverUrl,
selectedTool.name,
selectedTool,
);

// Merge cached params with defaults, giving preference to cached values
const mergedParams = { ...defaultParamsObj, ...cachedParams };
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First load the empty default params, then load the cached ones if they exist.


setParams(mergedParams);

// Reset validation errors when switching tools
setHasValidationErrors(false);

// Clear form refs for the previous tool
formRefs.current = {};
}, [selectedTool]);
}, [selectedTool, serverUrl]);

return (
<TabsContent value="tools">
Expand Down
1 change: 1 addition & 0 deletions client/src/components/__tests__/ToolsTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe("ToolsTab", () => {
error: null,
resourceContent: {},
onReadResource: jest.fn(),
serverUrl: "http://localhost:3000",
};

const renderToolsTab = (props = {}) => {
Expand Down
63 changes: 63 additions & 0 deletions client/src/utils/toolCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Tool } from "@modelcontextprotocol/sdk/types.js";

export interface CacheKey {
serverUrl: string;
toolName: string;
paramNames: string[];
}

export function generateCacheKey(
serverUrl: string,
toolName: string,
tool: Tool,
): string {
const paramNames = Object.keys(tool.inputSchema.properties ?? {}).sort();
const key = `tool_params_${btoa(`${serverUrl}_${toolName}_${JSON.stringify(paramNames)}`)}`;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures uniqueness in localStorage

return key;
}

export function saveToolParamsForCache(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We create a tool cache helper util that helps save and load things from localStorage

serverUrl: string,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably remove serverUrl. Lmk if that'd be better, but also down to just keep it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I can see advantages either way. Probably safer to start conservative, with the additional uniqueness of the server URL?

toolName: string,
tool: Tool,
params: Record<string, unknown>,
): void {
try {
const cacheKey = generateCacheKey(serverUrl, toolName, tool);
const cacheData = {
params,
timestamp: Date.now(),
toolName,
serverUrl,
};
localStorage.setItem(cacheKey, JSON.stringify(cacheData));
} catch (error) {
console.warn("Failed to save tool parameters to cache:", error);
}
}

export function loadToolParamsFromCache(
serverUrl: string,
toolName: string,
tool: Tool,
): Record<string, unknown> | null {
try {
const cacheKey = generateCacheKey(serverUrl, toolName, tool);
const cached = localStorage.getItem(cacheKey);

if (!cached) {
return null;
}

const cacheData = JSON.parse(cached);

if (!cacheData.params) {
return null;
}

return cacheData.params;
} catch (error) {
console.warn("Failed to load tool parameters from cache:", error);
return null;
}
}