refactor(mcp): extract registerTypedAppTool + typedAppSpec helper (KLA-419)#40
Open
jklaassenjc wants to merge 1 commit into
Open
refactor(mcp): extract registerTypedAppTool + typedAppSpec helper (KLA-419)#40jklaassenjc wants to merge 1 commit into
jklaassenjc wants to merge 1 commit into
Conversation
…A-419) After 1.17.0, the typed-input MCP App registration path was triplicated across apps_user.go, apps_device.go, apps_insights.go — each ~40 lines of identical mcp.Meta + wrap-fetch-errorResult / jsonResult + AddResource boilerplate. Every future typed MCP App would pay the same tax until extracted. This change adds: - typedAppSpec[In any]: mirrors appSpec for typed-input apps. Can't live in a []typedAppSpec slice (each In differs), but standardizes the per-app literal callers construct. - registerTypedAppTool[In](s, spec): wires the tool half with _meta.ui.resourceUri + the wrap-fetch / errorResult(name + ": ...") / jsonResult shape, plus the ui:// resource via the also-extracted registerUIResource helper. Refactors of any of those subroutines now happen in one place instead of three. - registerUIResource: factored out of registerAppResource so the typed path reuses the same 18 lines. Migrations: registerUserView, registerDeviceView, registerInsightsView all collapse to a single typedAppSpec literal + helper call. One incidental wording change: insights_view used to emit "fetching insights: <err>" on handler failure; it now emits "insights_view: <err>" matching user_view / device_view. Same information; standardized for AI clients that correlate errors back to the calling tool. The new test TestRegisterTypedAppTool_ErrorMessagePrefixedWithToolName pins the contract. Existing integration tests (TestUserView_*, TestDeviceView_*, TestInsightsView_*) cover the happy path through the helper end-to-end and pass unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KLA-419 — surfaced during the 1.17.0 code-quality review.
After KLA-404 / KLA-405 / KLA-406 landed, the typed-input MCP App registration path was triplicated across
apps_user.go,apps_device.go,apps_insights.go. Each was ~40 lines of identicalmcp.Meta+ wrap-fetch /errorResult/jsonResult+AddResourceboilerplate. Every future typed MCP App would pay the same tax until extracted.What changes
typedAppSpec[In any]— mirrorsappSpecfor typed-input apps. Can't live in a[]typedAppSpecslice (eachIndiffers), but standardizes the per-app literal callers construct.registerTypedAppTool[In](s, spec)— wires the tool half with_meta.ui.resourceUri+ the wrap-fetch /errorResult(spec.Name + ": ...")/jsonResultshape, plus the ui:// resource via the also-extractedregisterUIResourcehelper. Future refactors of any of those subroutines now happen in one place.registerUIResource— factored out ofregisterAppResourceso the typed path reuses the same 18 lines.Migrations
registerUserView,registerDeviceView,registerInsightsViewall collapse to a singletypedAppSpecliteral + helper call. Each register function dropped from ~40 lines to ~15.One incidental wording change
insights_viewused to emit"fetching insights: <err>"on handler failure; it now emits"insights_view: <err>"matchinguser_view/device_view. Same information; standardized for AI clients that correlate errors back to the calling tool. Pinned byTestRegisterTypedAppTool_ErrorMessagePrefixedWithToolName.Test plan
go test ./...full suite green (existingTestUserView_*,TestDeviceView_*,TestInsightsView_*cover the happy path through the helper end-to-end)go vet ./...cleanTestRegisterTypedAppTool_ErrorMessagePrefixedWithToolNamelocks the error-prefix contract_meta.uiunchanged for all four migrated/affected toolsNet diff
5 files changed, +140 / -127. The helper itself adds ~50 lines but each new typed app costs ~15 lines instead of ~40 going forward.
🤖 Generated with Claude Code
Note
Low Risk
Mechanical refactor of MCP registration with one minor user-visible error string change for
insights_view; behavior and metadata are intended to stay the same.Overview
Introduces
typedAppSpec[In]andregisterTypedAppToolso typed MCP Apps (user_view,device_view,insights_view) register tools andui://resources from one spec literal instead of repeating meta, handler wrapping, andAddResourceboilerplate in each file.registerUIResourceis factored out ofregisterAppResourceso both no-args and typed apps share the same HTML +common.jsinjection path.insights_viewhandler errors now use theinsights_view:prefix (aligned with the other typed apps); previously they usedfetching insights:. A new test locks that error-prefix contract.Reviewed by Cursor Bugbot for commit 6cfaee0. Bugbot is set up for automated code reviews on this repo. Configure here.