* core: refactor serverList.ts object↔Record casts into typed helper (#1690) Remove the four pre-existing `as unknown as Record<string, unknown>` / `as unknown as MCPServerConfig` casts in core/mcp/serverList.ts: - stripInspectorFields: rewrite as clone + `delete` over INSPECTOR_FIELD_KEYS. Every Inspector-extension key is optional on StoredMCPServer, so the result is still (a subtype of) MCPServerConfig with no cast. - oauth-strip in extractSecretsFromStored: `oauth` is optional, so a plain `delete stripped.oauth` is legal — drop the cast. - mcpConfigToServerEntries: route the one genuinely-needed widening through a single documented `toRecord(value: object)` helper. Taking `object` makes the single `as Record<string, unknown>` legal (no TS2352), so the double cast disappears from the call site. Net: zero `as unknown as` in serverList.ts; the one remaining structural cast lives inside the audited toRecord helper. Behavior-neutral — existing coverage (79 serverList tests) unchanged; full `npm run ci` passes. Closes #1690 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * core+web+test-servers: eliminate/justify remaining `as unknown as` casts (#1690) Codebase-wide follow-on to the serverList refactor: audit every `as unknown as` double cast outside tests and either remove it or ensure it carries a justifying comment (the review bar is "no *unjustified* double cast"). Removed (widened via a shared helper or narrowed to a single cast): - Promote the serverList `toRecord(value: object)` widening helper to core/json/jsonUtils.ts and reuse it at core/mcp/node/config.ts (normalize loop), core/mcp/remote/node/server.ts (pino level dispatch), and clients/web/src/App.tsx (window global lookup). - inspectorClient.rawWireRequest: type the frame as `JSONRPCRequest` and narrow only `params` with a single structural cast (the SDK's `_meta`-typed params is what blocked direct assignment). - toolOutputValidation: SDK `outputSchema` → `JsonSchemaType` is a single cast. - test-server-fixtures: the optional `task` handle is reachable with a single cast (target field is optional). Justified in place (genuinely unavoidable — private SDK internals, partial interface stubs, third-party/nominal bridges): the inspectorClient private-map accesses, resourceContext + tokenAuthProvider partial `OAuthClientProvider` stubs, the ext-apps peer bridge, modern/legacy `_requestHandlers` bypasses, and the happy-dom `matchMedia` test mock. Behavior-neutral. Full `npm run ci` passes (validate, ≥90 coverage gate, smoke, Storybook). Refs #1690 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * address @claude review nits: consistent cast justifications + drop reflow noise (#1690) - test-servers/src/modern-tasks.ts: add an inline justification above the `_requestHandlers` double cast, mirroring the sibling in composable-test-server.ts (was leaning on the function JSDoc only). - Annotate the partial-`AppBridge` mock double casts in the story fixtures (AppsScreen ×2, AppRenderer, InspectorView) so no non-test-file double cast is unjustified. - inspectorClient.ts: revert unrelated Prettier reflow hunks (protocolVersion, task-content array, timeout-reject wrap, reconnect guard) so the diff is the single rawWireRequest cast change only. Behavior-neutral (comment/format-only). `npm run ci` green apart from a known flaky redaction test whose assertion collided with a random request id. Refs #1690 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * actually revert the reflow noise this time (#1690) The reflow reverts in cc187ea were silently undone by a `prettier --write` I ran on the same file right after, so the four Prettier-only hunks from 6a3afa4a were still in the diff (caught in re-review). Revert them for real now — and without re-running Prettier on the file: - core/mcp/inspectorClient.ts: restore the protocolVersion line, task-content array, timeout-reject call, and reconnect guard to their origin/v2/main form, so the file's diff is only the single rawWireRequest cast change. - test-servers/src/modern-tasks.ts: restore the ModernTaskStatus union to its one-line form (the leading-`|` reflow was also Prettier churn), leaving only the new `_requestHandlers` justification comment. Formatting-only; `npm run validate` green. Refs #1690 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
110 lines
2.9 KiB
TypeScript
110 lines
2.9 KiB
TypeScript
import type { Tool } from "@modelcontextprotocol/client";
|
|
|
|
/**
|
|
* JSON value type used across the inspector project
|
|
*/
|
|
export type JsonValue =
|
|
| string
|
|
| number
|
|
| boolean
|
|
| null
|
|
| undefined
|
|
| JsonValue[]
|
|
| { [key: string]: JsonValue };
|
|
|
|
export type JsonObject = { [key: string]: JsonValue };
|
|
|
|
/**
|
|
* Widen a typed object to a generic string-keyed record so its keys can be
|
|
* iterated or read/written generically. Many of the project's config/SDK types
|
|
* (`StoredMCPServer`, `MCPServerConfig`, `pino.Logger`, DOM `Window`, …) have no
|
|
* index signature, so a direct `value as Record<string, unknown>` at a call
|
|
* site is a TS2352 error that would otherwise force an `as unknown as` double
|
|
* cast. Taking the argument as the general `object` type makes the single `as`
|
|
* legal — `Record<string, unknown>` is assignable to `object`, so the two types
|
|
* sufficiently overlap — letting this one audited spot own the widening while
|
|
* the double casts stay out of the call sites. Purely a structural view of the
|
|
* same object; no runtime effect.
|
|
*/
|
|
export function toRecord(value: object): Record<string, unknown> {
|
|
return value as Record<string, unknown>;
|
|
}
|
|
|
|
/**
|
|
* Simple schema type for parameter conversion
|
|
*/
|
|
type ParameterSchema = {
|
|
type?: string;
|
|
};
|
|
|
|
/**
|
|
* Convert a string parameter value to the appropriate JSON type based on schema
|
|
*/
|
|
export function convertParameterValue(
|
|
value: string,
|
|
schema: ParameterSchema,
|
|
): JsonValue {
|
|
if (!value) {
|
|
return value;
|
|
}
|
|
|
|
if (schema.type === "number" || schema.type === "integer") {
|
|
return Number(value);
|
|
}
|
|
|
|
if (schema.type === "boolean") {
|
|
return value.toLowerCase() === "true";
|
|
}
|
|
|
|
if (schema.type === "object" || schema.type === "array") {
|
|
try {
|
|
return JSON.parse(value) as JsonValue;
|
|
} catch {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Convert string parameters to JSON values based on tool schema
|
|
*/
|
|
export function convertToolParameters(
|
|
tool: Tool,
|
|
params: Record<string, string>,
|
|
): Record<string, JsonValue> {
|
|
const result: Record<string, JsonValue> = {};
|
|
const properties = tool.inputSchema?.properties || {};
|
|
|
|
for (const [key, value] of Object.entries(params)) {
|
|
const paramSchema = properties[key] as ParameterSchema | undefined;
|
|
|
|
if (paramSchema) {
|
|
result[key] = convertParameterValue(value, paramSchema);
|
|
} else {
|
|
result[key] = value;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Convert prompt arguments (JsonValue) to strings for prompt API
|
|
*/
|
|
export function convertPromptArguments(
|
|
args: Record<string, JsonValue>,
|
|
): Record<string, string> {
|
|
const stringArgs: Record<string, string> = {};
|
|
for (const [key, value] of Object.entries(args)) {
|
|
if (typeof value === "string") {
|
|
stringArgs[key] = value;
|
|
} else if (value === null || value === undefined) {
|
|
stringArgs[key] = String(value);
|
|
} else {
|
|
stringArgs[key] = JSON.stringify(value);
|
|
}
|
|
}
|
|
return stringArgs;
|
|
}
|