inspector/clients/tui/src/utils/uriTemplateToForm.ts
cliffhall 935113d1e0 fix: encode per RFC 3986, and derive the TUI form from the shared parser
Addresses Copilot's round-4 review on #2035 — again reported as "no new
comments" while carrying three suppressed ones. All three reproduced.

1. The TUI form still named its fields from the SDK. `uriTemplateToForm`
   read `UriTemplate.variableNames`, which mangles a name: `{;id}` yields
   ";id" and `{id:3}` yields "id:3". The form therefore submitted
   `{ ";id": "7" }` while the shared expander looked up `id`, found
   nothing, and dropped the expression — the value vanished silently.

   This is the finding that mattered most, because it falsified the claim
   this change is built on. Moving the expander into core only makes the
   clients agree if each client's FORM derives its names from the same
   parser: a form submits under the names it rendered. The TUI now reads
   `templateVariables`, and the SDK template is constructed only to
   validate, preserving the existing malformed-template diagnostic.

   Its `required` flag comes from `requiredGroups`, and only a variable
   that is the sole member of a non-omittable expression is marked: ink-
   form cannot express "any one of these", so marking every member of
   `{a,b}` required would refuse input the expander accepts.

2. `encodeURIComponent` is not RFC 3986's unreserved set — it leaves the
   sub-delims !'()* bare, which RFC 6570 requires encoded for every
   operator except + and #.

   Fixing that settled a design question left open in the previous round.
   With two encoders, the SAME value encoded differently depending on
   whether its expression happened to carry a modifier, since only the
   modifier pushed it onto our path. So delegation is gone entirely: one
   expander, one set of rules. The SDK's `UriTemplate` is still
   constructed, but only to validate a template.

3. The "Any one of: a, b" hint could contradict the disabled submit
   button. A name can sit in a singleton required group AND a shared one
   (`x://{a}/{a,b}`), where the singleton demands that exact field. The
   hint is suppressed in that case.

Signed-off-by: cliffhall <cliff@futurescale.com>
2026-08-16 21:04:20 -04:00

67 lines
2.2 KiB
TypeScript

/**
* Converts URI Template to ink-form format for resource templates
*/
import type { FormStructure, FormSection, FormField } from "ink-form";
import { UriTemplate } from "@modelcontextprotocol/client";
import {
requiredGroups,
templateVariables,
} from "@inspector/core/mcp/uriTemplate.js";
/**
* Converts a URI Template to ink-form structure.
*
* Fields come from core's `templateVariables`, the same parser
* `InspectorClient.readResourceFromTemplate` expands through, so the key this
* form submits is the key the expander looks up. Using the SDK's
* `variableNames` here instead is not merely untidy -- it mangles the name:
* `{;id}` yields `";id"` and `{id:3}` yields `"id:3"`, so the form would submit
* `{ ";id": "7" }` while the expander looks for `id`, silently dropping the
* value and the whole expression with it (#1919).
*
* The SDK template is still constructed, but only to validate: core's parser is
* deliberately lenient (an unclosed `{` becomes literal text), so this is what
* still surfaces a malformed template as an empty form plus a logged error.
*/
export function uriTemplateToForm(
uriTemplate: string,
templateName: string,
): FormStructure {
let fields: FormField[] = [];
try {
new UriTemplate(uriTemplate);
// Only a variable that is the *sole* member of a non-omittable expression
// is genuinely mandatory. RFC 6570 drops undefined names from a multi-name
// expression, so `{a,b}` needs only one of the two -- ink-form cannot
// express "any one of these", and marking both required would refuse input
// the expander accepts.
const mandatory = new Set(
requiredGroups(uriTemplate)
.filter((names) => names.length === 1)
.map(([name]) => name),
);
fields = templateVariables(uriTemplate).map(({ name }) => ({
name,
label: name,
type: "string",
required: mandatory.has(name),
}));
} catch (error) {
// If parsing fails, return empty form
console.error("Failed to parse URI template:", error);
}
const sections: FormSection[] = [
{
title: "Template Variables",
fields,
},
];
return {
title: `Read Resource: ${templateName}`,
sections,
};
}