Skip to content

Mcp tools contract refactor - #13

Open
imjustprism wants to merge 4 commits into
devfrom
refactor/mcp-tools-contract
Open

Mcp tools contract refactor#13
imjustprism wants to merge 4 commits into
devfrom
refactor/mcp-tools-contract

Conversation

@imjustprism

@imjustprism imjustprism commented Jun 11, 2026

Copy link
Copy Markdown
Owner

No description provided.

Comment thread src/plugins/mcp.dev/tools/utils.ts Fixed
@imjustprism imjustprism changed the title Mcp tools contract refactor + ContextMenuAPI anchor hardening Mcp tools contract refactor Jun 11, 2026
@imjustprism
imjustprism force-pushed the refactor/mcp-tools-contract branch from 4544ffe to 42f7f3c Compare June 11, 2026 14:02
@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors the MCP tools layer by introducing a typed contract in contract.ts that auto-derives TypeScript argument types and MCP JSON Schema definitions from a single DSL, eliminating duplication between definitions.ts and per-file handler types. It also improves the reporter with a new fragility analyzer, tightens context-menu patch regexes, adds a label parameter for lazy proxy debugging, and changes how the patch engine removes consumed patches from the evaluation list.

  • New contract.ts: A single source of truth for all tool specs — InferArgs<S> drives TypeScript types, and fieldToSchema() generates JSON Schema at module init time. The ActionMap<A> pattern is adopted across all handlers.
  • patchTurbopack.ts splice changes: validateOnly patches now stay in the list until a replacement match succeeds (anyMatched); regular patches stay until at least one replacement is actually applied (groupApplied > 0). The reportOrphanedPatches function is promoted from debug to warn with dedup logic.
  • turbopack.ts finder tracking: Adds an accessed flag so only finders that have been resolved are included in the failure report, and passes finderLabel strings to proxyLazy for clearer warnings.

Confidence Score: 4/5

The contract refactor and associated handler cleanup are correct; the splice logic change in patchTurbopack.ts needs attention before merging.

The contract DSL, handler refactors, reporter fragility analysis, lazy-label improvements, and finder access tracking are all clean. The changed patch-splice condition introduces a P1 regression: non-all patches with groupApplied=0 are never removed from the evaluation list, causing patchResults to accumulate one entry per shared-factory module rather than one per patch.

src/turbopack/patchTurbopack.ts — specifically the groupApplied > 0 splice condition at line 353 and the anyMatched splice condition at line 273.

Important Files Changed

Filename Overview
src/plugins/mcp.dev/tools/contract.ts New file — single source of truth DSL for all MCP tool specs; derives TypeScript argument types via InferArgs<S> and produces JSON Schema via fieldToSchema. Type inference is correct; required: [] for parameterless tools emits an empty array which is valid.
src/turbopack/patchTurbopack.ts Two splice-condition changes introduce a P1 regression (patchResults duplicate entries for shared-factory non-all patches) and a P2 concern (validateOnly patches that never match stay in the list indefinitely). The reportOrphanedPatches upgrade (warn level, dedup via seen) is sound.
src/plugins/mcp.dev/tools/patch.ts Consolidated internal helpers and adopted ActionMap/dispatch pattern. The typeof r.match === "function" guard in diagnoseOrphaned correctly addresses the previously flagged false-positive orphan issue.
src/plugins/mcp.dev/tools/store.ts Refactored to use ActionMap/dispatch; buildResult now correctly guards diffState with isObject(stateBefore) && isObject(stateAfter). actionSubscribe cleans up timing with a single dt variable.
src/turbopack/turbopack.ts Added accessed tracking to FinderRecord so reportFailedFinders only surfaces finders that have actually been resolved, reducing false positives. Labels are propagated to proxyLazy and LazyComponent for clearer warnings.
src/utils/lazy.ts Adds optional label parameter to makeLazy and proxyLazy; uses it in the max-retry warning message to identify which lazy value could not be resolved. Purely additive, no logic changes.
reporter/fragility.ts New reporter module detecting fragile patch patterns: CSS-anchored finds/replacements, exact sibling counts, and long rigid matches without string literals or bounded gaps. Analysis is sound and only runs when patch.noWarn is unset.
src/plugins/_api/contextMenu/index.tsx Both patch regexes simplified to use lookaheads instead of consuming full JSX expressions — avoids fragility from class name and children-structure drift. The semantics of $&/$1 replacements are preserved correctly under the new anchors.
src/plugins/mcp.dev/tools/types.ts Now re-exports all arg types from contract.ts via ToolArgsMap; removes hand-written per-tool arg interfaces, reducing drift risk. ActionMap<A> and ToolHandler added to support the dispatch pattern.
src/plugins/mcp.dev/tools/utils.ts Adds ActionMap<A> type, dispatch<A> helper, and clampConfig overload; utilities are well-guarded. The createGenerationalCache helper is correctly extracted here from per-file usage.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    TOOLS["TOOLS const\n(contract.ts)"] -->|InferArgs| TYPES["ToolArgsMap\n(types.ts)"]
    TOOLS -->|fieldToSchema| DEFS["TOOL_DEFINITIONS\nJSON Schema\n(contract.ts)"]
    TYPES --> HANDLERS["Typed handlers\n(index.ts)"]
    HANDLERS --> MODULE["handleModule"]
    HANDLERS --> PATCH["handlePatch"]
    HANDLERS --> STORE["handleStore"]
    HANDLERS --> REACT["handleReact"]
    HANDLERS --> INTERCEPT["handleIntercept"]
    HANDLERS --> GROK["handleGrok"]
    HANDLERS --> EVAL["handleEval"]
    HANDLERS --> SEARCH["handleSearch"]
    HANDLERS --> PLUGIN["handlePlugin"]
    UTILS["ActionMap / dispatch\n(utils.ts)"] -->|used by| PATCH
    UTILS -->|used by| STORE
    UTILS -->|used by| MODULE
    PATCH -->|patchResults / patchStats| PTCH["patchTurbopack.ts\npatchFactory"]
    PTCH -->|splice on groupApplied>0| PLIST["patches array\n(global list)"]
    PTCH -->|splice on anyMatched| VONLY["validateOnly branch"]
Loading

Reviews (2): Last reviewed commit: "Mcp: skip function matches in orphan che..." | Re-trigger Greptile

Comment thread src/plugins/mcp.dev/tools/patch.ts
Comment thread src/plugins/mcp.dev/tools/store.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants