refactor: extract 11 MCP tool handlers into src/tools/#37
Conversation
|
@codex review |
Greptile SummarySuccessfully extracted 11 MCP tool handlers from the monolithic Key improvements:
Code preservation:
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
|
| 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
Last reviewed commit: 351d3f4
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
There was a problem hiding this comment.
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.
351d3f4 to
6d5026b
Compare
There was a problem hiding this comment.
💡 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.
Summary
src/index.ts(2,227 lines) into 13 focused modules undersrc/tools/src/index.tsis now pure MCP protocol wiring (~607 lines) — no business logicChanges
New files —
src/tools/types.ts— sharedToolContext,ToolResponse,ToolPaths,IndexStateinterfacesindex.ts—TOOLSarray aggregator +dispatchTool()routersearch-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-memoryModified —
src/index.tsTOOLSarray (~260 lines) andCallToolRequestSchemaswitch statement (~1,390 lines)dispatchTool()call via injectedToolContextNew test —
tests/tools/dispatch.test.tsTest plan
pnpm build— zero TypeScript errorspnpm test— all 201 tests pass (195 pre-existing + 6 new)src/index.tsviagit show HEAD