Refactor: Split ToolFactory implementation #28
Merged
Conversation
added 8 commits
March 23, 2026 12:19
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the MCP server’s tool-registration system by splitting the former monolithic ToolFactory.kt into a small coordinator plus modular tool registrars, shared DSL/helpers, and extracted discovery logic—making tool registration and path resolution easier to maintain and test.
Changes:
- Introduces
ToolScopeDSL +ToolRegistrarinterface, and moves tool registrations into 4 platform/responsibility registrars. - Extracts filesystem/environment discovery into
ToolDiscoveryand pure parsing/formatting utilities intoToolHelpers. - Updates existing unit tests and documentation to target the extracted classes and new package layout.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| app/src/main/kotlin/com/example/visiontest/ToolFactory.kt | Replaced monolith with a thin coordinator that wires discovery + registrars and delegates registration. |
| app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt | Adds ToolScope registration DSL and CallToolRequest argument-parsing helpers. |
| app/src/main/kotlin/com/example/visiontest/tools/ToolRegistrar.kt | Defines the registrar interface for modular tool registration. |
| app/src/main/kotlin/com/example/visiontest/tools/AndroidDeviceToolRegistrar.kt | Registers Android device-management tools using ToolScope + ToolHelpers. |
| app/src/main/kotlin/com/example/visiontest/tools/AndroidAutomationToolRegistrar.kt | Registers Android automation tools and uses ToolDiscovery + request helpers. |
| app/src/main/kotlin/com/example/visiontest/tools/IOSDeviceToolRegistrar.kt | Registers iOS device-management tools using ToolScope + request helpers. |
| app/src/main/kotlin/com/example/visiontest/tools/IOSAutomationToolRegistrar.kt | Registers iOS automation tools and encapsulates xcodebuild process management. |
| app/src/main/kotlin/com/example/visiontest/tools/ToolHelpers.kt | Extracts pure parsing/formatting helpers from ToolFactory. |
| app/src/main/kotlin/com/example/visiontest/discovery/ToolDiscovery.kt | Extracts APK/Xcode/xctestrun/project-root/install-dir discovery logic. |
| app/src/test/kotlin/com/example/visiontest/ToolFactoryHelpersTest.kt | Migrates helper tests to cover ToolHelpers directly. |
| app/src/test/kotlin/com/example/visiontest/ToolFactoryPathTest.kt | Migrates path/discovery tests to cover ToolDiscovery and iOS command building. |
| CONTRIBUTING.md | Updates repo structure and “adding tools” workflow to match new registrars/DSL. |
| CLAUDE.md | Updates architecture + test documentation to reflect new tools/ and discovery/ layout. |
| openspec/changes/refactor-toolfactory/.openspec.yaml | Adds OpenSpec metadata for the refactor change set. |
| openspec/changes/refactor-toolfactory/tasks.md | Documents the refactor task plan/checklist. |
| openspec/changes/refactor-toolfactory/proposal.md | Captures motivation and scope of the refactor. |
| openspec/changes/refactor-toolfactory/design.md | Documents design decisions and trade-offs for the split/DSL/discovery extraction. |
| openspec/changes/refactor-toolfactory/specs/tool-registration-dsl/spec.md | Defines expected behavior for ToolScope/registrars/request helpers (needs alignment). |
| openspec/changes/refactor-toolfactory/specs/tool-discovery/spec.md | Defines expected behavior for ToolDiscovery extraction. |
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.
ToolFactory.ktis 1975 lines — the largest file in the codebase. It tangles three distinct concerns: tool registration (36+ tools with identical boilerplate), path/asset discovery, and string-parsing helpers. Every new tool added grows this monolith, and testing individual tool groups requires instantiating the entire factory with stubs for both platforms. Splitting it now keeps complexity manageable before more tools are added.What Changes
ToolScopeDSL class that absorbs the repeated try/runWithTimeout/catch/handleToolError pattern from every tool registration (~15 lines of boilerplate × 36 tools)ToolRegistrarinterface; split tool registrations into 4 platform-specific implementors (AndroidDevice, AndroidAutomation, IOSDevice, IOSAutomation)CallToolRequestextension helpers (.requireString(),.requireInt(),.optionalString()) to eliminate repeatedrequest.arguments["x"]?.jsonPrimitive?.contentpatternsdiscovery/package as aToolDiscoveryclassextractProperty,extractPattern,formatAppInfo) to atools/ToolHelpersobjectToolFactory.ktto a thin coordinator (~30 lines) that wires registrars togetherToolFactoryHelpersTest,ToolFactoryPathTest) to test the extracted classes directlyCapabilities
New Capabilities
tool-registration-dsl: ToolScope DSL and ToolRegistrar interface that standardize how MCP tools are registered, including timeout handling, error wrapping, and parameter extractiontool-discovery: Standalone discovery logic for locating APKs, Xcode projects, xctestrun bundles, install directories, and project rootsModified Capabilities
(none — this is a pure refactor with no behavior changes to any MCP tool or discovery logic)
Impact
app/src/main/kotlin/com/example/visiontest/ToolFactory.ktreplaced by ~8 new files acrosstools/anddiscovery/packagesToolFactoryHelpersTest.ktandToolFactoryPathTest.ktupdated with new imports; test classes simplified (no more stub/mock DeviceConfig needed for helpers and discovery)Main.ktunchanged —ToolFactory(android, ios, logger).registerAllTools(server)still works