Skip to content

Refactor: Split ToolFactory implementation #28

Merged
docer1990 merged 10 commits into
mainfrom
refactor/split-toolfactory
Mar 24, 2026
Merged

Refactor: Split ToolFactory implementation #28
docer1990 merged 10 commits into
mainfrom
refactor/split-toolfactory

Conversation

@docer1990

Copy link
Copy Markdown
Owner

ToolFactory.kt is 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

  • Introduce a ToolScope DSL class that absorbs the repeated try/runWithTimeout/catch/handleToolError pattern from every tool registration (~15 lines of boilerplate × 36 tools)
  • Introduce a ToolRegistrar interface; split tool registrations into 4 platform-specific implementors (AndroidDevice, AndroidAutomation, IOSDevice, IOSAutomation)
  • Add CallToolRequest extension helpers (.requireString(), .requireInt(), .optionalString()) to eliminate repeated request.arguments["x"]?.jsonPrimitive?.content patterns
  • Move discovery functions (APK, Xcode project, xctestrun, project root, install dir) to a new discovery/ package as a ToolDiscovery class
  • Move pure helper functions (extractProperty, extractPattern, formatAppInfo) to a tools/ToolHelpers object
  • Reduce ToolFactory.kt to a thin coordinator (~30 lines) that wires registrars together
  • Migrate existing tests (ToolFactoryHelpersTest, ToolFactoryPathTest) to test the extracted classes directly

Capabilities

New Capabilities

  • tool-registration-dsl: ToolScope DSL and ToolRegistrar interface that standardize how MCP tools are registered, including timeout handling, error wrapping, and parameter extraction
  • tool-discovery: Standalone discovery logic for locating APKs, Xcode projects, xctestrun bundles, install directories, and project roots

Modified Capabilities

(none — this is a pure refactor with no behavior changes to any MCP tool or discovery logic)

Impact

  • Code: app/src/main/kotlin/com/example/visiontest/ToolFactory.kt replaced by ~8 new files across tools/ and discovery/ packages
  • Tests: ToolFactoryHelpersTest.kt and ToolFactoryPathTest.kt updated with new imports; test classes simplified (no more stub/mock DeviceConfig needed for helpers and discovery)
  • APIs: Zero change — all MCP tool names, descriptions, schemas, and behavior remain identical
  • Dependencies: No new library dependencies
  • Entry point: Main.kt unchanged — ToolFactory(android, ios, logger).registerAllTools(server) still works

Copilot AI review requested due to automatic review settings March 23, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 ToolScope DSL + ToolRegistrar interface, and moves tool registrations into 4 platform/responsibility registrars.
  • Extracts filesystem/environment discovery into ToolDiscovery and pure parsing/formatting utilities into ToolHelpers.
  • 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.

Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt
Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt
Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt
Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 5 comments.

Comment thread openspec/changes/refactor-toolfactory/tasks.md
Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt
Comment thread app/src/main/kotlin/com/example/visiontest/tools/ToolDsl.kt

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@docer1990 docer1990 merged commit 760cd96 into main Mar 24, 2026
7 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