|
1 | 1 | import { InspectorConfig } from "@/lib/configurationTypes";
|
2 |
| -import { DEFAULT_MCP_PROXY_LISTEN_PORT } from "@/lib/constants"; |
| 2 | +import { |
| 3 | + DEFAULT_MCP_PROXY_LISTEN_PORT, |
| 4 | + DEFAULT_INSPECTOR_CONFIG, |
| 5 | +} from "@/lib/constants"; |
3 | 6 |
|
4 | 7 | export const getMCPProxyAddress = (config: InspectorConfig): string => {
|
5 | 8 | const proxyFullAddress = config.MCP_PROXY_FULL_ADDRESS.value as string;
|
@@ -67,3 +70,57 @@ export const getInitialArgs = (): string => {
|
67 | 70 | if (param) return param;
|
68 | 71 | return localStorage.getItem("lastArgs") || "";
|
69 | 72 | };
|
| 73 | + |
| 74 | +// Returns a map of config key -> value from query params if present |
| 75 | +export const getConfigOverridesFromQueryParams = ( |
| 76 | + defaultConfig: InspectorConfig, |
| 77 | +): Partial<InspectorConfig> => { |
| 78 | + const url = new URL(window.location.href); |
| 79 | + const overrides: Partial<InspectorConfig> = {}; |
| 80 | + for (const key of Object.keys(defaultConfig)) { |
| 81 | + const param = url.searchParams.get(key); |
| 82 | + if (param !== null) { |
| 83 | + // Try to coerce to correct type based on default value |
| 84 | + const defaultValue = defaultConfig[key as keyof InspectorConfig].value; |
| 85 | + let value: string | number | boolean = param; |
| 86 | + if (typeof defaultValue === "number") { |
| 87 | + value = Number(param); |
| 88 | + } else if (typeof defaultValue === "boolean") { |
| 89 | + value = param === "true"; |
| 90 | + } |
| 91 | + overrides[key as keyof InspectorConfig] = { |
| 92 | + ...defaultConfig[key as keyof InspectorConfig], |
| 93 | + value, |
| 94 | + }; |
| 95 | + } |
| 96 | + } |
| 97 | + return overrides; |
| 98 | +}; |
| 99 | + |
| 100 | +export const initializeInspectorConfig = ( |
| 101 | + localStorageKey: string, |
| 102 | +): InspectorConfig => { |
| 103 | + const savedConfig = localStorage.getItem(localStorageKey); |
| 104 | + let baseConfig: InspectorConfig; |
| 105 | + if (savedConfig) { |
| 106 | + // merge default config with saved config |
| 107 | + const mergedConfig = { |
| 108 | + ...DEFAULT_INSPECTOR_CONFIG, |
| 109 | + ...JSON.parse(savedConfig), |
| 110 | + } as InspectorConfig; |
| 111 | + |
| 112 | + // update description of keys to match the new description (in case of any updates to the default config description) |
| 113 | + for (const [key, value] of Object.entries(mergedConfig)) { |
| 114 | + mergedConfig[key as keyof InspectorConfig] = { |
| 115 | + ...value, |
| 116 | + label: DEFAULT_INSPECTOR_CONFIG[key as keyof InspectorConfig].label, |
| 117 | + }; |
| 118 | + } |
| 119 | + baseConfig = mergedConfig; |
| 120 | + } else { |
| 121 | + baseConfig = DEFAULT_INSPECTOR_CONFIG; |
| 122 | + } |
| 123 | + // Apply query param overrides |
| 124 | + const overrides = getConfigOverridesFromQueryParams(DEFAULT_INSPECTOR_CONFIG); |
| 125 | + return { ...baseConfig, ...overrides }; |
| 126 | +}; |
0 commit comments