Skip to content

refactor: extract 11 MCP tool handlers into src/tools/#37

Merged
PatrickSys merged 2 commits intomasterfrom
refactor/extract-mcp-tools
Feb 20, 2026
Merged

refactor: extract 11 MCP tool handlers into src/tools/#37
PatrickSys merged 2 commits intomasterfrom
refactor/extract-mcp-tools

Conversation

@PatrickSys
Copy link
Owner

@PatrickSys PatrickSys commented Feb 20, 2026

Summary

  • Splits src/index.ts (2,227 lines) into 13 focused modules under src/tools/
  • src/index.ts is now pure MCP protocol wiring (~607 lines) — no business logic
  • All 11 tool handlers are extracted verbatim; no behavior changes

Changes

New files — src/tools/

  • types.ts — shared ToolContext, ToolResponse, ToolPaths, IndexState interfaces
  • index.tsTOOLS array aggregator + dispatchTool() router
  • One file per tool: search-codebase, get-codebase-metadata, get-indexing-status, refresh-index, get-style-guide, get-team-patterns, get-symbol-references, get-component-usage, detect-circular-dependencies, remember, get-memory

Modified — src/index.ts

  • Removes inline TOOLS array (~260 lines) and CallToolRequestSchema switch statement (~1,390 lines)
  • Replaces with a single dispatchTool() call via injected ToolContext

New test — tests/tools/dispatch.test.ts

  • 6 tests: verifies all 11 tools exported, unique names, schemas present, routing works, error handling for unknown tools

Test plan

  • pnpm build — zero TypeScript errors
  • pnpm test — all 201 tests pass (195 pre-existing + 6 new)
  • All 11 tool schemas verified against original src/index.ts via git show HEAD
  • All handler implementations verified as exact copies with context injection substitutions

@PatrickSys
Copy link
Owner Author

@codex review

@greptile-apps
Copy link

greptile-apps bot commented Feb 20, 2026

Greptile Summary

Successfully extracted 11 MCP tool handlers from the monolithic src/index.ts (2,227 lines) into focused, single-responsibility modules under src/tools/. The main index file is now reduced to ~607 lines and focuses purely on MCP protocol wiring, following the project constraint that "src/index.ts is routing and protocol. No business logic" (AGENTS.md line 127).

Key improvements:

  • Each tool now has its own file with definition (Tool schema) and handle (async handler) exports
  • Introduced src/tools/types.ts with shared ToolContext, ToolResponse, ToolPaths, and IndexState interfaces
  • Created src/tools/index.ts aggregator that exports TOOLS array and dispatchTool() router
  • Replaced massive switch statement in src/index.ts with single dispatchTool() call via injected context
  • Added 6 integration tests in tests/tools/dispatch.test.ts validating tool routing and error handling

Code preservation:

  • All 11 tool handlers preserve identical logic from original implementation
  • Tool definitions (schemas, descriptions) remain unchanged
  • Error handling and response formats are consistent

This refactoring improves maintainability without changing runtime behavior, aligning with AGENTS.md Rule 1: "Janitor > Visionary - Success = noise removed, not complexity added."

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • Clean refactoring that extracts tool handlers into focused modules without changing runtime behavior. All tool logic is preserved identically, new abstractions (ToolContext, dispatchTool) are well-defined, and integration tests validate the routing mechanism. Follows AGENTS.md constraint that src/index.ts should be "routing and protocol. No business logic."
  • No files require special attention

Important Files Changed

Filename Overview
src/tools/types.ts New types file defining ToolContext, ToolResponse, ToolPaths, and IndexState interfaces for tool handlers
src/tools/index.ts Tool aggregator exporting TOOLS array and dispatchTool() router function
src/index.ts Reduced from 2,227 to ~607 lines by extracting tool handlers; now focuses on MCP protocol wiring
tests/tools/dispatch.test.ts New integration tests (6 tests) validating tool routing and dispatcher functionality

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[src/index.ts<br/>MCP Server] -->|imports| B[src/tools/index.ts]
    B -->|exports| C[TOOLS array]
    B -->|exports| D[dispatchTool function]
    B -->|imports| E[src/tools/types.ts]
    
    E -->|defines| F[ToolContext interface]
    E -->|defines| G[ToolResponse interface]
    E -->|defines| H[ToolPaths interface]
    
    B -->|aggregates| I[11 Tool Handlers]
    
    I --> J1[search-codebase.ts]
    I --> J2[get-codebase-metadata.ts]
    I --> J3[get-indexing-status.ts]
    I --> J4[refresh-index.ts]
    I --> J5[get-style-guide.ts]
    I --> J6[get-team-patterns.ts]
    I --> J7[get-symbol-references.ts]
    I --> J8[get-component-usage.ts]
    I --> J9[detect-circular-dependencies.ts]
    I --> J10[remember.ts]
    I --> J11[get-memory.ts]
    
    J1 -->|exports| K1[definition + handle]
    J2 -->|exports| K2[definition + handle]
    J3 -->|exports| K3[definition + handle]
    
    A -->|CallToolRequestSchema| L[Request Handler]
    L -->|creates| F
    L -->|calls| D
    D -->|routes to| I
    
    M[tests/tools/dispatch.test.ts] -.->|validates| B
    M -.->|validates| D
Loading

Last reviewed commit: 351d3f4

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Splits the monolithic src/index.ts (2,227 lines) into focused modules.
Each of the 11 tools now lives in its own file under src/tools/, and
src/index.ts is reduced to pure MCP protocol wiring (~607 lines).

Changes:
- Add src/tools/types.ts — ToolContext, ToolResponse, ToolPaths, IndexState
- Add src/tools/index.ts — TOOLS array aggregator and dispatchTool() router
- Add src/tools/{search-codebase, get-codebase-metadata, get-indexing-status,
  refresh-index, get-style-guide, get-team-patterns, get-symbol-references,
  get-component-usage, detect-circular-dependencies, remember, get-memory}.ts
- Remove inline TOOLS array and CallToolRequestSchema switch from src/index.ts
- Replace with a single dispatchTool() call via injected ToolContext
- Add tests/tools/dispatch.test.ts — 6 integration tests for tool routing
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the monolithic src/index.ts file (originally 2,227 lines) by extracting 11 MCP tool handlers into separate, focused modules under src/tools/. The refactoring reduces src/index.ts to ~607 lines of pure MCP protocol wiring while maintaining all existing functionality.

Changes:

  • Extracted 11 tool handlers from src/index.ts into dedicated files in src/tools/
  • Created src/tools/types.ts to define shared ToolContext, ToolResponse, and ToolPaths interfaces
  • Created src/tools/index.ts as an aggregator with TOOLS array and dispatchTool() router
  • Updated src/index.ts to use dependency injection via ToolContext for tool dispatching
  • Added integration tests for tool routing in tests/tools/dispatch.test.ts

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tools/types.ts Defines shared types for tool handlers (ToolContext, ToolResponse, ToolPaths, IndexState)
src/tools/index.ts Aggregates all 11 tools and provides dispatchTool() routing logic
src/tools/search-codebase.ts Extracted search_codebase tool with preflight checks and evidence gating
src/tools/get-codebase-metadata.ts Extracted get_codebase_metadata tool for framework and dependency info
src/tools/get-indexing-status.ts Extracted get_indexing_status tool for index state and progress
src/tools/refresh-index.ts Extracted refresh_index tool for manual re-indexing
src/tools/get-style-guide.ts Extracted get_style_guide tool for documentation querying
src/tools/get-team-patterns.ts Extracted get_team_patterns tool for pattern recommendations
src/tools/get-symbol-references.ts Extracted get_symbol_references tool for symbol usage tracking
src/tools/get-component-usage.ts Extracted get_component_usage tool for library/component usage
src/tools/detect-circular-dependencies.ts Extracted detect_circular_dependencies tool for cycle detection
src/tools/remember.ts Extracted remember tool for recording team conventions
src/tools/get-memory.ts Extracted get_memory tool for retrieving recorded conventions
src/index.ts Reduced to MCP protocol wiring, imports TOOLS and dispatchTool from src/tools/
tests/tools/dispatch.test.ts Added 6 integration tests for tool routing and structure validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@PatrickSys PatrickSys force-pushed the refactor/extract-mcp-tools branch from 351d3f4 to 6d5026b Compare February 20, 2026 22:19
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 351d3f43aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Without await, the surrounding catch only handles synchronous throws.
Any async rejection from a tool handler (e.g. detectMetadata() I/O
failure) would escape as an unhandled MCP error instead of returning
the structured JSON error response.
@PatrickSys PatrickSys merged commit 3614e2a into master Feb 20, 2026
1 of 3 checks passed
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