5.1 KiB
5.1 KiB
CLI ↔ Generator Code Reuse Plan
The goals below align mcporter list, the TypeScript CLI generator, and any future TS export modes so we build (and test) formatting logic once.
1. Shared Example Rendering
- Problem:
list-detail-helpers.tsshortensmcporter callexamples, butgenerate/template.tsstill prints--flagexamples viabuildExampleInvocation. - Plan:
- Export a non-colored
formatExampleBlock()utility (and the internaltruncateExample()helper) fromlist-detail-helpers.ts. - Import that helper inside
renderToolCommand()and replacebuildExampleInvocationwith the shared function-call output. - Drop the duplicate
buildExampleInvocation/pickExampleValuelogic once Commander help uses the shared examples. - Update the generator tests (in
tests/generate-cli.test.ts) to expect the new syntax.
- Export a non-colored
2. Optional Summary in Generated Help (Completed)
- Problem: The runtime CLI prints
// optional (n): …while generated CLIs enumerate every flag. - What we did:
- Reused
selectDisplayOptions()so both CLI/GH generator decide which params to display. - Added
formatOptionalSummary()+buildToolDoc()wiring so each surface appends the same// optional (…)hint only when options were hidden. - Updated
renderToolCommand()to include the shared hint via.addHelpText('afterAll', …)and aligned tests.
- Reused
- Next: No further action unless we change the minimum-visible threshold.
3. Consolidate Example Literal Selection (Completed)
- Problem: CLI used bespoke
buildExampleLiteral/buildFallbackLiterallogic, while generator helpers guessed examples viabuildExampleValue, so the call expressions could diverge. - What we did:
- Moved the literal + fallback logic into
pickExampleLiteral()/buildFallbackLiteral()exported fromsrc/cli/generate/tools.ts. buildToolDocnow imports those helpers, so bothmcporter listand generated CLIs share the same example arguments.- Added unit tests in
tests/generate-cli-helpers.test.tscovering enums, arrays, and ID/url fallbacks to keep behavior locked.
- Moved the literal + fallback logic into
- Next: Consider reusing these helpers for any future docs/export modes that show sample invocations.
4. Usage String Builder Parity (Completed)
- Problem: Commander
.usage()strings were hand-built (and always listed every flag) whilemcporter listused the pseudo-TS formatter, so the two could diverge. - What we did:
- Added
formatFlagUsage()+flagExtrassupport insidebuildToolDoc, so the same selector that powers TS signatures now emits the flag-based usage line. - Updated
renderToolCommand()to consumedoc.flagUsagefor both.summary()and.usage(); optional summaries stay unified throughbuildToolDoc. - Added helper unit tests covering mixed required/optional flags and extra entries (e.g.,
--raw <json>).
- Added
- Next: Expose the shared usage string in
mcporter listonce we add a--flagsview.
5. ToolDocModel Abstraction (Completed)
- Problem: The runtime CLI and generator still built flag labels + option descriptions separately, so changes to detail formatting required touching multiple files.
- What we did:
- Expanded
ToolDocModelwithoptionDocs+formatFlagLabel(), so each tool’s flag label/description is computed once insidebuildToolDoc. - Updated
renderToolCommand()to consumedoc.optionDocsinstead of reassembling strings, leaving only the parser wiring as generator-specific. - Added assertions in
tests/list-detail-helpers.test.tsfor the new metadata, keeping the abstraction covered.
- Expanded
- Next: With the model centralised, future surfaces (e.g.,
--emit-ts) can render signatures/examples/options straight frombuildToolDoc.
6. emit-ts Export Mode (Completed)
- Goal: Provide a typed contract (and optional client) for each MCP server so agents/tools no longer scrape CLI output.
- What we did:
- Added
mcporter emit-ts <server>with--mode types|client, auto-overwriting targets and deriving.d.tsnames for client mode. - Reused
buildToolDoc+ new templates to emit interfaces (promisified signatures + doc comments) and executable wrappers that returnCallResultobjects. - Added
tests/emit-ts.test.tsto snapshot the templates and run the command end-to-end with a stub runtime; documented the workflow indocs/emit-ts.md.
- Added
- Next: Consider supporting per-tool filters and shared schema maps inside the generated client for faster cold starts.
Sequencing recommendation:
- Implement shared example helper (small change, immediate parity). Done –
list-detail-helpers.tsnow exportsformatExampleBlock,formatCallExpressionExample, & the generator consumes them. - Extract
ToolDocModel+ optional summary builder. Done –buildToolDocinsrc/cli/list-detail-helpers.tsnow feeds bothhandleListandrenderToolCommand. - Update generator to consume the shared helpers (examples + optional summary + signatures). In progress – signatures/examples unified;
ToolDocModelstill pending. - Add unit tests for the new helper module.
- Build the
--emit-tsmode once reuse is in place.